0

I am trying to read memory from a game process (Just like what Cheat Engine does). I have study different posts, like:

And then, I am able to piece out my first memory reading script. Basically, it has 2 steps. Getting process handle, and then read the memory address from the handle.

But I value I get is wrong.

My code

import win32gui,win32com.client
import win32api
import ctypes
import win32ui
import win32process 
from ctypes import wintypes

### Initializing functions and permissions ###
OpenProcess = ctypes.windll.kernel32.OpenProcess
ReadProcessMemory = ctypes.windll.kernel32.ReadProcessMemory # Method 1

PROCESS_ALL_ACCESS = 0x1F0FFF
PROCESS_QUERY_INFORMATION = 0x0400
PROCESS_VM_OPERATION = 0x0008
PROCESS_VM_READ = 0x0010
PROCESS_VM_WRITE = 0x0020
### End of Initializing session

### Getting process handle ###
HWND = win32ui.FindWindow(None,'My Game').GetSafeHwnd()
PID = win32process.GetWindowThreadProcessId(HWND)[1]
processHandle = OpenProcess(PROCESS_ALL_ACCESS, False, PID) # Why is it zero
### End of Getting process handle ###

### Reading value of a Memory Address ###

ADDRESS = 0x0111FF62
buffer = ctypes.c_char_p("Hello, World".encode('utf-8'))
bufferSize = len(buffer.value)
bytesRead = ctypes.c_ulong(0)

memory_value = ReadProcessMemory(processHandle, ADDRESS, buffer, bufferSize, ctypes.byref(bytesRead)) # Why is it zero
print('Memory Value = ', memory_value)

Memory Value = 0

The true value I get from Memory scanning software

enter image description here

I have double check if my HWND value is correct by using the function win32gui.SetForegroundWindow(HWND ). It does bring the windows to Foreground. So I am pretty sure my program wrong until the line for HWND.

Raven Cheuk
  • 2,903
  • 4
  • 27
  • 54

1 Answers1

0

OpenProcess is failing, likely because PROCESS_ALL_ACCESS is not granted. You only need read access, so change PROCESS_ALL_ACCESS to PROCESS_VM_READ, check the return value to make sure it is not zero.

Documentation for OpenProcess

If the function succeeds, the return value is an open handle to the specified process.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

Note that Windows stores text in UTF16 format, not UTF8

Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77