【问题标题】:Ctypes- Can't get PEB Address using NtQueryInformationProcessCtypes-无法使用 NtQueryInformationProcess 获取 PEB 地址
【发布时间】:2019-08-31 17:24:31
【问题描述】:

我正在使用ctypes 并尝试使用NtQueryInformationProcess 函数获取PEB 地址。

返回值为0,表示函数成功完成。 但是PROCESS_BASIC_INFORMATION 结构(作为第三个参数给出)不包含PEB 地址。

class PROCESS_BASIC_INFORMATION(Structure):
    _fields_ = [
        ("Reserved1", c_void_p),
        ("PebBaseAddress", DWORD),
        ("Reserved2", c_void_p * 2),
        ("UniqueProcessId", DWORD),
        ("Reserved3", c_void_p)]

ntdll = WinDLL('ntdll')
NTSTATUS = LONG
ntdll.argtypes = [HANDLE, DWORD, c_void_p, DWORD, PDWORD]
ntdll.restype = NTSTATUS
processInformation = PROCESS_BASIC_INFORMATION()
processInformationLength = sizeof(PROCESS_BASIC_INFORMATION)
result = ntdll.NtQueryInformationProcess(hProcess, 0, processInformation, processInformationLength, byref(DWORD(0)))

考虑返回值为0的事实,可能是什么问题?

【问题讨论】:

  • 函数返回后PEB字段包含什么?

标签: python python-3.x winapi ctypes


【解决方案1】:

上市[Python 3.Docs]: ctypes - A foreign function library for Python

  1. 您为 错误 对象 (ntdll) 定义了 argtypesrestype。您应该为ntdll.NtQueryInformationProcess 定义它们。当我们在这里时,很明显您喜欢 DWORD。即使在这种情况下没有区别,也要保持函数签名与 C 中的一致:

    ntdll.NtQueryInformationProcess.argtypes = [HANDLE, c_int, c_void_p, ULONG, PULONG]
    ntdll.NtQueryInformationProcess.restype = NTSTATUS
    

    未能在函数上定义 argtypesrestype(或不正确),可能(并且很可能会)导致:

  2. 根据[MS.Docs]: NtQueryInformationProcess function强调是我的):

    ProcessInformation

    一个指针,指向由调用应用程序提供的缓冲区,函数将请求的信息写入该缓冲区。

    因此,您的调用应如下所示(通过引用传递 processInformation):

    result = ntdll.NtQueryInformationProcess(hProcess, 0, byref(processInformation), processInformationLength, byref(DWORD(0)))
    
  3. 根据同一页面,您的结构定义不正确。应该是:

    class PROCESS_BASIC_INFORMATION(Structure):
        _fields_ = [
            ("Reserved1", c_void_p),
            ("PebBaseAddress", c_void_p),
            ("Reserved2", c_void_p * 2),
            ("UniqueProcessId", c_void_p),
            ("Reserved3", c_void_p),
        ]
    

    您的版本(在 64 位 上)短了 8 个字节(因为 DWORD 和指针之间的(2)大小差异),导致传递的缓冲区太短到函数,这是Undefined B行为(它可能导致崩溃)。

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 1970-01-01
    • 2010-09-19
    • 2021-05-16
    相关资源
    最近更新 更多