Spaces:
Sleeping
Sleeping
Update header_analyzer.py
Browse files- header_analyzer.py +158 -156
header_analyzer.py
CHANGED
|
@@ -1,165 +1,167 @@
|
|
| 1 |
-
# body_analyzer.py
|
| 2 |
-
import os
|
| 3 |
import re
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
"
|
| 23 |
-
"invoice",
|
| 24 |
-
"payment required",
|
| 25 |
-
"unauthorized login",
|
| 26 |
-
"compromised",
|
| 27 |
-
"final reminder",
|
| 28 |
-
"account suspended",
|
| 29 |
-
"account deactivated",
|
| 30 |
-
"update your information",
|
| 31 |
-
"legal action",
|
| 32 |
-
"limited time offer",
|
| 33 |
-
"claim your prize",
|
| 34 |
-
"verify immediately",
|
| 35 |
-
"verify now",
|
| 36 |
-
"verify your credentials",
|
| 37 |
-
]
|
| 38 |
-
|
| 39 |
-
# Zero-shot candidate labels for intent/behavior
|
| 40 |
-
BEHAVIOR_LABELS = [
|
| 41 |
-
"credential harvesting",
|
| 42 |
-
"invoice/payment fraud",
|
| 43 |
-
"marketing",
|
| 44 |
-
"benign",
|
| 45 |
-
"malware",
|
| 46 |
-
"account takeover",
|
| 47 |
-
]
|
| 48 |
-
|
| 49 |
-
def _call_hf_text_model(model_name: str, text: str):
|
| 50 |
-
if not HF_API_KEY:
|
| 51 |
-
return None
|
| 52 |
-
try:
|
| 53 |
-
payload = {"inputs": text}
|
| 54 |
-
res = requests.post(
|
| 55 |
-
f"https://api-inference.huggingface.co/models/{model_name}",
|
| 56 |
-
headers=HF_HEADERS,
|
| 57 |
-
json=payload,
|
| 58 |
-
timeout=HF_TIMEOUT,
|
| 59 |
-
)
|
| 60 |
-
return res.json()
|
| 61 |
-
except Exception:
|
| 62 |
-
return None
|
| 63 |
-
|
| 64 |
-
def _call_hf_zero_shot(text: str, candidate_labels: List[str]):
|
| 65 |
-
if not HF_API_KEY:
|
| 66 |
-
return None
|
| 67 |
try:
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
)
|
| 75 |
-
return res.json()
|
| 76 |
except Exception:
|
| 77 |
return None
|
|
|
|
| 78 |
|
| 79 |
-
def
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
findings = []
|
| 98 |
score = 0
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
| 114 |
score += 10
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
if
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
if
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
findings.append(f"HuggingFace phishing model → {label} (conf {conf:.2f})")
|
| 131 |
-
score += int(conf * 100 * 0.9)
|
| 132 |
-
|
| 133 |
-
# Zero-shot behavior
|
| 134 |
-
behavior = None
|
| 135 |
-
behavior_conf = 0.0
|
| 136 |
-
if HF_API_KEY and model_input:
|
| 137 |
-
zs = _call_hf_zero_shot(model_input, BEHAVIOR_LABELS)
|
| 138 |
-
try:
|
| 139 |
-
if isinstance(zs, dict) and "labels" in zs and "scores" in zs:
|
| 140 |
-
behavior = zs["labels"][0]
|
| 141 |
-
behavior_conf = float(zs["scores"][0])
|
| 142 |
-
findings.append(f"Behavior inference → {behavior} (conf {behavior_conf:.2f})")
|
| 143 |
-
if behavior_conf >= 0.7:
|
| 144 |
-
score += int(behavior_conf * 30)
|
| 145 |
-
except Exception:
|
| 146 |
-
pass
|
| 147 |
-
|
| 148 |
-
if ml_conf >= 0.8 and ("phishing" in (ml_label or "").lower()):
|
| 149 |
-
score = max(score, 80)
|
| 150 |
-
|
| 151 |
-
score = int(max(0, min(score, 100)))
|
| 152 |
-
|
| 153 |
-
# Verdict
|
| 154 |
-
if score >= 70:
|
| 155 |
-
verdict = "🚨 Malicious"
|
| 156 |
-
elif 50 <= score < 70:
|
| 157 |
-
verdict = "⚠️ Suspicious"
|
| 158 |
-
elif 30 <= score < 50:
|
| 159 |
-
verdict = "📩 Spam"
|
| 160 |
else:
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import re
|
| 2 |
+
import difflib
|
| 3 |
+
import whois
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
|
| 6 |
+
# Official brand domains (extend as needed)
|
| 7 |
+
BRAND_OFFICIAL = {
|
| 8 |
+
"paypal": ["paypal.com"],
|
| 9 |
+
"amazon": ["amazon.com"],
|
| 10 |
+
"google": ["google.com", "gmail.com"],
|
| 11 |
+
"microsoft": ["microsoft.com", "outlook.com", "live.com"],
|
| 12 |
+
"apple": ["apple.com"],
|
| 13 |
+
"flowtoscale": ["flowtoscale.com"], # Example from your case
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
# Suspicious / cheap TLDs often abused
|
| 17 |
+
SUSPICIOUS_TLDS = {"info", "xyz", "top", "click", "work", "loan", "tk"}
|
| 18 |
+
|
| 19 |
+
def get_domain_age_days(domain: str):
|
| 20 |
+
"""Return domain age in days (or None if lookup fails)."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
try:
|
| 22 |
+
w = whois.whois(domain)
|
| 23 |
+
creation_date = w.creation_date
|
| 24 |
+
if isinstance(creation_date, list): # sometimes returns list
|
| 25 |
+
creation_date = creation_date[0]
|
| 26 |
+
if creation_date:
|
| 27 |
+
return (datetime.now() - creation_date).days
|
|
|
|
|
|
|
| 28 |
except Exception:
|
| 29 |
return None
|
| 30 |
+
return None
|
| 31 |
|
| 32 |
+
def parse_auth_results(auth_header: str):
|
| 33 |
+
"""
|
| 34 |
+
Parse the Authentication-Results header and return a readable summary.
|
| 35 |
+
"""
|
| 36 |
+
auth_header = (auth_header or "").lower()
|
| 37 |
+
findings = []
|
| 38 |
+
|
| 39 |
+
if not auth_header:
|
| 40 |
+
return "No Authentication-Results header found"
|
| 41 |
+
|
| 42 |
+
# SPF
|
| 43 |
+
if "spf=pass" in auth_header:
|
| 44 |
+
findings.append("SPF passed")
|
| 45 |
+
elif "spf=fail" in auth_header:
|
| 46 |
+
findings.append("SPF failed")
|
| 47 |
+
|
| 48 |
+
# DKIM
|
| 49 |
+
if "dkim=pass" in auth_header:
|
| 50 |
+
findings.append("DKIM passed")
|
| 51 |
+
elif "dkim=fail" in auth_header or "dkim=permerror" in auth_header:
|
| 52 |
+
findings.append("DKIM failed")
|
| 53 |
+
|
| 54 |
+
# DMARC
|
| 55 |
+
if "dmarc=pass" in auth_header:
|
| 56 |
+
findings.append("DMARC passed")
|
| 57 |
+
elif "dmarc=fail" in auth_header:
|
| 58 |
+
findings.append("DMARC failed")
|
| 59 |
+
|
| 60 |
+
if not findings:
|
| 61 |
+
return "Authentication results unclear or missing"
|
| 62 |
+
|
| 63 |
+
return ", ".join(findings)
|
| 64 |
+
|
| 65 |
+
def analyze_headers(headers, body=""):
|
| 66 |
+
"""
|
| 67 |
+
Input: headers dict, optional body text
|
| 68 |
+
Output: (findings: list[str], score: int, auth_summary: str)
|
| 69 |
+
"""
|
| 70 |
findings = []
|
| 71 |
score = 0
|
| 72 |
+
headers = headers or {}
|
| 73 |
+
|
| 74 |
+
auth_results = (headers.get("Authentication-Results") or headers.get("Authentication-results") or "").lower()
|
| 75 |
+
|
| 76 |
+
# Strict auth failures
|
| 77 |
+
if "dkim=fail" in auth_results or "dkim=permerror" in auth_results:
|
| 78 |
+
findings.append("Header: DKIM check failed")
|
| 79 |
+
score += 30
|
| 80 |
+
if "spf=fail" in auth_results:
|
| 81 |
+
findings.append("Header: SPF check failed")
|
| 82 |
+
score += 30
|
| 83 |
+
if "dmarc=fail" in auth_results:
|
| 84 |
+
findings.append("Header: DMARC check failed")
|
| 85 |
+
score += 30
|
| 86 |
+
|
| 87 |
+
# Softer auth problems
|
| 88 |
+
if any(x in auth_results for x in ["spf=softfail", "spf=neutral", "spf=none"]):
|
| 89 |
+
findings.append("Header: SPF not properly aligned")
|
| 90 |
score += 10
|
| 91 |
+
if any(x in auth_results for x in ["dmarc=temperror", "dkim=temperror"]):
|
| 92 |
+
findings.append("Header: Temporary auth errors (DKIM/DMARC)")
|
| 93 |
+
score += 5
|
| 94 |
+
|
| 95 |
+
# From and Reply-To domain compare
|
| 96 |
+
from_addr = headers.get("From", "") or ""
|
| 97 |
+
reply_to = headers.get("Reply-To", "") or ""
|
| 98 |
+
from_domain_m = re.search(r'@([a-zA-Z0-9.-]+)', from_addr)
|
| 99 |
+
reply_domain_m = re.search(r'@([a-zA-Z0-9.-]+)', reply_to)
|
| 100 |
+
if from_domain_m and reply_domain_m:
|
| 101 |
+
from_domain = from_domain_m.group(1).lower()
|
| 102 |
+
reply_domain = reply_domain_m.group(1).lower()
|
| 103 |
+
if from_domain != reply_domain:
|
| 104 |
+
findings.append(f"Header: Reply-To domain mismatch (From: {from_domain}, Reply-To: {reply_domain})")
|
| 105 |
+
score += 20
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
else:
|
| 107 |
+
from_domain = from_domain_m.group(1).lower() if from_domain_m else ""
|
| 108 |
+
|
| 109 |
+
# Sender domain analysis
|
| 110 |
+
if from_domain:
|
| 111 |
+
parts = from_domain.split('.')
|
| 112 |
+
tld = parts[-1]
|
| 113 |
+
|
| 114 |
+
# free provider detection
|
| 115 |
+
if from_domain in ["gmail.com", "yahoo.com", "outlook.com", "hotmail.com"]:
|
| 116 |
+
findings.append(f"Header: Free email provider used ({from_domain})")
|
| 117 |
+
score += 8
|
| 118 |
+
|
| 119 |
+
# suspicious domain structure
|
| 120 |
+
if len(parts) > 4 or (parts and any(ch.isdigit() for ch in parts[0])):
|
| 121 |
+
findings.append(f"Header: Suspicious-looking domain structure ({from_domain})")
|
| 122 |
+
score += 15
|
| 123 |
+
|
| 124 |
+
# suspicious TLD
|
| 125 |
+
if tld in SUSPICIOUS_TLDS:
|
| 126 |
+
findings.append(f"Header: Suspicious/abused TLD used ({tld})")
|
| 127 |
+
score += 20
|
| 128 |
+
|
| 129 |
+
# Domain age check
|
| 130 |
+
age_days = get_domain_age_days(from_domain)
|
| 131 |
+
if age_days is not None and age_days < 90:
|
| 132 |
+
findings.append(f"Header: Domain {from_domain} is very new ({age_days} days old)")
|
| 133 |
+
score += 35
|
| 134 |
+
|
| 135 |
+
# brand-squatting / look-alike check
|
| 136 |
+
for brand, official_list in BRAND_OFFICIAL.items():
|
| 137 |
+
if brand in from_domain:
|
| 138 |
+
is_official = any(
|
| 139 |
+
from_domain.endswith("." + off) or from_domain == off
|
| 140 |
+
for off in official_list
|
| 141 |
+
)
|
| 142 |
+
if not is_official:
|
| 143 |
+
findings.append(f"Header: Domain contains brand '{brand}' but is not official ({from_domain})")
|
| 144 |
+
score += 30
|
| 145 |
+
|
| 146 |
+
# fuzzy look-alike
|
| 147 |
+
for legit in official_list:
|
| 148 |
+
ratio = difflib.SequenceMatcher(None, from_domain, legit).ratio()
|
| 149 |
+
if ratio > 0.7 and from_domain != legit:
|
| 150 |
+
findings.append(f"Header: Possible look-alike spoofing ({from_domain} vs {legit})")
|
| 151 |
+
score += 40
|
| 152 |
+
|
| 153 |
+
# Content-to-domain mismatch (organization spoofing)
|
| 154 |
+
if body and "ravenmail" in body.lower() and "ravenmail" not in from_domain:
|
| 155 |
+
findings.append("Header/Content: Possible spoofing — mentions RavenMail but sender domain is unrelated")
|
| 156 |
+
score += 40
|
| 157 |
+
|
| 158 |
+
# Bcc usage
|
| 159 |
+
if headers.get("Bcc") or headers.get("bcc"):
|
| 160 |
+
findings.append("Header: Email sent with BCC (common in mass phishing)")
|
| 161 |
+
score += 12
|
| 162 |
+
|
| 163 |
+
if not findings:
|
| 164 |
+
return ["No suspicious issues found in headers."], 0, "No Authentication-Results header found"
|
| 165 |
+
|
| 166 |
+
# Return findings, cumulative score, and parsed authentication summary
|
| 167 |
+
return findings, score, parse_auth_results(auth_results)
|