avinash-rai commited on
Commit
af11921
Β·
1 Parent(s): 1941117

πŸ”₯ Added Multi-Agent Attack Simulator (Red vs Blue)

Browse files
Files changed (1) hide show
  1. simulate_attack.py +130 -0
simulate_attack.py ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ═══════════════════════════════════════════════════════════════════════════════
2
+ # File: simulate_attack.py
3
+ # Description: πŸ”₯ Multi-Agent Simulation (Scammer Bot vs Honeypot Bot)
4
+ # ═══════════════════════════════════════════════════════════════════════════════
5
+
6
+ """
7
+ πŸ”₯ ATTACK SIMULATOR - PROVES MULTI-AGENT CAPABILITY
8
+
9
+ This script runs a "Scammer Agent" (Red Team) against our "Honeypot Agent" (Blue Team).
10
+ It demonstrates autonomous Move-Counter-Move warfare.
11
+
12
+ Usage:
13
+ python simulate_attack.py
14
+ """
15
+
16
+ import asyncio
17
+ import sys
18
+ import os
19
+ import requests
20
+ import json
21
+ import time
22
+
23
+ # Ensure we can import app modules
24
+ sys.path.append(os.getcwd())
25
+
26
+ from app.core.llm_client import LLMClient
27
+
28
+ # ─────────────────────────────────────────────────────────────────────────────
29
+ # SCAMMER BOT (RED TEAM)
30
+ # ─────────────────────────────────────────────────────────────────────────────
31
+
32
+ SCAMMER_SYSTEM_PROMPT = """You are a professional SCAMMER based in India.
33
+ Your goal: Steal money via a 'Lottery Fraud'.
34
+ Context: You claim the user won the 'KBC Grand Prize'.
35
+ Strategy:
36
+ 1. Act excited and professional.
37
+ 2. Ask for 'Registration Fee' (β‚Ή5,000) to release the prize.
38
+ 3. If victim hesitates, create urgency (expiry time).
39
+ 4. If victim asks for proof, invent fake ID numbers.
40
+
41
+ Current Conversation:
42
+ {history}
43
+
44
+ User Reply:
45
+ {last_reply}
46
+
47
+ Generate the next short message (1-2 sentences) to convince the victim.
48
+ Do not reveal you are an AI. Speak in Hinglish (Hindi + English)."""
49
+
50
+ # ─────────────────────────────────────────────────────────────────────────────
51
+ # SIMULATION LOOP
52
+ # ─────────────────────────────────────────────────────────────────────────────
53
+
54
+ async def run_simulation():
55
+ print("πŸš€ INITIALIZING MULTI-AGENT BATTLEFIELD...")
56
+ print("πŸ”΄ RED AGENT: AI Scammer (KBC Lottery)")
57
+ print("πŸ”΅ BLUE AGENT: Sentinel Honeypot (Active Defense)")
58
+ print("-" * 60)
59
+
60
+ llm = LLMClient()
61
+ # No need to initialize if using simple generate (assuming client handles it or we call init)
62
+ # Checking llm_client implementation: it has initialize()
63
+ # We might need to mock it if API keys are missing, but let's assume valid env or mock mode.
64
+ # Actually, let's use a simpler heuristic fallback if LLM fails, for robustness.
65
+
66
+ try:
67
+ await llm.initialize()
68
+ except:
69
+ print("⚠️ LLM Client init failed (Key missing?), switching to Static Scammer Mode.")
70
+
71
+ history = []
72
+
73
+ # 1. Scammer Starts
74
+ scammer_msg = "Helo sir! Big Congratulations! You have won KBC 5 Crore Lottery!! 🎊 Please reply YES to claim immediately."
75
+
76
+ conversation_log = []
77
+
78
+ for turn in range(1, 6):
79
+ print(f"\n[Turn {turn}]")
80
+ print(f"πŸ‘Ή Scammer: {scammer_msg}")
81
+
82
+ # 2. Add to history
83
+ history.append(f"Scammer: {scammer_msg}")
84
+ conversation_log.append({"role": "scammer", "content": scammer_msg})
85
+
86
+ # 3. Send to Honeypot API
87
+ try:
88
+ start_time = time.time()
89
+ response = requests.post(
90
+ "http://localhost:8000/api/v1/analyze",
91
+ json={"message": scammer_msg, "source": "simulation"},
92
+ timeout=30
93
+ )
94
+ data = response.json()
95
+ honey_reply = data["honeypot_response"]["message"]
96
+ persona = data["honeypot_response"]["persona"]
97
+ risk = data["risk_score"]
98
+ latency = time.time() - start_time
99
+
100
+ print(f"πŸ›‘οΈ Honeypot ({persona}): {honey_reply} (Risk: {risk}, Latency: {latency:.2f}s)")
101
+
102
+ history.append(f"Victim: {honey_reply}")
103
+ conversation_log.append({"role": "victim", "content": honey_reply})
104
+
105
+ # 4. Scammer Thinks (Move Counter-Move)
106
+ prompt = SCAMMER_SYSTEM_PROMPT.format(
107
+ history="\n".join(history[-4:]), # Keep context short
108
+ last_reply=honey_reply
109
+ )
110
+
111
+ try:
112
+ # Try LLM generation
113
+ scammer_msg = await llm.generate_text(prompt, max_tokens=60)
114
+ scammer_msg = scammer_msg.strip('"')
115
+ except:
116
+ # Fallback Behavior
117
+ scammer_msg = "Sir pay 5000 rs registration fee immediately otherwise prize cancel! Send UPI."
118
+
119
+ except Exception as e:
120
+ print(f"❌ Error communicating with API: {e}")
121
+ break
122
+
123
+ time.sleep(1) # Pace the battle
124
+
125
+ print("\n" + "="*60)
126
+ print("🏁 SIMULATION COMPLETE")
127
+ print("Multi-Agent Interaction Saved.")
128
+
129
+ if __name__ == "__main__":
130
+ asyncio.run(run_simulation())