Skip to content

Commit 064cc2c

Browse files
feat: Add domain analysis to scam detection AI
1 parent 33007ae commit 064cc2c

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

collections/scam-detection-ai/email_analyzer.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
import re
22

3+
import requests
4+
5+
def get_domain_reputation(domain):
6+
"""
7+
Checks the reputation of a domain using a mock API.
8+
In a real application, this would integrate with a service like VirusTotal.
9+
"""
10+
# This is a mock response.
11+
if "fake" in domain or "suspicious" in domain:
12+
return "Malicious"
13+
return "Clean"
14+
315
def analyze_email(email_text):
416
"""
517
Analyzes an email for potential scam indicators.
@@ -16,7 +28,8 @@ def analyze_email(email_text):
1628
"suspicious_links": 0,
1729
"unusual_sender": 0,
1830
"payment_requests": 0,
19-
"attachments": 0
31+
"attachments": 0,
32+
"domain_analysis": {}
2033
}
2134

2235
# Urgency keywords
@@ -54,6 +67,14 @@ def analyze_email(email_text):
5467
if "Content-Disposition: attachment" in email_text:
5568
scam_indicators["attachments"] = 1
5669

70+
# Domain analysis
71+
urls = re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', email_text)
72+
domains = [re.search(r'//(.*?)/', url).group(1) for url in urls if re.search(r'//(.*?)/', url)]
73+
74+
for domain in domains:
75+
reputation = get_domain_reputation(domain)
76+
scam_indicators["domain_analysis"][domain] = reputation
77+
5778
return scam_indicators
5879

5980
if __name__ == '__main__':

collections/scam-detection-ai/templates/result.html

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,19 @@ <h1>Analysis Result</h1>
1717
<h2>{{ 'This email is likely a scam!' if is_scam else 'This email seems legitimate.' }}</h2>
1818
<ul>
1919
{% for indicator, value in analysis.items() %}
20-
<li>{{ indicator.replace('_', ' ')|title }}: {{ 'Detected' if value else 'Not Detected' }}</li>
20+
{% if indicator != 'domain_analysis' %}
21+
<li>{{ indicator.replace('_', ' ')|title }}: {{ 'Detected' if value else 'Not Detected' }}</li>
22+
{% endif %}
2123
{% endfor %}
2224
</ul>
25+
{% if analysis.domain_analysis %}
26+
<h3>Domain Analysis</h3>
27+
<ul>
28+
{% for domain, status in analysis.domain_analysis.items() %}
29+
<li>{{ domain }}: {{ status }}</li>
30+
{% endfor %}
31+
</ul>
32+
{% endif %}
2333
</div>
2434
<br>
2535
<a href="/">Analyze another email</a>

collections/scam-detection-ai/templates/results_list.html

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,19 @@ <h3>Subject: {{ email.subject }}</h3>
2121
<h4>{{ 'This email is likely a scam!' if email.is_scam else 'This email seems legitimate.' }}</h4>
2222
<ul>
2323
{% for indicator, value in email.analysis.items() %}
24-
<li>{{ indicator.replace('_', ' ')|title }}: {{ 'Detected' if value else 'Not Detected' }}</li>
24+
{% if indicator != 'domain_analysis' %}
25+
<li>{{ indicator.replace('_', ' ')|title }}: {{ 'Detected' if value else 'Not Detected' }}</li>
26+
{% endif %}
2527
{% endfor %}
2628
</ul>
29+
{% if email.analysis.domain_analysis %}
30+
<h4>Domain Analysis</h4>
31+
<ul>
32+
{% for domain, status in email.analysis.domain_analysis.items() %}
33+
<li>{{ domain }}: {{ status }}</li>
34+
{% endfor %}
35+
</ul>
36+
{% endif %}
2737
</div>
2838
{% endfor %}
2939
{% else %}

collections/scam-detection-ai/test_email_analyzer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def test_scam_email(self):
2020
self.assertEqual(analysis['generic_greeting'], 1)
2121
self.assertEqual(analysis['suspicious_links'], 1)
2222
self.assertEqual(analysis['payment_requests'], 1)
23+
self.assertEqual(analysis['domain_analysis']['fake-prize.com'], 'Malicious')
2324

2425
def test_legitimate_email(self):
2526
legitimate_email = """

0 commit comments

Comments
 (0)