|
| 1 | +from binaryninja import log_info, BinaryView |
| 2 | +from revengai import AnalysesCoreApi, Configuration, FunctionMapping |
| 3 | +from revengai import BaseResponseBasic, AnalysesCoreApi, ApiClient |
| 4 | +from binaryninja import BinaryView, log_error, log_info, Symbol, SymbolType, Function |
| 5 | + |
| 6 | +class AnalysisSyncService: |
| 7 | + |
| 8 | + sdk_config: Configuration |
| 9 | + |
| 10 | + def __init__(self, config): |
| 11 | + self.config = config |
| 12 | + self.sdk_config = config.api_config |
| 13 | + |
| 14 | + def _get_current_base_address(self, bv) -> int: |
| 15 | + return bv.start |
| 16 | + |
| 17 | + def _rebase_program(self, bv, base_address_delta: int) -> None: |
| 18 | + bv.rebase(bv.start + base_address_delta) |
| 19 | + |
| 20 | + def _fetch_basic_and_rebase(self, bv: BinaryView, analysis_id: int) -> BaseResponseBasic: |
| 21 | + """ |
| 22 | + Fetches basic analysis information and rebases the program if necessary. |
| 23 | + """ |
| 24 | + with ApiClient(self.sdk_config) as api_client: |
| 25 | + analyses_client = AnalysesCoreApi(api_client) |
| 26 | + analysis_details: BaseResponseBasic = analyses_client.get_analysis_basic_info( |
| 27 | + analysis_id=analysis_id |
| 28 | + ) |
| 29 | + |
| 30 | + local_base_address: int = self._get_current_base_address(bv) |
| 31 | + |
| 32 | + if analysis_details.data and analysis_details.data.base_address is not None: |
| 33 | + remote_base_address: int = analysis_details.data.base_address |
| 34 | + |
| 35 | + if local_base_address != remote_base_address: |
| 36 | + base_address_delta: int = remote_base_address - local_base_address |
| 37 | + self._rebase_program(bv, base_address_delta) |
| 38 | + |
| 39 | + def _fetch_function_map(self, analysis_id: int) -> FunctionMapping: |
| 40 | + """ |
| 41 | + Fetches the function map for the given analysis ID. |
| 42 | + """ |
| 43 | + with ApiClient(self.sdk_config) as api_client: |
| 44 | + analyses_client = AnalysesCoreApi(api_client) |
| 45 | + |
| 46 | + function_map = analyses_client.get_analysis_function_map( |
| 47 | + analysis_id=analysis_id |
| 48 | + ) |
| 49 | + func_map = function_map.data.function_maps |
| 50 | + return func_map |
| 51 | + |
| 52 | + def _match_functions( |
| 53 | + self, |
| 54 | + func_map: FunctionMapping, |
| 55 | + bv: BinaryView, |
| 56 | + ) -> None: |
| 57 | + function_map = func_map.function_map |
| 58 | + inverse_function_map = func_map.inverse_function_map |
| 59 | + |
| 60 | + log_info( |
| 61 | + f"RevEng.AI | Retrieved {len(function_map)} function mappings from analysis" |
| 62 | + ) |
| 63 | + |
| 64 | + # Compute which IDA functions match the revengai analysis functions |
| 65 | + matched_functions = [] |
| 66 | + unmatched_local_functions = [] |
| 67 | + unmatched_remote_functions = [] |
| 68 | + |
| 69 | + # Track local functions matched |
| 70 | + local_function_vaddrs_matched = set() |
| 71 | + |
| 72 | + for func in bv.functions: |
| 73 | + start_ea = func.start |
| 74 | + if str(start_ea) in inverse_function_map: |
| 75 | + new_name: str | None = func_map.name_map.get(str(start_ea), None) |
| 76 | + if new_name is None: |
| 77 | + continue |
| 78 | + |
| 79 | + # Check if function has a user-defined symbol, skip if it does |
| 80 | + if func.symbol and func.symbol.auto == False: |
| 81 | + log_info(f"RevEng.AI | Skipping user-defined function at 0x{start_ea:x}: {func.name}") |
| 82 | + local_function_vaddrs_matched.add(start_ea) |
| 83 | + continue |
| 84 | + |
| 85 | + # Rename local function |
| 86 | + new_symbol = Symbol(SymbolType.FunctionSymbol, start_ea, new_name) |
| 87 | + bv.define_user_symbol(new_symbol) |
| 88 | + |
| 89 | + matched_functions.append( |
| 90 | + (int(inverse_function_map[str(start_ea)]), start_ea) |
| 91 | + ) |
| 92 | + local_function_vaddrs_matched.add(start_ea) |
| 93 | + else: |
| 94 | + unmatched_local_functions.append(start_ea) |
| 95 | + |
| 96 | + unmatched_portal_map = {} |
| 97 | + # Track remote functions not matched |
| 98 | + for func_id_str, func_vaddr in function_map.items(): |
| 99 | + if int(func_vaddr) not in local_function_vaddrs_matched: |
| 100 | + unmatched_remote_functions.append((int(func_vaddr), int(func_id_str))) |
| 101 | + unmatched_portal_map[int(func_vaddr)] = int(func_id_str) |
| 102 | + |
| 103 | + log_info(f"RevEng.AI | Matched {len(matched_functions)} functions") |
| 104 | + log_info( |
| 105 | + f"RevEng.AI | {len(unmatched_local_functions)} local functions not matched" |
| 106 | + ) |
| 107 | + log_info( |
| 108 | + f"RevEng.AI | {len(unmatched_remote_functions)} remote functions not matched" |
| 109 | + ) |
| 110 | + |
| 111 | + def sync_analysis_data( |
| 112 | + self, analysis_id: int, bv: BinaryView |
| 113 | + ) -> None: |
| 114 | + """ |
| 115 | + Syncs the analysis data until completion or failure. |
| 116 | + """ |
| 117 | + response = self._fetch_function_map(analysis_id=analysis_id) |
| 118 | + |
| 119 | + function_mapping: FunctionMapping = response |
| 120 | + |
| 121 | + self._match_functions(func_map=function_mapping, bv=bv) |
| 122 | + |
| 123 | + self._fetch_basic_and_rebase(bv=bv, analysis_id=analysis_id) |
0 commit comments