_UNICODE_STRING

#pragma once
#include <ntifs.h>

#define  MAX_PATH 260
#define  BUFFER_SIZE 0x400
/********************************************/
/* 初始化                                    */
/********************************************/
void Sub_1(); //常量内存
void Sub_2(); //栈区内存
void Sub_3(); //动态内存
void Sub_4();//利用WCHAR
void SubI_1();//初始化常数字符串的一个宏
void Sub_9();//初始化为空

/************************************************************************/
/* 拷贝操作                                                             */
/************************************************************************/
void Sub_5();

/************************************************************************/
/*//字符串串联                                                         */
/************************************************************************/
void Sub_10();

/************************************************************************/
/*//字符串打印                                                       */
/************************************************************************/
void Sub_11();

/************************************************************************/
/* 转换                                                                 */
/************************************************************************/

BOOLEAN UnicodeStringToChar(char* DestinationString, PUNICODE_STRING SourceString);

BOOLEAN IsUnicodeStringValid(PUNICODE_STRING SourceString);

VOID DriverUnload(PDRIVER_OBJECT DriverObject);

  

#include "UnicodeString(Kernel).h"
//bp MyDriver1!DriverEntry

NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegisterPath)
{
	NTSTATUS Status = STATUS_SUCCESS;

	PDEVICE_OBJECT  DeviceObject = NULL;

	DriverObject->DriverUnload = DriverUnload;
	//Sub_1();
	Sub_10();
	return Status;
}

void Sub_1()
{
	UNICODE_STRING v1;
	RtlInitUnicodeString(&v1, L"HelloWorld");
	CHAR v2[20] = { 0 };
	/*
	v1.Buffer = 常量指针
	v1.Length = 20
	v1.MaximumLength = 22
	*/
	UnicodeStringToChar(v2, &v1);
	DbgPrint("%s\r\n", v2);
	DbgPrint("%wZ\r\n", &v1); //UNICODE_STRING要用 wZ输出 记住!!!
}

void Sub_2()
{
	UNICODE_STRING v1;
	WCHAR BufferData[] = L"HelloWorld";
	v1.Buffer = BufferData;
	v1.Length = wcslen(BufferData)*sizeof(WCHAR);
	v1.MaximumLength = (wcslen(BufferData)+1)*sizeof(WCHAR);
	DbgPrint("%wZ\r\n", &v1);

}

void Sub_3()
{
	UNICODE_STRING v1;
	WCHAR BufferData[] = L"HelloWorld";
	v1.Length = wcslen(BufferData) * sizeof(WCHAR);
	v1.MaximumLength = (wcslen(BufferData) + 1) * sizeof(WCHAR);
	v1.Buffer = ExAllocatePool(PagedPool, v1.MaximumLength);
	RtlZeroMemory(v1.Buffer, v1.MaximumLength);
	RtlCopyMemory(v1.Buffer,BufferData,v1.Length);

	DbgPrint("%wZ\r\n", &v1);
	if (v1.Buffer!=NULL)
	{
		ExFreePool(v1.Buffer);
		v1.Buffer = NULL;
		v1.Length = v1.MaximumLength = 0;
	}
}
void Sub_4()
{
	UNICODE_STRING str = { 0 };
	WCHAR strBuf[128] = { 0 };
	str.Buffer = strBuf;
	wcscpy(str.Buffer, L"hello");
	str.Length = str.MaximumLength = wcslen(L"hello") * sizeof(WCHAR);
	DbgPrint("%wZ\r\n", &str);
}
void SubI_1()
{
	UNICODE_STRING str = RTL_CONSTANT_STRING(L"hello");//用于初始化常数字符串的一个宏
	DbgPrint("%wZ\r\n", &str);
}
void Sub_9()//初始化为拥有缓冲长度为256的UNICODE_STRING空串
{
	UNICODE_STRING str;
	WCHAR str_buf[256] ;
	RtlInitEmptyUnicodeString(&str, str_buf ,256 * sizeof(WCHAR));
}

//拷贝操作
void Sub_5()
{
	UNICODE_STRING SourceString;
	RtlInitUnicodeString(&SourceString, L"HelloWorld");

	UNICODE_STRING DestinationString = { 0 };
	DestinationString.Buffer = (PWSTR)ExAllocatePool(PagedPool, BUFFER_SIZE);
	DestinationString.MaximumLength = BUFFER_SIZE;

    RtlCopyUnicodeString(&DestinationString, &SourceString);
	KdPrint(("SourceString:%wZ\n", &SourceString));
	KdPrint(("DestinationString:%wZ\n", &DestinationString));
	RtlFreeUnicodeString(&DestinationString);

}

//字符串串联

void Sub_10()
{
	UNICODE_STRING SourceString;
	RtlInitUnicodeString(&SourceString, L"HelloWorld");

	UNICODE_STRING DestinationString = { 0 };
	DestinationString.Buffer = (PWSTR)ExAllocatePool(PagedPool, BUFFER_SIZE);
	DestinationString.MaximumLength = BUFFER_SIZE;

    RtlCopyUnicodeString(&DestinationString, &SourceString);
	KdPrint(("SourceString:%wZ\n", &SourceString));
	KdPrint(("DestinationString:%wZ\n", &DestinationString));
	RtlAppendUnicodeStringToString(&DestinationString, &SourceString);
	KdPrint(("DestinationString:%wZ\n", &DestinationString));
	DbgPrint("%wZ\r\n", &DestinationString);
	RtlFreeUnicodeString(&DestinationString);
}

//字符串打印
void Sub_11()
{
	//在不能保证字符串的结尾为空时,尽量不要用%ws %s来打印
		UNICODE_STRING SourceString;
	RtlInitUnicodeString(&SourceString, L"HelloWorld");

	UNICODE_STRING DestinationString = { 0 };
	DestinationString.Buffer = (PWSTR)ExAllocatePool(PagedPool, BUFFER_SIZE);
	DestinationString.MaximumLength = BUFFER_SIZE;

    RtlCopyUnicodeString(&DestinationString, &SourceString);
	KdPrint(("SourceString:%wZ\n", &SourceString));
	KdPrint(("DestinationString:%wZ\n", &DestinationString));
	RtlAppendUnicodeStringToString(&DestinationString, &SourceString);
	KdPrint(("DestinationString:%wZ\n", &DestinationString));//Dbgprint无论是发行般还是调试般都有效,可以定义个宏 即 Kdprint(a)要用双重括号
	DbgPrint("%wZ\r\n", &DestinationString);//必须是PASSIVE_LEVEL
	RtlFreeUnicodeString(&DestinationString);
}
VOID DriverUnload(PDRIVER_OBJECT DriverObject)
{
	DbgPrint("DriverUnload()\r\n");
}

BOOLEAN UnicodeStringToChar(char* DestinationString, PUNICODE_STRING SourceString)
{
	ANSI_STRING	v1;
	NTSTATUS	Status;
	char*		v2 = NULL;
	__try
	{
		Status = RtlUnicodeStringToAnsiString(&v1, SourceString, TRUE);
		if (v1.Length < MAX_PATH)
		{
			v2 = (PCHAR)v1.Buffer;
			strcpy(DestinationString, _strupr(v2));
		}
		RtlFreeAnsiString(&v1);
	}
	__except (EXCEPTION_EXECUTE_HANDLER)
	{
		return FALSE;
	}
	return TRUE;
}

BOOLEAN IsUnicodeStringValid(PUNICODE_STRING SourceString)
{
	ULONG i = 0;

	__try
	{
		if (!MmIsAddressValid(SourceString))
		{
			return FALSE;
		}
		if (SourceString->Buffer == NULL || SourceString->Length == 0)
		{
			return FALSE;
		}
		for (i = 0; i < SourceString->Length; i++)
		{
			if (!MmIsAddressValid((PUCHAR)SourceString->Buffer + i))
			{
				return FALSE;
			}
		}
	}
	__except (EXCEPTION_EXECUTE_HANDLER)
	{
		return FALSE;
	}
	return TRUE;
}

  

typedef struct _UNICODE_STRING
{
USHORT Length;
USHORT MaximumLength;
PWCHAR Buffer;
}UNICODE_STRING,*PUNICODE_STRING;

kd> dt v1
dtx is unsupported for this scenario. It only recognizes dtx [<type>] [<address>] with -a, -h, and -r. Reverting to dt.
Local var @ 0x8df079c0 Type _UNICODE_STRING
"HelloWorld"
+0x000 Length : 0x14
+0x002 MaximumLength : 0x16
+0x004 Buffer : 0xa60e4082 "HelloWorld"
kd> db 0xa60e4082
a60e4082 48 00 65 00 6c 00 6c 00-6f 00 57 00 6f 00 72 00 H.e.l.l.o.W.o.r.
a60e4092 6c 00 64 00 00 00 25 77-5a 0d 0a 00 44 72 69 76 l.d...%wZ...Driv
a60e40a2 65 72 55 6e 6c 6f 61 64-28 29 0d 0a 00 00 00 00 erUnload()......
a60e40b2 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
a60e40c2 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
a60e40d2 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
a60e40e2 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
a60e40f2 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................

时间: 2024-08-28 01:56:07

_UNICODE_STRING的相关文章

Windows7 x64 了解堆

一.前言 堆对于开发者一般来说是熟悉又陌生的,熟悉是因为我们常常使用new/delete或者malloc/free使用堆,陌生是因为我们基本没有去了解堆的结构.堆在什么地方?怎么申请?怎么释放?系统又是怎么管理堆的呢? 带着疑问,这两天看了<软件漏洞分析技术>与<漏洞战争>中关于堆的说明,终于对于堆有一点点的了解了.这里记录一下在学习和调试中的一点笔记. 二.关于堆的基本知识 1).首先了解空闲双向链表和快速单向链表的概念 1.空闲双向链表(空表) 空闲堆块的块首中包含一对重要的指

文件删除的windows下面的三种路径(轻量级)

文件常规删除的三种方法都比较熟悉. #define FILE_DELETE_ON_CLOSE 0x00001000 @1 Nt/ZwCreateFile Nt/ZwOpenFile 填充OpenPacket结构,标记FILE_DELETE_ON_CLOSE (WRK) @2 Nt/ZwDeleteFile 填充OpenPacket结构,标记FILE_DELETE_ON_CLOSE (WRK) @3 Nt/ZwSetInformationFile 使用FileDispositionInformat

windbg命令详解

DLL 该扩展仅在内核模式下使用,即使它是在Ext.dll中的. Windows NT 4.0 Ext.dll Windows 2000 Ext.dll Windows XP和之后 Ext.dll 注释 如果不提供参数,调试器会列出所有进程,以及时间和优先级统计.这和使用!process @#Process 0 作为CommandString值一样. To terminate execution at any point, press CTRL+BREAK (in WinDbg) or CTRL

硬件断点 DrxHook

硬件断点的实现需要依赖于调试寄存器 DR0~DR7  调试寄存器 DR0~DR3-----调试地址寄存器DR4~DR5-----保留DR6 -----调试状态寄存器 指示哪个调试寄存器被命中DR7 -----调试控制寄存器 关于Dr7寄存器每个标志位的解释: 总结如下 DR7调试控制寄存器: R/W0~R/W3:与DR0~DR3相对应,用来指定监控地址的访问类型,表示意义如下:              00:仅当执行对应的地址时中断              01:仅当写入对应的地址时中断 

DbgPrint格式 输出

DbgPrint 输出 1) 直接打印字符串.DbgPrint(“Hello World!”); 2) 空结尾的字符串,你可以用普通得C语法表示字符串常量char variable_string[]  =  “Hello World”;DbgPrint(“%s”, variable_string); 3) 空结尾的宽字符串(WCHAR类型)WCHAR     string_w[]  =  L“Hello World!”;DbgPrint(“%ws”,  string_w); 或者 DbgPrin

PEB结构

PEB进程环境快,下面是其一些结构 //===============================================================================================// typedef struct _UNICODE_STR { USHORT Length; USHORT MaximumLength; PWSTR pBuffer; } UNICODE_STR, *PUNICODE_STR; // WinDbg> dt -v ntd

对象管理器

对象管理器使用对象头中保存的数据来管理这些对象,而无需关注它们的类型,标准对象头中的属性 1.对象名称:使一个对象对于其他的进程也是可见的,便于共享 2.对象目录:提供了一个层次结构来存储对象名称 3.安全描述符:决定了谁可以使用该对象,以及允许它们如何使用它 4.配额花费:列出当一个进程打开一个指向该对象的句柄时,针对该进程收取的资源花费额 5.已打开的句柄计数:记录了"打开一个句柄来指向该对象"的次数 6.对象类型:指向一个类型对象,该对象包含了针对这种类型的对象都是公共的属性 7

进程自我保护 适用于WIN7 X64

1 //进程自我保护,注意只有X64 WIN7可用 2 #include <ntddk.h> 3 #define PROCESS_TERMINATE 1 4 typedef struct _LDR_DATA // 24 elements, 0xE0 bytes (sizeof) 5 { 6 struct _LIST_ENTRY InLoadOrderLinks; // 2 elements, 0x10 bytes (sizeof) 7 struct _LIST_ENTRY InMemoryOr

内核模式下的字符串操作

分类: WINDOWS 1)ASCII字符串和宽字符串    在应用程序中使用两种字符:a) char型字符串,负责记录ANSI字符集,它是指向一个char数组的指针,每个char型变量大小是一个字节,字符串是以0标志字符串结束的:b) wchar_t型的宽字符串,负责描述unicode字符集,它是指向一个wchar_t数组的指针,wchar_t字符大小为两个字节,字符串以0标志字符串结束.   例: ANSI字符构造如下:   char *str1 = "ASCE";   UNICO