KdPrint/DbgPrint and UNICODE_STRING/ANSI_STRING

本文介绍了如何使用 MS 扩展的内核 CRTL 函数以简化格式输出 Unicode 和 ANSI 字符串的方法。通过 Z 格式说明符可以轻松地将 PUNICODE_STRING 和 PANSI_STRING 类型的指针内容输出到 KdPrint 等函数中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

typedef struct _STRING {
    USHORT Length;
    USHORT MaximumLength;
    PCHAR Buffer;
} STRING;
typedef STRING *PSTRING;

typedef STRING ANSI_STRING;
typedef PSTRING PANSI_STRING;

To make life easier MS have extended kernel CRTL output() function with Z format specifier. This works for all kernel functions those understand formatted strings (e.g. sprintf_vsnprintfKdPrint/DbgPrint). For example:

PUNICODE_STRING pUStr;
PANSI_STRING    pAStr;
...
KdPrint(("Unicode string: %wZ\n", pUStr));
KdPrint(("ANSI    string: %Z\n",  pAStr));

Though, you can use a little more complicated documented way. Btw, this form is suitable for printing byte array of strictly defined length.

KdPrint(("Unicode string: %*.*ws\n",pUStr->Length/sizeof(WCHAR),
    pUStr->Length/sizeof(WCHAR), pUStr));
KdPrint(("Unicode string: %*.*S\n",pUStr->Length/sizeof(WCHAR),
    pUStr->Length/sizeof(WCHAR), pUStr));
KdPrint(("ANSI    string: %*.*s\n", pAStr->Length/sizeof(CHAR),
    pAStr->Length/sizeof(CHAR),  pAStr));

Or, if you want to take into account NULL-terminator, but limit output length to specified number of characters:

KdPrint(("Unicode string: %.*ws\n",
    pUStr->Length/sizeof(WCHAR), pUStr));
KdPrint(("Unicode string: %.*S\n",
    pUStr->Length/sizeof(WCHAR), pUStr));
KdPrint(("ANSI    string: %.*s\n",
    pAStr->Length/sizeof(CHAR),  pAStr));

分析下这个代码PVOID SearchSpecialCode1(PVOID pSearchBeginAddr, ULONG ulSearchLength, PUCHAR pSpecialCode, ULONG ulSpecialCodeLength) { PVOID pDestAddr = NULL; PUCHAR pBeginAddr = (PUCHAR)pSearchBeginAddr; PUCHAR pEndAddr = pBeginAddr + ulSearchLength; PUCHAR i = NULL; ULONG j = 0; KdPrint(("pEndAddr:%p %s\n", pEndAddr)); for (i = pBeginAddr; i <= pEndAddr; i++)//遍历地址 { if (MmIsAddressValid((PVOID)(i)))KdPrint(("dz:%p %p\n", i, *(PUCHAR)(i))); // 遍历特征码 for (j = 0; j < ulSpecialCodeLength; j++) { // 判断地址是否有效 if (!MmIsAddressValid((PVOID)(i + j))) { break; } // 匹配特征码 if (*(PUCHAR)(i + j) != pSpecialCode[j]) { break; } } // 匹配成功 if (j >= ulSpecialCodeLength) { pDestAddr = (PVOID)i; break; } } return pDestAddr; } struct _MMPTE//全名_MMPTE_PROTOTYPE { ULONGLONG Valid : 1; //0x0 ULONGLONG DemandFillProto : 1; //0x0 ULONGLONG HiberVerifyConverted : 1; //0x0 ULONGLONG ReadOnly : 1; //0x0 ULONGLONG SwizzleBit : 1; //0x0 ULONGLONG Protection : 5; //0x0 ULONGLONG Prototype : 1; //0x0 ULONGLONG Combined : 1; //0x0 ULONGLONG Unused1 : 4; //0x0 LONGLONG ProtoAddress : 48; //0x0 }; typedef struct _MMPFN { void* padding1; void* PteAddress;//0x8 struct _MMPTE OriginalPte; //0x10 char padding2[0x18];//_MMPFN 结构是0x30大小 这里0x18给他补上 }_MMPFN, * P_MMPFN; P_MMPFN* get_MmPfnDataBase() { UNICODE_STRING st = { 0 }; RtlInitUnicodeString(&st, L"MmGetVirtualForPhysical"); PVOID start = MmGetSystemRoutineAddress(&st); ULONG64 sub140608650start = SearchSpecialCode1(start, 0x20, "\x48\x03\xD2", 3); //DbgBreakPoint(); ULONG64 sub140608650start1 = sub140608650start + 0x5; P_MMPFN* fanh = *(LONG64*)sub140608650start1; return fanh; } UINT64 va_to_pa(void* va) { return MmGetPhysicalAddress(va).QuadPart; } BOOLEAN hide_mem(HANDLE pid, void* va, ULONG attribute) { PEPROCESS process = 0; KAPC_STATE apc; NTSTATUS status; //DbgBreakPoint(); status = PsLookupProcessByProcessId(pid, &process); if (!NT_SUCCESS(status)) { KdPrint(("PsLookupProcessByProcessId ...sb status:0x%x\n", status)); return FALSE; } KeStackAttachProcess((PVOID)process, &apc); void* align_va = PAGE_ALIGN(va); UINT64 pa = va_to_pa(align_va); if (pa == 0) { KdPrint(("va_err\n")); ObDereferenceObject(process); KeUnstackDetachProcess(&apc); return FALSE; } UINT32 pfn = pa >> 12; //P_MMPFN MmPfndataBase = get_MmPfnDataBase(); // 假设返回类型是PMMPFN ULONG64 mmpf = get_MmPfnDataBase(); P_MMPFN MmPfndataBase1 = mmpf - 0x8; P_MMPFN mmpfn = &MmPfndataBase1[pfn]; mmpfn->OriginalPte.Protection = attribute; ObDereferenceObject(process); KeUnstackDetachProcess(&apc); return TRUE; }
08-10
PVOID SearchSpecialCode1(PVOID pSearchBeginAddr, ULONG ulSearchLength, PUCHAR pSpecialCode, ULONG ulSpecialCodeLength) { PVOID pDestAddr = NULL; PUCHAR pBeginAddr = (PUCHAR)pSearchBeginAddr; PUCHAR pEndAddr = pBeginAddr + ulSearchLength; PUCHAR i = NULL; ULONG j = 0; KdPrint(("pEndAddr:%p %s\n", pEndAddr)); for (i = pBeginAddr; i <= pEndAddr; i++)//遍历地址 { if (MmIsAddressValid((PVOID)(i)))KdPrint(("dz:%p %p\n", i, *(PUCHAR)(i))); // 遍历特征码 for (j = 0; j < ulSpecialCodeLength; j++) { // 判断地址是否有效 if (!MmIsAddressValid((PVOID)(i + j))) { break; } // 匹配特征码 if (*(PUCHAR)(i + j) != pSpecialCode[j]) { break; } } // 匹配成功 if (j >= ulSpecialCodeLength) { pDestAddr = (PVOID)i; break; } } return pDestAddr; } struct _MMPTE//全名_MMPTE_PROTOTYPE { ULONGLONG Valid : 1; //0x0 ULONGLONG DemandFillProto : 1; //0x0 ULONGLONG HiberVerifyConverted : 1; //0x0 ULONGLONG ReadOnly : 1; //0x0 ULONGLONG SwizzleBit : 1; //0x0 ULONGLONG Protection : 5; //0x0 ULONGLONG Prototype : 1; //0x0 ULONGLONG Combined : 1; //0x0 ULONGLONG Unused1 : 4; //0x0 LONGLONG ProtoAddress : 48; //0x0 }; typedef struct _MMPFN { void* padding1; void* PteAddress;//0x8 struct _MMPTE OriginalPte; //0x10 char padding2[0x18];//_MMPFN 结构是0x30大小 这里0x18给他补上 }_MMPFN, * P_MMPFN; P_MMPFN* get_MmPfnDataBase() { UNICODE_STRING st = { 0 }; RtlInitUnicodeString(&st, L"MmGetVirtualForPhysical"); PVOID start = MmGetSystemRoutineAddress(&st); ULONG64 sub140608650start = SearchSpecialCode1(start, 0x20, "\x48\x03\xD2", 3); //DbgBreakPoint(); ULONG64 sub140608650start1 = sub140608650start + 0x5; P_MMPFN* fanh = *(LONG64*)sub140608650start1; return fanh; } UINT64 va_to_pa(void* va) { return MmGetPhysicalAddress(va).QuadPart; } BOOLEAN hide_mem(HANDLE pid, void* va, ULONG attribute) { PEPROCESS process = 0; KAPC_STATE apc; NTSTATUS status; //DbgBreakPoint(); status = PsLookupProcessByProcessId(pid, &process); if (!NT_SUCCESS(status)) { KdPrint((“PsLookupProcessByProcessId …sb status:0x%x\n”, status)); return FALSE; } KeStackAttachProcess((PVOID)process, &apc); void* align_va = PAGE_ALIGN(va); UINT64 pa = va_to_pa(align_va); if (pa == 0) { KdPrint(("va_err\n")); ObDereferenceObject(process); KeUnstackDetachProcess(&apc); return FALSE; } UINT32 pfn = pa >> 12; //P_MMPFN MmPfndataBase = get_MmPfnDataBase(); // 假设返回类型是PMMPFN ULONG64 mmpf = get_MmPfnDataBase(); P_MMPFN MmPfndataBase1 = mmpf - 0x8; P_MMPFN mmpfn = &MmPfndataBase1[pfn]; mmpfn->OriginalPte.Protection = attribute; ObDereferenceObject(process); KeUnstackDetachProcess(&apc); return TRUE; }这些代码修改内存来对抗crc 效果怎么样
08-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值