Advertisement
Guest User

Untitled

a guest
Dec 8th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.68 KB | None | 0 0
  1. from ctypes import create_string_buffer, c_ulong, byref, windll
  2. from subprocess import check_output
  3. import psutil, struct, win32security, win32api, win32ui, win32process, win32con
  4.  
  5.  
  6. PROCNAME = "Calculator.exe"
  7. for proc in psutil.process_iter():
  8.     if proc.name() == PROCNAME:
  9.         pid = proc.pid
  10.  
  11. print(pid)
  12.  
  13. hwnd = 1
  14. priv_flags = win32security.TOKEN_ADJUST_PRIVILEGES | win32security.TOKEN_QUERY
  15. hToken = win32security.OpenProcessToken (win32api.GetCurrentProcess(),
  16.                                          priv_flags)
  17. # enable "debug process"
  18. privilege_id = win32security.LookupPrivilegeValue(None,
  19.                                                   win32security.SE_DEBUG_NAME)
  20. old_privs = win32security.AdjustTokenPrivileges(hToken, 0,
  21.                                                 [(privilege_id,
  22.                                                   win32security.SE_PRIVILEGE_ENABLED)])
  23.  
  24. # Open the process, and query it's filename
  25. pshandle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, False,
  26.                                 pid)
  27.  
  28. bytes= 1000
  29. address=0x1
  30. ReadProcessMemory = windll.kernel32.ReadProcessMemory
  31. buffer = create_string_buffer(bytes)
  32. bytesRead = c_ulong(0)
  33. bufferSize = bytes
  34. ReadProcessMemory(pshandle.handle, address, buffer, bufferSize, byref(bytesRead))
  35. string = buffer.raw
  36. print(string)
  37. try:
  38.     exename = win32process.GetModuleFileNameEx(pshandle, 0)
  39. except pywintypes.error:
  40.     # insert code to call GetProcessImageName if we can find it..
  41.     # returning None from here will hopefully break all following code
  42.     exename = None
  43. finally:
  44.     # clean up
  45.     win32api.CloseHandle(pshandle)
  46.     win32api.CloseHandle(hToken)
  47.  
  48. print(exename)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement