驅動開發:內核字符串拷貝與比較

在上一篇文章《驅動開發:內核字符串轉換方法》中簡單介紹了內核是如何使用字符串以及字符串之間的轉換方法,本章將繼續探索字符串的拷貝與比較,與應用層不同內核字符串拷貝與比較也需要使用內核專用的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;
}

代碼輸出效果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章