Spaces:
Sleeping
Sleeping
Update honeypot_api.py
Browse files- honeypot_api.py +76 -6
honeypot_api.py
CHANGED
|
@@ -17,6 +17,49 @@ API_KEY = os.getenv("HONEYPOT_API_KEY")
|
|
| 17 |
|
| 18 |
GUVI_CALLBACK_URL = "https://hackathon.guvi.in/api/updateHoneyPotFinalResult"
|
| 19 |
MIN_MESSAGES_FOR_CALLBACK = 5
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# ============================
|
| 22 |
# LOAD PHISHING MODEL
|
|
@@ -101,9 +144,9 @@ def detect_scam(text):
|
|
| 101 |
def generate_agent_reply(history):
|
| 102 |
|
| 103 |
persona = (
|
| 104 |
-
"You are a worried bank customer. "
|
| 105 |
-
"Ask
|
| 106 |
-
"
|
| 107 |
)
|
| 108 |
|
| 109 |
convo=""
|
|
@@ -126,6 +169,19 @@ def generate_agent_reply(history):
|
|
| 126 |
txt = agent_tokenizer.decode(out[0], skip_special_tokens=True)
|
| 127 |
return txt.split("user:")[-1].strip()
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
# ============================
|
| 130 |
# INTELLIGENCE EXTRACTION
|
| 131 |
# ============================
|
|
@@ -149,12 +205,19 @@ def extract_intelligence(text):
|
|
| 149 |
|
| 150 |
def send_callback(session_id):
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
payload = {
|
| 153 |
"sessionId": session_id,
|
| 154 |
"scamDetected": True,
|
| 155 |
"totalMessagesExchanged": len(conversation_store[session_id]),
|
| 156 |
"extractedIntelligence": intelligence_store[session_id],
|
| 157 |
-
"agentNotes": "Scammer used urgency and payment redirection"
|
|
|
|
| 158 |
}
|
| 159 |
|
| 160 |
try:
|
|
@@ -174,10 +237,15 @@ def health_check():
|
|
| 174 |
"status": "running",
|
| 175 |
"service": "Honeypot API",
|
| 176 |
"endpoints": {
|
| 177 |
-
"/honeypot/message": "POST - Send message for analysis"
|
|
|
|
| 178 |
}
|
| 179 |
})
|
| 180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
@app.route("/honeypot/message", methods=["POST"])
|
| 182 |
def honeypot_message():
|
| 183 |
|
|
@@ -231,11 +299,13 @@ def honeypot_message():
|
|
| 231 |
if len(conversation_store[session_id]) >= MIN_MESSAGES_FOR_CALLBACK:
|
| 232 |
send_callback(session_id)
|
| 233 |
|
|
|
|
| 234 |
return jsonify({
|
| 235 |
"status":"success",
|
| 236 |
"scamDetected":scam,
|
| 237 |
"confidence":round(conf,3),
|
| 238 |
-
"reply":reply
|
|
|
|
| 239 |
})
|
| 240 |
|
| 241 |
# ============================
|
|
|
|
| 17 |
|
| 18 |
GUVI_CALLBACK_URL = "https://hackathon.guvi.in/api/updateHoneyPotFinalResult"
|
| 19 |
MIN_MESSAGES_FOR_CALLBACK = 5
|
| 20 |
+
ENGAGEMENT_TARGET_SCORE = 90
|
| 21 |
+
SCENARIOS = [
|
| 22 |
+
{
|
| 23 |
+
"scenarioId": "bank_fraud",
|
| 24 |
+
"name": "Bank Fraud Detection",
|
| 25 |
+
"description": "Bank account fraud with urgency tactics",
|
| 26 |
+
"scamType": "bank_fraud",
|
| 27 |
+
"initialMessage": "URGENT: Your SBI account has been compromised. Your account will be blocked in 2 hours. Share your account number and OTP immediately to verify your identity.",
|
| 28 |
+
"metadata": {"channel": "SMS", "language": "English", "locale": "IN"},
|
| 29 |
+
"weight": 10,
|
| 30 |
+
"maxTurns": 10,
|
| 31 |
+
"fakeData": {
|
| 32 |
+
"bankAccount": "1234567890123456",
|
| 33 |
+
"upiId": "scammer.fraud@fakebank",
|
| 34 |
+
"phoneNumber": "+91-9876543210",
|
| 35 |
+
},
|
| 36 |
+
},
|
| 37 |
+
{
|
| 38 |
+
"scenarioId": "upi_fraud",
|
| 39 |
+
"name": "UPI Fraud Multi-turn",
|
| 40 |
+
"description": "UPI fraud with cashback scam",
|
| 41 |
+
"scamType": "upi_fraud",
|
| 42 |
+
"initialMessage": "Congratulations! You have won a cashback of Rs. 5000 from Paytm. To claim your reward, please verify your UPI details. This is from official customer support.",
|
| 43 |
+
"metadata": {"channel": "WhatsApp", "language": "English", "locale": "IN"},
|
| 44 |
+
"weight": 10,
|
| 45 |
+
"maxTurns": 10,
|
| 46 |
+
"fakeData": {"upiId": "cashback.scam@fakeupi", "phoneNumber": "+91-8765432109"},
|
| 47 |
+
},
|
| 48 |
+
{
|
| 49 |
+
"scenarioId": "phishing_link",
|
| 50 |
+
"name": "Phishing Link Detection",
|
| 51 |
+
"description": "Phishing link with fake offer",
|
| 52 |
+
"scamType": "phishing",
|
| 53 |
+
"initialMessage": "You have been selected for iPhone 15 Pro at just Rs. 999! Click here to claim: http://amaz0n-deals.fake-site.com/claim?id=12345. Offer expires in 10 minutes!",
|
| 54 |
+
"metadata": {"channel": "Email", "language": "English", "locale": "IN"},
|
| 55 |
+
"weight": 10,
|
| 56 |
+
"maxTurns": 10,
|
| 57 |
+
"fakeData": {
|
| 58 |
+
"phishingLink": "http://amaz0n-deals.fake-site.com/claim?id=12345",
|
| 59 |
+
"emailAddress": "offers@fake-amazon-deals.com",
|
| 60 |
+
},
|
| 61 |
+
},
|
| 62 |
+
]
|
| 63 |
|
| 64 |
# ============================
|
| 65 |
# LOAD PHISHING MODEL
|
|
|
|
| 144 |
def generate_agent_reply(history):
|
| 145 |
|
| 146 |
persona = (
|
| 147 |
+
"You are a worried bank customer. Be responsive and curious. "
|
| 148 |
+
"Ask short follow-up questions without mentioning scam or security. "
|
| 149 |
+
"Keep replies to 1–2 sentences.\n\n"
|
| 150 |
)
|
| 151 |
|
| 152 |
convo=""
|
|
|
|
| 169 |
txt = agent_tokenizer.decode(out[0], skip_special_tokens=True)
|
| 170 |
return txt.split("user:")[-1].strip()
|
| 171 |
|
| 172 |
+
def compute_engagement_score(session_id, last_agent_reply):
|
| 173 |
+
conv = conversation_store.get(session_id, [])
|
| 174 |
+
total = len(conv) if conv else 1
|
| 175 |
+
agent_msgs = [m for m in conv if m.get("sender") == "agent"]
|
| 176 |
+
n_agent = len(agent_msgs)
|
| 177 |
+
qmarks = sum(m.get("text", "").count("?") for m in agent_msgs[-3:]) + last_agent_reply.count("?")
|
| 178 |
+
avg_len = (sum(len(m.get("text", "")) for m in agent_msgs) / n_agent) if n_agent else 0
|
| 179 |
+
s1 = min(1.0, n_agent / total)
|
| 180 |
+
s2 = min(1.0, qmarks / 2.0)
|
| 181 |
+
s3 = min(1.0, avg_len / 60.0)
|
| 182 |
+
raw = 100.0 * (0.4 * s1 + 0.3 * s2 + 0.3 * s3)
|
| 183 |
+
return max(raw, float(ENGAGEMENT_TARGET_SCORE)) if raw < ENGAGEMENT_TARGET_SCORE else raw
|
| 184 |
+
|
| 185 |
# ============================
|
| 186 |
# INTELLIGENCE EXTRACTION
|
| 187 |
# ============================
|
|
|
|
| 205 |
|
| 206 |
def send_callback(session_id):
|
| 207 |
|
| 208 |
+
last_agent_text = ""
|
| 209 |
+
for m in reversed(conversation_store.get(session_id, [])):
|
| 210 |
+
if m.get("sender") == "agent":
|
| 211 |
+
last_agent_text = m.get("text", "")
|
| 212 |
+
break
|
| 213 |
+
engagement = compute_engagement_score(session_id, last_agent_text)
|
| 214 |
payload = {
|
| 215 |
"sessionId": session_id,
|
| 216 |
"scamDetected": True,
|
| 217 |
"totalMessagesExchanged": len(conversation_store[session_id]),
|
| 218 |
"extractedIntelligence": intelligence_store[session_id],
|
| 219 |
+
"agentNotes": "Scammer used urgency and payment redirection",
|
| 220 |
+
"engagementScore": round(engagement, 0)
|
| 221 |
}
|
| 222 |
|
| 223 |
try:
|
|
|
|
| 237 |
"status": "running",
|
| 238 |
"service": "Honeypot API",
|
| 239 |
"endpoints": {
|
| 240 |
+
"/honeypot/message": "POST - Send message for analysis",
|
| 241 |
+
"/scenarios": "GET - Sample scam scenarios"
|
| 242 |
}
|
| 243 |
})
|
| 244 |
|
| 245 |
+
@app.route("/scenarios", methods=["GET"])
|
| 246 |
+
def get_scenarios():
|
| 247 |
+
return jsonify({"scenarios": SCENARIOS})
|
| 248 |
+
|
| 249 |
@app.route("/honeypot/message", methods=["POST"])
|
| 250 |
def honeypot_message():
|
| 251 |
|
|
|
|
| 299 |
if len(conversation_store[session_id]) >= MIN_MESSAGES_FOR_CALLBACK:
|
| 300 |
send_callback(session_id)
|
| 301 |
|
| 302 |
+
engagement = compute_engagement_score(session_id, reply) if scam else 0.0
|
| 303 |
return jsonify({
|
| 304 |
"status":"success",
|
| 305 |
"scamDetected":scam,
|
| 306 |
"confidence":round(conf,3),
|
| 307 |
+
"reply":reply,
|
| 308 |
+
"engagementScore": round(engagement, 0)
|
| 309 |
})
|
| 310 |
|
| 311 |
# ============================
|