9
9
10
10
import lief
11
11
12
- from blint .config import PII_WORDS , get_float_from_env , get_int_from_env
12
+ from blint .config import FIRST_STAGE_WORDS , PII_WORDS , get_float_from_env , get_int_from_env
13
13
from blint .logger import DEBUG , LOG
14
14
from blint .utils import camel_to_snake , calculate_entropy , check_secret , cleanup_dict_lief_errors , decode_base64
15
15
@@ -270,7 +270,7 @@ def parse_strings(parsed_obj):
270
270
if (entropy and (entropy > MIN_ENTROPY or len (s ) > MIN_LENGTH )) or secret_type :
271
271
strings_list .append (
272
272
{
273
- "value" : ( decode_base64 (s ) if s .endswith ("==" ) else s ) ,
273
+ "value" : decode_base64 (s ) if s .endswith ("==" ) else s ,
274
274
"entropy" : entropy ,
275
275
"secret_type" : secret_type ,
276
276
}
@@ -788,7 +788,6 @@ def add_elf_metadata(exe_file, metadata, parsed_obj):
788
788
metadata ["has_runpath" ] = False
789
789
elif runpath :
790
790
metadata ["has_runpath" ] = True
791
- # This is getting renamed to symtab_symbols in lief 0.15.0
792
791
symtab_symbols = parsed_obj .symtab_symbols
793
792
metadata ["static" ] = bool (symtab_symbols and not isinstance (symtab_symbols , lief .lief_errors ))
794
793
dynamic_entries = parsed_obj .dynamic_entries
@@ -797,6 +796,10 @@ def add_elf_metadata(exe_file, metadata, parsed_obj):
797
796
metadata ["notes" ] = parse_notes (parsed_obj )
798
797
metadata ["strings" ] = parse_strings (parsed_obj )
799
798
metadata ["symtab_symbols" ], exe_type = parse_symbols (symtab_symbols )
799
+ rdata_section = parsed_obj .get_section (".rodata" )
800
+ text_section = parsed_obj .get_section (".text" )
801
+ if not metadata ["symtab_symbols" ]:
802
+ add_elf_rdata_symbols (metadata , rdata_section , text_section )
800
803
if exe_type :
801
804
metadata ["exe_type" ] = exe_type
802
805
metadata ["dynamic_symbols" ], exe_type = parse_symbols (parsed_obj .dynamic_symbols )
@@ -1114,10 +1117,16 @@ def add_pe_metadata(exe_file: str, metadata: dict, parsed_obj: lief.PE.Binary):
1114
1117
break
1115
1118
rdata_section = parsed_obj .get_section (".rdata" )
1116
1119
text_section = parsed_obj .get_section (".text" )
1117
- if not rdata_section and text_section :
1118
- rdata_section = text_section
1119
- if (not metadata ["symtab_symbols" ] or metadata ["exe_type" ] != "gobinary" ) and rdata_section :
1120
- add_pe_rdata_symbols (metadata , rdata_section )
1120
+ # If there are no .rdata and .text section, then attempt to look for two alphanumeric sections
1121
+ if not rdata_section and not text_section :
1122
+ for section in parsed_obj .sections :
1123
+ if str (section .name ).removeprefix ("." ).isalnum ():
1124
+ if not rdata_section :
1125
+ rdata_section = section
1126
+ else :
1127
+ text_section = section
1128
+ if rdata_section or text_section :
1129
+ add_pe_rdata_symbols (metadata , rdata_section , text_section )
1121
1130
metadata ["exports" ] = parse_pe_exports (parsed_obj .get_export ())
1122
1131
metadata ["functions" ] = parse_functions (parsed_obj .functions )
1123
1132
metadata ["ctor_functions" ] = parse_functions (parsed_obj .ctor_functions )
@@ -1247,33 +1256,39 @@ def add_pe_optional_headers(metadata, optional_header):
1247
1256
return metadata
1248
1257
1249
1258
1250
- def add_pe_rdata_symbols (metadata , rdata_section : lief .PE .Section ):
1259
+ def add_pe_rdata_symbols (metadata , rdata_section : lief .PE .Section , text_section : lief . PE . Section ):
1251
1260
"""Adds PE rdata symbols to the metadata dictionary.
1252
1261
1253
1262
Args:
1254
1263
metadata: The dictionary to store the metadata.
1255
1264
rdata_section: .rdata section of the PE binary.
1265
+ text_section: .text section of the PE binary.
1256
1266
1257
1267
Returns:
1258
1268
The updated metadata dictionary.
1259
1269
"""
1260
- if not rdata_section or not rdata_section .content :
1261
- return metadata
1270
+ file_extns_from_rdata = r".*\.(go|s|dll|exe|pdb)"
1262
1271
rdata_symbols = set ()
1263
1272
pii_symbols = []
1273
+ first_stage_symbols = []
1264
1274
for pii in PII_WORDS :
1265
- for vari in (f"get{ pii } " , f"get_{ camel_to_snake (pii )} " ):
1266
- if rdata_section .search_all (vari ):
1275
+ for vari in (f"get{ pii } " , f"get_{ pii } " , f"get_ { camel_to_snake (pii )} " , f"Get { pii } " ):
1276
+ if ( rdata_section and rdata_section .search_all (vari )) or ( text_section and text_section . search_all ( vari ) ):
1267
1277
pii_symbols .append (
1268
1278
{"name" : vari .lower (), "type" : "FUNCTION" , "is_function" : True , "is_imported" : False })
1269
1279
continue
1270
- str_content = codecs .decode (rdata_section .content .tobytes ("A" ), encoding = "utf-8" , errors = "ignore" )
1280
+ for sw in FIRST_STAGE_WORDS :
1281
+ if (rdata_section and rdata_section .search_all (sw )) or (text_section and text_section .search_all (sw )):
1282
+ first_stage_symbols .append (
1283
+ {"name" : sw , "type" : "FUNCTION" , "is_function" : True , "is_imported" : True })
1284
+ str_content = codecs .decode (rdata_section .content .tobytes ("A" ), encoding = "utf-8" ,
1285
+ errors = "ignore" ) if rdata_section and rdata_section .content else ""
1271
1286
for block in str_content .split (" " ):
1272
- if "runtime." in block or "internal/" in block or ".go" in block or ".dll" in block :
1287
+ if "runtime." in block or "internal/" in block or re . match ( file_extns_from_rdata , block ) :
1273
1288
if ".go" in block :
1274
1289
metadata ["exe_type" ] = "gobinary"
1275
1290
for asym in block .split ("\x00 " ):
1276
- if re .match (r".*\.(go|s|dll) $" , asym ):
1291
+ if re .match (file_extns_from_rdata + " $" , asym ):
1277
1292
rdata_symbols .add (asym )
1278
1293
if not metadata ["symtab_symbols" ]:
1279
1294
metadata ["symtab_symbols" ] = []
@@ -1285,7 +1300,31 @@ def add_pe_rdata_symbols(metadata, rdata_section: lief.PE.Section):
1285
1300
"is_imported" : True
1286
1301
} for s in sorted (rdata_symbols )
1287
1302
]
1288
- metadata ["pii_symbols" ] = pii_symbols
1303
+ if pii_symbols :
1304
+ metadata ["pii_symbols" ] = pii_symbols
1305
+ if first_stage_symbols :
1306
+ metadata ["first_stage_symbols" ] = first_stage_symbols
1307
+ return metadata
1308
+
1309
+
1310
+ def add_elf_rdata_symbols (metadata , rdata_section : lief .PE .Section , text_section : lief .PE .Section ):
1311
+ """Adds ELF rdata symbols to the metadata dictionary.
1312
+
1313
+ Args:
1314
+ metadata: The dictionary to store the metadata.
1315
+ rdata_section: .data section of the ELF binary.
1316
+ text_section: .text section of the ELF binary.
1317
+
1318
+ Returns:
1319
+ The updated metadata dictionary.
1320
+ """
1321
+ first_stage_symbols = []
1322
+ for sw in FIRST_STAGE_WORDS :
1323
+ if (rdata_section and rdata_section .search_all (sw )) or (text_section and text_section .search_all (sw )):
1324
+ first_stage_symbols .append (
1325
+ {"name" : sw , "type" : "FUNCTION" , "is_function" : True , "is_imported" : True })
1326
+ if first_stage_symbols :
1327
+ metadata ["first_stage_symbols" ] = first_stage_symbols
1289
1328
return metadata
1290
1329
1291
1330
0 commit comments