簡體   English   中英

如何在 Windows 中讀取 Python 中另一個進程的內存?

[英]How can I read the memory of another process in Python in Windows?

我正在嘗試編寫一個 Python 腳本來讀取特定進程的一系列內存位置。

我怎樣才能在 Python 中做到這一點?

如果重要的話,我會使用 Windows。 我有我試圖讀取/編輯的進程 PID。

我將不得不恢復調用 ReadProcessMemory() 並使用 ctypes 嗎?

我在標准 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 (或win32all )和ReadProcessMemory正是要走的路。 你在尋找額外/不同的東西嗎? 什么,特別是?

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

您可以使用 tasklist.exe 列出進程,然后抓取結果。 然后使用 taskkill.exe(或 tstskill.exe)來結束它們。

但是 ctypes 和 kernal32 可能更安全。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM