Repository navigation
Expand file tree
/
Copy pathphase7_gmail_v2.py
More file actions
225 lines (170 loc) · 8.03 KB
/
Copy pathphase7_gmail_v2.py
File metadata and controls
225 lines (170 loc) · 8.03 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
#!/usr/bin/env python3
"""
Phase 7 – Gmail Message Extraction (Refactored)
Decompress and extract Gmail messages from bigTopDataDB
"""
import sqlite3
import zlib
import re
import json
from pathlib import Path
from datetime import datetime
from typing import Dict, List, Optional
import sys
sys.path.insert(0, str(Path(__file__).parent))
from config import EXTRACTED, find_databases, get_output_dir, PhaseOutput, calculate_sha256
def decompress_message(blob: bytes) -> Optional[bytes]:
if not blob:
return None
try:
data = blob[1:] if blob[0] == 0x00 else blob
decompressed = zlib.decompress(data)
return decompressed
except:
return None
def extract_strings(data: bytes, min_length: int = 5) -> List[str]:
strings = []
current = b""
for byte in data:
if 32 <= byte < 127:
current += bytes([byte])
else:
if len(current) >= min_length:
try:
strings.append(current.decode('ascii'))
except:
pass
current = b""
if len(current) >= min_length:
try:
strings.append(current.decode('ascii'))
except:
pass
return strings
def extract_email_metadata(strings: List[str]) -> Dict:
metadata = {
'sender': None,
'sender_email': None,
'recipient': None,
'subject': None,
'date': None,
'content_preview': None,
}
full_text = ' | '.join(strings)
email_pattern = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}'
emails = re.findall(email_pattern, full_text)
if emails:
for email in emails:
if 'gmail.com' not in email.lower() and 'google.com' not in email.lower():
if email.startswith('no-reply') or email.startswith('notification') or 'noreply' in email.lower():
metadata['sender_email'] = email
break
for email in emails:
if 'heisenbergcarro@gmail.com' in email:
metadata['recipient'] = email
break
for platform in ['Twitter', 'Instagram', 'TikTok', 'Facebook', 'Reddit', 'YouTube', 'LinkedIn']:
if platform.lower() in full_text.lower():
metadata['sender'] = platform
break
content_parts = []
for s in strings:
if len(s) > 20 and not any(skip in s.lower() for skip in ['margin', 'padding', 'border', 'width=', 'height=', 'style=']):
content_parts.append(s)
if len(content_parts) >= 5:
break
if content_parts:
metadata['content_preview'] = ' '.join(content_parts[:5])[:500]
return metadata
def main():
output = PhaseOutput(7, "gmail")
out_dir = get_output_dir("gmail")
gmail_dbs = find_databases("com.google.android.gm")
md = ["# Phase 7 – Gmail Message Extraction\n\n"]
md.append(f"**Erstellt:** {datetime.now().strftime('%Y-%m-%d %H:%M')} \n\n---\n\n")
md.append("## 1. Datenbanken\n\n")
if not gmail_dbs:
md.append("**Keine Gmail Datenbank gefunden**\n\n")
output.set_summary("status", "no_database_found")
else:
md.append(f"**Gefundene Datenbanken:** {len(gmail_dbs)}\n\n")
output.set_summary("databases_found", len(gmail_dbs))
for db_path in gmail_dbs:
md.append(f"- {db_path.name}\n")
bigtop_dbs = [d for d in gmail_dbs if "bigTopDataDB" in d.name]
if not bigtop_dbs:
for search_path in [
EXTRACTED / "Dump" / "data_mirror" / "data_ce" / "null" / "0" / "com.google.android.gm" / "databases",
EXTRACTED / "Dump" / "data" / "user" / "0" / "com.google.android.gm" / "databases",
EXTRACTED / "Dump" / "data" / "data" / "com.google.android.gm" / "databases",
]:
if search_path.exists():
for f in search_path.glob("bigTopDataDB*"):
if '-wal' not in f.name and '-shm' not in f.name:
bigtop_dbs.append(f)
if bigtop_dbs:
bigtop_dbs = sorted(bigtop_dbs, key=lambda x: x.stat().st_size, reverse=True)
main_db = bigtop_dbs[0]
output.add_source(main_db, "Gmail bigTopDataDB database")
md.append(f"\n**Analysiere:** {main_db.name}\n\n")
md.append("## 2. Nachrichten-Extraktion\n\n")
try:
con = sqlite3.connect(f"file:{main_db}?mode=ro", uri=True)
con.execute("SELECT COUNT(*) FROM item_messages WHERE zipped_message_proto IS NOT NULL")
total_count = con.execute("SELECT COUNT(*) FROM item_messages WHERE zipped_message_proto IS NOT NULL").fetchone()[0]
md.append(f"**Gesamt-Nachrichten:** {total_count}\n\n")
output.set_summary("total_messages", total_count)
messages = []
decompressed_count = 0
cur = con.execute("""
SELECT row_id, server_perm_id, zipped_message_proto
FROM item_messages
WHERE zipped_message_proto IS NOT NULL
ORDER BY row_id
LIMIT 200
""")
for row_id, server_perm_id, blob in cur.fetchall():
decompressed = decompress_message(blob)
if decompressed:
decompressed_count += 1
strings = extract_strings(decompressed)
metadata = extract_email_metadata(strings)
msg = {
'row_id': row_id,
'server_perm_id': server_perm_id,
'size_compressed': len(blob),
'size_decompressed': len(decompressed),
}
msg.update(metadata)
messages.append(msg)
if metadata.get('sender'):
output.add_finding("email_sender", f"msg_{row_id}", metadata['sender'], metadata.get('sender_email', ''))
output.set_summary("decompressed", decompressed_count)
output.set_summary("success_rate", f"{100*decompressed_count//total_count if total_count else 0}%")
md.append(f"**Erfolgreich dekomprimiert:** {decompressed_count}/{total_count}\n\n")
md.append("## 3. Absender\n\n")
senders = {}
for msg in messages:
sender = msg.get('sender') or msg.get('sender_email')
if sender:
senders[sender] = senders.get(sender, 0) + 1
md.append("| Absender | Anzahl |\n|---|---|\n")
for sender, count in sorted(senders.items(), key=lambda x: x[1], reverse=True)[:20]:
md.append(f"| {sender} | {count} |\n")
json_path = out_dir / "gmail_messages.json"
json_path.write_text(json.dumps(messages, indent=2, default=str))
md.append(f"\n**JSON gespeichert:** {json_path.name}\n\n")
con.close()
except Exception as e:
md.append(f"**Fehler:** {e}\n\n")
output.set_summary("error", str(e))
md.append("\n---\n\n")
md.append(f"**Report erstellt:** {datetime.now().isoformat()}\n")
report_path = out_dir / "gmail_report.md"
report_path.write_text("".join(md))
outputs = output.write()
print(f"[OK] Phase 7 Complete")
print(f" JSON: {list(outputs.keys())[0]}")
print(f" Markdown: {report_path}")
if __name__ == "__main__":
main()