-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
226 lines (175 loc) · 7.77 KB
/
main.py
File metadata and controls
226 lines (175 loc) · 7.77 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env sage
import os
import sys
import sqlite3
import parser
import tempfile
import re
import subprocess
import hashlib
import traceback
def extract_file_with_7z(contianer_path, file_path):
try:
output = subprocess.check_output(['7z', 'x', '-so', contianer_path, file_path], stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
output = e.output
return output
def process_file_contents_with_7z(container_path, file_path, process_func, container_extension=None):
process_func(extract_file_with_7z(container_path, file_path), file_path, container_extension)
def process_container_with_7z(file_path):
files = {}
# Use 7zip to list the contents of the MSI file
command = ['7z', 'l', '-slt', '-r', '-sdel', '-so', file_path]
try:
output = subprocess.check_output(command, universal_newlines=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
output = e.output
# Parse the output
lines = output.splitlines()
i = 0
while i < len(lines):
file = {'path': '', 'size': 0, 'packed_size': 0, 'created': '', 'modified': ''}
if lines[i].startswith('Path ='):
# Extract the path, size, packed size, created, and modified information
file['path'] = re.search(r'Path = (.+)', lines[i]).group(1)
while i < len(lines):
line = lines[i]
try:
if line.startswith('Size = '):
file['size'] = re.search(r'Size = (\d+)', line).group(1)
elif line.startswith('Packed Size = '):
file['packed_size'] = re.search(r'Packed Size = (\d+)', line).group(1)
elif line.startswith('Created = '):
file['created'] = re.search(r'Created = (.+)', line).group(1)
elif line.startswith('Modified = '):
file['modified'] = re.search(r'Modified = (.+)', line).group(1)
elif line == "":
break
except AttributeError:
i = i
i += 1
files[file['path']] = file
i += 1
return files
file_prefixes = ['pidgen', 'licdll', 'dpcdll', 'mso', 'msa', 'pidca']
def process_nested_file(temp_container_path, path):
path_lower = path.lower()
if any(path_lower.startswith(prefix) for prefix in file_prefixes):
if path_lower.endswith('dll'):
compressed_file_data = extract_file_with_7z(temp_container_path, path)
process_dll(compressed_file_data, path)
if path_lower.endswith('dl_'):
compressed_file_data = extract_file_with_7z(temp_container_path, path)
process_container(compressed_file_data, path, container_extension='.dl_')
def process_container(file_data, file_path, container_extension=None):
# Create a temporary file
with tempfile.NamedTemporaryFile(suffix=container_extension, delete=False) as temp_container_file:
temp_container_path = temp_container_file.name
temp_container_file.write(file_data)
temp_container_file.close()
files = process_container_with_7z(temp_container_path)
if container_extension == '.msi':
for path, file in files.items():
process_nested_file(temp_container_path, path)
if path.lower().startswith('binary.'):
# Read the contents of files starting with 'Binary.'
print(f'Parsing MSI Stream Name: {path}')
process_file_contents_with_7z(temp_container_path, path, process_dll)
if container_extension == '.cab':
for path, file in files.items():
process_nested_file(temp_container_path, path)
if container_extension == '.dl_':
process_file_contents_with_7z(temp_container_path, file_path, process_dll)
# Remove the temporary container file
os.remove(temp_container_path)
def process_dll(file_data, file_path, container_extension=None):
# Process the DLL file as needed
print(f'[{file_path}]: Parsing file')
pidgen_data = parser.pidgen.parse(file_data)
if pidgen_data != {}:
print(f'[{file_path}]: Found PIDGEN data')
sha1 = hashlib.sha1(file_data).hexdigest()
print(f'[{file_path}]: SHA1: {sha1}')
print(pidgen_data)
try:
dpcll_data = parser.dpcdll.parse(file_data)
if dpcll_data != {}:
print(f'[{file_path}]: Found DPCDLL data')
sha1 = hashlib.sha1(file_data).hexdigest()
print(f'[{file_path}]: SHA1: {sha1}')
except ValueError:
dpcll_data = {}
if any(file_path.lower().startswith(prefix) for prefix in ['licdll', 'mso.dll', 'msa.dll']):
print(f'[{file_path}]: Cataloguing a LICDLL type file')
sha1 = hashlib.sha1(file_data).hexdigest()
print(f'[{file_path}]: SHA1: {sha1}')
def process_iso(file_path):
files = process_container_with_7z(file_path)
for path, file in files.items():
if path.lower().endswith('.msi'):
print(f'[{path}]: Processing MSI file')
process_file_contents_with_7z(file_path, path, process_container, container_extension='.msi')
if path.lower().endswith('.cab'):
print(f'[{path}]: Processing CAB file')
process_file_contents_with_7z(file_path, path, process_container, container_extension='.cab')
if path.lower().endswith('.dll'):
print(f'[{path}]: Processing DLL file')
process_file_contents_with_7z(file_path, path, process_dll)
def process_file_or_folder(path):
extensions = ['.iso', '.img']
if os.path.isfile(path):
print(f'[{path}]: Processing ISO/Disk Image file')
process_iso(path)
elif os.path.isdir(path):
print(f'[{path}]: Recursing through folder')
for root, dirs, files in os.walk(path):
for file in files:
if file.lower().endswith('.iso'):
iso_path = os.path.join(root, file)
print(f'Processing ISO file: {iso_path}')
process_iso(iso_path)
else:
print(f'Invalid file or folder: {path}')
def check_7z_command():
if sys.platform.startswith('win'): # Windows
try:
# Use the 'where' command to check if '7z' is in the path
subprocess.check_output(['where', '7z'])
return True
except subprocess.CalledProcessError:
return False
else: # Unix-based systems (Linux, macOS, etc.)
try:
# Use the 'which' command to check if '7z' is in the path
subprocess.check_output(['which', '7z'])
return True
except subprocess.CalledProcessError:
return False
# Main function
def main():
if len(sys.argv) != 3:
print('Usage: {} <file.iso|folder> <database>'.format(sys.argv[0]))
print('Parses <file.iso|folder> for various DLLs required for product licensing and activation')
print('Data is saved to the SQLite3 Database <database> and will be created if it does not exist')
print('If a <folder> is specified, it will search recursively for files ending in .iso/.img')
sys.exit(1)
path = sys.argv[1]
database = sys.argv[2]
if not os.path.exists(database):
conn = sqlite3.connect(database)
with open('newdb.sql') as f:
conn.executescript(f.read())
conn.close()
conn = sqlite3.connect(database)
process_file_or_folder(path)
conn.close()
# Entry point
if __name__ == '__main__':
if not check_7z_command():
print('7zip is not in the path, please add the 7z executable to the system path and try again')
try:
main()
except Exception as e:
print('An error occurred:', e)
traceback.print_exc()
sys.exit(1)