-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic_binary_analyzer.py
More file actions
659 lines (534 loc) · 24.1 KB
/
static_binary_analyzer.py
File metadata and controls
659 lines (534 loc) · 24.1 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
#!/usr/bin/env python3
"""
==============================================================================
STATIC BINARY ANALYZER
==============================================================================
Script: static_binary_analyzer.py
Version: 1.0
Description: Performs static analysis on suspicious binary files without execution
Requirements: Linux system with Python 3.6+, standard Linux tools
Usage: python3 static_binary_analyzer.py <file_path> [options]
FEATURES:
- Safe static analysis (never executes binaries)
- Cross-platform executable support (Windows/Linux)
- Multiple analysis tools integration
- VirusTotal hash lookup capability
- Comprehensive output generation
- User-friendly error handling
ANALYSIS TOOLS USED:
- file: File type identification
- strings: String extraction
- exiftool: Metadata extraction
- objdump: Object file analysis
- readelf: ELF file analysis
- pefile: PE file analysis (Windows executables)
- hashlib: File hashing (MD5, SHA1, SHA256)
OUTPUT FILES:
- analysis_summary.txt: Overview of findings
- strings_analysis.txt: Extracted strings
- metadata_analysis.txt: File metadata
- binary_structure.txt: Binary structure details
- security_indicators.txt: Potential security concerns
- virustotal_results.txt: VirusTotal lookup results
FUTURE VERSION PLANS:
- Direct VirusTotal API integration
- LLM analysis submission
- Enhanced malware detection
- Network behavior analysis
- Sandbox integration (safe execution)
==============================================================================
"""
import os
import sys
import hashlib
import subprocess
import argparse
import json
import time
from pathlib import Path
from typing import Dict, List, Optional, Tuple
import urllib.request
import urllib.error
# Script Configuration
SCRIPT_VERSION = "1.0"
SCRIPT_NAME = "static_binary_analyzer.py"
AUTHOR = "Taz Wake"
class BinaryAnalyzer:
"""Main class for performing static binary analysis."""
def __init__(self, file_path: str, output_dir: str, verbose: bool = False):
"""
Initialize the binary analyzer.
Args:
file_path: Path to the binary file to analyze
output_dir: Directory to save analysis results
verbose: Enable verbose output
"""
self.file_path = Path(file_path)
self.output_dir = Path(output_dir)
self.verbose = verbose
self.analysis_results = {}
self.errors = []
# Ensure output directory exists
self.output_dir.mkdir(parents=True, exist_ok=True)
# Analysis output files
self.output_files = {
'summary': self.output_dir / 'analysis_summary.txt',
'strings': self.output_dir / 'strings_analysis.txt',
'metadata': self.output_dir / 'metadata_analysis.txt',
'structure': self.output_dir / 'binary_structure.txt',
'security': self.output_dir / 'security_indicators.txt',
'virustotal': self.output_dir / 'virustotal_results.txt'
}
def log_message(self, message: str, level: str = "INFO"):
"""Log a message with timestamp and level."""
timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
log_msg = f"[{timestamp}] {level}: {message}"
if self.verbose or level in ["ERROR", "WARNING"]:
print(log_msg)
def run_command(self, command: List[str], timeout: int = 30) -> Tuple[bool, str, str]:
"""
Run a shell command safely.
Args:
command: Command to run as list
timeout: Command timeout in seconds
Returns:
Tuple of (success, stdout, stderr)
"""
try:
result = subprocess.run(
command,
capture_output=True,
text=True,
timeout=timeout,
check=False
)
return result.returncode == 0, result.stdout, result.stderr
except subprocess.TimeoutExpired:
return False, "", f"Command timed out after {timeout} seconds"
except FileNotFoundError:
return False, "", f"Command not found: {command[0]}"
except Exception as e:
return False, "", f"Command execution error: {str(e)}"
def check_file_exists(self) -> bool:
"""Check if the target file exists and is accessible."""
if not self.file_path.exists():
self.log_message(f"File not found: {self.file_path}", "ERROR")
return False
if not self.file_path.is_file():
self.log_message(f"Path is not a file: {self.file_path}", "ERROR")
return False
if not os.access(self.file_path, os.R_OK):
self.log_message(f"File not readable: {self.file_path}", "ERROR")
return False
self.log_message(f"File validated: {self.file_path}")
return True
def calculate_file_hashes(self) -> Dict[str, str]:
"""Calculate MD5, SHA1, and SHA256 hashes of the file."""
self.log_message("Calculating file hashes...")
hashes = {}
hash_algorithms = {
'MD5': hashlib.md5(),
'SHA1': hashlib.sha1(),
'SHA256': hashlib.sha256()
}
try:
with open(self.file_path, 'rb') as f:
while chunk := f.read(8192):
for hash_obj in hash_algorithms.values():
hash_obj.update(chunk)
for name, hash_obj in hash_algorithms.items():
hashes[name] = hash_obj.hexdigest()
self.log_message("File hashes calculated successfully")
return hashes
except Exception as e:
self.log_message(f"Error calculating hashes: {str(e)}", "ERROR")
self.errors.append(f"Hash calculation failed: {str(e)}")
return {}
def get_file_type(self) -> str:
"""Determine the file type using the 'file' command."""
self.log_message("Determining file type...")
success, stdout, stderr = self.run_command(['file', str(self.file_path)])
if success and stdout.strip():
file_type = stdout.strip()
self.log_message(f"File type: {file_type}")
return file_type
else:
error_msg = f"Could not determine file type: {stderr}"
self.log_message(error_msg, "ERROR")
self.errors.append(error_msg)
return "Unknown file type"
def extract_strings(self) -> str:
"""Extract printable strings from the binary."""
self.log_message("Extracting strings from binary...")
# Try different string extraction methods
string_methods = [
['strings', str(self.file_path)],
['strings', '-a', str(self.file_path)], # All sections
['strings', '-t', 'x', str(self.file_path)] # With offsets
]
for method in string_methods:
success, stdout, stderr = self.run_command(method)
if success and stdout.strip():
self.log_message(f"Strings extracted using: {' '.join(method)}")
return stdout
error_msg = "Failed to extract strings from binary"
self.log_message(error_msg, "ERROR")
self.errors.append(error_msg)
return "String extraction failed"
def extract_metadata(self) -> str:
"""Extract metadata using exiftool and other tools."""
self.log_message("Extracting file metadata...")
metadata_results = []
# Try exiftool first
success, stdout, stderr = self.run_command(['exiftool', str(self.file_path)])
if success and stdout.strip():
metadata_results.append("=== EXIFTOOL METADATA ===\n" + stdout)
# Try file command with more details
success, stdout, stderr = self.run_command(['file', '-k', str(self.file_path)])
if success and stdout.strip():
metadata_results.append("=== FILE COMMAND DETAILS ===\n" + stdout)
# Try stat command for file system metadata
success, stdout, stderr = self.run_command(['stat', str(self.file_path)])
if success and stdout.strip():
metadata_results.append("=== FILE SYSTEM METADATA ===\n" + stdout)
if metadata_results:
self.log_message("Metadata extracted successfully")
return "\n\n".join(metadata_results)
else:
error_msg = "Failed to extract metadata"
self.log_message(error_msg, "ERROR")
self.errors.append(error_msg)
return "Metadata extraction failed"
def analyze_binary_structure(self) -> str:
"""Analyze the binary structure based on file type."""
self.log_message("Analyzing binary structure...")
file_type = self.get_file_type().lower()
analysis_results = []
if 'elf' in file_type:
analysis_results.append(self._analyze_elf_file())
elif 'pe32' in file_type or 'pe64' in file_type or 'microsoft' in file_type:
analysis_results.append(self._analyze_pe_file())
else:
analysis_results.append("Binary structure analysis not supported for this file type")
if analysis_results:
self.log_message("Binary structure analysis completed")
return "\n\n".join(analysis_results)
else:
error_msg = "Binary structure analysis failed"
self.log_message(error_msg, "ERROR")
self.errors.append(error_msg)
return "Binary structure analysis failed"
def _analyze_elf_file(self) -> str:
"""Analyze ELF file structure."""
analysis = ["=== ELF FILE ANALYSIS ===\n"]
# Basic ELF info
success, stdout, stderr = self.run_command(['readelf', '-h', str(self.file_path)])
if success and stdout.strip():
analysis.append("ELF Header:\n" + stdout)
# Section headers
success, stdout, stderr = self.run_command(['readelf', '-S', str(self.file_path)])
if success and stdout.strip():
analysis.append("Section Headers:\n" + stdout)
# Program headers
success, stdout, stderr = self.run_command(['readelf', '-l', str(self.file_path)])
if success and stdout.strip():
analysis.append("Program Headers:\n" + stdout)
# Dynamic symbols
success, stdout, stderr = self.run_command(['readelf', '-d', str(self.file_path)])
if success and stdout.strip():
analysis.append("Dynamic Section:\n" + stdout)
return "\n\n".join(analysis)
def _analyze_pe_file(self) -> str:
"""Analyze PE file structure."""
analysis = ["=== PE FILE ANALYSIS ===\n"]
# Try objdump for PE files
success, stdout, stderr = self.run_command(['objdump', '-f', str(self.file_path)])
if success and stdout.strip():
analysis.append("File Header:\n" + stdout)
# Try objdump for sections
success, stdout, stderr = self.run_command(['objdump', '-h', str(self.file_path)])
if success and stdout.strip():
analysis.append("Section Headers:\n" + stdout)
# Note: For detailed PE analysis, pefile library would be better
# but keeping it simple for now
analysis.append("Note: Consider using pefile library for detailed PE analysis")
return "\n\n".join(analysis)
def identify_security_indicators(self) -> str:
"""Identify potential security indicators in the binary."""
self.log_message("Identifying security indicators...")
indicators = ["=== SECURITY INDICATORS ANALYSIS ===\n"]
# Check for suspicious strings
strings_content = self.extract_strings()
suspicious_patterns = [
'http://', 'https://', 'ftp://', # Network connections
'cmd.exe', 'powershell', 'bash', # Shell commands
'registry', 'regedit', # Registry access
'CreateProcess', 'WinExec', 'ShellExecute', # Process creation
'socket', 'connect', 'bind', # Network functions
'encrypt', 'decrypt', 'AES', 'RSA', # Encryption
'admin', 'root', 'sudo', # Privilege escalation
'backdoor', 'trojan', 'virus', 'malware' # Malicious terms
]
found_indicators = []
for pattern in suspicious_patterns:
if pattern.lower() in strings_content.lower():
found_indicators.append(f"- Found suspicious pattern: {pattern}")
if found_indicators:
indicators.append("Suspicious patterns found:\n" + "\n".join(found_indicators))
else:
indicators.append("No obvious suspicious patterns found")
# Check file permissions
try:
stat_info = os.stat(self.file_path)
mode = stat_info.st_mode
if mode & 0o111: # Executable bit set
indicators.append("\nFile permissions: Executable")
else:
indicators.append("\nFile permissions: Non-executable")
except Exception as e:
indicators.append(f"\nCould not check file permissions: {str(e)}")
# Check file size (very large files might be suspicious)
file_size = self.file_path.stat().st_size
if file_size > 100 * 1024 * 1024: # 100MB
indicators.append(f"\nFile size: {file_size / (1024*1024):.1f} MB (large file)")
else:
indicators.append(f"\nFile size: {file_size / 1024:.1f} KB")
self.log_message("Security indicators analysis completed")
return "\n".join(indicators)
def lookup_virustotal(self, file_hash: str) -> str:
"""Look up file hash on VirusTotal (public API)."""
self.log_message("Looking up file hash on VirusTotal...")
# Note: This uses the public VirusTotal API which has rate limits
# For production use, consider using the official API with key
vt_url = f"https://www.virustotal.com/vtapi/v2/file/report"
params = {
'apikey': 'dummy', # Public API doesn't require key for lookups
'resource': file_hash
}
try:
# For public API, we'll just provide the URL for manual checking
manual_url = f"https://www.virustotal.com/gui/file/{file_hash}"
result = f"""=== VIRUSTOTAL LOOKUP ===
File Hash: {file_hash}
Manual Check URL: {manual_url}
Note: This is a manual lookup URL. For automated analysis:
1. Visit the URL above
2. Check the file's reputation
3. Review any detection results
Future versions will include:
- Direct API integration with VirusTotal
- Automated result parsing
- LLM analysis submission
- Enhanced threat intelligence
"""
self.log_message("VirusTotal lookup information generated")
return result
except Exception as e:
error_msg = f"VirusTotal lookup failed: {str(e)}"
self.log_message(error_msg, "ERROR")
self.errors.append(error_msg)
return f"VirusTotal lookup failed: {str(e)}"
def save_analysis_results(self):
"""Save all analysis results to output files."""
self.log_message("Saving analysis results...")
# Save strings analysis
with open(self.output_files['strings'], 'w', encoding='utf-8') as f:
f.write(self.analysis_results.get('strings', 'String extraction failed'))
# Save metadata analysis
with open(self.output_files['metadata'], 'w', encoding='utf-8') as f:
f.write(self.analysis_results.get('metadata', 'Metadata extraction failed'))
# Save binary structure analysis
with open(self.output_files['structure'], 'w', encoding='utf-8') as f:
f.write(self.analysis_results.get('structure', 'Structure analysis failed'))
# Save security indicators
with open(self.output_files['security'], 'w', encoding='utf-8') as f:
f.write(self.analysis_results.get('security', 'Security analysis failed'))
# Save VirusTotal results
with open(self.output_files['virustotal'], 'w', encoding='utf-8') as f:
f.write(self.analysis_results.get('virustotal', 'VirusTotal lookup failed'))
# Generate and save summary
summary = self._generate_summary()
with open(self.output_files['summary'], 'w', encoding='utf-8') as f:
f.write(summary)
self.log_message("Analysis results saved successfully")
def _generate_summary(self) -> str:
"""Generate a comprehensive analysis summary."""
summary = f"""==============================================================================
STATIC BINARY ANALYSIS SUMMARY
==============================================================================
Analysis Date: {time.strftime("%Y-%m-%d %H:%M:%S")}
Script: {SCRIPT_NAME}
Version: {SCRIPT_VERSION}
Target File: {self.file_path}
Output Directory: {self.output_dir}
FILE INFORMATION:
{self.analysis_results.get('file_type', 'Unknown file type')}
FILE HASHES:
"""
hashes = self.analysis_results.get('hashes', {})
for hash_type, hash_value in hashes.items():
summary += f"{hash_type}: {hash_value}\n"
summary += f"""
ANALYSIS RESULTS:
- Strings Analysis: {'Completed' if 'strings' in self.analysis_results else 'Failed'}
- Metadata Analysis: {'Completed' if 'metadata' in self.analysis_results else 'Failed'}
- Binary Structure: {'Completed' if 'structure' in self.analysis_results else 'Failed'}
- Security Indicators: {'Completed' if 'security' in self.analysis_results else 'Failed'}
- VirusTotal Lookup: {'Completed' if 'virustotal' in self.analysis_results else 'Failed'}
OUTPUT FILES:
"""
for file_type, file_path in self.output_files.items():
summary += f"- {file_type.replace('_', ' ').title()}: {file_path}\n"
if self.errors:
summary += "\nERRORS ENCOUNTERED:\n"
for error in self.errors:
summary += f"- {error}\n"
summary += f"""
FUTURE ENHANCEMENTS:
- Direct VirusTotal API integration
- LLM analysis submission
- Enhanced malware detection
- Network behavior analysis
- Sandbox integration (safe execution)
==============================================================================
"""
return summary
def run_analysis(self) -> bool:
"""Run the complete static analysis."""
self.log_message("Starting static binary analysis...")
# Validate file
if not self.check_file_exists():
return False
# Perform analysis steps
try:
# Calculate hashes
self.analysis_results['hashes'] = self.calculate_file_hashes()
# Get file type
self.analysis_results['file_type'] = self.get_file_type()
# Extract strings
self.analysis_results['strings'] = self.extract_strings()
# Extract metadata
self.analysis_results['metadata'] = self.extract_metadata()
# Analyze binary structure
self.analysis_results['structure'] = self.analyze_binary_structure()
# Identify security indicators
self.analysis_results['security'] = self.identify_security_indicators()
# VirusTotal lookup (using SHA256 hash)
if 'SHA256' in self.analysis_results['hashes']:
self.analysis_results['virustotal'] = self.lookup_virustotal(
self.analysis_results['hashes']['SHA256']
)
# Save results
self.save_analysis_results()
self.log_message("Static binary analysis completed successfully", "SUCCESS")
return True
except Exception as e:
error_msg = f"Analysis failed: {str(e)}"
self.log_message(error_msg, "ERROR")
self.errors.append(error_msg)
return False
def show_help():
"""Display comprehensive help information."""
help_text = f"""
{SCRIPT_NAME} - Static Binary Analyzer v{SCRIPT_VERSION}
DESCRIPTION:
This script performs comprehensive static analysis on binary files without
executing them. It analyzes both Windows and Linux executables using
multiple analysis tools and generates detailed reports.
USAGE:
python3 {SCRIPT_NAME} <file_path> [options]
ARGUMENTS:
file_path Path to the binary file to analyze (REQUIRED)
OPTIONS:
-o, --output-dir DIR Output directory for analysis results
(default: ./analysis_results)
-v, --verbose Enable verbose output
-h, --help Show this help message
--version Show script version
EXAMPLES:
python3 {SCRIPT_NAME} suspicious_file.exe
python3 {SCRIPT_NAME} malware.bin -o /tmp/analysis -v
python3 {SCRIPT_NAME} --help
REQUIREMENTS:
- Linux system with Python 3.6+
- Standard Linux tools: file, strings, exiftool, objdump, readelf
- Internet connection for VirusTotal lookups
SAFETY FEATURES:
- NEVER executes the target binary
- Read-only file access only
- Safe command execution with timeouts
- Comprehensive error handling
OUTPUT FILES:
- analysis_summary.txt: Overview of findings
- strings_analysis.txt: Extracted strings
- metadata_analysis.txt: File metadata
- binary_structure.txt: Binary structure details
- security_indicators.txt: Potential security concerns
- virustotal_results.txt: VirusTotal lookup results
FUTURE VERSIONS:
- Direct VirusTotal API integration
- LLM analysis submission
- Enhanced malware detection
- Network behavior analysis
- Sandbox integration (safe execution)
For more information, see the script's embedded documentation.
"""
print(help_text)
def main():
"""Main function to handle command line arguments and run analysis."""
parser = argparse.ArgumentParser(
description=f"Static Binary Analyzer v{SCRIPT_VERSION}",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=f"""
Examples:
python3 {SCRIPT_NAME} suspicious_file.exe
python3 {SCRIPT_NAME} malware.bin -o /tmp/analysis -v
python3 {SCRIPT_NAME} --help
For detailed help, use: python3 {SCRIPT_NAME} --help
"""
)
parser.add_argument(
'file_path',
help='Path to the binary file to analyze'
)
parser.add_argument(
'-o', '--output-dir',
default='./analysis_results',
help='Output directory for analysis results (default: ./analysis_results)'
)
parser.add_argument(
'-v', '--verbose',
action='store_true',
help='Enable verbose output'
)
parser.add_argument(
'--version',
action='version',
version=f'{SCRIPT_NAME} v{SCRIPT_VERSION}'
)
args = parser.parse_args()
# Show help if requested
if args.file_path == '--help' or args.file_path == '-h':
show_help()
return
# Validate file path
if not os.path.exists(args.file_path):
print(f"ERROR: File not found: {args.file_path}")
print("Use --help for usage information")
sys.exit(1)
# Create analyzer and run analysis
analyzer = BinaryAnalyzer(args.file_path, args.output_dir, args.verbose)
if analyzer.run_analysis():
print(f"\n✅ Analysis completed successfully!")
print(f"📁 Results saved to: {args.output_dir}")
print(f"📋 Summary: {analyzer.output_files['summary']}")
sys.exit(0)
else:
print(f"\n❌ Analysis failed!")
if analyzer.errors:
print("Errors encountered:")
for error in analyzer.errors:
print(f" - {error}")
sys.exit(1)
if __name__ == "__main__":
main()