def test_is64bitprocess(self): """Make sure a 64-bit process detection returns correct results""" if is_x64_OS(): # Test a 32-bit app running on x64 expected_is64bit = False if is_x64_Python(): exe32bit = os.path.join(os.path.dirname(__file__), r"..\..\apps\MFC_samples\RowList.exe") app = Application().start(exe32bit, timeout=20) pid = app.RowListSampleApplication.process_id() res_is64bit = is64bitprocess(pid) try: self.assertEquals(expected_is64bit, res_is64bit) finally: # make sure to close an additional app we have opened app.kill_() # setup expected for a 64-bit app on x64 expected_is64bit = True else: # setup expected for a 32-bit app on x86 expected_is64bit = False # test native Notepad app res_is64bit = is64bitprocess(self.app.UntitledNotepad.process_id()) self.assertEquals(expected_is64bit, res_is64bit)
def test_is64bitprocess(self): "Make sure a 64-bit process detection returns correct results" if is_x64_OS(): # Test a 32-bit app running on x64 expected_is64bit = False if is_x64_Python(): exe32bit = os.path.join(os.path.dirname(__file__), r"..\..\apps\MFC_samples\RowList.exe") app = Application().start(exe32bit, timeout=20) pid = app.RowListSampleApplication.ProcessID() res_is64bit = is64bitprocess(pid) try: self.assertEquals(expected_is64bit, res_is64bit) finally: # make sure to close an additional app we have opened app.kill_() # setup expected for a 64-bit app on x64 expected_is64bit = True else: # setup expected for a 32-bit app on x86 expected_is64bit = False # test native Notepad app res_is64bit = is64bitprocess(self.app.UntitledNotepad.ProcessID()) self.assertEquals(expected_is64bit, res_is64bit)
def __init__(self, pid, backend_name, dll_name, is_unicode=False): """Constructor inject dll (one application - one class instanse)""" self.is_unicode = is_unicode self.pid = pid if not sysinfo.is_x64_Python() == is64bitprocess(self.pid): raise RuntimeError( "Application and Python must be both 32-bit or both 64-bit") self.h_process = self._get_process_handle(self.pid) self.dll_path = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'backends', backend_name, 'bin', 'x{}'.format("64" if is64bitprocess(self.pid) else "86"), '{}.dll'.format(dll_name)).encode( 'utf-16' if self.is_unicode else locale.getpreferredencoding()) self._inject_dll_to_process()
def __init__(self, app, is_unicode=False): """Constructor inject dll, set socket and hook (one application - one class instanse)""" self.app = app self.is_unicode = is_unicode self.pid = processid(self.app.handle) if not sysinfo.is_x64_Python() == is64bitprocess(self.pid): raise RuntimeError( "Application and Python must be both 32-bit or both 64-bit") self.h_process = self._get_process_handle(self.pid) self.dll_path = os.path.abspath("{0}pywinmsg{1}{2}.dll".format( dll_path, "64" if is64bitprocess(self.pid) else "32", "u" if self.is_unicode else "")) self._inject_dll_to_process() self.sock = socket(AF_INET, SOCK_DGRAM) self.sock.bind(('', 0)) port = self.sock.getsockname()[1] self._remote_call_int_param_func("InitSocket", port) self._remote_call_void_func("SetMsgHook")
def remote_call_int_param_func(self, func_name, param): # Resolve paramtype for different applications a = ctypes.c_int64(param) if is64bitprocess( self.pid) else ctypes.c_int32(param) arg_address = cfuncs.VirtualAllocEx(self.h_process, 0, ctypes.sizeof(a), cfuncs.VIRTUAL_MEM, cfuncs.PAGE_READWRITE) if not cfuncs.WriteProcessMemory(self.h_process, arg_address, ctypes.byref(a), ctypes.sizeof(a), 0): raise AttributeError( "Couldn't write data to process memory, check python acceess.") proc_address = self._get_dll_proc_address(func_name) self._create_remote_thread_with_timeout( proc_address, arg_address, 1000, "Couldn't create remote thread, dll not injected, inject and try again!", "{0}(int) function call time out".format(func_name))