python - 查找正在运行的进程的基地址

标签 python windows ctypes python-3.2

我得到了以下代码:

import subprocess
from ctypes import *

#-Part where I get the PID and declare all variables-#

OpenProcess = windll.kernel32.OpenProcess
ReadProcessMemory = windll.kernel32.ReadProcessMemory

processHandle = OpenProcess(PROCESS_ALL_ACCESS, False, PID)

ReadProcessMemory(processHandle, address, buffer, bufferSize, byref(bytesRead))

所有这一切都完美无缺,但由于某些进程使用所谓的 BaseAddressStartAddress。在我的例子中,这个 BaseAddress 的大小有时是随机的。 按照建议here我尝试使用以下代码:

BaseAddress = win32api.GetModuleHandle(None)

它所做的只是一遍又一遍地给出相同的十六进制值,即使我确定我的 BaseAddress 已更改。

来自链接线程的屏幕截图显示了我正在寻找的内容(左侧部分是基地址): CE BaseAddress

最佳答案

我确实设法找到了 python 3.5 32 位和 64 位的解决方案。

对于 32 位,我使用了 psutil 和 pymem(正如这个问题上已经建议的那样):

import psutil
import pymem

my_pid = None
pids = psutil.pids()
for pid in pids:
    ps = psutil.Process(pid)
    # find process by .exe name, but note that there might be more instances of solitaire.exe
    if "solitaire.exe" in ps.name():
        my_pid = ps.pid
        print( "%s running with pid: %d" % (ps.name(), ps.pid) )

base_address = pymem.process.base_address(pid)

对于 64 位 pymem 不工作。我找到了使用 win32api.GetModuleHandle(fileName) 的建议,但它需要 win32api.LoadLibrary(fileName) ,它没有使用已经运行的进程。

因此我找到了这个次优的解决方案,因为它返回了一个完整的可能性列表:

import win32process
import win32api

# first get pid, see the 32-bit solution

PROCESS_ALL_ACCESS = 0x1F0FFF
processHandle = win32api.OpenProcess(PROCESS_ALL_ACCESS, False, my_pid)
modules = win32process.EnumProcessModules(processHandle)
processHandle.close()
base_addr = modules[0] # for me it worked to select the first item in list...

关于python - 查找正在运行的进程的基地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14027459/

相关文章:

Python共享读内存

python - Google App Engine 中的 key 生成

python - 返回模型 Django Rest Framework 的键/属性 JSON 对象而不是 JSON 数组

windows - Windows 中如何生成短文件名?

ruby - Dashing 在启动时没有提示,我如何才能找到更多信息?

python - 使用 Python 从 C++ dll 返回数据

python - 通过 ctypes 使用扩展 dll 中的类所需的帮助

python - 为什么减小 np.linspace 的范围会提高数值积分的精度?

python - 如何在 OSX 上通过 Python Tkinter 安装和使用 TkDnD?

java - 无论如何在 Java 中显示一个类似于 TrayIcon.displayMessage() 的自定义表单?