1- from binaryninja import BinaryView , log_info , log_error
2- from reait .api import RE_models
1+ from binaryninja import BinaryView , log_info , log_error , log_debug , SymbolType , BinaryViewType
2+ from reait .api import RE_models , RE_upload , RE_analysis_lookup , RE_analyse
3+ import json
34
45class BinaryUploader :
56 def __init__ (self , config ):
67 self .config = config
78
89
9- def get_models (self ):
10+ def get_models (self , bv : BinaryView ):
1011 try :
12+ guess_model_platform = ""
13+ if bv .view_type == "PE" :
14+ guess_model_platform = "windows"
15+ elif bv .view_type == "ELF" :
16+ guess_model_platform = "linux"
17+ elif bv .view_type == "MACHO" :
18+ guess_model_platform = "macos"
19+
20+ guess_model_arch = ""
21+ if bv .arch .name == "x86" :
22+ guess_model_arch = "x86-32"
23+ elif bv .arch .name == "x86_64" :
24+ guess_model_arch = "x86"
25+ else :
26+ guess_model_arch = bv .arch .name
27+
28+ log_info (f"RevEng.AI | Architecture: { bv .arch .name } | File type: { bv .view_type } " )
1129 models = RE_models ().json ()
12- return set ([model ["model_name" ] for model in models ["models" ]])
30+ models = list ([model ["model_name" ] for model in models ["models" ]])
31+
32+ guess_model = f"{ guess_model_arch } -{ guess_model_platform } "
33+ log_info (f"RevEng.AI | Guess model: { guess_model } " )
34+ for i , model in enumerate (models ):
35+ if guess_model in model :
36+ log_info (f"RevEng.AI | Found model: { model } " )
37+ models .insert (0 , models .pop (i ))
38+ break
39+ log_info (f"RevEng.AI | Models: { models } " )
40+ return models
1341 except Exception as e :
14- log_error (f"Failed to get models: { str (e )} " )
15- return set ()
42+ log_error (f"RevEng.AI | Failed to get models: { str (e )} " )
43+ return []
1644
1745
1846 def upload_binary (self , bv : BinaryView , options : dict ):
1947 try :
20- log_info (f"Uploading binary { bv .file .filename } with options: { options } " )
21- log_info (f"Options: { options } " )
22- log_info (f"Binary size: { bv .file } " )
23- self .config .save_config ()
48+ log_info (f"RevEng.AI | Uploading binary { bv .file .filename } ." )
49+
50+ upload = RE_upload (bv .file .filename ).json ()
51+
52+ if not upload .get ("success" , False ):
53+ log_error (f"RevEng.AI | Failed to upload binary { bv .file .filename } ." )
54+ return False
55+
56+ log_info (f"RevEng.AI | Binary { bv .file .filename } uploaded successfully." )
57+
58+ sha_256_hash = upload ["sha_256_hash" ]
59+ log_info (f"RevEng.AI | SHA-256 hash: { sha_256_hash } " )
2460
25- # TODO: Implement actual upload logic here
26- # This will involve:
27- # 1. Getting the binary data from bv
28- # 2. Use the RE_upload_binary function to upload the binary
29- # 3. Handling the response
61+ # Convert symbols to a list of dictionaries with hex strings
62+ symbols = []
63+ log_info (f"RevEng.AI | Image Base: { bv .image_base :x} " )
64+ for key , value in bv .symbols .items ():
65+ if value [0 ].type == SymbolType .FunctionSymbol :
66+ func = bv .get_function_at (value [0 ].address )
67+ symbols .append ({
68+ "name" : func .name ,
69+ "start" : func .start ,
70+ "end" : func .start + func .total_bytes ,
71+ })
72+ log_info (f"RevEng.AI | Name: { key } | Start: { func .start :x} | End: { func .start + func .total_bytes :x} | Size: { func .total_bytes } " )
73+
74+ # Log all analysis parameters
75+ log_info ("RevEng.AI | Analysis parameters:" )
76+ log_info (f"RevEng.AI | - File path: { bv .file .filename } " )
77+ log_info (f"RevEng.AI | - Binary scope: { 'PRIVATE' if options ['is_private' ] else 'PUBLIC' } " )
78+ log_info (f"RevEng.AI | - Debug info path: { options .get ('debug_info' , None )} " )
79+ log_info (f"RevEng.AI | - Model name: { options ['model' ]} " )
80+ log_info (f"RevEng.AI | - Tags: { options .get ('tags' , [])} " )
81+ log_info (f"RevEng.AI | - Number of symbols: { len (symbols )} " )
82+
83+ analysis = RE_analyse (
84+ fpath = bv .file .filename ,
85+ binary_scope = "PRIVATE" if options ["is_private" ] else "PUBLIC" ,
86+ model_name = options ["model" ],
87+ tags = options ["tags" ],
88+ symbols = symbols
89+ ).json ()
90+
91+ log_info (f"RevEng.AI | Analysis response: { analysis } " )
92+
93+ binary_id = analysis ["binary_id" ]
94+ analysis_info = RE_analysis_lookup (str (binary_id )).json ()
95+
96+ log_info (f"RevEng.AI | Analysis info: { analysis_info } " )
3097
31- log_info (f"Binary { bv .file .filename } uploaded successfully with options: { options } " )
98+ self .config .set_binary_id (binary_id )
99+ self .config .set_analysis_id (analysis_info ["analysis_id" ])
100+
32101 return True
33102
34103 except Exception as e :
35- log_error (f"Failed to upload binary: { str (e )} " )
104+ log_error (f"RevEng.AI | Failed to upload binary: { str (e )} " )
36105 return False
0 commit comments