如何在Windows中读取另一个Python进程的内存?

2024-05-20 14:09:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写一个Python脚本,用于读取特定进程的一系列内存位置

如何在Python中实现这一点

如果有必要的话,我会使用Windows。我有我正在尝试读取/编辑的进程PID

我是否必须恢复调用ReadProcessMemory()并使用ctypes


Tags: 内存脚本编辑进程windowsctypespid我会
3条回答

http://www.windowsreference.com/windows-xp/dos-commands-and-equivalent-linux-commands/

您可以使用tasklist.exe列出进程,然后刮取结果。然后使用taskkill.exe(或tstskill.exe)结束它们

但是ctypes和kernal32可能更安全

我在标准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)

是的,ctypes(或^{})和ReadProcessMemory正是要走的路。你是在寻找额外的/与众不同的东西吗?特别是什么

相关问题 更多 >