Spaces:
Runtime error
Runtime error
fix round 1
Browse files- env/env.py +38 -16
- inference.py +5 -2
env/env.py
CHANGED
|
@@ -12,10 +12,14 @@ class DeceptionEnv:
|
|
| 12 |
self.done = False
|
| 13 |
self.max_steps = 5
|
| 14 |
self.current_step = 0
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def reset(self):
|
| 17 |
self.done = False
|
| 18 |
self.current_step = 0
|
|
|
|
|
|
|
| 19 |
|
| 20 |
logs = requests.get(f"{SERVER}/logs").json()
|
| 21 |
self._state = logs
|
|
@@ -32,57 +36,75 @@ class DeceptionEnv:
|
|
| 32 |
failed_logins = logs.get("failed_logins", 0)
|
| 33 |
requests_log = logs.get("requests", [])
|
| 34 |
|
| 35 |
-
# Detect
|
| 36 |
if action == "detect_attack":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
if failed_logins > 3:
|
| 38 |
reward += 0.15
|
| 39 |
-
|
| 40 |
-
reward -= 0.05
|
| 41 |
|
| 42 |
-
|
| 43 |
-
if action == "detect_attack":
|
| 44 |
for r in requests_log:
|
| 45 |
if isinstance(r, dict) and r.get("type") == "port_scan":
|
| 46 |
reward += 0.15
|
|
|
|
| 47 |
break
|
| 48 |
|
| 49 |
-
|
| 50 |
-
if action == "detect_attack":
|
| 51 |
for r in requests_log:
|
| 52 |
if isinstance(r, dict) and r.get("type") == "sql_injection":
|
| 53 |
reward += 0.15
|
|
|
|
| 54 |
break
|
| 55 |
|
| 56 |
-
|
| 57 |
-
if action == "detect_attack":
|
| 58 |
for r in requests_log:
|
| 59 |
if isinstance(r, dict) and r.get("type") == "directory_traversal":
|
| 60 |
reward += 0.15
|
|
|
|
| 61 |
break
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
elif action == "deploy_honeypot":
|
| 65 |
deploy_honeypot()
|
| 66 |
reward += 0.30
|
|
|
|
| 67 |
|
| 68 |
-
# Fake
|
| 69 |
elif action == "fake_database":
|
| 70 |
fake_database()
|
| 71 |
reward += 0.20
|
|
|
|
| 72 |
|
| 73 |
-
# Block
|
| 74 |
elif action == "block_ip":
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
| 77 |
block_attacker(ip)
|
|
|
|
| 78 |
reward += 0.50
|
| 79 |
self.done = True
|
| 80 |
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
if self.current_step >= self.max_steps:
|
| 83 |
self.done = True
|
| 84 |
|
| 85 |
-
# Clamp reward
|
| 86 |
reward = min(max(reward, 0.0), 1.0)
|
| 87 |
|
| 88 |
self._state = logs
|
|
|
|
| 12 |
self.done = False
|
| 13 |
self.max_steps = 5
|
| 14 |
self.current_step = 0
|
| 15 |
+
self.detected = False
|
| 16 |
+
self.deployed = False
|
| 17 |
|
| 18 |
def reset(self):
|
| 19 |
self.done = False
|
| 20 |
self.current_step = 0
|
| 21 |
+
self.detected = False
|
| 22 |
+
self.deployed = False
|
| 23 |
|
| 24 |
logs = requests.get(f"{SERVER}/logs").json()
|
| 25 |
self._state = logs
|
|
|
|
| 36 |
failed_logins = logs.get("failed_logins", 0)
|
| 37 |
requests_log = logs.get("requests", [])
|
| 38 |
|
| 39 |
+
# ---------------- Detect Attack ----------------
|
| 40 |
if action == "detect_attack":
|
| 41 |
+
|
| 42 |
+
detected_any = False
|
| 43 |
+
|
| 44 |
+
# Detect brute force
|
| 45 |
if failed_logins > 3:
|
| 46 |
reward += 0.15
|
| 47 |
+
detected_any = True
|
|
|
|
| 48 |
|
| 49 |
+
# Detect port scan
|
|
|
|
| 50 |
for r in requests_log:
|
| 51 |
if isinstance(r, dict) and r.get("type") == "port_scan":
|
| 52 |
reward += 0.15
|
| 53 |
+
detected_any = True
|
| 54 |
break
|
| 55 |
|
| 56 |
+
# Detect SQL injection
|
|
|
|
| 57 |
for r in requests_log:
|
| 58 |
if isinstance(r, dict) and r.get("type") == "sql_injection":
|
| 59 |
reward += 0.15
|
| 60 |
+
detected_any = True
|
| 61 |
break
|
| 62 |
|
| 63 |
+
# Detect directory traversal
|
|
|
|
| 64 |
for r in requests_log:
|
| 65 |
if isinstance(r, dict) and r.get("type") == "directory_traversal":
|
| 66 |
reward += 0.15
|
| 67 |
+
detected_any = True
|
| 68 |
break
|
| 69 |
|
| 70 |
+
if detected_any:
|
| 71 |
+
self.detected = True
|
| 72 |
+
else:
|
| 73 |
+
reward -= 0.05
|
| 74 |
+
|
| 75 |
+
# ---------------- Deploy Honeypot ----------------
|
| 76 |
elif action == "deploy_honeypot":
|
| 77 |
deploy_honeypot()
|
| 78 |
reward += 0.30
|
| 79 |
+
self.deployed = True
|
| 80 |
|
| 81 |
+
# ---------------- Fake Database ----------------
|
| 82 |
elif action == "fake_database":
|
| 83 |
fake_database()
|
| 84 |
reward += 0.20
|
| 85 |
+
self.deployed = True
|
| 86 |
|
| 87 |
+
# ---------------- Block Attacker ----------------
|
| 88 |
elif action == "block_ip":
|
| 89 |
+
|
| 90 |
+
# Require detection + deception first
|
| 91 |
+
if logs.get("suspicious_ips") and self.detected and self.deployed:
|
| 92 |
+
|
| 93 |
+
ip = logs["suspicious_ips"][-1]
|
| 94 |
block_attacker(ip)
|
| 95 |
+
|
| 96 |
reward += 0.50
|
| 97 |
self.done = True
|
| 98 |
|
| 99 |
+
else:
|
| 100 |
+
# penalize early blocking
|
| 101 |
+
reward -= 0.10
|
| 102 |
+
|
| 103 |
+
# ---------------- Episode Boundary ----------------
|
| 104 |
if self.current_step >= self.max_steps:
|
| 105 |
self.done = True
|
| 106 |
|
| 107 |
+
# ---------------- Clamp reward ----------------
|
| 108 |
reward = min(max(reward, 0.0), 1.0)
|
| 109 |
|
| 110 |
self._state = logs
|
inference.py
CHANGED
|
@@ -10,11 +10,14 @@ random.seed(42)
|
|
| 10 |
from env.fake_server import run_server
|
| 11 |
def start_server():
|
| 12 |
try:
|
| 13 |
-
requests.get("http://127.0.0.1:7860/
|
| 14 |
-
|
| 15 |
except:
|
|
|
|
| 16 |
threading.Thread(target=run_server, daemon=True).start()
|
| 17 |
time.sleep(2)
|
|
|
|
|
|
|
| 18 |
start_server()
|
| 19 |
from env.env import DeceptionEnv
|
| 20 |
from env.attacker import simulate_attack
|
|
|
|
| 10 |
from env.fake_server import run_server
|
| 11 |
def start_server():
|
| 12 |
try:
|
| 13 |
+
requests.get("http://127.0.0.1:7860/", timeout=1)
|
| 14 |
+
print("Server already running")
|
| 15 |
except:
|
| 16 |
+
print("Starting server...")
|
| 17 |
threading.Thread(target=run_server, daemon=True).start()
|
| 18 |
time.sleep(2)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
start_server()
|
| 22 |
from env.env import DeceptionEnv
|
| 23 |
from env.attacker import simulate_attack
|