Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,73 +13,81 @@ def analyze_email(file_path, export_pdf=False):
|
|
| 13 |
if file_path is None:
|
| 14 |
return "<p style='color:red;'>Please upload a .eml file to analyze.</p>", None
|
| 15 |
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
# Correct handling of attack_score
|
| 27 |
-
score_val = int(attack_score)
|
| 28 |
-
if score_val > 100:
|
| 29 |
score_val = 100
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# Verdict color
|
| 32 |
-
verdict_color = "#2ecc71"
|
| 33 |
-
|
|
|
|
| 34 |
verdict_color = "#e74c3c"
|
| 35 |
-
elif "
|
| 36 |
verdict_color = "#e67e22"
|
| 37 |
-
elif "
|
| 38 |
verdict_color = "#f1c40f"
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
except ValueError:
|
| 46 |
-
html = "<pre>" + "\n".join(results_str) + "</pre>"
|
| 47 |
-
return html, None
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
# HTML Report
|
| 54 |
html = f"""
|
| 55 |
-
<div style='font-family: Arial, sans-serif; padding: 20px; max-width:
|
| 56 |
<h2 style="color:#2c3e50;">π§ Email Security Report</h2>
|
| 57 |
-
|
| 58 |
-
<
|
|
|
|
| 59 |
<div style="width:{score_val}%; background:#3498db; height:100%; text-align:center; color:white; line-height:24px;">
|
| 60 |
{score_val}/100
|
| 61 |
</div>
|
| 62 |
</div>
|
|
|
|
| 63 |
<p><b>Attack Type:</b> {attack_type}</p>
|
| 64 |
<p><b>Final Verdict:</b> <span style="background:{verdict_color}; color:white; padding:4px 10px; border-radius:6px;">
|
| 65 |
-
{verdict
|
| 66 |
</span></p>
|
| 67 |
-
|
|
|
|
| 68 |
<summary>π Attack Analysis Tags ({len(tags)})</summary>
|
| 69 |
-
<ul>
|
| 70 |
-
{''.join(f'<li>{tag}</li>' for tag in tags)}
|
| 71 |
-
</ul>
|
| 72 |
</details>
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
<
|
| 76 |
-
|
| 77 |
-
</ul>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
</details>
|
|
|
|
| 79 |
<details style="margin-top:15px;">
|
| 80 |
<summary>π© Highlighted Email Body</summary>
|
| 81 |
-
<div style='border:1px solid #ccc; padding:15px; background:#fdfdfd; border-radius:8px;'>
|
| 82 |
-
{
|
| 83 |
</div>
|
| 84 |
</details>
|
| 85 |
</div>
|
|
@@ -87,7 +95,10 @@ def analyze_email(file_path, export_pdf=False):
|
|
| 87 |
|
| 88 |
pdf_path = None
|
| 89 |
if export_pdf:
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
return html, pdf_path
|
| 93 |
|
|
|
|
| 13 |
if file_path is None:
|
| 14 |
return "<p style='color:red;'>Please upload a .eml file to analyze.</p>", None
|
| 15 |
|
| 16 |
+
# Get structured results
|
| 17 |
+
summary, details = analyze(file_path)
|
| 18 |
|
| 19 |
+
# Safely extract summary fields
|
| 20 |
+
attack_score = summary.get("Attack Score", 0)
|
| 21 |
+
try:
|
| 22 |
+
score_val = int(round(float(attack_score)))
|
| 23 |
+
except Exception:
|
| 24 |
+
score_val = 0
|
| 25 |
+
if score_val > 100:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
score_val = 100
|
| 27 |
+
if score_val < 0:
|
| 28 |
+
score_val = 0
|
| 29 |
+
|
| 30 |
+
attack_type = summary.get("Attack Type", "Unknown")
|
| 31 |
+
verdict = summary.get("Final Verdict", "No Verdict")
|
| 32 |
|
| 33 |
# Verdict color
|
| 34 |
+
verdict_color = "#2ecc71"
|
| 35 |
+
v_lower = verdict.lower()
|
| 36 |
+
if "malicious" in v_lower:
|
| 37 |
verdict_color = "#e74c3c"
|
| 38 |
+
elif "suspicious" in v_lower:
|
| 39 |
verdict_color = "#e67e22"
|
| 40 |
+
elif "spam" in v_lower:
|
| 41 |
verdict_color = "#f1c40f"
|
| 42 |
|
| 43 |
+
# Collect details safely
|
| 44 |
+
header_findings = details.get("Header Findings", []) or []
|
| 45 |
+
body_findings = details.get("Body Findings", []) or []
|
| 46 |
+
url_findings = details.get("URL Findings", []) or []
|
| 47 |
+
highlighted_body = details.get("Highlighted Body", "") or ""
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
# Tags (from summary Main Tags if present)
|
| 50 |
+
main_tags_str = summary.get("Main Tags", "")
|
| 51 |
+
tags = [t.strip() for t in main_tags_str.split(",")] if main_tags_str else []
|
| 52 |
|
| 53 |
# HTML Report
|
| 54 |
html = f"""
|
| 55 |
+
<div style='font-family: Arial, sans-serif; padding: 20px; max-width: 900px; margin:auto;'>
|
| 56 |
<h2 style="color:#2c3e50;">π§ Email Security Report</h2>
|
| 57 |
+
|
| 58 |
+
<h3 style="margin-bottom:6px;">Attack Score</h3>
|
| 59 |
+
<div style="background:#ecf0f1; border-radius:10px; overflow:hidden; height:24px; width:100%; margin-bottom:8px;">
|
| 60 |
<div style="width:{score_val}%; background:#3498db; height:100%; text-align:center; color:white; line-height:24px;">
|
| 61 |
{score_val}/100
|
| 62 |
</div>
|
| 63 |
</div>
|
| 64 |
+
|
| 65 |
<p><b>Attack Type:</b> {attack_type}</p>
|
| 66 |
<p><b>Final Verdict:</b> <span style="background:{verdict_color}; color:white; padding:4px 10px; border-radius:6px;">
|
| 67 |
+
{verdict}
|
| 68 |
</span></p>
|
| 69 |
+
|
| 70 |
+
<details style="margin-top:15px;" open>
|
| 71 |
<summary>π Attack Analysis Tags ({len(tags)})</summary>
|
| 72 |
+
{"<ul>" + "".join(f"<li>{tag}</li>" for tag in tags if tag) + "</ul>" if tags else "<p>No special tags</p>"}
|
|
|
|
|
|
|
| 73 |
</details>
|
| 74 |
+
|
| 75 |
+
<details style="margin-top:15px;" open>
|
| 76 |
+
<summary>π΅οΈ Detailed Findings</summary>
|
| 77 |
+
<h4>Headers ({len(header_findings)})</h4>
|
| 78 |
+
{"<ul>" + "".join(f"<li>{f}</li>" for f in header_findings) + "</ul>" if header_findings else "<p>No header findings.</p>"}
|
| 79 |
+
|
| 80 |
+
<h4>Body ({len(body_findings)})</h4>
|
| 81 |
+
{"<ul>" + "".join(f"<li>{f}</li>" for f in body_findings) + "</ul>" if body_findings else "<p>No body findings.</p>"}
|
| 82 |
+
|
| 83 |
+
<h4>URLs ({len(url_findings)})</h4>
|
| 84 |
+
{"<ul>" + "".join(f"<li>{f}</li>" for f in url_findings) + "</ul>" if url_findings else "<p>No URL findings.</p>"}
|
| 85 |
</details>
|
| 86 |
+
|
| 87 |
<details style="margin-top:15px;">
|
| 88 |
<summary>π© Highlighted Email Body</summary>
|
| 89 |
+
<div style='border:1px solid #ccc; padding:15px; background:#fdfdfd; border-radius:8px; white-space:pre-wrap;'>
|
| 90 |
+
{highlighted_body}
|
| 91 |
</div>
|
| 92 |
</details>
|
| 93 |
</div>
|
|
|
|
| 95 |
|
| 96 |
pdf_path = None
|
| 97 |
if export_pdf:
|
| 98 |
+
try:
|
| 99 |
+
pdf_path = html_to_pdf(html)
|
| 100 |
+
except Exception as e:
|
| 101 |
+
html += f"<p style='color:#c0392b;'><b>PDF export failed:</b> {e}</p>"
|
| 102 |
|
| 103 |
return html, pdf_path
|
| 104 |
|