【问题标题】:Finding the baseaddress of a running process查找正在运行的进程的基地址
【发布时间】:2012-12-25 03:32:15
【问题描述】:

我得到以下代码:

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

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

【问题讨论】:

  • 有没有找到问题的答案?我自己也在寻找答案。
  • @DreamLane 不,仍然没有找到任何东西。决定改用c#。真可惜,因为我喜欢蟒蛇。
  • 我也一直在争论改用 C++ 还是 C#。 Python 非常适合用于原型设计......

标签: python windows ctypes python-3.2


【解决方案1】:

我确实设法找到了适用于 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...

【讨论】:

  • 第一个 sn-p 中 pid 的来源?
【解决方案2】:

请参阅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

【讨论】:

  • 使用 pymem 可以执行 base_address = pymem.process.base_address(pid) 但您链接到的 MODULEENTRY32 类不起作用 - 每次运行时它都会返回不同的 modeBaseAddr ,这是错误的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-09
  • 2019-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多