python - Ctypes-无法使用 NtQueryInformationProcess 获取 PEB 地址

标签 python python-3.x winapi ctypes

我正在使用 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 的事实,可能是什么问题?

最佳答案

列表[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

    A pointer to a buffer supplied by the calling application into which the function writes the requested information.

    因此,您的调用应该类似于(通过引用传递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)大小差异),导致传递的缓冲区太短函数,这是U未定义的B行为(它可能导致崩溃)。

关于python - Ctypes-无法使用 NtQueryInformationProcess 获取 PEB 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57740201/

相关文章:

python - 如何使用Python Pandas使用动态字符串来过滤数据帧

Python 双下划线重整

C/C++ Allegro 程序导致 Windows 7 切换到 Aero Basic

python - 如何在 TensorFlow 中使用伯努利分布初始化变量?

python - Pandas:将每一行转换为 <column name,row value> 字典并添加为新列

python - 如何将 PostgreSQL 警告提升为 psycopg2 中的异常?

python - urllib.request.urlopen 返回字节,但我无法对其进行解码

python - PyQT5 - 警告使用 'exit' 、 'quit' 或 Ctrl-D 退出 jupyter notebook 中的应用程序

c - 如何使用 win32 API 获取链接标签的句柄

c++ - 如何在 Windows 中更改系统时区?