查找 Windows 内核函数的地址

逆向工程 视窗 调试器
2021-07-03 06:31:41

如何找到 Windows 内核函数的地址?

在这种情况下,我试图找到 CreateThread。

这可以从调试器完成吗?奥利/免疫?

3个回答

它可以用编程的组合来完成NtQuerySystemInformationLoadLibraryExGetProcAddress

下面的代码可能无法完美运行,因为我没有 Windows 框来构建它以进行测试。但是,它应该使您朝着正确的方向前进。

#include "stdafx.h"

#include <string.h>
#include <windows.h>

enum { SystemModuleInformation = 11 };

typedef struct _RTL_PROCESS_MODULE_INFORMATION {
    ULONG Section;
    PVOID MappedBase;
    PVOID ImageBase;
    ULONG ImageSize;
    ULONG Flags;
    USHORT LoadOrderIndex;
    USHORT InitOrderIndex;
    USHORT LoadCount;
    USHORT OffsetToFileName;
    CHAR FullPathName[256];
} RTL_PROCESS_MODULE_INFORMATION, *PRTL_PROCESS_MODULE_INFORMATION;

typedef struct _RTL_PROCESS_MODULES {
    ULONG NumberOfModules;
    RTL_PROCESS_MODULE_INFORMATION Modules[1];
} RTL_PROCESS_MODULES, *PRTL_PROCESS_MODULES;

typedef NTSTATUS (*NtQuerySystemInformationFunc)(
    _In_      DWORD SystemInformationClass,
    _Inout_   PVOID                    SystemInformation,
    _In_      ULONG                    SystemInformationLength,
    _Out_opt_ PULONG                   ReturnLength
);

ULONG64 GetKernelFunctionAddress(LPCSTR Name) {
    NtQuerySystemInformationFunc NtQuerySystemInformation = NULL;
    HMODULE hKernel = NULL;
    HMODULE hNtdll = NULL;
    ULONG64 KernelBase = NULL;
    ULONG64 KernelFunctionAddress = NULL;
    RTL_PROCESS_MODULES ModuleInfo = { 0 };

    // Get the address of NtQuerySystemInformation
    hNtdll = GetModuleHandle("ntdll");
    NtQuerySystemInformation = (NtQuerySystemInformationFunc)GetProcAddress(hNtdll, "NtQuerySystemInformation");

    // Get the base address of the kernel
    NtQuerySystemInformation(SystemModuleInformation, &ModuleInfo, sizeof(ModuleInfo), NULL);
    KernelBase = (ULONG64)ModuleInfo.Modules[0].ImageBase;

    // Load the kernel
    hKernel = LoadLibraryEx(strrchr(ModuleInfo.Modules[0].FullPathName, '\\') + 1, 0, LOAD_LIBRARY_AS_IMAGE_RESOURCE);

    // Look up the function in the kernel
    KernelFunctionAddress = (ULONG64)GetProcAddress(hKernel, Name);

    // Adjust the address based on the kernel load address
    KernelFunctionAddress -= (ULONG64)hKernel;
    KernelFunctionAddress += KernelBase;

    return KernelFunctionAddress;
}

从 WinDbg,您可以使用该x命令。

例如:

kd> x nt!NtCreateThread
830e4fda          nt!NtCreateThread (<no parameter info>)

当我查找上述NtCreateThread函数的地址时,WinDbg 告诉我它NtCreateThread位于 address 0x830e4fda

(您可能想改用LiveKd,因为它比连接远程内核调试器更容易。)

在 Ollydbg 中,您可以选择反汇编窗口并点击CTRL + g将出现一个对话框,只需输入即可CreateThread搜索区分大小写。

编辑

这不适用于 Windows 内核函数,但它适用于由正在调试的程序导入的 DLL 中的任何函数。由于您正在搜索CreateThread地址,因此我认为这就是您问题中的意思。