Commit Β·
af11921
1
Parent(s): 1941117
π₯ Added Multi-Agent Attack Simulator (Red vs Blue)
Browse files- 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())
|