Guest User

Untitled

a guest
Nov 24th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.23 KB | None | 0 0
  1. import ctypes
  2. import ctypes.wintypes
  3. kernel32 = ctypes.wintypes.windll.kernel32
  4. # Various access flag definitions:
  5. class Access:
  6.     DELETE      = 0x00010000
  7.     READ_CONTROL= 0x00020000
  8.     SYNCHRONIZE = 0x00100000
  9.     WRITE_DAC   = 0x00040000
  10.     WRITE_OWNER = 0x00080000
  11.     PROCESS_VM_WRITE = 0x0020
  12.     PROCESS_VM_READ = 0x0010
  13.     PROCESS_VM_OPERATION = 0x0008
  14.     PROCESS_TERMINATE = 0x0001
  15.     PROCESS_SUSPEND_RESUME = 0x0800
  16.     PROCESS_SET_QUOTA = 0x0100
  17.     PROCESS_SET_INFORMATION = 0x0200
  18.     PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
  19.     PROCESS_QUERY_INFORMATION = 0x0400
  20.     PROCESS_DUP_HANDLE = 0x0040
  21.     PROCESS_CREATE_THREAD = 0x0002
  22.     PROCESS_CREATE_PROCESS = 0x0080
  23. def read_process_mem(pid, address, size):
  24.     """Read memory of the specified process ID."""
  25.     buf = ctypes.create_string_buffer(size)
  26.     gotBytes = ctypes.c_ulong(0)
  27.     h = kernel32.OpenProcess(Access.PROCESS_VM_READ, False, pid)
  28.     try:
  29.         if kernel32.ReadProcessMemory(h, address, buf, size, ctypes.byref(gotBytes)):
  30.             return buf
  31.         else:
  32.             # TODO: report appropriate error GetLastError
  33.             raise Exception("Failed to access process memory.")
  34.     finally:
  35.         kernel32.CloseHandle(h)
Add Comment
Please, Sign In to add comment