С помощью
ctypes:
import sys
import ctypes
import ctypes.wintypes as wintypes
PROCESS_ALL_ACCESS = 0x1F0FFF
kernel32 = ctypes.windll.kernel32
kernel32.OpenProcess.restype = wintypes.HANDLE
kernel32.OpenProcess.argtypes = [
wintypes.DWORD,
wintypes.BOOL,
wintypes.DWORD
]
kernel32.CloseHandle.restype = wintypes.BOOL
kernel32.CloseHandle.argtypes = [ wintypes.HANDLE ]
kernel32.WriteProcessMemory.restype = wintypes.BOOL
kernel32.WriteProcessMemory.argtypes = [
wintypes.HANDLE,
wintypes.LPVOID,
wintypes.LPCVOID,
ctypes.c_size_t,
ctypes.POINTER(ctypes.c_size_t)
]
pid = int(sys.argv[1])
addr = int(sys.argv[2], 16)
val = 42
buf = (val).to_bytes(4, byteorder='little')
buf_ptr = ctypes.c_char_p(buf)
ph = kernel32.OpenProcess(PROCESS_ALL_ACCESS, False, pid)
kernel32.WriteProcessMemory(ph, addr, buf_ptr, len(buf), None)
kernel32.CloseHandle(ph)