-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.py
More file actions
394 lines (348 loc) · 15.5 KB
/
scanner.py
File metadata and controls
394 lines (348 loc) · 15.5 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
# =============================================================================
# scanner.py — PhantomEye v1.2
# Red Parrot Accounting Ltd
#
# Three scan engines:
# scan_firewall_logs() — Windows Firewall pfirewall.log
# scan_dns_cache() — Windows DNS resolver cache via PowerShell
# analyse_email_headers() — Raw email header IOC extraction
#
# FIXES v1.2:
# - scan_firewall_logs now also checks src_ip (inbound from malicious IP).
# - analyse_email_headers: IP extraction no longer triggered by any "[" in
# a line — now restricted to Received: headers only, eliminating false
# positives from Message-ID, Content-Type, etc.
# - Bare except: pass replaced with logged warnings.
# - Domain deduplication cap ([:20]) raised to 50 for better coverage.
# =============================================================================
import re
import sqlite3
import subprocess
from datetime import datetime, timedelta
from alerts import record_alert
from config import DB_PATH, FIREWALL_LOG, FIREWALL_LOG_DAYS
from logger import log
from lookup import is_ioc_known
from utils import is_private_ip, is_valid_domain, is_valid_ip, is_whitelisted
# ---------------------------------------------------------------------------
# Firewall log scanner
# ---------------------------------------------------------------------------
def scan_firewall_logs(callback=None) -> list[dict]:
"""
Parse Windows Firewall log and check destination AND source IPs against
the threat intelligence database.
- ALLOW + malicious dst_ip → active outbound C2 connection (CRITICAL)
- DROP + malicious dst_ip → infection attempt caught by firewall (CRITICAL)
- ALLOW/DROP + malicious src_ip → inbound scan/attack from known bad actor (HIGH)
"""
import os
log.info("Scanning Windows Firewall logs...")
if not os.path.exists(FIREWALL_LOG):
msg = (
f"Firewall log not found: {FIREWALL_LOG}\n"
"Enable logging: Windows Defender Firewall → Advanced Settings "
"→ Properties → each Profile → Logging"
)
log.warning(msg)
if callback:
callback("WARNING: " + msg)
return []
hits = []
cutoff = datetime.now() - timedelta(days=FIREWALL_LOG_DAYS)
checked = set()
conn = sqlite3.connect(DB_PATH)
try:
with open(FIREWALL_LOG, encoding="utf-8", errors="ignore") as f:
for line in f:
line = line.strip()
if line.startswith("#") or not line:
continue
# Format: date time action protocol src-ip dst-ip src-port dst-port ...
parts = line.split()
if len(parts) < 6:
continue
try:
log_time = datetime.strptime(f"{parts[0]} {parts[1]}", "%Y-%m-%d %H:%M:%S")
if log_time < cutoff:
continue
except ValueError:
continue
action = parts[2].upper() if len(parts) > 2 else ""
src_ip = parts[4] if len(parts) > 4 else ""
dst_ip = parts[5] if len(parts) > 5 else ""
if action not in ("ALLOW", "DROP"):
continue
# --- Check destination IP (outbound / egress) ---
if is_valid_ip(dst_ip) and not is_private_ip(dst_ip):
key = ("dst", dst_ip)
if key not in checked:
checked.add(key)
if is_ioc_known(dst_ip, "ip"):
if action == "DROP":
sev = "CRITICAL"
a_type = "MALICIOUS IP — BLOCKED BY FIREWALL (infection indicator)"
detail = (
"A machine on this network attempted to connect to a known "
"malicious IP and was blocked by the firewall. This strongly "
"indicates an infected machine."
)
else:
sev = "CRITICAL"
a_type = "MALICIOUS IP IN FIREWALL LOG — ACTIVE OUTBOUND CONNECTION"
detail = (
"An outbound connection to a known malicious IP was allowed "
"through the firewall. Investigate immediately."
)
context = f"Firewall log — dst — action={action} at {parts[0]} {parts[1]}"
recorded = record_alert(
severity=sev,
alert_type=a_type,
ioc_value=dst_ip,
ioc_type="ip",
source_feed="firewall_scan",
context=context,
details=detail,
conn=conn,
)
conn.commit()
hit = {
"ioc": dst_ip,
"type": "ip",
"direction": "outbound",
"action": action,
"context": context,
}
hits.append(hit)
msg = f"[HIT] {dst_ip} (dst) — {action} — {'(new alert)' if recorded else '(dedupe)'}"
log.warning(msg)
if callback:
callback(msg)
# --- Check source IP (inbound / ingress) ---
if is_valid_ip(src_ip) and not is_private_ip(src_ip):
key = ("src", src_ip)
if key not in checked:
checked.add(key)
if is_ioc_known(src_ip, "ip"):
sev = "HIGH"
a_type = "MALICIOUS IP — INBOUND CONNECTION ATTEMPT"
detail = (
"A connection attempt was received from a known malicious IP. "
f"Action taken by firewall: {action}."
)
context = f"Firewall log — src — action={action} at {parts[0]} {parts[1]}"
recorded = record_alert(
severity=sev,
alert_type=a_type,
ioc_value=src_ip,
ioc_type="ip",
source_feed="firewall_scan",
context=context,
details=detail,
conn=conn,
)
conn.commit()
hit = {
"ioc": src_ip,
"type": "ip",
"direction": "inbound",
"action": action,
"context": context,
}
hits.append(hit)
msg = f"[HIT] {src_ip} (src) — {action} — {'(new alert)' if recorded else '(dedupe)'}"
log.warning(msg)
if callback:
callback(msg)
except PermissionError:
msg = "Cannot read firewall log — run as Administrator"
log.error(msg)
if callback:
callback(f"ERROR: {msg}")
except OSError as e:
log.error("Error reading firewall log: %s", e)
if callback:
callback(f"ERROR reading firewall log: {e}")
finally:
conn.close()
unique_ips = len({k[1] for k in checked})
log.info("Firewall scan complete. %d unique IPs checked, %d malicious hits.", unique_ips, len(hits))
if callback:
callback(f"Firewall scan: {unique_ips} IPs checked, {len(hits)} malicious hits")
return hits
# ---------------------------------------------------------------------------
# DNS cache scanner
# ---------------------------------------------------------------------------
def scan_dns_cache(callback=None) -> list[dict]:
"""
Read the Windows DNS resolver cache via PowerShell and check all domains
against the threat intelligence database.
"""
log.info("Scanning DNS cache...")
hits = []
try:
ps_cmd = "Get-DnsClientCache | Select-Object -ExpandProperty Entry | Sort-Object -Unique"
proc_result = subprocess.run(
["powershell", "-NoProfile", "-NonInteractive", "-Command", ps_cmd],
capture_output=True,
text=True,
timeout=30,
)
if proc_result.returncode != 0 and proc_result.stderr:
log.warning("PowerShell DNS cache stderr: %s", proc_result.stderr.strip())
domains = [d.strip().lower() for d in proc_result.stdout.splitlines() if d.strip()]
except Exception as e:
log.error("Could not read DNS cache: %s", e)
if callback:
callback(f"ERROR reading DNS cache: {e}")
return []
checked = set()
conn = sqlite3.connect(DB_PATH)
try:
for domain in domains:
if not is_valid_domain(domain) or domain in checked:
continue
if is_whitelisted(domain, "domain"):
continue
checked.add(domain)
if not is_ioc_known(domain, "domain"):
continue
context = "Found in Windows DNS resolver cache"
recorded = record_alert(
severity="CRITICAL",
alert_type="MALICIOUS DOMAIN IN DNS CACHE",
ioc_value=domain,
ioc_type="domain",
source_feed="dns_scan",
context=context,
details="Machine recently resolved this known malicious domain.",
conn=conn,
)
conn.commit()
hit = {"ioc": domain, "type": "domain", "context": context}
hits.append(hit)
msg = f"[HIT] {domain} — {'(new alert)' if recorded else '(dedupe)'}"
log.warning(msg)
if callback:
callback(msg)
except Exception as e:
log.error("DNS scan error: %s", e)
finally:
conn.close()
log.info("DNS scan complete. %d domains checked, %d hits.", len(checked), len(hits))
if callback:
callback(f"DNS scan: {len(checked)} domains checked, {len(hits)} malicious hits")
return hits
# ---------------------------------------------------------------------------
# Email header analyser
# ---------------------------------------------------------------------------
def analyse_email_headers(header_text: str, callback=None) -> str:
"""
Parse raw email headers, extract all sender/relay IPs and domains,
check each against the threat intelligence feeds.
FIX: IP extraction now restricted to lines starting with 'Received:'.
Previously any line containing '[' was also searched, causing false
positives from Message-ID headers and other bracketed fields.
"""
lines = header_text.splitlines()
ip_pattern = re.compile(r"\b(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\b")
domain_pattern = re.compile(r"from\s+([a-zA-Z0-9.\-]+\.[a-zA-Z]{2,})", re.IGNORECASE)
# --- Extract From and Reply-To domains ---
from_domain = ""
reply_domain = ""
for line in lines:
ll = line.lower()
if ll.startswith("from:") and not from_domain:
m = re.search(r"@([a-zA-Z0-9.\-]+)", line)
if m:
from_domain = m.group(1).lower()
if ll.startswith("reply-to:") and not reply_domain:
m = re.search(r"@([a-zA-Z0-9.\-]+)", line)
if m:
reply_domain = m.group(1).lower()
mismatch_alert = bool(from_domain and reply_domain and from_domain != reply_domain)
# --- Extract IPs from Received: headers only (FIX: removed "[" in line branch) ---
received_ips = []
for line in lines:
if line.lower().startswith("received:"):
for ip in ip_pattern.findall(line):
if is_valid_ip(ip) and not is_private_ip(ip):
received_ips.append(ip)
# --- Extract domains from Received: headers ---
received_domains = []
for line in lines:
if line.lower().startswith("received:"):
for domain in domain_pattern.findall(line):
domain = domain.lower().strip(".")
if is_valid_domain(domain) and not is_whitelisted(domain, "domain"):
received_domains.append(domain)
# --- Build report ---
report = []
report.append("=" * 60)
report.append(" PhantomEye — Email Header Analysis")
report.append("=" * 60)
if from_domain:
report.append(f" From domain : {from_domain}")
if reply_domain:
report.append(f" Reply-To : {reply_domain}")
if mismatch_alert:
report.append(" WARNING: From != Reply-To — possible phishing redirect!")
unique_ips = list(dict.fromkeys(received_ips))
unique_domains = list(dict.fromkeys(received_domains))[:50] # raised from 20 to 50
report.append(f" IPs found : {len(unique_ips)}")
report.append(f" Domains found : {len(unique_domains)}")
report.append("-" * 60)
threat_count = 0
conn = sqlite3.connect(DB_PATH)
try:
for ip in unique_ips:
hit = is_ioc_known(ip, "ip")
if hit:
threat_count += 1
record_alert(
"CRITICAL",
"MALICIOUS IP IN EMAIL HEADER",
ip,
"ip",
"email_analysis",
"Email header analysis",
"",
conn=conn,
)
conn.commit()
report.append(f" IP {ip:<20} MALICIOUS")
else:
report.append(f" IP {ip:<20} Clean")
for domain in unique_domains:
if is_whitelisted(domain, "domain"):
continue
hit = is_ioc_known(domain, "domain")
if hit:
threat_count += 1
record_alert(
"CRITICAL",
"MALICIOUS DOMAIN IN EMAIL HEADER",
domain,
"domain",
"email_analysis",
"Email header analysis",
"",
conn=conn,
)
conn.commit()
report.append(f" DOM {domain:<35} MALICIOUS")
else:
report.append(f" DOM {domain:<35} Clean")
except Exception as e:
log.error("Email header analysis error: %s", e)
report.append(f" ERROR during analysis: {e}")
finally:
conn.close()
report.append("-" * 60)
if threat_count > 0 or mismatch_alert:
report.append(f" VERDICT: SUSPICIOUS — {threat_count} threat(s) detected")
report.append(" Do NOT click links or open attachments in this email.")
else:
report.append(" VERDICT: No known threats found in email headers.")
report.append(" Note: Clean headers don't guarantee the email is safe.")
report.append("=" * 60)
return "\n".join(report)