我试图通过输入进程名称,然后使用psutil查找PID来从进程的内存中读取数据。到目前为止,我有这个:
import ctypes
from ctypes import *
from ctypes.wintypes import *
import win32ui
import psutil # install, not a default module
import sys
# input process name
nameprocess = "notepad.exe"
# find pid
def getpid():
for proc in psutil.process_iter():
if proc.name() == nameprocess:
return proc.pid
PROCESS_ID = getpid()
if PROCESS_ID == None:
print "Process was not found"
sys.exit(1)
# read from addresses
STRLEN = 255
PROCESS_VM_READ = 0x0010
process = windll.kernel32.OpenProcess(PROCESS_VM_READ, 0, PROCESS_ID)
readProcMem = windll.kernel32.ReadProcessMemory
buf = ctypes.create_string_buffer(STRLEN)
for i in range(1,100):
if readProcMem(process, hex(i), buf, STRLEN, 0):
print buf.raw
如果我正确的话,最后一个for循环应读取并打印进程中前100个地址的内容。唯一的事情是,输出看起来完全是乱码。
我这里有两个问题:首先,我真的是这样从所选进程中读取地址的吗?其次,如果可能有某种结束地址,我如何确定循环应该走多长时间?
答案 0 :(得分:2)
我没有安装var compareList=[]
var productName = {productID:'saban',productHref:'http://saulic.com'};
compareList.push(productName);
console.log(compareList.length);
,而是使用任务管理器和SysInternals VMMap拉了一个进程ID和有效的虚拟地址。这些数字当然会有所不同。
ctypes的优良作法是定义参数类型并通过psutil
和.argtypes
返回值。获取您自己的kernel32库实例,因为更改缓存的.restype
实例的属性可能会导致其他使用ctypes和kernel32的模块出现问题。
您需要一个有效的虚拟地址。为了回答您的第二个问题,我认为VMMap证明了有一种方法可以做到。拿起Windows Internals的副本以学习技术。
windll.kernel32
输出(注意“ MZ” 是程序头的开头):
from ctypes import *
from ctypes.wintypes import *
PROCESS_ID = 9476 # From TaskManager for Notepad.exe
PROCESS_HEADER_ADDR = 0x7ff7b81e0000 # From SysInternals VMMap utility
# read from addresses
STRLEN = 255
PROCESS_VM_READ = 0x0010
k32 = WinDLL('kernel32')
k32.OpenProcess.argtypes = DWORD,BOOL,DWORD
k32.OpenProcess.restype = HANDLE
k32.ReadProcessMemory.argtypes = HANDLE,LPVOID,LPVOID,c_size_t,POINTER(c_size_t)
k32.ReadProcessMemory.restype = BOOL
process = k32.OpenProcess(PROCESS_VM_READ, 0, PROCESS_ID)
buf = create_string_buffer(STRLEN)
s = c_size_t()
if k32.ReadProcessMemory(process, PROCESS_HEADER_ADDR, buf, STRLEN, byref(s)):
print(s.value,buf.raw)
下面是VMMap的屏幕快照,指示notepad.exe的标头地址:
下面是与程序输出匹配的notepad.exe内容的十六进制转储的屏幕截图:
答案 1 :(得分:0)
在Windows上,PyMem库可以帮助您:https://pymem.readthedocs.io/