def get_module_base_address(pid, module_name): """ Return the base address of a module given its name and process identifier. @type pid: int @param pid: the identifier of the process in which the module is loaded. @type process_name: str @param process_name: string by which the modules is identified. """ address_to_return = None h_module_snap = None try: h_module_snap = kernel32.create_tool_help32snapshot( kernel32.TH32CS_SNAPMODULE, pid) except OSError: while kernel32.get_last_error() == ERROR_BAD_LENGTH: try: h_module_snap = kernel32.create_tool_help32snapshot( kernel32.TH32CS_SNAPMODULE, pid) except OSError: pass if h_module_snap: try: me32 = kernel32.module32first(h_module_snap) while me32: if module_name.encode('ascii') == me32.sz_module: address_to_return = me32.mod_base_addr break me32 = kernel32.module32next(h_module_snap, me32) kernel32.close_handle(h_module_snap) return address_to_return except OSError: kernel32.close_handle(h_module_snap) return None
def get_pids_by_process_name(process_name): """ Return the process identifier for each process object in the system that matches the given process name as a list. @type process_name: str @param process_name: string by which the enum. of processes is filtered. """ pids = [] try: process_ids = psapi.enum_processes() for process_id in process_ids: try: h_process = kernel32.open_process( kernel32.PROCESS_QUERY_INFORMATION, False, process_id ) if h_process: process_path = psapi.get_process_image_file_name(h_process) filename = os.path.basename(process_path) if filename == process_name: pids.append(process_id) kernel32.close_handle(h_process) except OSError: pass except OSError: traceback.print_exc() return pids
def _read_address(self, process_handle, address): try: memory_value = kernel32.read_process_memory( process_handle, address, type_limits.get_size(self.value)) return struct.unpack(type_limits.get_struct_format(self.value), memory_value)[0] except (OSError, struct.error): kernel32.close_handle(process_handle) raise
def __write_on_address(self, process_handle, offset): try: address = self.process_memory.module_address + offset if self.value != self._read_address(process_handle, address): kernel32.write_process_memory( process_handle, address, struct.pack(type_limits.get_struct_format(self.value), self.value)) except (OSError, TypeError): kernel32.close_handle(process_handle) raise
def __write_on_multilevel_pointer(self, process_handle, offsets): address = self.__get_address_of_multilevel_pointer( process_handle, offsets) if address: try: if self.value != self._read_address(process_handle, address): kernel32.write_process_memory( process_handle, address, struct.pack(type_limits.get_struct_format(self.value), self.value)) except OSError: kernel32.close_handle(process_handle)
def get_updated_state(self, rollback_frame=0): """ """ if self.is_pid_valid() and self.module_address is not None: game_state = [None, None] process_handle = kernel32.open_process(kernel32.PROCESS_VM_READ, False, self.pid) try: if not self.window_handler: try: self.window_handler = user32.find_window( lp_class_name='UnrealWindow', lp_window_name='TEKKEN 7 ') except OSError: pass else: game_state[0] = self.get_graphic_settings(process_handle) player_data_base_address = self.module_address for i, offset in enumerate(self.player_data_pointer_offset): player_data_base_address = self.get_pointer_value( process_handle, player_data_base_address + offset) if not player_data_base_address: break if player_data_base_address == 0: if not self.reacquire_game_state: sys.stdout.write( 'No fight detected. Gamestate not updated.') self.is_in_battle = False self.reacquire_game_state = True self.reacquire_names = True else: last_eight_frames = [] second_address_base = self.get_value_from_address( process_handle, player_data_base_address, is_64bit=True) # for rollback purposes, there are 8 copies of the # game state, each one updatating once every 8 frames for i in range(8): potential_second_address = ( second_address_base + i * self.config['MemoryAddressOffsets'] ['rollback_frame_offset']) potential_frame_count = self.get_value_from_address( process_handle, potential_second_address + self.config['GameDataAddress']['frame_count']) last_eight_frames.append( (potential_frame_count, potential_second_address)) if rollback_frame >= len(last_eight_frames): sys.stdout.write( 'ERROR: requesting {} frame of {} '.format( rollback_frame, len(last_eight_frames)) + 'long rollback frame') rollback_frame = len(last_eight_frames) - 1 best_frame_count, player_data_second_address = sorted( last_eight_frames, key=lambda x: -x[0])[rollback_frame] player_data_frame = self.get_block_data( process_handle, player_data_second_address, self.config['MemoryAddressOffsets'] ['rollback_frame_offset']) bot_facing = self.get_value_from_data_block( player_data_frame, self.config['GameDataAddress']['facing']) timer_in_frames = self.get_value_from_data_block( player_data_frame, self.config['GameDataAddress']['timer_in_frames']) p1_bot, p2_bot = self.initialize_bots( player_data_frame, bot_facing, best_frame_count) if self.reacquire_game_state: self.reacquire_game_state = False sys.stdout.write('Fight detected. Updating gamestate.') self.is_in_battle = True if self.reacquire_names: if (p1_bot.is_character_name_loaded() and p2_bot.is_character_name_loaded()): self.opponent_name = ( self.get_value_at_end_of_pointer_trail( process_handle, 'opponent_name', True)) self.opponent_side = ( self.get_value_at_end_of_pointer_trail( process_handle, 'opponent_side', False)) self.is_player_player_one = ( self.opponent_side == 1) # sys.stdout.write(self.opponent_char_id) # sys.stdout.write(self.is_player_player_one) self.p1_movelist_to_use = ( p1_bot.get_movelist_to_use()) self.p2_movelist_to_use = ( p2_bot.get_movelist_to_use()) p1_movelist_block, p1_movelist_address = ( self.populate_movelists( process_handle, 'p1_movelist')) p2_movelist_block, p2_movelist_address = ( self.populate_movelists( process_handle, 'p2_movelist')) self.p1_movelist_parser = ( MovelistParser.MovelistParser( p1_movelist_block, p1_movelist_address)) self.p2_movelist_parser = ( MovelistParser.MovelistParser( p2_movelist_block, p2_movelist_address)) # self.write_movelists_to_file( # p1_movelist_block, p1_bot.character_name # ) # self.write_movelists_to_file( # p2_movelist_block, p2_bot.character_name # ) # TODO: figure out the actual size of the name # movelist self.p1_movelist_names = p1_movelist_block[ 0x2E8:200000].split(b'\00') self.p2_movelist_names = p2_movelist_block[ 0x2E8:200000].split(b'\00') #sys.stdout.write(p1_movelist_names[(1572 * 2)]) self.reacquire_names = False game_state[1] = GameSnapshot(p1_bot, p2_bot, best_frame_count, timer_in_frames, bot_facing, self.opponent_name, self.is_player_player_one) except (OSError, struct.error, TypeError): self.reacquire_everything() raise OSError finally: kernel32.close_handle(process_handle) return game_state raise OSError('invalid PID or module address')