網站首頁 編程語言 正文
在上一篇文章《驅動開發:內核字符串轉換方法》中簡單介紹了內核是如何使用字符串以及字符串之間的轉換方法,本章將繼續探索字符串的拷貝與比較,與應用層不同內核字符串拷貝與比較也需要使用內核專用的API函數,字符串的拷貝往往伴隨有內核內存分配,我們將首先簡單介紹內核如何分配堆空間,然后再以此為契機簡介字符串的拷貝與比較。
首先內核中的堆棧分配可以使用ExAllocatePool()
這個內核函數實現,此外還可以使用ExAllocatePoolWithTag()
函數,兩者的區別是,第一個函數可以直接分配內存,第二個函數在分配時需要指定一個標簽,此外內核屬性常用的有兩種NonPagedPool
用于分配非分頁內存,而PagePool
則用于分配分頁內存,在開發中推薦使用非分頁內存,因為分頁內存數量有限。
內存分配使用ExAllocatePool
函數,內存拷貝可使用RtlCopyMemory
函數,需要注意該函數其實是對Memcpy
函數的包裝。
#include <ntifs.h> VOID UnDriver(PDRIVER_OBJECT driver) { DbgPrint("驅動已卸載 \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"; // 設置最大長度 uncode_buffer.MaximumLength = 1024; // 分配內存空間 uncode_buffer.Buffer = (PWSTR)ExAllocatePool(PagedPool, 1024); // 設置字符長度 因為是寬字符,所以是字符長度的 2 倍 uncode_buffer.Length = wcslen(wchar_string) * 2; // 保證緩沖區足夠大,否則程序終止 ASSERT(uncode_buffer.MaximumLength >= uncode_buffer.Length); // 將 wchar_string 中的字符串拷貝到 uncode_buffer.Buffer RtlCopyMemory(uncode_buffer.Buffer, wchar_string, uncode_buffer.Length); // 設置字符串長度 并輸出 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("驅動已加載 \n"); Driver->DriverUnload = UnDriver; return STATUS_SUCCESS; }
代碼輸出效果:
實現空間分配
字符串結構UNICODE_STRING
可以定義數組,空間的分配也可以循環進行,例如我們分配十個字符串結構,并輸出結構內的參數。
#include <ntifs.h> VOID UnDriver(PDRIVER_OBJECT driver) { DbgPrint("驅動已卸載 \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("數組長度: %d \n", size); for (int x = 0; x < size; x++) { // 分配空間 uncode_buffer[x].Buffer = (PWSTR)ExAllocatePool(PagedPool, 1024); // 設置長度 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("循環: %d 輸出字符串: %wZ \n", x, uncode_buffer[x]); // 釋放內存 ExFreePool(uncode_buffer[x].Buffer); uncode_buffer[x].Buffer = NULL; uncode_buffer[x].Length = uncode_buffer[x].MaximumLength = 0; } DbgPrint("驅動加載成功 \n"); Driver->DriverUnload = UnDriver; return STATUS_SUCCESS; }
代碼輸出效果:
實現字符串拷貝
此處可以直接使用RtlCopyMemory
函數直接對內存操作,也可以調用內核提供的RtlCopyUnicodeString
函數來實現,具體代碼如下。
#include <ntifs.h> VOID UnDriver(PDRIVER_OBJECT driver) { DbgPrint("驅動已卸載 \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 }; // 該函數可用于初始化字符串 RtlInitUnicodeString(&uncode_buffer_source, L"hello lyshark"); // 初始化target字符串,分配空間 uncode_buffer_target.Buffer = (PWSTR)ExAllocatePool(PagedPool, 1024); uncode_buffer_target.MaximumLength = 1024; // 將source中的內容拷貝到target中 RtlCopyUnicodeString(&uncode_buffer_target, &uncode_buffer_source); // 輸出結果 DbgPrint("source = %wZ \n", &uncode_buffer_source); DbgPrint("target = %wZ \n", &uncode_buffer_target); // 釋放空間 source 無需銷毀 // 如果強制釋放掉source則會導致系統藍屏,因為source是在棧上的 RtlFreeUnicodeString(&uncode_buffer_target); DbgPrint("驅動加載成功 \n"); Driver->DriverUnload = UnDriver; return STATUS_SUCCESS; }
代碼輸出效果:
實現字符串比較
如果需要比較兩個UNICODE_STRING
字符串結構體是否相等,那么可以使用RtlEqualUnicodeString
這個內核函數實現,該函數第三個參數是返回值類型,如果是TRUE則默認返回真,否則返回假,具體代碼如下。
#include <ntifs.h> VOID UnDriver(PDRIVER_OBJECT driver) { DbgPrint("驅動已卸載 \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 }; // 該函數可用于初始化字符串 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("驅動加載成功 \n"); Driver->DriverUnload = UnDriver; return STATUS_SUCCESS; }
代碼輸出效果:
有時在字符串比較時需要統一字符串格式,例如全部變大寫以后在做比較等,此時可以使用RtlUpcaseUnicodeString
函數將小寫字符串為大寫,然后在做比較,代碼如下。
#include <ntifs.h> VOID UnDriver(PDRIVER_OBJECT driver) { DbgPrint("驅動已卸載 \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 }; // 該函數可用于初始化字符串 RtlInitUnicodeString(&uncode_buffer_source, L"hello lyshark"); RtlInitUnicodeString(&uncode_buffer_target, L"HELLO LYSHARK"); // 字符串小寫變大寫 RtlUpcaseUnicodeString(&uncode_buffer_target, &uncode_buffer_source, TRUE); DbgPrint("小寫輸出: %wZ \n", &uncode_buffer_source); DbgPrint("變大寫輸出: %wZ \n", &uncode_buffer_target); // 銷毀字符串 RtlFreeUnicodeString(&uncode_buffer_target); DbgPrint("驅動加載成功 \n"); Driver->DriverUnload = UnDriver; return STATUS_SUCCESS; }
代碼輸出效果:
原文鏈接:https://www.cnblogs.com/LyShark/p/16740467.html
相關推薦
- 2022-07-28 C++超詳細講解函數參數的默認值_C 語言
- 2022-02-28 npm ERR! While resolving: undefined@undefined npm
- 2023-10-10 uniapp實現預覽請求后臺接口返回的文件
- 2023-03-27 Android三種雙屏異顯實現方法介紹_Android
- 2022-05-06 Redis定時任務原理的實現_Redis
- 2022-11-03 anaconda?部署Jupyter?Notebook服務器過程詳解_python
- 2022-06-24 React如何使用refresh_token實現無感刷新頁面_React
- 2022-03-31 利用OpenCV實現質心跟蹤算法_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支