Wil*_*lly 4 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))
Run Code Online (Sandbox Code Playgroud)
所有这些都完美无缺,但由于某些进程使用所谓的BaseAddressor StartAddress。在我的情况下,这个 BaseAddress 的大小不时是随机的。正如这里所建议的,我尝试使用以下代码:
BaseAddress = win32api.GetModuleHandle(None)
Run Code Online (Sandbox Code Playgroud)
它所做的只是一遍又一遍地给出相同的十六进制值,即使我肯定知道我的 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)
Run Code Online (Sandbox Code Playgroud)
对于 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...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4199 次 |
| 最近记录: |