Spaces:
Runtime error
Runtime error
fix round 1
Browse files- env/env.py +18 -11
- inference.py +49 -28
- models.py +4 -4
- tasks/easy/grader.py +6 -6
- tasks/hard/grader.py +6 -9
- tasks/medium/grader.py +7 -7
env/env.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import requests
|
| 2 |
from env.deception import deploy_honeypot, fake_database, block_attacker
|
|
|
|
| 3 |
|
| 4 |
SERVER = "http://127.0.0.1:7860"
|
| 5 |
|
|
@@ -19,7 +20,7 @@ class DeceptionEnv:
|
|
| 19 |
logs = requests.get(f"{SERVER}/logs").json()
|
| 20 |
self._state = logs
|
| 21 |
|
| 22 |
-
return self._state
|
| 23 |
|
| 24 |
def step(self, action):
|
| 25 |
|
|
@@ -34,7 +35,7 @@ class DeceptionEnv:
|
|
| 34 |
# Detect brute force
|
| 35 |
if action == "detect_attack":
|
| 36 |
if failed_logins > 3:
|
| 37 |
-
reward += 0.
|
| 38 |
else:
|
| 39 |
reward -= 0.05
|
| 40 |
|
|
@@ -42,51 +43,57 @@ class DeceptionEnv:
|
|
| 42 |
if action == "detect_attack":
|
| 43 |
for r in requests_log:
|
| 44 |
if isinstance(r, dict) and r.get("type") == "port_scan":
|
| 45 |
-
reward += 0.
|
| 46 |
break
|
| 47 |
|
| 48 |
# Detect SQL injection
|
| 49 |
if action == "detect_attack":
|
| 50 |
for r in requests_log:
|
| 51 |
if isinstance(r, dict) and r.get("type") == "sql_injection":
|
| 52 |
-
reward += 0.
|
| 53 |
break
|
| 54 |
|
| 55 |
# Detect directory traversal
|
| 56 |
if action == "detect_attack":
|
| 57 |
for r in requests_log:
|
| 58 |
if isinstance(r, dict) and r.get("type") == "directory_traversal":
|
| 59 |
-
reward += 0.
|
| 60 |
break
|
| 61 |
|
| 62 |
# Deploy honeypot
|
| 63 |
elif action == "deploy_honeypot":
|
| 64 |
deploy_honeypot()
|
| 65 |
-
reward += 0.
|
| 66 |
|
| 67 |
# Fake database
|
| 68 |
elif action == "fake_database":
|
| 69 |
fake_database()
|
| 70 |
-
reward += 0.
|
| 71 |
|
| 72 |
# Block attacker
|
| 73 |
elif action == "block_ip":
|
| 74 |
if logs.get("suspicious_ips"):
|
| 75 |
ip = logs["suspicious_ips"][0]
|
| 76 |
block_attacker(ip)
|
| 77 |
-
reward += 0.
|
| 78 |
self.done = True
|
| 79 |
|
| 80 |
# Episode boundary
|
| 81 |
if self.current_step >= self.max_steps:
|
| 82 |
self.done = True
|
| 83 |
|
|
|
|
|
|
|
|
|
|
| 84 |
self._state = logs
|
| 85 |
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
def state(self):
|
| 89 |
-
return self._state
|
| 90 |
|
| 91 |
def action_space(self):
|
| 92 |
return [
|
|
@@ -94,4 +101,4 @@ class DeceptionEnv:
|
|
| 94 |
"deploy_honeypot",
|
| 95 |
"fake_database",
|
| 96 |
"block_ip"
|
| 97 |
-
]
|
|
|
|
| 1 |
import requests
|
| 2 |
from env.deception import deploy_honeypot, fake_database, block_attacker
|
| 3 |
+
from models import Observation, Reward
|
| 4 |
|
| 5 |
SERVER = "http://127.0.0.1:7860"
|
| 6 |
|
|
|
|
| 20 |
logs = requests.get(f"{SERVER}/logs").json()
|
| 21 |
self._state = logs
|
| 22 |
|
| 23 |
+
return Observation(**self._state)
|
| 24 |
|
| 25 |
def step(self, action):
|
| 26 |
|
|
|
|
| 35 |
# Detect brute force
|
| 36 |
if action == "detect_attack":
|
| 37 |
if failed_logins > 3:
|
| 38 |
+
reward += 0.15
|
| 39 |
else:
|
| 40 |
reward -= 0.05
|
| 41 |
|
|
|
|
| 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 |
# Detect SQL injection
|
| 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 |
# Detect directory traversal
|
| 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 |
# Deploy honeypot
|
| 64 |
elif action == "deploy_honeypot":
|
| 65 |
deploy_honeypot()
|
| 66 |
+
reward += 0.30
|
| 67 |
|
| 68 |
# Fake database
|
| 69 |
elif action == "fake_database":
|
| 70 |
fake_database()
|
| 71 |
+
reward += 0.20
|
| 72 |
|
| 73 |
# Block attacker
|
| 74 |
elif action == "block_ip":
|
| 75 |
if logs.get("suspicious_ips"):
|
| 76 |
ip = logs["suspicious_ips"][0]
|
| 77 |
block_attacker(ip)
|
| 78 |
+
reward += 0.50
|
| 79 |
self.done = True
|
| 80 |
|
| 81 |
# Episode boundary
|
| 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
|
| 89 |
|
| 90 |
+
observation = Observation(**self._state)
|
| 91 |
+
reward_obj = Reward(reward=reward, done=self.done)
|
| 92 |
+
|
| 93 |
+
return observation, reward_obj.reward, reward_obj.done, {}
|
| 94 |
|
| 95 |
def state(self):
|
| 96 |
+
return Observation(**self._state)
|
| 97 |
|
| 98 |
def action_space(self):
|
| 99 |
return [
|
|
|
|
| 101 |
"deploy_honeypot",
|
| 102 |
"fake_database",
|
| 103 |
"block_ip"
|
| 104 |
+
]
|
inference.py
CHANGED
|
@@ -9,11 +9,44 @@ random.seed(42)
|
|
| 9 |
from env.env import DeceptionEnv
|
| 10 |
from env.attacker import simulate_attack
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
|
| 13 |
MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-7B-Instruct")
|
| 14 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 15 |
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def run_task(task_name):
|
| 18 |
|
| 19 |
try:
|
|
@@ -24,7 +57,7 @@ def run_task(task_name):
|
|
| 24 |
)
|
| 25 |
|
| 26 |
env = DeceptionEnv()
|
| 27 |
-
env.reset()
|
| 28 |
|
| 29 |
print(
|
| 30 |
f"[START] task={task_name} env=ai-deception-openenv model={MODEL_NAME}",
|
|
@@ -46,30 +79,12 @@ def run_task(task_name):
|
|
| 46 |
except Exception:
|
| 47 |
pass
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
client.chat.completions.create(
|
| 52 |
-
model=MODEL_NAME,
|
| 53 |
-
messages=[{"role": "user", "content": "choose action"}],
|
| 54 |
-
timeout=10
|
| 55 |
-
)
|
| 56 |
-
except Exception:
|
| 57 |
-
pass
|
| 58 |
-
|
| 59 |
-
# Task logic
|
| 60 |
-
if task_name == "easy":
|
| 61 |
-
action = "detect_attack" if step < 3 else "deploy_honeypot"
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
else: # hard
|
| 67 |
-
if step == 1:
|
| 68 |
-
action = "detect_attack"
|
| 69 |
-
elif step == 2:
|
| 70 |
-
action = "deploy_honeypot"
|
| 71 |
-
else:
|
| 72 |
-
action = "block_ip"
|
| 73 |
|
| 74 |
state, reward, done, _ = env.step(action)
|
| 75 |
|
|
@@ -86,10 +101,16 @@ def run_task(task_name):
|
|
| 86 |
break
|
| 87 |
|
| 88 |
steps = len(rewards)
|
| 89 |
-
score = sum(rewards) / steps if steps > 0 else 0.0
|
| 90 |
-
score = min(max(score, 0.05), 0.95)
|
| 91 |
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
print(
|
| 95 |
f"[END] success={str(success).lower()} steps={steps} "
|
|
@@ -110,4 +131,4 @@ run_task("medium")
|
|
| 110 |
run_task("hard")
|
| 111 |
|
| 112 |
# Keep alive briefly
|
| 113 |
-
time.sleep(180)
|
|
|
|
| 9 |
from env.env import DeceptionEnv
|
| 10 |
from env.attacker import simulate_attack
|
| 11 |
|
| 12 |
+
# Import graders
|
| 13 |
+
from tasks.easy.grader import grade as easy_grade
|
| 14 |
+
from tasks.medium.grader import grade as medium_grade
|
| 15 |
+
from tasks.hard.grader import grade as hard_grade
|
| 16 |
+
|
| 17 |
+
|
| 18 |
API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
|
| 19 |
MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-7B-Instruct")
|
| 20 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 21 |
|
| 22 |
|
| 23 |
+
def choose_action(client, state):
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
response = client.chat.completions.create(
|
| 27 |
+
model=MODEL_NAME,
|
| 28 |
+
messages=[
|
| 29 |
+
{
|
| 30 |
+
"role": "system",
|
| 31 |
+
"content": "You are a cybersecurity AI agent. Choose one action from: detect_attack, deploy_honeypot, fake_database, block_ip"
|
| 32 |
+
},
|
| 33 |
+
{
|
| 34 |
+
"role": "user",
|
| 35 |
+
"content": f"Current state: {state}"
|
| 36 |
+
}
|
| 37 |
+
],
|
| 38 |
+
max_tokens=10,
|
| 39 |
+
temperature=0.0
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
action = response.choices[0].message.content.strip()
|
| 43 |
+
|
| 44 |
+
except Exception:
|
| 45 |
+
action = "detect_attack"
|
| 46 |
+
|
| 47 |
+
return action
|
| 48 |
+
|
| 49 |
+
|
| 50 |
def run_task(task_name):
|
| 51 |
|
| 52 |
try:
|
|
|
|
| 57 |
)
|
| 58 |
|
| 59 |
env = DeceptionEnv()
|
| 60 |
+
state = env.reset()
|
| 61 |
|
| 62 |
print(
|
| 63 |
f"[START] task={task_name} env=ai-deception-openenv model={MODEL_NAME}",
|
|
|
|
| 79 |
except Exception:
|
| 80 |
pass
|
| 81 |
|
| 82 |
+
# Model chooses action
|
| 83 |
+
action = choose_action(client, state)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
# fallback
|
| 86 |
+
if action not in env.action_space():
|
| 87 |
+
action = "detect_attack"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
state, reward, done, _ = env.step(action)
|
| 90 |
|
|
|
|
| 101 |
break
|
| 102 |
|
| 103 |
steps = len(rewards)
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
# Use graders
|
| 106 |
+
if task_name == "easy":
|
| 107 |
+
score = easy_grade(rewards)
|
| 108 |
+
elif task_name == "medium":
|
| 109 |
+
score = medium_grade(rewards)
|
| 110 |
+
else:
|
| 111 |
+
score = hard_grade(rewards)
|
| 112 |
+
|
| 113 |
+
success = score >= 0.5
|
| 114 |
|
| 115 |
print(
|
| 116 |
f"[END] success={str(success).lower()} steps={steps} "
|
|
|
|
| 131 |
run_task("hard")
|
| 132 |
|
| 133 |
# Keep alive briefly
|
| 134 |
+
time.sleep(180)
|
models.py
CHANGED
|
@@ -1,13 +1,13 @@
|
|
| 1 |
-
from pydantic import BaseModel
|
| 2 |
from typing import List, Optional, Dict, Any
|
| 3 |
|
| 4 |
|
| 5 |
class Observation(BaseModel):
|
| 6 |
failed_logins: int
|
| 7 |
port_scans: int
|
| 8 |
-
suspicious_ips: List[str]
|
| 9 |
total_requests: Optional[int] = 0
|
| 10 |
-
attack_types:
|
| 11 |
|
| 12 |
|
| 13 |
class Action(BaseModel):
|
|
@@ -17,4 +17,4 @@ class Action(BaseModel):
|
|
| 17 |
class Reward(BaseModel):
|
| 18 |
reward: float
|
| 19 |
done: bool
|
| 20 |
-
info:
|
|
|
|
| 1 |
+
from pydantic import BaseModel, Field
|
| 2 |
from typing import List, Optional, Dict, Any
|
| 3 |
|
| 4 |
|
| 5 |
class Observation(BaseModel):
|
| 6 |
failed_logins: int
|
| 7 |
port_scans: int
|
| 8 |
+
suspicious_ips: List[str] = Field(default_factory=list)
|
| 9 |
total_requests: Optional[int] = 0
|
| 10 |
+
attack_types: List[str] = Field(default_factory=list)
|
| 11 |
|
| 12 |
|
| 13 |
class Action(BaseModel):
|
|
|
|
| 17 |
class Reward(BaseModel):
|
| 18 |
reward: float
|
| 19 |
done: bool
|
| 20 |
+
info: Dict[str, Any] = Field(default_factory=dict)
|
tasks/easy/grader.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
-
def grade(
|
|
|
|
| 2 |
|
| 3 |
-
if
|
| 4 |
return 1.0
|
| 5 |
-
|
| 6 |
-
elif reward >= 0.2:
|
| 7 |
return 0.5
|
| 8 |
-
|
| 9 |
-
|
|
|
|
| 1 |
+
def grade(rewards):
|
| 2 |
+
score = sum(rewards) / len(rewards)
|
| 3 |
|
| 4 |
+
if score >= 0.4:
|
| 5 |
return 1.0
|
| 6 |
+
elif score >= 0.2:
|
|
|
|
| 7 |
return 0.5
|
| 8 |
+
else:
|
| 9 |
+
return 0.0
|
tasks/hard/grader.py
CHANGED
|
@@ -1,12 +1,9 @@
|
|
| 1 |
-
def grade(
|
|
|
|
| 2 |
|
| 3 |
-
if
|
| 4 |
return 1.0
|
| 5 |
-
|
| 6 |
-
elif reward >= 0.5:
|
| 7 |
return 0.7
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
return 0.4
|
| 11 |
-
|
| 12 |
-
return 0.0
|
|
|
|
| 1 |
+
def grade(rewards):
|
| 2 |
+
score = sum(rewards) / len(rewards)
|
| 3 |
|
| 4 |
+
if score >= 0.6:
|
| 5 |
return 1.0
|
| 6 |
+
elif score >= 0.4:
|
|
|
|
| 7 |
return 0.7
|
| 8 |
+
else:
|
| 9 |
+
return 0.3
|
|
|
|
|
|
|
|
|
tasks/medium/grader.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
-
def grade(
|
|
|
|
| 2 |
|
| 3 |
-
if
|
| 4 |
return 1.0
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
return 0.0
|
|
|
|
| 1 |
+
def grade(rewards):
|
| 2 |
+
score = sum(rewards) / len(rewards)
|
| 3 |
|
| 4 |
+
if score >= 0.5:
|
| 5 |
return 1.0
|
| 6 |
+
elif score >= 0.3:
|
| 7 |
+
return 0.6
|
| 8 |
+
else:
|
| 9 |
+
return 0.2
|
|
|