Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,90 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from analyze_email_main import
|
| 3 |
|
| 4 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
try:
|
| 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 |
if __name__ == "__main__":
|
| 34 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from analyze_email_main import analyze
|
| 3 |
|
| 4 |
+
def analyze_email(file_obj):
|
| 5 |
+
if file_obj is None:
|
| 6 |
+
return "<p style='color:red;'>Please upload a .eml file to analyze.</p>"
|
| 7 |
+
|
| 8 |
+
results = analyze(file_obj.name)
|
| 9 |
+
|
| 10 |
+
# Basic verdict info
|
| 11 |
+
attack_score = results[0] # e.g., "Attack Score: 85"
|
| 12 |
+
attack_type = results[1] # e.g., "Attack Type: Credential Harvesting"
|
| 13 |
+
verdict = results[2] # e.g., "Final Verdict: Malicious"
|
| 14 |
+
|
| 15 |
+
score_val = int("".join(filter(str.isdigit, attack_score))) # extract numeric score
|
| 16 |
+
if score_val > 100:
|
| 17 |
+
score_val = 100
|
| 18 |
+
|
| 19 |
+
# Assign verdict color
|
| 20 |
+
if "Malicious" in verdict:
|
| 21 |
+
verdict_color = "#e74c3c" # red
|
| 22 |
+
elif "Suspicious" in verdict:
|
| 23 |
+
verdict_color = "#e67e22" # orange
|
| 24 |
+
elif "Spam" in verdict:
|
| 25 |
+
verdict_color = "#f1c40f" # yellow
|
| 26 |
+
else:
|
| 27 |
+
verdict_color = "#2ecc71" # green
|
| 28 |
+
|
| 29 |
+
# Extract report sections
|
| 30 |
try:
|
| 31 |
+
tags_index = results.index("---- Attack Analysis Tags ----")
|
| 32 |
+
findings_index = results.index("---- Detailed Findings ----")
|
| 33 |
+
body_index = results.index("---- Highlighted Body ----")
|
| 34 |
+
except ValueError:
|
| 35 |
+
return "<pre>" + "\n".join(results) + "</pre>"
|
| 36 |
+
|
| 37 |
+
tags = results[tags_index + 1 : findings_index]
|
| 38 |
+
findings = results[findings_index + 1 : body_index]
|
| 39 |
+
highlighted_body = results[body_index + 1 :]
|
| 40 |
+
|
| 41 |
+
# Styled HTML Report
|
| 42 |
+
html = f"""
|
| 43 |
+
<div style='font-family: Arial, sans-serif; padding: 20px; max-width: 800px; margin:auto;'>
|
| 44 |
+
|
| 45 |
+
<h2 style="color:#2c3e50;">π§ Email Security Report</h2>
|
| 46 |
+
|
| 47 |
+
<h3>Attack Score</h3>
|
| 48 |
+
<div style="background:#ecf0f1; border-radius:10px; overflow:hidden; height:24px; width:100%;">
|
| 49 |
+
<div style="width:{score_val}%; background:#3498db; height:100%; text-align:center; color:white; line-height:24px;">
|
| 50 |
+
{score_val}/100
|
| 51 |
+
</div>
|
| 52 |
+
</div>
|
| 53 |
+
|
| 54 |
+
<p><b>Attack Type:</b> {attack_type}</p>
|
| 55 |
+
<p><b>Final Verdict:</b> <span style="background:{verdict_color}; color:white; padding:4px 10px; border-radius:6px;">
|
| 56 |
+
{verdict.replace("Final Verdict:", "").strip()}
|
| 57 |
+
</span></p>
|
| 58 |
+
|
| 59 |
+
<h3>π Attack Analysis Tags</h3>
|
| 60 |
+
<ul>
|
| 61 |
+
{''.join(f'<li>{tag}</li>' for tag in tags)}
|
| 62 |
+
</ul>
|
| 63 |
+
|
| 64 |
+
<h3>π΅οΈ Detailed Findings</h3>
|
| 65 |
+
<ul>
|
| 66 |
+
{''.join(f'<li>{finding}</li>' for finding in findings)}
|
| 67 |
+
</ul>
|
| 68 |
+
|
| 69 |
+
<h3>π© Highlighted Email Body</h3>
|
| 70 |
+
<div style='border:1px solid #ccc; padding:15px; background:#fdfdfd; border-radius:8px;'>
|
| 71 |
+
{"<br>".join(highlighted_body)}
|
| 72 |
+
</div>
|
| 73 |
+
|
| 74 |
+
</div>
|
| 75 |
+
"""
|
| 76 |
+
|
| 77 |
+
return html
|
| 78 |
+
|
| 79 |
+
# Gradio Interface
|
| 80 |
+
demo = gr.Interface(
|
| 81 |
+
fn=analyze_email,
|
| 82 |
+
inputs=gr.File(label="Upload .eml File", type="file"),
|
| 83 |
+
outputs=gr.HTML(label="Analysis Report"),
|
| 84 |
+
title="π§ Email Security Analyzer",
|
| 85 |
+
description="Upload an .eml file to detect phishing, spam, or malicious emails. "
|
| 86 |
+
"This analyzer combines AI, heuristics, and threat intelligence feeds."
|
| 87 |
)
|
| 88 |
|
| 89 |
if __name__ == "__main__":
|
| 90 |
+
demo.launch()
|