-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_scanner.py
More file actions
67 lines (54 loc) · 1.95 KB
/
Copy pathmemory_scanner.py
File metadata and controls
67 lines (54 loc) · 1.95 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
#!/usr/bin/env python3
import re
import os
import mmap
import contextlib
def scan_for_patterns(data, patterns):
"""
This function doesn't need any changes.
It will scan any data-like object (bytes or mmap)
that supports the regex interface.
"""
findings = []
for pattern_name, pattern in patterns.items():
matches = re.finditer(pattern, data)
for match in matches:
findings.append({
'type': pattern_name,
'offset': match.start(),
'data': match.group().hex() if isinstance(match.group(), bytes) else match.group()
})
return findings
def analyze_memory_dump(file_path):
patterns = {
'ip_address': rb'\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b',
'email': rb'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b',
'url': rb'https?://[^\s<>"]+|www\.[^\s<>"]+',
'hex_string': rb'[0-9a-fA-F]{8,}',
'executable': rb'MZ|ELF'
}
try:
with open(file_path, 'rb') as f:
with contextlib.closing(mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)) as content:
results = scan_for_patterns(content, patterns)
return results
except Exception as e:
return f"Error: {str(e)}"
def main():
target_file = input("Enter memory dump path: ").strip()
if not os.path.exists(target_file):
print("File not found")
return
print(f"\nScanning: {target_file} (Memory-Efficient Mode)")
print("=" * 50)
findings = analyze_memory_dump(target_file)
if isinstance(findings, str):
print(findings)
return
for finding in findings[:20]:
if isinstance(finding['data'], bytes):
print(f"{finding['type']}: {finding['data'].decode('utf-8', 'ignore')}")
else:
print(f"{finding['type']}: {finding['data']}")
if __name__ == "__main__":
main()