網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
在上一篇文章《驅(qū)動(dòng)開(kāi)發(fā):內(nèi)核字符串轉(zhuǎn)換方法》中簡(jiǎn)單介紹了內(nèi)核是如何使用字符串以及字符串之間的轉(zhuǎn)換方法,本章將繼續(xù)探索字符串的拷貝與比較,與應(yīng)用層不同內(nèi)核字符串拷貝與比較也需要使用內(nèi)核專(zhuān)用的API函數(shù),字符串的拷貝往往伴隨有內(nèi)核內(nèi)存分配,我們將首先簡(jiǎn)單介紹內(nèi)核如何分配堆空間,然后再以此為契機(jī)簡(jiǎn)介字符串的拷貝與比較。
首先內(nèi)核中的堆棧分配可以使用ExAllocatePool()
這個(gè)內(nèi)核函數(shù)實(shí)現(xiàn),此外還可以使用ExAllocatePoolWithTag()
函數(shù),兩者的區(qū)別是,第一個(gè)函數(shù)可以直接分配內(nèi)存,第二個(gè)函數(shù)在分配時(shí)需要指定一個(gè)標(biāo)簽,此外內(nèi)核屬性常用的有兩種NonPagedPool
用于分配非分頁(yè)內(nèi)存,而PagePool
則用于分配分頁(yè)內(nèi)存,在開(kāi)發(fā)中推薦使用非分頁(yè)內(nèi)存,因?yàn)榉猪?yè)內(nèi)存數(shù)量有限。
內(nèi)存分配使用ExAllocatePool
函數(shù),內(nèi)存拷貝可使用RtlCopyMemory
函數(shù),需要注意該函數(shù)其實(shí)是對(duì)Memcpy
函數(shù)的包裝。
#include <ntifs.h> VOID UnDriver(PDRIVER_OBJECT driver) { DbgPrint("驅(qū)動(dòng)已卸載 \n"); } // PowerBy: LyShark NTSTATUS DriverEntry(IN PDRIVER_OBJECT Driver, PUNICODE_STRING RegistryPath) { UNICODE_STRING uncode_buffer = { 0 }; DbgPrint("hello lyshark \n"); wchar_t * wchar_string = L"hello lyshark"; // 設(shè)置最大長(zhǎng)度 uncode_buffer.MaximumLength = 1024; // 分配內(nèi)存空間 uncode_buffer.Buffer = (PWSTR)ExAllocatePool(PagedPool, 1024); // 設(shè)置字符長(zhǎng)度 因?yàn)槭菍捵址?,所以是字符長(zhǎng)度的 2 倍 uncode_buffer.Length = wcslen(wchar_string) * 2; // 保證緩沖區(qū)足夠大,否則程序終止 ASSERT(uncode_buffer.MaximumLength >= uncode_buffer.Length); // 將 wchar_string 中的字符串拷貝到 uncode_buffer.Buffer RtlCopyMemory(uncode_buffer.Buffer, wchar_string, uncode_buffer.Length); // 設(shè)置字符串長(zhǎng)度 并輸出 uncode_buffer.Length = wcslen(wchar_string) * 2; DbgPrint("輸出字符串: %wZ \n", uncode_buffer); // 釋放堆空間 ExFreePool(uncode_buffer.Buffer); uncode_buffer.Buffer = NULL; uncode_buffer.Length = uncode_buffer.MaximumLength = 0; DbgPrint("驅(qū)動(dòng)已加載 \n"); Driver->DriverUnload = UnDriver; return STATUS_SUCCESS; }
代碼輸出效果:
實(shí)現(xiàn)空間分配
字符串結(jié)構(gòu)UNICODE_STRING
可以定義數(shù)組,空間的分配也可以循環(huán)進(jìn)行,例如我們分配十個(gè)字符串結(jié)構(gòu),并輸出結(jié)構(gòu)內(nèi)的參數(shù)。
#include <ntifs.h> VOID UnDriver(PDRIVER_OBJECT driver) { DbgPrint("驅(qū)動(dòng)已卸載 \n"); } // PowerBy: LyShark NTSTATUS DriverEntry(IN PDRIVER_OBJECT Driver, PUNICODE_STRING RegistryPath) { UNICODE_STRING uncode_buffer[10] = { 0 }; wchar_t * wchar_string = L"hello lyshark"; DbgPrint("hello lyshark \n"); int size = sizeof(uncode_buffer) / sizeof(uncode_buffer[0]); DbgPrint("數(shù)組長(zhǎng)度: %d \n", size); for (int x = 0; x < size; x++) { // 分配空間 uncode_buffer[x].Buffer = (PWSTR)ExAllocatePool(PagedPool, 1024); // 設(shè)置長(zhǎng)度 uncode_buffer[x].MaximumLength = 1024; uncode_buffer[x].Length = wcslen(wchar_string) * sizeof(WCHAR); ASSERT(uncode_buffer[x].MaximumLength >= uncode_buffer[x].Length); // 拷貝字符串并輸出 RtlCopyMemory(uncode_buffer[x].Buffer, wchar_string, uncode_buffer[x].Length); uncode_buffer[x].Length = wcslen(wchar_string) * sizeof(WCHAR); DbgPrint("循環(huán): %d 輸出字符串: %wZ \n", x, uncode_buffer[x]); // 釋放內(nèi)存 ExFreePool(uncode_buffer[x].Buffer); uncode_buffer[x].Buffer = NULL; uncode_buffer[x].Length = uncode_buffer[x].MaximumLength = 0; } DbgPrint("驅(qū)動(dòng)加載成功 \n"); Driver->DriverUnload = UnDriver; return STATUS_SUCCESS; }
代碼輸出效果:
實(shí)現(xiàn)字符串拷貝
此處可以直接使用RtlCopyMemory
函數(shù)直接對(duì)內(nèi)存操作,也可以調(diào)用內(nèi)核提供的RtlCopyUnicodeString
函數(shù)來(lái)實(shí)現(xiàn),具體代碼如下。
#include <ntifs.h> VOID UnDriver(PDRIVER_OBJECT driver) { DbgPrint("驅(qū)動(dòng)已卸載 \n"); } // PowerBy: LyShark NTSTATUS DriverEntry(IN PDRIVER_OBJECT Driver, PUNICODE_STRING RegistryPath) { DbgPrint("hello lyshark \n"); UNICODE_STRING uncode_buffer_source = { 0 }; UNICODE_STRING uncode_buffer_target = { 0 }; // 該函數(shù)可用于初始化字符串 RtlInitUnicodeString(&uncode_buffer_source, L"hello lyshark"); // 初始化target字符串,分配空間 uncode_buffer_target.Buffer = (PWSTR)ExAllocatePool(PagedPool, 1024); uncode_buffer_target.MaximumLength = 1024; // 將source中的內(nèi)容拷貝到target中 RtlCopyUnicodeString(&uncode_buffer_target, &uncode_buffer_source); // 輸出結(jié)果 DbgPrint("source = %wZ \n", &uncode_buffer_source); DbgPrint("target = %wZ \n", &uncode_buffer_target); // 釋放空間 source 無(wú)需銷(xiāo)毀 // 如果強(qiáng)制釋放掉source則會(huì)導(dǎo)致系統(tǒng)藍(lán)屏,因?yàn)閟ource是在棧上的 RtlFreeUnicodeString(&uncode_buffer_target); DbgPrint("驅(qū)動(dòng)加載成功 \n"); Driver->DriverUnload = UnDriver; return STATUS_SUCCESS; }
代碼輸出效果:
實(shí)現(xiàn)字符串比較
如果需要比較兩個(gè)UNICODE_STRING
字符串結(jié)構(gòu)體是否相等,那么可以使用RtlEqualUnicodeString
這個(gè)內(nèi)核函數(shù)實(shí)現(xiàn),該函數(shù)第三個(gè)參數(shù)是返回值類(lèi)型,如果是TRUE則默認(rèn)返回真,否則返回假,具體代碼如下。
#include <ntifs.h> VOID UnDriver(PDRIVER_OBJECT driver) { DbgPrint("驅(qū)動(dòng)已卸載 \n"); } // PowerBy: LyShark NTSTATUS DriverEntry(IN PDRIVER_OBJECT Driver, PUNICODE_STRING RegistryPath) { DbgPrint("hello lyshark \n"); UNICODE_STRING uncode_buffer_source = { 0 }; UNICODE_STRING uncode_buffer_target = { 0 }; // 該函數(shù)可用于初始化字符串 RtlInitUnicodeString(&uncode_buffer_source, L"hello lyshark"); RtlInitUnicodeString(&uncode_buffer_target, L"hello lyshark"); // 比較字符串是否相等 if (RtlEqualUnicodeString(&uncode_buffer_source, &uncode_buffer_target, TRUE)) { DbgPrint("字符串相等 \n"); } else { DbgPrint("字符串不相等 \n"); } DbgPrint("驅(qū)動(dòng)加載成功 \n"); Driver->DriverUnload = UnDriver; return STATUS_SUCCESS; }
代碼輸出效果:
有時(shí)在字符串比較時(shí)需要統(tǒng)一字符串格式,例如全部變大寫(xiě)以后在做比較等,此時(shí)可以使用RtlUpcaseUnicodeString
函數(shù)將小寫(xiě)字符串為大寫(xiě),然后在做比較,代碼如下。
#include <ntifs.h> VOID UnDriver(PDRIVER_OBJECT driver) { DbgPrint("驅(qū)動(dòng)已卸載 \n"); } // PowerBy: LyShark NTSTATUS DriverEntry(IN PDRIVER_OBJECT Driver, PUNICODE_STRING RegistryPath) { DbgPrint("hello lyshark \n"); UNICODE_STRING uncode_buffer_source = { 0 }; UNICODE_STRING uncode_buffer_target = { 0 }; // 該函數(shù)可用于初始化字符串 RtlInitUnicodeString(&uncode_buffer_source, L"hello lyshark"); RtlInitUnicodeString(&uncode_buffer_target, L"HELLO LYSHARK"); // 字符串小寫(xiě)變大寫(xiě) RtlUpcaseUnicodeString(&uncode_buffer_target, &uncode_buffer_source, TRUE); DbgPrint("小寫(xiě)輸出: %wZ \n", &uncode_buffer_source); DbgPrint("變大寫(xiě)輸出: %wZ \n", &uncode_buffer_target); // 銷(xiāo)毀字符串 RtlFreeUnicodeString(&uncode_buffer_target); DbgPrint("驅(qū)動(dòng)加載成功 \n"); Driver->DriverUnload = UnDriver; return STATUS_SUCCESS; }
代碼輸出效果:
原文鏈接:https://www.cnblogs.com/LyShark/p/16740467.html
相關(guān)推薦
- 2023-07-05 uni-app滾動(dòng)分頁(yè) 兼容(App 小程序 H5)
- 2022-10-31 Go語(yǔ)言面試題之select和channel的用法_Golang
- 2022-04-18 python?numpy中對(duì)ndarry按照index增刪改查_(kāi)python
- 2023-04-02 Android自定義View實(shí)現(xiàn)動(dòng)畫(huà)效果詳解_Android
- 2022-05-02 Entity?Framework常用查詢(xún)語(yǔ)句_實(shí)用技巧
- 2023-03-27 Python中元組的概念及應(yīng)用小結(jié)_python
- 2022-06-02 C++的智能指針你真的了解嗎_C 語(yǔ)言
- 2022-06-30 C語(yǔ)言實(shí)例上手深入理解操作符的使用_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支