在內核編程中字串有兩種格式ANSI_STRING與UNICODE_STRING,這兩種格式是微軟推出的安全版本的字串結構體,也是微軟推薦使用的格式,通常情況下ANSI_STRING代表的型別是char *也就是ANSI多位元組模式的字串,而UNICODE_STRING則代表的是wchar*也就是UNCODE型別的字符,如下文章將介紹這兩種字符格式在內核中是如何轉換的,
在內核開發模式下初始化字串也需要呼叫專用的初始化函式,如下分別初始化ANSI和UNCODE字串,我們來看看代碼是如何實作的,
#include <ntifs.h>
#include <ntstrsafe.h>
VOID UnDriver(PDRIVER_OBJECT driver)
{
DbgPrint("驅動卸載成功 \n");
}
NTSTATUS DriverEntry(IN PDRIVER_OBJECT Driver, PUNICODE_STRING RegistryPath)
{
// 定義內核字串
ANSI_STRING ansi;
UNICODE_STRING unicode;
UNICODE_STRING str;
// 定義普通字串
char * char_string = "hello lyshark";
wchar_t *wchar_string = (WCHAR*)"hello lyshark";
// 初始化字串的多種方式
RtlInitAnsiString(&ansi, char_string);
RtlInitUnicodeString(&unicode, wchar_string);
RtlUnicodeStringInit(&str, L"hello lyshark");
// 改變原始字串
char_string[0] = (CHAR)"A"; // char型別每個占用1位元組
char_string[1] = (CHAR)"B";
wchar_string[0] = (WCHAR)"A"; // wchar型別每個占用2位元組
wchar_string[2] = (WCHAR)"B";
// 輸出字串 %Z
DbgPrint("輸出ANSI: %Z \n", &ansi);
DbgPrint("輸出WCHAR: %Z \n", &unicode);
DbgPrint("輸出字串: %wZ \n", &str);
DbgPrint("驅動加載成功 \n");
Driver->DriverUnload = UnDriver;
return STATUS_SUCCESS;
}
代碼輸出效果:

內核中還可實作字串與整數之間的靈活轉換,內核中提供了RtlUnicodeStringToInteger這個函式來實作字串轉整數,與之對應的RtlIntegerToUnicodeString則是將整數轉為字串這兩個內核函式也是非常常用的,
#include <ntifs.h>
#include <ntstrsafe.h>
VOID UnDriver(PDRIVER_OBJECT driver)
{
DbgPrint("驅動卸載成功 \n");
}
// Power: lyshark
NTSTATUS DriverEntry(IN PDRIVER_OBJECT Driver, PUNICODE_STRING RegistryPath)
{
NTSTATUS flag;
ULONG number;
DbgPrint("hello lyshark \n");
UNICODE_STRING uncode_buffer_source = { 0 };
UNICODE_STRING uncode_buffer_target = { 0 };
// 字串轉為數字
// By:LyShark
RtlInitUnicodeString(&uncode_buffer_source, L"100");
flag = RtlUnicodeStringToInteger(&uncode_buffer_source, 10, &number);
if (NT_SUCCESS(flag))
{
DbgPrint("字串 -> 數字: %d \n", number);
}
// 數字轉為字串
uncode_buffer_target.Buffer = (PWSTR)ExAllocatePool(PagedPool, 1024);
uncode_buffer_target.MaximumLength = 1024;
flag = RtlIntegerToUnicodeString(number, 10, &uncode_buffer_target);
if (NT_SUCCESS(flag))
{
DbgPrint("數字 -> 字串: %wZ \n", &uncode_buffer_target);
}
// 釋放堆空間
RtlFreeUnicodeString(&uncode_buffer_target);
DbgPrint("驅動加載成功 \n");
Driver->DriverUnload = UnDriver;
return STATUS_SUCCESS;
}
代碼輸出效果:

繼續看另一種轉換模式,將UNICODE_STRING結構轉換成ANSI_STRING結構,代碼中呼叫了RtlUnicodeStringToAnsiString內核函式,該函式也是微軟提供的,
#include <ntifs.h>
#include <ntstrsafe.h>
VOID UnDriver(PDRIVER_OBJECT driver)
{
DbgPrint("驅動卸載成功 \n");
}
// Power: lyshark
NTSTATUS DriverEntry(IN PDRIVER_OBJECT Driver, PUNICODE_STRING RegistryPath)
{
DbgPrint("hello lyshark \n");
UNICODE_STRING uncode_buffer_source = { 0 };
ANSI_STRING ansi_buffer_target = { 0 };
// 初始化 UNICODE 字串
RtlInitUnicodeString(&uncode_buffer_source, L"hello lyshark");
// 轉換函式
NTSTATUS flag = RtlUnicodeStringToAnsiString(&ansi_buffer_target, &uncode_buffer_source, TRUE);
if (NT_SUCCESS(flag))
{
DbgPrint("ANSI: %Z \n", &ansi_buffer_target);
}
// 銷毀ANSI字串
RtlFreeAnsiString(&ansi_buffer_target);
DbgPrint("驅動加載成功 \n");
Driver->DriverUnload = UnDriver;
return STATUS_SUCCESS;
}
代碼輸出效果:

如果將上述程序反過來,將ANSI_STRING轉換為UNICODE_STRING結構,則需要呼叫RtlAnsiStringToUnicodeString這個內核專用函式實作,
#include <ntifs.h>
#include <ntstrsafe.h>
VOID UnDriver(PDRIVER_OBJECT driver)
{
DbgPrint("驅動卸載成功 \n");
}
// Power: lyshark
NTSTATUS DriverEntry(IN PDRIVER_OBJECT Driver, PUNICODE_STRING RegistryPath)
{
DbgPrint("hello lyshark \n");
UNICODE_STRING uncode_buffer_source = { 0 };
ANSI_STRING ansi_buffer_target = { 0 };
// 初始化字串
RtlInitString(&ansi_buffer_target, "hello lyshark");
// 轉換函式
NTSTATUS flag = RtlAnsiStringToUnicodeString(&uncode_buffer_source, &ansi_buffer_target, TRUE);
if (NT_SUCCESS(flag))
{
DbgPrint("UNICODE: %wZ \n", &uncode_buffer_source);
}
// 銷毀UNICODE字串
RtlFreeUnicodeString(&uncode_buffer_source);
DbgPrint("驅動加載成功 \n");
Driver->DriverUnload = UnDriver;
return STATUS_SUCCESS;
}
代碼輸出效果:

如上代碼是內核通用結構體之間的轉換型別,又是還需要將各類結構體轉為普通的字符型別,例如下方的兩個案例:
例如將UNICODE_STRING 轉為 CHAR*型別,
#define _CRT_SECURE_NO_WARNINGS
#include <ntifs.h>
#include <windef.h>
#include <ntstrsafe.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 };
ANSI_STRING ansi_buffer_target = { 0 };
char szBuf[1024] = { 0 };
// 初始化 UNICODE 字串
RtlInitUnicodeString(&uncode_buffer_source, L"hello lyshark");
// 轉換函式
NTSTATUS flag = RtlUnicodeStringToAnsiString(&ansi_buffer_target, &uncode_buffer_source, TRUE);
if (NT_SUCCESS(flag))
{
strcpy(szBuf, ansi_buffer_target.Buffer);
DbgPrint("輸出char*字串: %s \n", szBuf);
}
// 銷毀ANSI字串
RtlFreeAnsiString(&ansi_buffer_target);
DbgPrint("驅動加載成功 \n");
Driver->DriverUnload = UnDriver;
return STATUS_SUCCESS;
}
代碼輸出效果:

如果反過來,將 CHAR*型別轉為UNICODE_STRING結構呢,可以進行中轉最終轉為UNICODE_STRING結構體,
#define _CRT_SECURE_NO_WARNINGS
#include <ntifs.h>
#include <windef.h>
#include <ntstrsafe.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 };
ANSI_STRING ansi_buffer_target = { 0 };
// 設定CHAR*
char szBuf[1024] = { 0 };
strcpy(szBuf, "hello lyshark");
// 初始化ANSI字串
RtlInitString(&ansi_buffer_target, szBuf);
// 轉換函式
NTSTATUS flag = RtlAnsiStringToUnicodeString(&uncode_buffer_source, &ansi_buffer_target, TRUE);
if (NT_SUCCESS(flag))
{
DbgPrint("UNICODE: %wZ \n", &uncode_buffer_source);
}
// 銷毀UNICODE字串
RtlFreeUnicodeString(&uncode_buffer_source);
DbgPrint("驅動加載成功 \n");
Driver->DriverUnload = UnDriver;
return STATUS_SUCCESS;
}
代碼輸出效果:

文章出處:https://www.cnblogs.com/LyShark/p/16739228.html
著作權宣告:本博客文章與代碼均為學習時整理的筆記,文章 [均為原創] 作品,轉載請 [添加出處] ,您添加出處是我創作的動力!
轉載文章,請遵守《中華人民共和國著作權法》相關規定或遵守《署名CC BY-ND 4.0國際》禁止演繹規范,合理合規,攜帶原創出處轉載,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/509755.html
標籤:C++
上一篇:驅動開發:內核中的自旋鎖結構
下一篇:Protobuf生成檔案報錯
