-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sandbox.py
More file actions
177 lines (145 loc) · 5.9 KB
/
test_sandbox.py
File metadata and controls
177 lines (145 loc) · 5.9 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
#!/usr/bin/env python3
"""
MemHawk Sandbox Test Suite
Tests the sandbox functionality with various scenarios
Authors: Adriteyo Das, Anvita Warjri, Shivam Lahoty
"""
import sys
import os
import tempfile
import json
from pathlib import Path
# Add src directory to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
try:
from sandbox import MemHawkSandbox, SandboxedVolatilityRunner
from volatility_bridge import VolatilityRunner
except ImportError as e:
print(f"Import error: {e}")
print("Make sure you're in the correct directory and dependencies are installed")
sys.exit(1)
def create_test_memory_dump():
"""Create a small test memory dump file for testing"""
test_file = Path("test_memory.dmp")
if not test_file.exists():
# Create a small fake memory dump with proper header
with open(test_file, 'wb') as f:
# Write a fake PAGEDUM header (Windows crash dump signature)
f.write(b'PAGEDUM\x00')
f.write(b'\x00' * 1000) # 1KB of null bytes
print(f"Created test memory dump: {test_file}")
return str(test_file)
def test_sandbox_validation():
"""Test file validation functionality"""
print("\n" + "="*50)
print("Testing Sandbox File Validation")
print("="*50)
test_file = create_test_memory_dump()
with MemHawkSandbox() as sandbox:
# Test valid file
result = sandbox.validate_file(test_file)
print(f"Valid file test: {'PASS' if result['valid'] else 'FAIL'}")
print(f" - File size: {result.get('file_size_human', 'N/A')}")
print(f" - Risk level: {result.get('risk_level', 'N/A')}")
# Test invalid extension
invalid_file = "test.exe"
Path(invalid_file).touch() # Create empty file
result = sandbox.validate_file(invalid_file)
print(f"Invalid extension test: {'PASS' if not result['valid'] else 'FAIL'}")
Path(invalid_file).unlink() # Clean up
# Test non-existent file
result = sandbox.validate_file("nonexistent.dmp")
print(f"Non-existent file test: {'PASS' if not result['valid'] else 'FAIL'}")
def test_sandbox_execution():
"""Test sandboxed execution"""
print("\n" + "="*50)
print("Testing Sandbox Execution")
print("="*50)
test_file = create_test_memory_dump()
with MemHawkSandbox(max_execution_time=30) as sandbox:
# Test command execution (simple test command)
result = sandbox.execute_volatility(['python', '--version'])
print(f"Command execution test: {'PASS' if result['success'] else 'FAIL'}")
if result['success']:
print(f" - Execution time: {result['execution_time']:.2f}s")
print(f" - Output: {result['stdout'].strip()}")
else:
print(f" - Error: {result.get('error', 'Unknown error')}")
def test_volatility_integration():
"""Test Volatility integration with sandbox"""
print("\n" + "="*50)
print("Testing Volatility Integration")
print("="*50)
# Test standard runner
runner = VolatilityRunner(use_sandbox=False)
print(f"Standard runner - Volatility found: {'YES' if runner.volatility_path else 'NO'}")
# Test sandboxed runner
try:
sandbox_runner = VolatilityRunner(use_sandbox=True)
print(f"Sandboxed runner - Sandbox enabled: {'YES' if sandbox_runner.use_sandbox else 'NO'}")
# Test plugin execution (will use demo data if no real memory dump)
result = sandbox_runner.run_plugin("fake_memory.dmp", "windows.pslist")
print(f"Plugin execution test: {'PASS' if result['success'] else 'FAIL'}")
print(f" - Security mode: {result.get('security_mode', 'standard')}")
except Exception as e:
print(f"Sandboxed runner error: {e}")
def test_resource_monitoring():
"""Test resource monitoring capabilities"""
print("\n" + "="*50)
print("Testing Resource Monitoring")
print("="*50)
config = {
'max_memory_mb': 100, # Low limit for testing
'max_cpu_percent': 50,
'max_execution_time': 10
}
with MemHawkSandbox(**config) as sandbox:
# Test resource usage collection
usage = sandbox._get_resource_usage()
print(f"Resource monitoring test: {'PASS' if 'memory_mb' in usage else 'FAIL'}")
if 'memory_mb' in usage:
print(f" - Memory usage: {usage['memory_mb']:.2f}MB")
print(f" - CPU usage: {usage['cpu_percent']}%")
def test_cleanup():
"""Test sandbox cleanup"""
print("\n" + "="*50)
print("Testing Sandbox Cleanup")
print("="*50)
sandbox_dir = None
# Create and cleanup sandbox
with MemHawkSandbox() as sandbox:
sandbox_dir = sandbox.sandbox_dir
print(f"Sandbox directory created: {sandbox_dir}")
print(f"Directory exists: {'YES' if sandbox_dir.exists() else 'NO'}")
# Check if cleanup worked
if sandbox_dir:
cleanup_success = not sandbox_dir.exists()
print(f"Cleanup test: {'PASS' if cleanup_success else 'FAIL'}")
def run_all_tests():
"""Run all sandbox tests"""
print("MemHawk Sandbox Test Suite")
print("=" * 50)
try:
test_sandbox_validation()
test_sandbox_execution()
test_volatility_integration()
test_resource_monitoring()
test_cleanup()
print("\n" + "="*50)
print("All tests completed!")
print("="*50)
except KeyboardInterrupt:
print("\nTests interrupted by user")
except Exception as e:
print(f"\nTest suite error: {e}")
import traceback
traceback.print_exc()
finally:
# Clean up test files
test_files = ["test_memory.dmp"]
for file in test_files:
if Path(file).exists():
Path(file).unlink()
print(f"Cleaned up: {file}")
if __name__ == "__main__":
run_all_tests()