Advertisement
Guest User

Untitled

a guest
Oct 13th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.22 KB | None | 0 0
  1. import os.path, ctypes, ctypes.wintypes
  2. from ctypes import *
  3. from ctypes.wintypes import *
  4.  
  5. PROCESS_QUERY_INFORMATION = (0x0400)
  6. PROCESS_VM_OPERATION = (0x0008)
  7. PROCESS_VM_READ = (0x0010)
  8. PROCESS_VM_WRITE = (0x0020)
  9. TH32CS_SNAPMODULE = (0x00000008)
  10.  
  11. CreateToolhelp32Snapshot= ctypes.windll.kernel32.CreateToolhelp32Snapshot
  12. Process32First = ctypes.windll.kernel32.Process32First
  13. Process32Next = ctypes.windll.kernel32.Process32Next
  14. Module32First = ctypes.windll.kernel32.Module32First
  15. Module32Next = ctypes.windll.kernel32.Module32Next
  16. GetLastError = ctypes.windll.kernel32.GetLastError
  17. OpenProcess = ctypes.windll.kernel32.OpenProcess
  18. GetPriorityClass = ctypes.windll.kernel32.GetPriorityClass
  19. CloseHandle = ctypes.windll.kernel32.CloseHandle
  20.  
  21. class MODULEENTRY32(Structure):
  22.        _fields_ = [ ( 'dwSize' , DWORD ) ,
  23.                 ( 'th32ModuleID' , DWORD ),
  24.                 ( 'th32ProcessID' , DWORD ),
  25.                 ( 'GlblcntUsage' , DWORD ),
  26.                 ( 'ProccntUsage' , DWORD ) ,
  27.                 ( 'modBaseAddr' , POINTER(BYTE)) ,
  28.                 ( 'modBaseSize' , DWORD ) ,
  29.                 ( 'hModule' , HMODULE ) ,
  30.                 ( 'szModule' , c_char * 256 ),
  31.                 ( 'szExePath' , c_char * 260 ) ]
  32.  
  33.  
  34.  
  35. def GetBaseAddr(ProcId, ProcName):
  36.        me32 = MODULEENTRY32()
  37.        me32.dwSize = sizeof(me32)
  38.        hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, ProcId)
  39.        if GetLastError() != 0:
  40.               CloseHandle(hSnapshot)
  41.               print 'Handle Error %s' % WinError()
  42.               return 'Error'
  43.  
  44.        else:
  45.               if Module32First(hSnapshot, byref(me32)):
  46.                      if me32.szModule == ProcName:
  47.                             CloseHandle(hSnapshot)
  48.                             return id(me32.modBaseAddr)
  49.  
  50.                      else:
  51.                             Module32Next(hSnapshot, byref(me32))
  52.                             while int(GetLastError())!= 18:
  53.                                    if me32.szModule == ProcName:
  54.                                           CloseHandle(hSnapshot)
  55.                                           return id(me32.modBaseAddr)
  56.  
  57.                                    else:
  58.                                           Module32Next(hSnapshot, byref(me32))
  59.  
  60.                             CloseHandle(hSnapshot)
  61.                             print 'Couldn\'t find Process with name %s' % ProcName
  62.  
  63.               else:
  64.                      print 'Module32First is False %s' % WinError()
  65.                      CloseHandle(hSnapshot)
  66.  
  67. def GetProcessIdByName( pName):
  68.        if pName.endswith('.exe'):
  69.               pass
  70.        else:
  71.               pName = pName+'.exe'
  72.        
  73.        ProcessIds, BytesReturned = EnumProcesses()
  74.  
  75.        for index in range(BytesReturned / ctypes.sizeof(ctypes.wintypes.DWORD)):
  76.               ProcessId = ProcessIds[index]
  77.               hProcess = ctypes.windll.kernel32.OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessId)
  78.               if hProcess:
  79.                      ImageFileName = (ctypes.c_char*MAX_PATH)()
  80.                      if ctypes.windll.psapi.GetProcessImageFileNameA(hProcess, ImageFileName, MAX_PATH)>0:
  81.                             filename = os.path.basename(ImageFileName.value)
  82.                             if filename == pName:
  83.                                    return ProcessId
  84.                      CloseHandle(hProcess)
  85.  
  86. def EnumProcesses():
  87.        count = 32
  88.        while True:
  89.               ProcessIds = (ctypes.wintypes.DWORD*count)()
  90.               cb = ctypes.sizeof(ProcessIds)
  91.               BytesReturned = ctypes.wintypes.DWORD()
  92.               if ctypes.windll.Psapi.EnumProcesses(ctypes.byref(ProcessIds), cb, ctypes.byref(BytesReturned)):
  93.                      if BytesReturned.value<cb:
  94.                             return ProcessIds, BytesReturned.value
  95.                             break
  96.                      else:
  97.                             count *= 2
  98.               else:
  99.                      return None
  100.                        
  101.  
  102.    
  103. if __name__ == '__main__':
  104.        ProcId = GetProcessIdByName('RocketLeague.exe')
  105.        #print ProcId
  106.        print hex(GetBaseAddr(ProcId, 'RocketLeague.exe'))
  107.        #print hex(GetBaseAddr(8252,'RocketLeague.exe'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement