11from 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
2+ from reait .api import RE_authentication , RE_search , RE_nearest_symbols_batch , RE_analyze_functions , RE_name_score
33from concurrent .futures import ThreadPoolExecutor , as_completed
44from typing import List , Dict , Tuple
55import math
@@ -36,22 +36,28 @@ def _rename_function(self, bv: BinaryView, addr: int, new_name: str, new_name_ma
3636 def _process_batch (self , function_ids : List [int ], id_to_addr : Dict [int , int ], bv : BinaryView ) -> Tuple [int , List [str ]]:
3737 """Process a batch of function IDs and return the number of renamed functions"""
3838 try :
39- ret = RE_nearest_symbols_batch (
39+ functions_by_distance = RE_nearest_symbols_batch (
4040 function_ids = function_ids ,
4141 distance = self .auto_unstrip_distance ,
4242 debug_enabled = True ,
4343 nns = 1
4444 ).json ()["function_matches" ]
4545
46+ functions = []
47+ for function in functions_by_distance :
48+ functions .append ({"function_id" : function ['origin_function_id' ], "function_name" : function ['nearest_neighbor_function_name' ]})
49+ log_info (f"RevEng.AI | Functions by distance: { functions } " )
50+ functions_by_score = RE_name_score (functions ).json ()["data" ]
51+ log_info (f"RevEng.AI | Functions by score: { functions_by_score } " )
4652 renamed_count = 0
4753 errors = []
48- for result in ret :
54+ for result in functions_by_distance :
4955 try :
5056 func_id = result ['origin_function_id' ]
5157 func_addr = id_to_addr .get (func_id )
5258 if not func_addr :
5359 continue
54-
60+
5561 new_name = result ['nearest_neighbor_function_name' ]
5662 if not new_name or new_name .startswith (("sub_" , "FUN_" )):
5763 continue
@@ -60,8 +66,18 @@ def _process_batch(self, function_ids: List[int], id_to_addr: Dict[int, int], bv
6066 if not new_name_mangled or new_name_mangled .startswith (("sub_" , "FUN_" )):
6167 continue
6268
63- if self ._rename_function (bv , func_addr , new_name , new_name_mangled ):
64- renamed_count += 1
69+ for function in functions_by_score :
70+ if function ['function_id' ] == func_id :
71+ if function ['box_plot' ]["average" ] < 0.9 :
72+ log_info (f"RevEng.AI | Function { function ['function_id' ]} has a score of { function ['box_plot' ]["average" ]:.2f} for name { function ['function_name' ]} , skipping" )
73+ break
74+ else :
75+ log_info (f"RevEng.AI | Function { function ['function_id' ]} has a score of { function ['box_plot' ]["average" ]:.2f} for name { function ['function_name' ]} , renaming" )
76+ if self ._rename_function (bv , func_addr , new_name , new_name_mangled ):
77+ renamed_count += 1
78+ break
79+
80+
6581 except Exception as e :
6682 errors .append (str (e ))
6783
@@ -76,16 +92,17 @@ def auto_unstrip(self, bv: BinaryView):
7692
7793 self .base_addr = bv .image_base
7894 self .path = bv .file .filename
95+ binary_id = self .config .get_binary_id (bv )
7996 log_info (f"RevEng.AI | Path: { self .path } " )
80- log_info (f"RevEng.AI | Binary ID: { self . config . binary_id } " )
97+ log_info (f"RevEng.AI | Binary ID: { binary_id } " )
8198
8299 results = RE_search (fpath = self .path ).json ()["query_results" ]
83100 log_info (f"RevEng.AI | Search Results: { results } " )
84101
85102 if not len (results ):
86103 raise Exception ("Binary not found in RevEng.AI, try uploading again." )
87104
88- analyzed_functions = RE_analyze_functions (self .path , self . config . binary_id ).json ()["functions" ]
105+ analyzed_functions = RE_analyze_functions (self .path , binary_id ).json ()["functions" ]
89106 function_ids = [func ["function_id" ] for func in analyzed_functions ]
90107
91108 id_to_addr = {
0 commit comments