如何在Windows中用Python读取其他进程的内存?
我正在尝试写一个Python脚本,用来读取某个特定进程的一系列内存地址。
我该如何在Python中做到这一点呢?
我会在Windows系统上操作,如果这有影响的话。我已经有了我想要读取或编辑的进程的PID(进程ID)。
我是不是需要使用ReadProcessMemory()这个函数,并且用到ctypes库呢?
3 个回答
-7
可以查看这个链接:http://www.windowsreference.com/windows-xp/dos-commands-and-equivalent-linux-commands/
你可以用tasklist.exe这个工具来列出正在运行的程序,然后把结果提取出来。接着可以用taskkill.exe(或者tstskill.exe)来结束这些程序。
不过,使用ctypes和kernal32可能会更安全一些。
0
是的,ctypes
(或者说win32all
)和ReadProcessMemory
确实是解决这个问题的好方法。你是在寻找其他的东西吗?具体想要什么呢?
27
我在标准的Python库里没找到什么,但我在另一个网站上找到了一个使用ctypes的例子,就像你建议的那样:
from ctypes import *
from ctypes.wintypes import *
OpenProcess = windll.kernel32.OpenProcess
ReadProcessMemory = windll.kernel32.ReadProcessMemory
CloseHandle = windll.kernel32.CloseHandle
PROCESS_ALL_ACCESS = 0x1F0FFF
pid = 4044 # I assume you have this from somewhere.
address = 0x1000000 # Likewise; for illustration I'll get the .exe header.
buffer = c_char_p("The data goes here")
bufferSize = len(buffer.value)
bytesRead = c_ulong(0)
processHandle = OpenProcess(PROCESS_ALL_ACCESS, False, pid)
if ReadProcessMemory(processHandle, address, buffer, bufferSize, byref(bytesRead)):
print "Success:", buffer
else:
print "Failed."
CloseHandle(processHandle)