princemaxp commited on
Commit
d6c2f43
Β·
verified Β·
1 Parent(s): e09248b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -43
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
- results = analyze(file_path)
 
17
 
18
- # Ensure all items are strings
19
- results_str = [str(r) for r in results]
20
-
21
- # Extract basic verdict info
22
- attack_score = results_str[0]
23
- attack_type = results_str[1]
24
- verdict = results_str[2]
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" # default green
33
- if "Malicious" in verdict:
 
34
  verdict_color = "#e74c3c"
35
- elif "Suspicious" in verdict:
36
  verdict_color = "#e67e22"
37
- elif "Spam" in verdict:
38
  verdict_color = "#f1c40f"
39
 
40
- # Extract sections safely
41
- try:
42
- tags_index = results_str.index("---- Attack Analysis Tags ----")
43
- findings_index = results_str.index("---- Detailed Findings ----")
44
- body_index = results_str.index("---- Highlighted Body ----")
45
- except ValueError:
46
- html = "<pre>" + "\n".join(results_str) + "</pre>"
47
- return html, None
48
 
49
- tags = results_str[tags_index + 1 : findings_index]
50
- findings = results_str[findings_index + 1 : body_index]
51
- highlighted_body = results_str[body_index + 1 :]
52
 
53
  # HTML Report
54
  html = f"""
55
- <div style='font-family: Arial, sans-serif; padding: 20px; max-width: 800px; margin:auto;'>
56
  <h2 style="color:#2c3e50;">πŸ“§ Email Security Report</h2>
57
- <h3>Attack Score</h3>
58
- <div style="background:#ecf0f1; border-radius:10px; overflow:hidden; height:24px; width:100%;">
 
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.replace('Final Verdict:', '').strip()}
66
  </span></p>
67
- <details style="margin-top:15px;">
 
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
- <details style="margin-top:15px;">
74
- <summary>πŸ•΅οΈ Detailed Findings ({len(findings)})</summary>
75
- <ul>
76
- {''.join(f'<li>{finding}</li>' for finding in findings)}
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
- {"<br>".join(highlighted_body)}
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
- pdf_path = html_to_pdf(html)
 
 
 
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