查找正在运行的进程的baseaddress

时间:2012-12-25 03:32:15

标签: 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已经改变了。

链接线程的屏幕截图,显示我正在寻找的内容(左侧部分是baseaddress): CE BaseAddress

2 个答案:

答案 0 :(得分:1)

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

对于32位,我使用了psutil和pymem(如本问题所示)。:

import psutil
import pymem

my_pid = None
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...

答案 1 :(得分:0)

请参阅How to enumerate modules in python 64bit以获取一些好的代码。您正在寻找'modBaseAddr'。

有关tagMODULEENTRY32的更多信息,请参阅http://msdn.microsoft.com/en-us/library/windows/desktop/ms684225(v=vs.85).aspx

你也可以使用pymem('过时的'项目,但仍然可以)使用以下代码(你想要modBaseAddr):

  for m in self.listModules():
    if m.szModule==szModule:
      print m.szModule, m.szExePath, m.modBaseAddr