Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read the memory of another process in Python in Windows?

Tags:

python

I'm trying to write a Python script that reads a series of memory locations of a particular process.

How can I do this in Python?

I'll be using Windows if it matters. I have the processes PID that I'm attempting to read/edit.

Am I going to have to revert to calling ReadProcessMemory() and using ctypes?

like image 336
Filet-O-Fish Avatar asked Nov 25 '09 04:11

Filet-O-Fish


People also ask

Can you read memory with Python?

The ReadWriteMemory Class is made on Python for reading and writing to the memory of any process. This Class does not depend on any extra modules and only uses standard Python libraries like ctypes.

How do you access memory in Python?

In Python, you don't generally use pointers to access memory unless you're interfacing with a C application. If that is what you need, have a look at the ctypes module for the accessor functions.

Can a process read another process memory?

That processes can read/write memory of other processes of the same user is a fact.

Can Python edit memory?

mem_edit is a multi-platform memory editing library written in Python.


1 Answers

I didn't see anything in the standard python libraries but I found an example using ctypes like you suggested on another site:

from ctypes import *
from ctypes.wintypes import *

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

PROCESS_ALL_ACCESS = 0x1F0FFF

pid = 4044   # I assume you have this from somewhere.
address = 0x1000000  # Likewise; for illustration I'll get the .exe header.

buffer = c_char_p("The data goes here")
bufferSize = len(buffer.value)
bytesRead = c_ulong(0)

processHandle = OpenProcess(PROCESS_ALL_ACCESS, False, pid)
if ReadProcessMemory(processHandle, address, buffer, bufferSize, byref(bytesRead)):
    print "Success:", buffer
else:
    print "Failed."

CloseHandle(processHandle)
like image 71
KingNestor Avatar answered Nov 03 '22 22:11

KingNestor