10

我很难将从用户模式类型 LPWSTR 传递的字符串与 LDR 表条目类型 UNICODE_STRING 进行比较

内核 C:

struct {
    int pid;
    int user_pid;
    int size;
    int protection_mode;
    int allocation_type;
    void* address;
    void* write_buffer;
    LPWSTR module_name;
}
userland_operation;

该结构通过 deviceiocontrol 传递给内核。对应的用户态结构如下:

public struct MemOperation
{
    public int Pid;
    public int UserPid;
    public int Size;
    public int protection_mode;
    public int allocation_type;
    public IntPtr Addr;
    public IntPtr WriteBuffer;
    [MarshalAs(UnmanagedType.LPWStr)] public String ModuleName;
}

其中字符串ModuleName被编组为 LPWStr。

ModuleName是进程中已加载模块的所需搜索词。现在,这就是事情变得棘手的地方。我可以通过 访问的字符串_LDR_DATA_TABLE_ENTRYUNICODE_STRING. 我想将此 UNICODE_STRING 与我的 LPWSTR 进行比较。

我尝试了以下方法,但没有奏效:

{
    UNICODE_STRING str;
    RtlInitUnicodeString(&str, module_name) // module name is the userland passed string LPWSTR
    if (RtlCompareUnicodeString(&str, &module_ldr->BaseDllName, TRUE) {


    }
}

我也尝试过 wcscmp 和其他一些东西。我不确定如何正确比较这两者。我在函数中添加了一些次要的伪代码,以提供关于我想要做什么的额外上下文。

NTSTATUS GetModuleList(HANDLE PID, PVOID UserBuffer, LPWSTR module_name) {
    KAPC_STATE APC;
    __try {
        PEPROCESS TargetProcess;

        PsLookupProcessByProcessId(PID, &TargetProcess);

        PPEB Peb = PsGetProcessPeb(TargetProcess);

        if (!Peb)
            return STATUS_INVALID_PARAMETER;

        KeStackAttachProcess(TargetProcess, &APC);

        UINT64 Ldr = (UINT64)Peb + PEBLDR_OFFSET;
        ProbeForRead((CONST PVOID)Ldr, 8, 8);

        PLIST_ENTRY ModListHead = (PLIST_ENTRY)(*(PULONG64)Ldr + PEBLDR_MEMORYLOADED_OFFSET);
        ProbeForRead((CONST PVOID)ModListHead, 8, 8);

        PLIST_ENTRY Module = ModListHead->Flink;

        while (ModListHead != Module) {
            LDR_DATA_TABLE_ENTRY* Module_Ldr = (LDR_DATA_TABLE_ENTRY*)(Module);

    //psuedo  if (module_name is in Module_Ldr->BaseDllName) // the comparison, where BaseDllName is type UNICODE_STRING

            Module = Module->Flink;
        }
        KeUnstackDetachProcess(&APC);
        ObDereferenceObject(TargetProcess);
        return STATUS_SUCCESS;
4

1 回答 1

0

在下面的这个电话中:

if (RtlCompareUnicodeString(&str, &module_ldr->BaseDllName) {

此函数采用您未传递的附加参数。请参考https://docs.microsoft.com/en-us/windows/win32/devnotes/rtlcompareunicodestring

于 2020-06-05T05:41:46.573 回答