-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings_extractor.py
More file actions
63 lines (46 loc) · 1.5 KB
/
strings_extractor.py
File metadata and controls
63 lines (46 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os
import json
from DataString import DataString
import sys
import subprocess
def get_strings_from_binary(binary_path):
'''
This function returns all the strings from the given binary file.
Args:
binary_path (str): The path to the binary file.
Returns:
list: A list of DataString objects.
'''
output_path = os.path.join(
os.getcwd(), f'.temp_output_{os.getpid()}.json')
floss_args = ['floss', '-j', binary_path, '-o', output_path]
subprocess.run(floss_args, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
strings = get_strings_from_output(output_path)
os.remove(output_path)
return strings
def get_strings_from_output(output_path):
'''
This function returns all the strings from the given output file.
Args:
output_path (str): The path to the output file.
Returns:
list: A list of DataString objects.
'''
with open(output_path, 'r') as f:
data = json.load(f)
return get_strings(data)
def get_strings(output_json):
'''
This function returns all the strings from the given output json.
Args:
output_json (dict): The output json.
Returns:
list: A list of DataString objects.
'''
strings = []
for str_type, string_lst in output_json['strings'].items():
for string_obj in string_lst:
ds = DataString(string_obj['string'], str_type)
strings.append(ds)
return sorted(strings, reverse=True)