開發環境:win10+vs2019+wdk10+vm_win7_x86
本文內容:介紹內核中常見的字符串操作,字符串定義,字符串初始化,字符串複製,連接,比較。
說明:環境部分大家自己裝,網上教程很多,這裏就不重複了。由於自己內核,驅動這方面知識缺乏,怕工作中偶爾碰見這樣的病毒,繞不過的,只得學!大家加油!直接貼代碼,註釋我都標好了。
#include <ntddk.h>
#define BUFFER_SIZE 256
//驅動卸載函數,不寫的話驅動無法卸載,只能重啓卸載
NTSTATUS Unload(PDRIVER_OBJECT driver)
{
DbgPrint("driver unload success.....\n");
return STATUS_SUCCESS;
}
//入口點,類似於mian(),雙擊調試時windbg可以下斷點:driver_name!DriverEntry,就可以斷在這裏了
NTSTATUS DriverEntry(PDRIVER_OBJECT driver, PUNICODE_STRING reg_path)
{
//內核調試輸出,類似於printf,DbgPrint和KdPrint一樣,注意參數,KdPrint在發佈版會被優化掉
/*
DbgPrint("this is my first driver.....\n");
*/
//下面是內核中的字符串類型,內核中常用的是unicode類型的字符串,使用時得用RtlInitUnicodeString()函數初始化,寬字符長度前面加個L
/*
CHAR* char_string = "hello CHAR";
WCHAR* wchar_string = L"hello WCHAR";
UNICODE_STRING unicode_string;
RtlInitUnicodeString(&unicode_string, L"hello unicode string");
KdPrint(("%s", char_string));
KdPrint(("%S", wchar_string));
KdPrint(("%wZ", &unicode_string));
*/
//下面是字符串的常見操作
//1.字符串複製,記得初始化目標buffer
/*
UNICODE_STRING source_string;
UNICODE_STRING dest_string;
RtlInitUnicodeString(&source_string, L"this is source string");
//分配內存
dest_string.Buffer = (PWSTR)ExAllocatePool(PagedPool, BUFFER_SIZE);
dest_string.MaximumLength = BUFFER_SIZE;
//複製到目標buffer
RtlCopyUnicodeString(&dest_string, &source_string);
KdPrint(("dest string is :%wZ", &dest_string));
//記得釋放開闢的buffer
RtlFreeUnicodeString(&dest_string);
*/
//2.字符串連接,使用RtlAppendUnicodeToString()函數
/*
UNICODE_STRING source_string;
UNICODE_STRING dest_string;
RtlInitUnicodeString(&source_string, L"hello wait append : ");
dest_string.Buffer = (PWSTR)ExAllocatePool(PagedPool, BUFFER_SIZE);
dest_string.MaximumLength = BUFFER_SIZE;
RtlCopyUnicodeString(&dest_string, &source_string);
if (STATUS_BUFFER_TOO_SMALL == RtlAppendUnicodeToString(&dest_string, L"append string"))
{
KdPrint(("STATUS_BUFFER_TOO_SMALL"));
}
KdPrint(("%wZ", &dest_string));
RtlFreeUnicodeString(&dest_string);
*/
//3.字符串比較,使用RtlCompareUnicodeString()函數
/*
UNICODE_STRING compare_string_1;
UNICODE_STRING compare_string_2;
int return_num = 0;
RtlInitUnicodeString(&compare_string_1, L"I am string 1");
RtlInitUnicodeString(&compare_string_2, L"I am string 2");
//TRUE:區分大小寫,FALSE:不區分大小寫
return_num = RtlCompareUnicodeString(&compare_string_1, &compare_string_2, TRUE);
if (return_num == 0)
{
KdPrint(("字符串1等與字符串2\n"));
}
if (return_num > 0)
{
KdPrint(("字符串1大於字符串2\n"));
}
if (return_num < 0)
{
KdPrint(("字符串1小於字符串2\n"));
}
*/
//定義驅動卸載函數
driver->DriverUnload = Unload;
return STATUS_SUCCESS;
}