Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 如何在Python中查找进程内多级指针的值?_Windows_Winapi_Python 3.x_Ctypes - Fatal编程技术网

Windows 如何在Python中查找进程内多级指针的值?

Windows 如何在Python中查找进程内多级指针的值?,windows,winapi,python-3.x,ctypes,Windows,Winapi,Python 3.x,Ctypes,我有一个进程,我想在该进程中查找地址的值,但该地址是一个多级指针,并且有一些偏移量附加到它。如何在Python中实现这一点?我在回答我自己的问题,以记录在Python 3中实现这一点的方法 首先,您需要某种方法来查找我们正在处理的流程的pid 我使用模块psutil来实现这一点,但也有其他方法 import psutil def get_pid(process_name): pid = None for proc in psutil.process_iter():

我有一个进程,我想在该进程中查找地址的值,但该地址是一个多级指针,并且有一些偏移量附加到它。如何在Python中实现这一点?

我在回答我自己的问题,以记录在Python 3中实现这一点的方法

首先,您需要某种方法来查找我们正在处理的流程的pid

我使用模块psutil来实现这一点,但也有其他方法

import psutil

def get_pid(process_name):
    pid = None
    for proc in psutil.process_iter():
        try:
            if (proc.name() == process_name):
                pid = proc.pid
        except (PermissionError, psutil.AccessDenied):
            pass
    return pid
现在我们有了我们想要处理的流程的pid。我们稍后将使用它来处理我们想要处理的流程

现在我说这是一个多级指针。这是因为我们有一个初始地址。以及偏移量列表。我们首先查找初始地址的值。然后将第一个偏移量应用于该值以获得下一个地址。我们查找该地址的值,将下一个偏移量应用于该值,并获取下一个要查找的地址。这可以根据偏移量列表的大小继续进行,但是最后一次查找是最后一次,这就给出了我们的最终地址。当我们得到它的值时,我们就得到了我们所追求的实际值

要以编程方式实现这一点,我们需要pid(例如4045)、地址(例如0x0163B4D8)、偏移量列表(例如[0x37C,0x3C])和数据大小(例如,无符号整数是4个字节,因此这就是数据的大小)

这是基本概念,当然代码可以改进

from ctypes import *
from ctypes.wintypes import *

PROCESS_ALL_ACCESS = 0x1F0FFF

def read_process_memory(pid, address, offsets, size_of_data):
    # Open the process and get the handle.
    process_handle = windll.kernel32.OpenProcess(PROCESS_ALL_ACCESS, False, pid)
    size_of_data = 4 # Size of your data
    data = ""
    read_buff = create_string_buffer(size_of_data)
    count = c_ulong(0)
    current_address = address
    offsets.append(None) # We want a final loop where we actually get the data out, this lets us do that in one go.
    for offset in offsets:
        if not windll.kernel32.ReadProcessMemory(process_handle, current_address, cast(read_buff, LPVOID), size_of_data, byref(count)):
            return -1 # Error, so we're quitting.
        else:
            val = read_buff.value
            result = int.from_bytes(val, byteorder='little')
            # Here that None comes into play.
            if(offset != None):
                current_address = result+offset
            else:
                windll.kernel32.CloseHandle(process_handle)
                return result