-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisassembler.py
More file actions
323 lines (262 loc) · 11.6 KB
/
Copy pathdisassembler.py
File metadata and controls
323 lines (262 loc) · 11.6 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/usr/bin/env python3
"""
Advanced DLL Disassembler and Analysis Tool
For educational and legitimate reverse engineering purposes only
"""
import os
import sys
import struct
import pefile
from typing import Dict, List, Optional, Tuple
import argparse
import json
import re
# Try to import capstone for disassembly
try:
from capstone import Cs, CS_ARCH_X86, CS_MODE_32, CS_MODE_64
CAPSTONE_AVAILABLE = True
except ImportError:
CAPSTONE_AVAILABLE = False
print("Warning: Capstone not available. Install with: pip install capstone")
class AdvancedDLLAnalyzer:
def __init__(self, dll_path: str):
self.dll_path = dll_path
self.pe = None
self.cs = None
self.is_64bit = False
def load_dll(self) -> bool:
"""Load and parse the DLL file"""
try:
self.pe = pefile.PE(self.dll_path)
self.is_64bit = self.pe.FILE_HEADER.Machine == 0x8664
if CAPSTONE_AVAILABLE:
if self.is_64bit:
self.cs = Cs(CS_ARCH_X86, CS_MODE_64)
else:
self.cs = Cs(CS_ARCH_X86, CS_MODE_32)
return True
except Exception as e:
print(f"Error loading DLL: {e}")
return False
def disassemble_function(self, address: int, size: int = 100) -> List[Dict]:
"""Disassemble code at given address"""
if not self.cs or not self.pe:
return []
try:
# Get raw data at the address
section = self.pe.get_section_by_rva(address)
if not section:
return []
offset = address - section.VirtualAddress
data = section.get_data()[offset:offset+size]
instructions = []
for insn in self.cs.disasm(data, address):
instructions.append({
'address': hex(insn.address),
'mnemonic': insn.mnemonic,
'op_str': insn.op_str,
'size': insn.size,
'bytes': insn.bytes.hex()
})
return instructions
except Exception as e:
print(f"Error disassembling at 0x{address:08x}: {e}")
return []
def analyze_function_patterns(self) -> Dict:
"""Analyze common function patterns"""
patterns = {
'entry_points': [],
'api_calls': [],
'string_references': [],
'loops': [],
'conditions': []
}
if not self.pe:
return patterns
# Find entry points
if hasattr(self.pe, 'DIRECTORY_ENTRY_EXPORT'):
for exp in self.pe.DIRECTORY_ENTRY_EXPORT.symbols:
if exp.name and exp.address:
patterns['entry_points'].append({
'name': exp.name.decode('utf-8', errors='ignore'),
'address': exp.address
})
# Analyze strings for API references
strings = self.extract_strings()
api_patterns = [
r'CreateFile', r'OpenFile', r'ReadFile', r'WriteFile',
r'Registry', r'RegOpen', r'RegCreate', r'RegSetValue',
r'Socket', r'Connect', r'Bind', r'Listen',
r'VirtualAlloc', r'VirtualProtect', r'VirtualFree',
r'LoadLibrary', r'GetProcAddress', r'CreateThread'
]
for string in strings:
for pattern in api_patterns:
if re.search(pattern, string, re.IGNORECASE):
patterns['api_calls'].append(string)
return patterns
def extract_strings(self, min_length: int = 4) -> List[str]:
"""Extract printable strings from the DLL"""
strings = []
try:
with open(self.dll_path, 'rb') as f:
data = f.read()
current_string = ""
for byte in data:
if 32 <= byte <= 126: # Printable ASCII
current_string += chr(byte)
else:
if len(current_string) >= min_length:
strings.append(current_string)
current_string = ""
if len(current_string) >= min_length:
strings.append(current_string)
except Exception as e:
print(f"Error extracting strings: {e}")
return strings
def analyze_imports_heuristics(self) -> Dict:
"""Analyze imports for suspicious patterns"""
imports_analysis = {
'total_imports': 0,
'unique_dlls': set(),
'suspicious_apis': [],
'network_apis': [],
'file_apis': [],
'registry_apis': [],
'crypto_apis': [],
'process_apis': []
}
if not hasattr(self.pe, 'DIRECTORY_ENTRY_IMPORT'):
return imports_analysis
suspicious_categories = {
'network_apis': [r'socket', r'connect', r'bind', r'listen', r'send', r'recv', r'WSA'],
'file_apis': [r'CreateFile', r'OpenFile', r'ReadFile', r'WriteFile', r'DeleteFile'],
'registry_apis': [r'RegOpen', r'RegCreate', r'RegSetValue', r'RegDelete'],
'crypto_apis': [r'Crypt', r'Encrypt', r'Decrypt', r'Hash', r'Signature'],
'process_apis': [r'CreateProcess', r'OpenProcess', r'TerminateProcess', r'VirtualAlloc']
}
for entry in self.pe.DIRECTORY_ENTRY_IMPORT:
dll_name = entry.dll.decode('utf-8', errors='ignore').lower()
imports_analysis['unique_dlls'].add(dll_name)
for imp in entry.imports:
if imp.name:
func_name = imp.name.decode('utf-8', errors='ignore').lower()
imports_analysis['total_imports'] += 1
# Categorize APIs
for category, patterns in suspicious_categories.items():
for pattern in patterns:
if re.search(pattern, func_name, re.IGNORECASE):
imports_analysis[category].append(func_name)
# Convert sets to lists for JSON serialization
imports_analysis['unique_dlls'] = list(imports_analysis['unique_dlls'])
return imports_analysis
def analyze_sections_entropy(self) -> List[Dict]:
"""Analyze section entropy for packed/encrypted content"""
sections_analysis = []
if not self.pe:
return sections_analysis
for section in self.pe.sections:
section_name = section.Name.decode('utf-8', errors='ignore').rstrip('\x00')
entropy = section.get_entropy()
# High entropy might indicate packing/encryption
is_suspicious = entropy > 7.0
sections_analysis.append({
'name': section_name,
'entropy': entropy,
'is_suspicious': is_suspicious,
'virtual_size': section.Misc_VirtualSize,
'raw_size': section.SizeOfRawData,
'characteristics': hex(section.Characteristics)
})
return sections_analysis
def generate_function_signatures(self) -> List[Dict]:
"""Generate function signatures for exported functions"""
signatures = []
if not hasattr(self.pe, 'DIRECTORY_ENTRY_EXPORT'):
return signatures
for exp in self.pe.DIRECTORY_ENTRY_EXPORT.symbols:
if exp.name and exp.address:
# Try to disassemble the function prologue
instructions = self.disassemble_function(exp.address, 20)
signature = {
'name': exp.name.decode('utf-8', errors='ignore'),
'ordinal': exp.ordinal,
'address': exp.address,
'prologue': instructions[:5], # First 5 instructions
'estimated_size': self._estimate_function_size(exp.address)
}
signatures.append(signature)
return signatures
def _estimate_function_size(self, address: int) -> int:
"""Estimate function size by looking for common patterns"""
if not self.pe:
return 0
try:
section = self.pe.get_section_by_rva(address)
if not section:
return 0
offset = address - section.VirtualAddress
data = section.get_data()[offset:]
# Look for common function endings (ret, jmp, etc.)
for i in range(0, min(len(data), 1000), 1):
if data[i] in [0xC3, 0xC2, 0xCB, 0xCA]: # ret instructions
return i + 1
elif data[i] == 0xE9 and i < len(data) - 4: # jmp
return i + 5
return 100 # Default size if no ending found
except:
return 0
def comprehensive_analysis(self) -> Dict:
"""Perform comprehensive DLL analysis"""
if not self.load_dll():
return {}
return {
'dll_info': self._get_dll_info(),
'function_signatures': self.generate_function_signatures(),
'patterns': self.analyze_function_patterns(),
'imports_analysis': self.analyze_imports_heuristics(),
'sections_entropy': self.analyze_sections_entropy(),
'strings': self.extract_strings()
}
def _get_dll_info(self) -> Dict:
"""Extract basic DLL information"""
if not self.pe:
return {}
info = {
'file_path': self.dll_path,
'file_size': os.path.getsize(self.dll_path),
'machine': self.pe.FILE_HEADER.Machine,
'is_64bit': self.is_64bit,
'timestamp': self.pe.FILE_HEADER.TimeDateStamp,
'entry_point': self.pe.OPTIONAL_HEADER.AddressOfEntryPoint,
'image_base': self.pe.OPTIONAL_HEADER.ImageBase
}
return info
def main():
parser = argparse.ArgumentParser(description='Advanced DLL Disassembler')
parser.add_argument('dll_path', help='Path to DLL file')
parser.add_argument('-o', '--output', help='Output file for report')
parser.add_argument('-f', '--format', choices=['json', 'txt'], default='json', help='Output format')
parser.add_argument('--disassemble', help='Disassemble function at address (hex)')
parser.add_argument('--entropy-threshold', type=float, default=7.0, help='Entropy threshold for suspicious sections')
args = parser.parse_args()
if not os.path.exists(args.dll_path):
print(f"Error: DLL file not found: {args.dll_path}")
return 1
analyzer = AdvancedDLLAnalyzer(args.dll_path)
if args.disassemble:
if analyzer.load_dll():
address = int(args.disassemble, 16)
instructions = analyzer.disassemble_function(address)
for insn in instructions:
print(f"{insn['address']}: {insn['mnemonic']} {insn['op_str']}")
else:
results = analyzer.comprehensive_analysis()
print(json.dumps(results, indent=2))
if args.output:
with open(args.output, 'w') as f:
json.dump(results, f, indent=2)
print(f"\nReport exported to: {args.output}")
return 0
if __name__ == "__main__":
sys.exit(main())