在上一篇文章《驅動開發:內核字串轉換方法》中簡單介紹了內核是如何使用字串以及字串之間的轉換方法,本章將繼續探索字串的拷貝與比較,與應用層不同內核字串拷貝與比較也需要使用內核專用的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
著作權宣告:本博客文章與代碼均為學習時整理的筆記,文章 [均為原創] 作品,轉載請 [添加出處] ,您添加出處是我創作的動力!
轉載文章,請遵守《中華人民共和國著作權法》相關規定或遵守《署名CC BY-ND 4.0國際》禁止演繹規范,合理合規,攜帶原創出處轉載,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/510651.html
標籤:C++
下一篇:C語言常見的八大排序(詳解)
