【问题标题】:How can I read the memory of another process in Python in Windows?如何在 Windows 中读取 Python 中另一个进程的内存?
【发布时间】:2021-06-09 20:12:27
【问题描述】:

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

如何在 Python 中做到这一点?

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

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

【问题讨论】:

    标签: python


    【解决方案1】:

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

    【讨论】:

    • 你会为'the data goes here'写什么
    【解决方案2】:

    是的,ctypes(或win32all)和ReadProcessMemory 正是要走的路。您是否在寻找额外/不同的东西?特别是什么?

    【讨论】:

    • 可能是他想克隆 ArtMoney :P
    • 我想我在 Python 库中寻找一些东西,它本质上是一个包装器,试图避免使用 ctypes。感谢您的信息!
    【解决方案3】:

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

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

    但是 ctypes 和 kernal32 可能更安全。

    【讨论】:

      猜你喜欢
      • 2012-12-17
      • 2020-03-24
      • 1970-01-01
      • 1970-01-01
      • 2012-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多