|
| 1 | +from binaryninja import BinaryView, log_info, log_error, Symbol, SymbolType |
| 2 | +from reait.api import RE_authentication, RE_search, RE_nearest_symbols_batch, RE_analyze_functions |
| 3 | +from concurrent.futures import ThreadPoolExecutor, as_completed |
| 4 | +from typing import List, Dict, Tuple |
| 5 | +import math |
| 6 | + |
| 7 | +class AutoUnstrip: |
| 8 | + def __init__(self, config): |
| 9 | + self.config = config |
| 10 | + self.auto_unstrip_distance = 0.09999999999999998 |
| 11 | + self.base_addr = None |
| 12 | + self.path = None |
| 13 | + self._analysed_functions = {} |
| 14 | + self._functions = [] |
| 15 | + self.function_ids = [] |
| 16 | + self.max_workers = 4 # Number of parallel threads |
| 17 | + |
| 18 | + def _get_all_functions(self): |
| 19 | + return list(self.bv.functions) |
| 20 | + |
| 21 | + def _get_analysed_functions(self): |
| 22 | + return self._analysed_functions |
| 23 | + |
| 24 | + def _get_sync_analysed_ids_local(self): |
| 25 | + return self.function_ids |
| 26 | + |
| 27 | + def _rename_function(self, bv: BinaryView, addr: int, new_name: str, new_name_mangled: str) -> bool: |
| 28 | + try: |
| 29 | + func = bv.get_function_at(addr) |
| 30 | + if not func: |
| 31 | + log_error(f"RevEng.AI | No function found at address {hex(addr)}") |
| 32 | + return False |
| 33 | + |
| 34 | + if func.name == new_name or func.name == new_name_mangled: |
| 35 | + log_info(f"RevEng.AI | Function at {hex(addr)} already has name {func.name}") |
| 36 | + return False |
| 37 | + |
| 38 | + new_symbol = Symbol(SymbolType.FunctionSymbol, addr, new_name_mangled) |
| 39 | + bv.define_user_symbol(new_symbol) |
| 40 | + |
| 41 | + log_info(f"RevEng.AI | Renamed function at {hex(addr)} to {new_name}") |
| 42 | + return True |
| 43 | + |
| 44 | + except Exception as e: |
| 45 | + log_error(f"RevEng.AI | Error renaming function at {hex(addr)}: {str(e)}") |
| 46 | + return False |
| 47 | + |
| 48 | + def _process_batch(self, function_ids: List[int], id_to_addr: Dict[int, int], bv: BinaryView) -> Tuple[int, List[str]]: |
| 49 | + """Process a batch of function IDs and return the number of renamed functions""" |
| 50 | + try: |
| 51 | + ret = RE_nearest_symbols_batch( |
| 52 | + function_ids=function_ids, |
| 53 | + distance=self.auto_unstrip_distance, |
| 54 | + debug_enabled=True, |
| 55 | + nns=1 |
| 56 | + ).json()["function_matches"] |
| 57 | + |
| 58 | + renamed_count = 0 |
| 59 | + errors = [] |
| 60 | + for result in ret: |
| 61 | + try: |
| 62 | + func_id = result['origin_function_id'] |
| 63 | + func_addr = id_to_addr.get(func_id) |
| 64 | + if not func_addr: |
| 65 | + continue |
| 66 | + |
| 67 | + new_name = result['nearest_neighbor_function_name'] |
| 68 | + if not new_name or new_name.startswith(("sub_", "FUN_")): |
| 69 | + continue |
| 70 | + |
| 71 | + new_name_mangled = result['nearest_neighbor_function_name_mangled'] |
| 72 | + if not new_name_mangled or new_name_mangled.startswith(("sub_", "FUN_")): |
| 73 | + continue |
| 74 | + |
| 75 | + if self._rename_function(bv, func_addr, new_name, new_name_mangled): |
| 76 | + renamed_count += 1 |
| 77 | + except Exception as e: |
| 78 | + errors.append(str(e)) |
| 79 | + |
| 80 | + return renamed_count, errors |
| 81 | + |
| 82 | + except Exception as e: |
| 83 | + return 0, [str(e)] |
| 84 | + |
| 85 | + def auto_unstrip(self, bv: BinaryView): |
| 86 | + try: |
| 87 | + log_info("RevEng.AI | Auto Unstripping binary") |
| 88 | + |
| 89 | + self.base_addr = bv.image_base |
| 90 | + self.path = bv.file.filename |
| 91 | + log_info(f"RevEng.AI | Path: {self.path}") |
| 92 | + log_info(f"RevEng.AI | Binary ID: {self.config.binary_id}") |
| 93 | + self._analysed_functions = {} |
| 94 | + |
| 95 | + results = RE_search(fpath=self.path).json()["query_results"] |
| 96 | + log_info(f"RevEng.AI | Search Results: {results}") |
| 97 | + |
| 98 | + if not len(results): |
| 99 | + raise Exception("Binary not found in RevEng.AI, try uploading again.") |
| 100 | + |
| 101 | + analyzed_functions = RE_analyze_functions(self.path, self.config.binary_id).json()["functions"] |
| 102 | + function_ids = [func["function_id"] for func in analyzed_functions] |
| 103 | + |
| 104 | + id_to_addr = { |
| 105 | + func["function_id"]: func["function_vaddr"] + self.base_addr |
| 106 | + for func in analyzed_functions |
| 107 | + } |
| 108 | + |
| 109 | + # Use fixed chunk size of 50 |
| 110 | + chunk_size = 50 |
| 111 | + chunks = [function_ids[i:i + chunk_size] for i in range(0, len(function_ids), chunk_size)] |
| 112 | + |
| 113 | + log_info(f"RevEng.AI | Processing {len(function_ids)} functions in {len(chunks)} chunks of size {chunk_size}") |
| 114 | + |
| 115 | + # Process chunks in parallel |
| 116 | + total_renamed = 0 |
| 117 | + all_errors = [] |
| 118 | + with ThreadPoolExecutor(max_workers=self.max_workers) as executor: |
| 119 | + # Submit all tasks |
| 120 | + future_to_chunk = { |
| 121 | + executor.submit(self._process_batch, chunk, id_to_addr, bv): i |
| 122 | + for i, chunk in enumerate(chunks) |
| 123 | + } |
| 124 | + |
| 125 | + # Wait for all tasks to complete and collect results |
| 126 | + for future in as_completed(future_to_chunk): |
| 127 | + chunk_index = future_to_chunk[future] |
| 128 | + try: |
| 129 | + renamed_count, errors = future.result() |
| 130 | + total_renamed += renamed_count |
| 131 | + all_errors.extend(errors) |
| 132 | + log_info(f"RevEng.AI | Chunk {chunk_index} completed: renamed {renamed_count} functions") |
| 133 | + except Exception as e: |
| 134 | + log_error(f"RevEng.AI | Error processing chunk {chunk_index}: {str(e)}") |
| 135 | + |
| 136 | + if total_renamed > 0: |
| 137 | + message = f"Successfully renamed {total_renamed} functions!" |
| 138 | + else: |
| 139 | + message = "After analyzing the binary, no functions were found to be renamed." |
| 140 | + |
| 141 | + if all_errors: |
| 142 | + message += f"\nEncountered {len(all_errors)} errors during processing." |
| 143 | + |
| 144 | + log_info(f"RevEng.AI | {message}") |
| 145 | + return True, message |
| 146 | + |
| 147 | + except Exception as e: |
| 148 | + log_error(f"RevEng.AI | Error: {str(e)}") |
| 149 | + return False, str(e) |
| 150 | + """ |
| 151 | + self._functions = self._get_all_functions() |
| 152 | + self._get_analysed_functions() |
| 153 | + self.function_ids = self._get_sync_analysed_ids_local() |
| 154 | +
|
| 155 | + log_info(f"RevEng.AI | {bv.file.original_filename}") |
| 156 | + """ |
| 157 | + |
0 commit comments