Spaces:
Sleeping
Sleeping
Btr4k
commited on
Commit
·
f77f85c
1
Parent(s):
9a15f21
Initial commit: Phishing detector
Browse files- README.md +37 -13
- app.py +205 -0
- requirements.txt +3 -0
README.md
CHANGED
@@ -1,13 +1,37 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# 📧 Phishing Detector (Text + Screenshot)
|
2 |
+
|
3 |
+
This Hugging Face Space provides an interface to analyze emails and SMS messages for potential phishing attempts. It accepts both direct text input and screenshots, using OCR to extract text from images when needed.
|
4 |
+
|
5 |
+
## Features
|
6 |
+
|
7 |
+
- Support for both text input and screenshot upload
|
8 |
+
- OCR for processing screenshots (microsoft/trocr-base-printed)
|
9 |
+
- Zero-shot classification to detect phishing patterns
|
10 |
+
- Keyword analysis for common phishing indicators
|
11 |
+
- Confidence scores for classifications
|
12 |
+
- Detailed explanations of suspicious elements
|
13 |
+
- Example messages for demonstration
|
14 |
+
|
15 |
+
## How to Use
|
16 |
+
|
17 |
+
1. Either:
|
18 |
+
- Paste your message text into the input box, OR
|
19 |
+
- Upload a screenshot of the message
|
20 |
+
2. Click "Submit" to analyze
|
21 |
+
3. Review the classification and explanation
|
22 |
+
|
23 |
+
## Technical Details
|
24 |
+
|
25 |
+
- Uses microsoft/trocr-base-printed for OCR
|
26 |
+
- Uses facebook/bart-large-mnli for zero-shot classification
|
27 |
+
- Implements keyword-based heuristics
|
28 |
+
- Built with Gradio for the user interface
|
29 |
+
- No fine-tuning required
|
30 |
+
|
31 |
+
## Disclaimer
|
32 |
+
|
33 |
+
This is an educational demo and should not be used as your only method of detecting phishing attempts. Always exercise caution with suspicious messages and follow your organization's security guidelines.
|
34 |
+
|
35 |
+
## License
|
36 |
+
|
37 |
+
MIT License
|
app.py
ADDED
@@ -0,0 +1,205 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# Initialize pipelines
|
5 |
+
ocr = pipeline("image-to-text", model="microsoft/trocr-base-printed")
|
6 |
+
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
7 |
+
|
8 |
+
# Common phishing indicators
|
9 |
+
SUSPICIOUS_PHRASES = [
|
10 |
+
"urgent", "immediately", "password", "account locked", "wire transfer",
|
11 |
+
"bank verification", "click here", "verification code", "credit card",
|
12 |
+
"suspended", "login now", "reset your password", "act now", "unusual activity",
|
13 |
+
"security alert", "confirm your identity", "gift card", "lottery winner",
|
14 |
+
"inheritance", "tax refund", "payment pending"
|
15 |
+
]
|
16 |
+
|
17 |
+
def extract_text_from_image(image):
|
18 |
+
"""Extract text from uploaded image using OCR."""
|
19 |
+
if image is None:
|
20 |
+
return ""
|
21 |
+
try:
|
22 |
+
result = ocr(image)
|
23 |
+
return result[0]["generated_text"] if result else ""
|
24 |
+
except Exception as e:
|
25 |
+
return f"Error processing image: {str(e)}"
|
26 |
+
|
27 |
+
def analyze_text(text):
|
28 |
+
"""Analyze text for phishing indicators."""
|
29 |
+
if not text.strip():
|
30 |
+
return "No text to analyze.", "", gr.update(visible=False)
|
31 |
+
|
32 |
+
# Zero-shot Classification
|
33 |
+
candidate_labels = ["Phishing Email", "Legitimate Email"]
|
34 |
+
result = classifier(text, candidate_labels=candidate_labels)
|
35 |
+
|
36 |
+
label = result["labels"][0]
|
37 |
+
confidence = result["scores"][0]
|
38 |
+
|
39 |
+
# Determine alert level and color
|
40 |
+
if label == "Phishing Email":
|
41 |
+
if confidence > 0.8:
|
42 |
+
alert_html = """
|
43 |
+
<div style="padding: 15px; background-color: #ffebee; border-left: 6px solid #f44336; margin-bottom: 15px;">
|
44 |
+
<h3 style="color: #d32f2f; margin: 0;">⚠️ High Risk - Likely Phishing Attempt</h3>
|
45 |
+
</div>
|
46 |
+
"""
|
47 |
+
else:
|
48 |
+
alert_html = """
|
49 |
+
<div style="padding: 15px; background-color: #fff3e0; border-left: 6px solid #ff9800; margin-bottom: 15px;">
|
50 |
+
<h3 style="color: #f57c00; margin: 0;">⚠️ Medium Risk - Suspicious Content Detected</h3>
|
51 |
+
</div>
|
52 |
+
"""
|
53 |
+
else:
|
54 |
+
alert_html = """
|
55 |
+
<div style="padding: 15px; background-color: #e8f5e9; border-left: 6px solid #4caf50; margin-bottom: 15px;">
|
56 |
+
<h3 style="color: #2e7d32; margin: 0;">✅ Low Risk - Likely Legitimate</h3>
|
57 |
+
</div>
|
58 |
+
"""
|
59 |
+
|
60 |
+
# Keyword Analysis
|
61 |
+
found_phrases = []
|
62 |
+
text_lower = text.lower()
|
63 |
+
for phrase in SUSPICIOUS_PHRASES:
|
64 |
+
if phrase in text_lower:
|
65 |
+
found_phrases.append(phrase)
|
66 |
+
|
67 |
+
# Generate Detailed Report
|
68 |
+
report = [
|
69 |
+
f"### Analysis Results",
|
70 |
+
f"**Confidence Score:** {confidence:.1%}",
|
71 |
+
f"**Classification:** {label}",
|
72 |
+
"\n### Detected Indicators"
|
73 |
+
]
|
74 |
+
|
75 |
+
if found_phrases:
|
76 |
+
report.append("🚩 **Suspicious elements found:**")
|
77 |
+
for phrase in found_phrases:
|
78 |
+
report.append(f"- Found '{phrase}'")
|
79 |
+
else:
|
80 |
+
report.append("✅ No common suspicious phrases detected.")
|
81 |
+
|
82 |
+
report.append("\n### Recommendation")
|
83 |
+
if confidence > 0.9:
|
84 |
+
report.append("🔴 **High confidence in classification - exercise extreme caution!**")
|
85 |
+
elif confidence > 0.7:
|
86 |
+
report.append("🟡 **Moderate confidence - review carefully and verify sender.**")
|
87 |
+
else:
|
88 |
+
report.append("🟢 **Low confidence - but always remain vigilant.**")
|
89 |
+
|
90 |
+
return alert_html, "\n".join(report), gr.update(visible=True)
|
91 |
+
|
92 |
+
def process_input(text_input, image_input):
|
93 |
+
"""Process either text input or image input."""
|
94 |
+
# If text is provided, use it directly
|
95 |
+
if text_input.strip():
|
96 |
+
return analyze_text(text_input)
|
97 |
+
|
98 |
+
# If image is provided, extract text first
|
99 |
+
if image_input is not None:
|
100 |
+
extracted_text = extract_text_from_image(image_input)
|
101 |
+
if extracted_text.strip():
|
102 |
+
return analyze_text(extracted_text)
|
103 |
+
return (
|
104 |
+
"""<div style="padding: 15px; background-color: #fff3e0; border-left: 6px solid #ff9800; margin-bottom: 15px;">
|
105 |
+
<h3 style="color: #f57c00; margin: 0;">⚠️ OCR Processing Error</h3>
|
106 |
+
</div>""",
|
107 |
+
"Could not extract text from image. Please ensure the image contains clear, readable text.",
|
108 |
+
gr.update(visible=False)
|
109 |
+
)
|
110 |
+
|
111 |
+
return (
|
112 |
+
"",
|
113 |
+
"Please provide either text or an image to analyze.",
|
114 |
+
gr.update(visible=False)
|
115 |
+
)
|
116 |
+
|
117 |
+
# Custom theme
|
118 |
+
custom_theme = gr.themes.Soft().set(
|
119 |
+
body_background_fill='*background-1',
|
120 |
+
block_background_fill='*background-2',
|
121 |
+
block_label_background_fill='*background-3',
|
122 |
+
input_background_fill='*background-2',
|
123 |
+
button_primary_background_fill='*primary-500',
|
124 |
+
button_primary_text_color='white',
|
125 |
+
)
|
126 |
+
|
127 |
+
# Create Gradio interface with tabs
|
128 |
+
with gr.Blocks(theme=custom_theme) as demo:
|
129 |
+
gr.Markdown("""
|
130 |
+
# 📧 AI Phishing Detector
|
131 |
+
### Analyze emails and messages for potential phishing attempts
|
132 |
+
Upload a screenshot or paste text to check for suspicious content
|
133 |
+
""")
|
134 |
+
|
135 |
+
with gr.Row():
|
136 |
+
with gr.Column(scale=1):
|
137 |
+
gr.Markdown("""
|
138 |
+
### How to Use
|
139 |
+
1. Either paste message text or upload a screenshot
|
140 |
+
2. Click 'Analyze' to check for phishing indicators
|
141 |
+
3. Review the detailed analysis results
|
142 |
+
|
143 |
+
ℹ️ This tool uses AI to detect:
|
144 |
+
- Suspicious language patterns
|
145 |
+
- Common phishing phrases
|
146 |
+
- Urgency indicators
|
147 |
+
- Security threat language
|
148 |
+
""")
|
149 |
+
|
150 |
+
with gr.Column(scale=2):
|
151 |
+
with gr.Tab("✏️ Text Input"):
|
152 |
+
text_input = gr.Textbox(
|
153 |
+
lines=8,
|
154 |
+
label="Message Text",
|
155 |
+
placeholder="Paste email or message content here...",
|
156 |
+
elem_id="text_input"
|
157 |
+
)
|
158 |
+
|
159 |
+
with gr.Tab("📷 Screenshot Upload"):
|
160 |
+
image_input = gr.Image(
|
161 |
+
label="Upload Screenshot",
|
162 |
+
type="pil",
|
163 |
+
elem_id="image_input"
|
164 |
+
)
|
165 |
+
|
166 |
+
analyze_button = gr.Button("🔍 Analyze", variant="primary", size="lg")
|
167 |
+
|
168 |
+
with gr.Row():
|
169 |
+
with gr.Column():
|
170 |
+
alert_html = gr.HTML(label="Alert")
|
171 |
+
analysis = gr.Markdown(label="Detailed Analysis")
|
172 |
+
|
173 |
+
with gr.Accordion("ℹ️ About this tool", open=False):
|
174 |
+
gr.Markdown("""
|
175 |
+
This tool uses advanced AI models to analyze messages for potential phishing attempts:
|
176 |
+
- OCR technology to extract text from screenshots
|
177 |
+
- Zero-shot classification for pattern detection
|
178 |
+
- Keyword analysis for suspicious content
|
179 |
+
|
180 |
+
**Disclaimer:** This is an educational demo. Always verify suspicious messages with your IT department.
|
181 |
+
""")
|
182 |
+
|
183 |
+
# Examples
|
184 |
+
examples = [
|
185 |
+
["Subject: URGENT - Account Security Alert\n\nDear User,\n\nWe detected unusual activity in your account. Click here immediately to verify your identity and reset your password. If you don't respond within 24 hours, your account will be suspended.\n\nBank Security Team", None],
|
186 |
+
["Subject: Team Meeting Tomorrow\n\nHi everyone,\n\nJust a reminder that we have our weekly team meeting tomorrow at 10 AM in the main conference room. Please bring your project updates.\n\nBest regards,\nSarah", None],
|
187 |
+
]
|
188 |
+
gr.Examples(
|
189 |
+
examples=examples,
|
190 |
+
inputs=[text_input, image_input],
|
191 |
+
outputs=[alert_html, analysis],
|
192 |
+
fn=process_input,
|
193 |
+
cache_examples=True
|
194 |
+
)
|
195 |
+
|
196 |
+
# Set up event handler
|
197 |
+
analyze_button.click(
|
198 |
+
fn=process_input,
|
199 |
+
inputs=[text_input, image_input],
|
200 |
+
outputs=[alert_html, analysis]
|
201 |
+
)
|
202 |
+
|
203 |
+
# Launch the app
|
204 |
+
if __name__ == "__main__":
|
205 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio>=3.0
|
2 |
+
transformers>=4.25
|
3 |
+
torch
|