-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_forensic_tool.py
More file actions
324 lines (260 loc) · 10.4 KB
/
Copy pathtest_forensic_tool.py
File metadata and controls
324 lines (260 loc) · 10.4 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
#!/usr/bin/env python3
"""
Forensic Analysis Tool - Test Suite
Validates core functionality without GUI dependencies
"""
import sys
import os
import tempfile
import json
import csv
from pathlib import Path
# Add the current directory to path to import the forensic tool modules
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
def test_file_recovery():
"""Test file recovery functionality"""
print("🔍 Testing File Recovery Engine...")
try:
# Import without GUI dependencies
from forensic_analysis_tool import FileRecoveryEngine
recovery = FileRecoveryEngine()
# Test recycle bin scan
print(" - Testing Recycle Bin scan...")
recycle_files = recovery.scan_recycle_bin()
print(f" - Found {len(recycle_files)} files in recycle bin")
# Test deleted file scan (safe directories only)
print(" - Testing deleted file scan...")
if os.name == 'nt':
deleted_files = recovery.scan_deleted_files("C:\\Windows\\Temp")
else:
deleted_files = recovery.scan_deleted_files("/tmp")
print(f" - Found {len(deleted_files)} deleted files")
print("✅ File Recovery Engine: PASSED")
return True
except Exception as e:
print(f"❌ File Recovery Engine: FAILED - {e}")
return False
def test_log_analyzer():
"""Test log analysis functionality"""
print("🔍 Testing Log Analyzer...")
try:
from forensic_analysis_tool import LogAnalyzer
analyzer = LogAnalyzer()
# Test Windows logs (will gracefully handle non-Windows systems)
print(" - Testing Windows Event Log analysis...")
event_logs = analyzer.analyze_windows_logs()
print(f" - Processed {len(event_logs)} event log entries")
# Test browser history
print(" - Testing browser history extraction...")
browser_history = analyzer.analyze_browser_history()
print(f" - Found {len(browser_history)} browser history entries")
print("✅ Log Analyzer: PASSED")
return True
except Exception as e:
print(f"❌ Log Analyzer: FAILED - {e}")
return False
def test_timeline_creator():
"""Test timeline creation functionality"""
print("🔍 Testing Timeline Creator...")
try:
from forensic_analysis_tool import TimelineCreator
import datetime
timeline = TimelineCreator()
# Create sample data for testing
sample_files = [
{
'name': 'test.txt',
'path': '/tmp/test.txt',
'modified': datetime.datetime.now(),
'accessed': datetime.datetime.now()
}
]
sample_logs = [
{
'log_type': 'System',
'event_id': '1001',
'source': 'Test',
'timestamp': datetime.datetime.now().strftime('%m/%d/%Y %I:%M:%S %p'),
'message': 'Test log entry'
}
]
sample_browser = [
{
'browser': 'Chrome',
'title': 'Test Page',
'url': 'https://test.com',
'timestamp': datetime.datetime.now()
}
]
# Test timeline creation
print(" - Creating timeline from sample data...")
timeline_data = timeline.create_timeline(sample_files, sample_logs, sample_browser)
print(f" - Generated timeline with {len(timeline_data)} events")
print("✅ Timeline Creator: PASSED")
return True
except Exception as e:
print(f"❌ Timeline Creator: FAILED - {e}")
return False
def test_evidence_collector():
"""Test evidence collection functionality"""
print("🔍 Testing Evidence Collector...")
try:
from forensic_analysis_tool import EvidenceCollector
collector = EvidenceCollector()
# Create a temporary test file
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt') as tmp_file:
tmp_file.write("This is a test evidence file.")
test_file_path = tmp_file.name
# Test evidence addition
print(" - Adding test evidence file...")
evidence_item = collector.add_evidence(test_file_path, "Test evidence file", "TEST001")
print(f" - Added evidence item with ID: {evidence_item['id']}")
# Test evidence export
with tempfile.TemporaryDirectory() as tmp_dir:
print(" - Testing ZIP export...")
zip_path = collector.export_evidence(tmp_dir, 'zip')
print(f" - Created ZIP archive: {zip_path}")
print(" - Testing folder export...")
folder_path = collector.export_evidence(tmp_dir, 'folder')
print(f" - Created evidence folder: {folder_path}")
# Cleanup
os.unlink(test_file_path)
print("✅ Evidence Collector: PASSED")
return True
except Exception as e:
print(f"❌ Evidence Collector: FAILED - {e}")
return False
def test_report_generator():
"""Test report generation functionality"""
print("🔍 Testing Report Generator...")
try:
from forensic_analysis_tool import ReportGenerator
import datetime
generator = ReportGenerator()
# Sample data for reporting
sample_data = [
{
'name': 'test.txt',
'path': '/tmp/test.txt',
'size': 1024,
'modified': datetime.datetime.now(),
'source': 'test',
'hash': 'abc123'
}
]
with tempfile.TemporaryDirectory() as tmp_dir:
# Test CSV report generation
print(" - Testing CSV report generation...")
csv_path = generator.generate_csv_report(sample_data, tmp_dir, 'test')
print(f" - Generated CSV report: {csv_path}")
# Verify CSV content
with open(csv_path, 'r') as csv_file:
reader = csv.DictReader(csv_file)
rows = list(reader)
print(f" - CSV contains {len(rows)} data rows")
# Test HTML report generation
print(" - Testing HTML report generation...")
html_path = generator.generate_html_report(sample_data, [], [], [], tmp_dir)
print(f" - Generated HTML report: {html_path}")
# Verify HTML content
with open(html_path, 'r', encoding='utf-8') as html_file:
html_content = html_file.read()
print(f" - HTML report size: {len(html_content)} characters")
print("✅ Report Generator: PASSED")
return True
except Exception as e:
print(f"❌ Report Generator: FAILED - {e}")
return False
def test_system_compatibility():
"""Test system compatibility and requirements"""
print("🔍 Testing System Compatibility...")
try:
# Check Python version
python_version = sys.version_info
print(f" - Python version: {python_version.major}.{python_version.minor}.{python_version.micro}")
if python_version.major < 3 or (python_version.major == 3 and python_version.minor < 6):
print(" ⚠️ Warning: Python 3.6+ recommended")
# Check required modules
required_modules = ['os', 'sys', 'json', 'csv', 'sqlite3', 'datetime', 'subprocess',
'shutil', 'zipfile', 'hashlib', 'xml.etree.ElementTree', 'pathlib',
'collections', 'threading', 'time']
missing_modules = []
for module in required_modules:
try:
__import__(module)
except ImportError:
missing_modules.append(module)
if missing_modules:
print(f" ❌ Missing required modules: {missing_modules}")
return False
else:
print(" ✅ All required standard library modules available")
# Check optional modules
optional_modules = {
'PyQt5': 'GUI interface',
'send2trash': 'Recycle bin operations',
'python-evtx': 'Windows Event Log parsing',
'browser-history': 'Browser history extraction'
}
for module, description in optional_modules.items():
try:
if module == 'python-evtx':
import evtx.Evtx
elif module == 'browser-history':
import browser_history
else:
__import__(module.replace('-', '_'))
print(f" ✅ {module} available ({description})")
except ImportError:
print(f" ⚠️ {module} not available ({description})")
# Check platform
platform = sys.platform
print(f" - Platform: {platform}")
if platform.startswith('win'):
print(" ✅ Windows platform detected - Full functionality available")
else:
print(" ⚠️ Non-Windows platform - Some features may be limited")
print("✅ System Compatibility: PASSED")
return True
except Exception as e:
print(f"❌ System Compatibility: FAILED - {e}")
return False
def main():
"""Run all tests"""
print("🧪 Forensic Analysis Tool - Test Suite")
print("=" * 50)
tests = [
("System Compatibility", test_system_compatibility),
("File Recovery Engine", test_file_recovery),
("Log Analyzer", test_log_analyzer),
("Timeline Creator", test_timeline_creator),
("Evidence Collector", test_evidence_collector),
("Report Generator", test_report_generator)
]
passed = 0
failed = 0
for test_name, test_func in tests:
print()
try:
if test_func():
passed += 1
else:
failed += 1
except Exception as e:
print(f"❌ {test_name}: CRITICAL FAILURE - {e}")
failed += 1
print()
print("=" * 50)
print("🏁 Test Summary")
print(f"✅ Passed: {passed}")
print(f"❌ Failed: {failed}")
print(f"📊 Success Rate: {(passed / (passed + failed)) * 100:.1f}%")
if failed == 0:
print("🎉 All tests passed! The forensic tool is ready for production use.")
return 0
else:
print("⚠️ Some tests failed. Please review the issues above.")
return 1
if __name__ == '__main__':
exit_code = main()
sys.exit(exit_code)