spidey121 commited on
Commit
5b93dcc
·
1 Parent(s): 860fc85

fix round 1

Browse files
env/__pycache__/__init__.cpython-312.pyc CHANGED
Binary files a/env/__pycache__/__init__.cpython-312.pyc and b/env/__pycache__/__init__.cpython-312.pyc differ
 
env/__pycache__/attacker.cpython-312.pyc CHANGED
Binary files a/env/__pycache__/attacker.cpython-312.pyc and b/env/__pycache__/attacker.cpython-312.pyc differ
 
env/__pycache__/deception.cpython-312.pyc CHANGED
Binary files a/env/__pycache__/deception.cpython-312.pyc and b/env/__pycache__/deception.cpython-312.pyc differ
 
env/__pycache__/env.cpython-312.pyc CHANGED
Binary files a/env/__pycache__/env.cpython-312.pyc and b/env/__pycache__/env.cpython-312.pyc differ
 
env/__pycache__/fake_server.cpython-312.pyc CHANGED
Binary files a/env/__pycache__/fake_server.cpython-312.pyc and b/env/__pycache__/fake_server.cpython-312.pyc differ
 
env/attacker.py CHANGED
@@ -33,7 +33,23 @@ def credential_stuffing():
33
  )
34
 
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  def simulate_attack():
37
  brute_force()
38
  port_scan()
39
  credential_stuffing()
 
 
 
33
  )
34
 
35
 
36
+ # ---------------- SQL Injection ----------------
37
+
38
+ def sql_injection():
39
+ requests.get(f"{TARGET}/sql")
40
+
41
+
42
+ # ---------------- Directory Traversal ----------------
43
+
44
+ def directory_traversal():
45
+ requests.get(f"{TARGET}/download")
46
+
47
+
48
+ # ---------------- Full Attack Simulation ----------------
49
+
50
  def simulate_attack():
51
  brute_force()
52
  port_scan()
53
  credential_stuffing()
54
+ sql_injection()
55
+ directory_traversal()
env/env.py CHANGED
@@ -6,31 +6,37 @@ SERVER = "http://127.0.0.1:7860"
6
 
7
  class DeceptionEnv:
8
 
9
- def _init_(self):
10
- self.state = {}
11
  self.done = False
 
 
12
 
13
  def reset(self):
14
  self.done = False
 
 
15
  logs = requests.get(f"{SERVER}/logs").json()
16
- self.state = logs
17
- return self.state
 
18
 
19
  def step(self, action):
20
 
21
- reward = 0
 
22
 
23
  logs = requests.get(f"{SERVER}/logs").json()
24
 
25
- failed_logins = logs["failed_logins"]
26
- requests_log = logs["requests"]
27
 
28
  # Detect brute force
29
  if action == "detect_attack":
30
  if failed_logins > 3:
31
  reward += 0.2
32
  else:
33
- reward -= 0.1
34
 
35
  # Detect port scan
36
  if action == "detect_attack":
@@ -40,29 +46,33 @@ class DeceptionEnv:
40
  break
41
 
42
  # Deploy honeypot
43
- if action == "deploy_honeypot":
44
  deploy_honeypot()
45
  reward += 0.3
46
 
47
  # Fake database
48
- if action == "fake_database":
49
  fake_database()
50
  reward += 0.2
51
 
52
  # Block attacker
53
- if action == "block_ip":
54
- if logs["suspicious_ips"]:
55
  ip = logs["suspicious_ips"][0]
56
  block_attacker(ip)
57
  reward += 0.5
58
  self.done = True
59
 
60
- self.state = logs
 
 
 
 
61
 
62
- return self.state, reward, self.done, {}
63
 
64
  def state(self):
65
- return self.state
66
 
67
  def action_space(self):
68
  return [
 
6
 
7
  class DeceptionEnv:
8
 
9
+ def __init__(self):
10
+ self._state = {}
11
  self.done = False
12
+ self.max_steps = 5
13
+ self.current_step = 0
14
 
15
  def reset(self):
16
  self.done = False
17
+ self.current_step = 0
18
+
19
  logs = requests.get(f"{SERVER}/logs").json()
20
+ self._state = logs
21
+
22
+ return self._state
23
 
24
  def step(self, action):
25
 
26
+ reward = 0.0
27
+ self.current_step += 1
28
 
29
  logs = requests.get(f"{SERVER}/logs").json()
30
 
31
+ failed_logins = logs.get("failed_logins", 0)
32
+ requests_log = logs.get("requests", [])
33
 
34
  # Detect brute force
35
  if action == "detect_attack":
36
  if failed_logins > 3:
37
  reward += 0.2
38
  else:
39
+ reward -= 0.05
40
 
41
  # Detect port scan
42
  if action == "detect_attack":
 
46
  break
47
 
48
  # Deploy honeypot
49
+ elif action == "deploy_honeypot":
50
  deploy_honeypot()
51
  reward += 0.3
52
 
53
  # Fake database
54
+ elif action == "fake_database":
55
  fake_database()
56
  reward += 0.2
57
 
58
  # Block attacker
59
+ elif action == "block_ip":
60
+ if logs.get("suspicious_ips"):
61
  ip = logs["suspicious_ips"][0]
62
  block_attacker(ip)
63
  reward += 0.5
64
  self.done = True
65
 
66
+ # Episode boundary
67
+ if self.current_step >= self.max_steps:
68
+ self.done = True
69
+
70
+ self._state = logs
71
 
72
+ return self._state, reward, self.done, {}
73
 
74
  def state(self):
75
+ return self._state
76
 
77
  def action_space(self):
78
  return [
env/fake_server.py CHANGED
@@ -60,6 +60,42 @@ def scan():
60
  return jsonify({"status": "scan detected"})
61
 
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  # Common scan endpoints
64
 
65
  @app.route("/admin")
@@ -95,7 +131,8 @@ def state():
95
  "failed_logins": logs["failed_logins"],
96
  "port_scans": logs["port_scans"],
97
  "suspicious_ips": logs["suspicious_ips"],
98
- "total_requests": len(logs["requests"])
 
99
  })
100
 
101
 
@@ -104,18 +141,21 @@ def state():
104
  @app.route("/reset", methods=["GET", "POST"])
105
  def reset():
106
  global logs
 
107
  logs = {
108
  "failed_logins": 0,
109
  "port_scans": 0,
110
  "suspicious_ips": [],
111
  "requests": []
112
  }
 
113
  return jsonify({
114
  "status": "reset",
115
  "success": True
116
  }), 200
117
 
118
- # ---------------- Step API (IMPORTANT) ----------------
 
119
 
120
  @app.route("/step", methods=["GET", "POST"])
121
  def step():
@@ -128,17 +168,15 @@ def step():
128
 
129
  reward = 0.0
130
  done = False
131
- # Detect brute force
132
  if action == "detect_bruteforce":
133
  if logs["failed_logins"] > 3:
134
  reward = 0.4
135
 
136
- # Detect port scan
137
  elif action == "detect_portscan":
138
  if logs["port_scans"] > 0:
139
  reward = 0.3
140
 
141
- # Mitigation step
142
  elif action == "mitigate_attack":
143
  if len(logs["suspicious_ips"]) > 0:
144
  reward = 1.0
@@ -152,6 +190,13 @@ def step():
152
  })
153
 
154
 
 
 
 
 
 
 
 
155
  # ---------------- Status ----------------
156
 
157
  @app.route("/status")
@@ -161,7 +206,9 @@ def status():
161
  "attacks": [
162
  "brute_force",
163
  "port_scan",
164
- "credential_stuffing"
 
 
165
  ],
166
  "status": "running"
167
  })
 
60
  return jsonify({"status": "scan detected"})
61
 
62
 
63
+ # ---------------- SQL Injection Attack ----------------
64
+
65
+ @app.route("/sql")
66
+ def sql():
67
+ ip = request.remote_addr
68
+
69
+ logs["requests"].append({
70
+ "ip": ip,
71
+ "type": "sql_injection",
72
+ "time": time.time()
73
+ })
74
+
75
+ if ip not in logs["suspicious_ips"]:
76
+ logs["suspicious_ips"].append(ip)
77
+
78
+ return jsonify({"status": "sql injection detected"})
79
+
80
+
81
+ # ---------------- Directory Traversal ----------------
82
+
83
+ @app.route("/download")
84
+ def download():
85
+ ip = request.remote_addr
86
+
87
+ logs["requests"].append({
88
+ "ip": ip,
89
+ "type": "directory_traversal",
90
+ "time": time.time()
91
+ })
92
+
93
+ if ip not in logs["suspicious_ips"]:
94
+ logs["suspicious_ips"].append(ip)
95
+
96
+ return jsonify({"status": "directory traversal attempt"})
97
+
98
+
99
  # Common scan endpoints
100
 
101
  @app.route("/admin")
 
131
  "failed_logins": logs["failed_logins"],
132
  "port_scans": logs["port_scans"],
133
  "suspicious_ips": logs["suspicious_ips"],
134
+ "total_requests": len(logs["requests"]),
135
+ "attack_types": [r["type"] for r in logs["requests"]]
136
  })
137
 
138
 
 
141
  @app.route("/reset", methods=["GET", "POST"])
142
  def reset():
143
  global logs
144
+
145
  logs = {
146
  "failed_logins": 0,
147
  "port_scans": 0,
148
  "suspicious_ips": [],
149
  "requests": []
150
  }
151
+
152
  return jsonify({
153
  "status": "reset",
154
  "success": True
155
  }), 200
156
 
157
+
158
+ # ---------------- Step API ----------------
159
 
160
  @app.route("/step", methods=["GET", "POST"])
161
  def step():
 
168
 
169
  reward = 0.0
170
  done = False
171
+
172
  if action == "detect_bruteforce":
173
  if logs["failed_logins"] > 3:
174
  reward = 0.4
175
 
 
176
  elif action == "detect_portscan":
177
  if logs["port_scans"] > 0:
178
  reward = 0.3
179
 
 
180
  elif action == "mitigate_attack":
181
  if len(logs["suspicious_ips"]) > 0:
182
  reward = 1.0
 
190
  })
191
 
192
 
193
+ # ---------------- Health Check ----------------
194
+
195
+ @app.route("/health")
196
+ def health():
197
+ return jsonify({"status": "ok"})
198
+
199
+
200
  # ---------------- Status ----------------
201
 
202
  @app.route("/status")
 
206
  "attacks": [
207
  "brute_force",
208
  "port_scan",
209
+ "credential_stuffing",
210
+ "sql_injection",
211
+ "directory_traversal"
212
  ],
213
  "status": "running"
214
  })
inference.py CHANGED
@@ -7,11 +7,11 @@ from openai import OpenAI
7
 
8
  random.seed(42)
9
 
10
- from env.fake_server import run_server
11
  from env.env import DeceptionEnv
12
  from env.attacker import simulate_attack
13
 
14
- # Start server
15
  threading.Thread(target=run_server, daemon=True).start()
16
  time.sleep(2)
17
 
@@ -33,22 +33,23 @@ def run_task(task_name):
33
  env.reset()
34
 
35
  print(
36
- f"[START] task={task_name} env=cyber-security model=AI-agent",
37
  flush=True
38
  )
39
 
40
  rewards = []
 
41
 
42
  for step in range(1, 4):
43
 
44
  try:
45
  simulate_attack()
46
- except:
47
  pass
48
 
49
  try:
50
  state = env.state()
51
- except:
52
  pass
53
 
54
  # Required OpenAI call (validator requirement)
@@ -58,7 +59,7 @@ def run_task(task_name):
58
  messages=[{"role": "user", "content": "choose action"}],
59
  timeout=10
60
  )
61
- except:
62
  pass
63
 
64
  # Task-specific logic
@@ -88,7 +89,7 @@ def run_task(task_name):
88
 
89
  state, reward, done, _ = env.step(action)
90
 
91
- # keep reward strictly (0,1)
92
  reward = min(max(reward, 0.05), 0.95)
93
 
94
  rewards.append(reward)
@@ -99,12 +100,18 @@ def run_task(task_name):
99
  flush=True
100
  )
101
 
102
- score = sum(rewards) / 3
 
 
 
 
103
  score = min(max(score, 0.05), 0.95)
104
 
 
 
105
  print(
106
- f"[END] success=true steps=3 "
107
- f"score={score:.2f} rewards={','.join(f'{r:.2f}' for r in rewards)}",
108
  flush=True
109
  )
110
 
@@ -116,10 +123,10 @@ def run_task(task_name):
116
  )
117
 
118
 
119
- # Run 3 Tasks
120
  run_task("easy")
121
  run_task("medium")
122
  run_task("hard")
123
 
124
- # allow validator reset calls
125
- time.sleep(120)
 
7
 
8
  random.seed(42)
9
 
10
+ from server.app import main as run_server
11
  from env.env import DeceptionEnv
12
  from env.attacker import simulate_attack
13
 
14
+ # Start server in background
15
  threading.Thread(target=run_server, daemon=True).start()
16
  time.sleep(2)
17
 
 
33
  env.reset()
34
 
35
  print(
36
+ f"[START] task={task_name} env=cyber-security model={MODEL_NAME}",
37
  flush=True
38
  )
39
 
40
  rewards = []
41
+ done = False
42
 
43
  for step in range(1, 4):
44
 
45
  try:
46
  simulate_attack()
47
+ except Exception:
48
  pass
49
 
50
  try:
51
  state = env.state()
52
+ except Exception:
53
  pass
54
 
55
  # Required OpenAI call (validator requirement)
 
59
  messages=[{"role": "user", "content": "choose action"}],
60
  timeout=10
61
  )
62
+ except Exception:
63
  pass
64
 
65
  # Task-specific logic
 
89
 
90
  state, reward, done, _ = env.step(action)
91
 
92
+ # clamp reward to (0,1)
93
  reward = min(max(reward, 0.05), 0.95)
94
 
95
  rewards.append(reward)
 
100
  flush=True
101
  )
102
 
103
+ if done:
104
+ break
105
+
106
+ steps = len(rewards)
107
+ score = sum(rewards) / steps if steps > 0 else 0.0
108
  score = min(max(score, 0.05), 0.95)
109
 
110
+ success = score >= 0.3
111
+
112
  print(
113
+ f"[END] success={str(success).lower()} steps={steps} "
114
+ f"score={score:.3f} rewards={','.join(f'{r:.2f}' for r in rewards)}",
115
  flush=True
116
  )
117
 
 
123
  )
124
 
125
 
126
+ # Run tasks
127
  run_task("easy")
128
  run_task("medium")
129
  run_task("hard")
130
 
131
+ # Keep space alive briefly for validation (3 min)
132
+ time.sleep(180)
models.py CHANGED
@@ -1,14 +1,20 @@
1
  from pydantic import BaseModel
2
- from typing import List
 
3
 
4
  class Observation(BaseModel):
5
  failed_logins: int
6
  port_scans: int
7
  suspicious_ips: List[str]
 
 
 
8
 
9
  class Action(BaseModel):
10
  action: str
11
 
 
12
  class Reward(BaseModel):
13
  reward: float
14
  done: bool
 
 
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: Optional[List[str]] = []
11
+
12
 
13
  class Action(BaseModel):
14
  action: str
15
 
16
+
17
  class Reward(BaseModel):
18
  reward: float
19
  done: bool
20
+ info: Optional[Dict[str, Any]] = {}
openenv.yaml CHANGED
@@ -1,4 +1,6 @@
1
  name: ai-deception-env
 
 
2
  description: AI Cyber Deception Environment
3
 
4
  tasks:
 
1
  name: ai-deception-env
2
+ version: 1
3
+ environment: cyber-security
4
  description: AI Cyber Deception Environment
5
 
6
  tasks:
requirements.txt CHANGED
@@ -3,3 +3,5 @@ requests
3
  numpy
4
  pydantic
5
  openai
 
 
 
3
  numpy
4
  pydantic
5
  openai
6
+ gunicorn
7
+ uvicorn
server/__init__.py ADDED
File without changes
server/__init__.py:Zone.Identifier ADDED
Binary file (25 Bytes). View file
 
tasks/easy/__init__.py ADDED
File without changes
tasks/easy/__init__.py:Zone.Identifier ADDED
Binary file (25 Bytes). View file
 
tasks/easy/grader.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ def grade(reward):
2
+
3
+ if reward >= 0.4:
4
+ return 1.0
5
+
6
+ elif reward >= 0.2:
7
+ return 0.5
8
+
9
+ return 0.0
tasks/{easy.py → easy/task.py} RENAMED
@@ -1,13 +1,14 @@
1
  from env.env import DeceptionEnv
2
  from env.attacker import brute_force
3
 
4
- def run():
5
 
6
- brute_force()
7
 
8
  env = DeceptionEnv()
9
  env.reset()
10
 
 
 
11
  _, reward, _, _ = env.step("detect_attack")
12
 
13
  return reward
 
1
  from env.env import DeceptionEnv
2
  from env.attacker import brute_force
3
 
 
4
 
5
+ def run():
6
 
7
  env = DeceptionEnv()
8
  env.reset()
9
 
10
+ brute_force()
11
+
12
  _, reward, _, _ = env.step("detect_attack")
13
 
14
  return reward
tasks/hard/__init__.py ADDED
File without changes
tasks/hard/__init__.py:Zone.Identifier ADDED
Binary file (25 Bytes). View file
 
tasks/hard/grader.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def grade(reward):
2
+
3
+ if reward >= 0.8:
4
+ return 1.0
5
+
6
+ elif reward >= 0.5:
7
+ return 0.7
8
+
9
+ elif reward >= 0.3:
10
+ return 0.4
11
+
12
+ return 0.0
tasks/{hard.py → hard/task.py} RENAMED
@@ -1,15 +1,16 @@
1
  from env.env import DeceptionEnv
2
  from env.attacker import brute_force, port_scan, credential_stuffing
3
 
 
4
  def run():
5
 
 
 
 
6
  brute_force()
7
  port_scan()
8
  credential_stuffing()
9
 
10
- env = DeceptionEnv()
11
- env.reset()
12
-
13
  total_reward = 0
14
 
15
  _, r, _, _ = env.step("detect_attack")
 
1
  from env.env import DeceptionEnv
2
  from env.attacker import brute_force, port_scan, credential_stuffing
3
 
4
+
5
  def run():
6
 
7
+ env = DeceptionEnv()
8
+ env.reset()
9
+
10
  brute_force()
11
  port_scan()
12
  credential_stuffing()
13
 
 
 
 
14
  total_reward = 0
15
 
16
  _, r, _, _ = env.step("detect_attack")
tasks/medium/__init__.py ADDED
File without changes
tasks/medium/__init__.py:Zone.Identifier ADDED
Binary file (25 Bytes). View file
 
tasks/medium/grader.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ def grade(reward):
2
+
3
+ if reward >= 0.6:
4
+ return 1.0
5
+
6
+ elif reward >= 0.3:
7
+ return 0.5
8
+
9
+ return 0.0
tasks/{medium.py → medium/task.py} RENAMED
@@ -1,13 +1,14 @@
1
  from env.env import DeceptionEnv
2
  from env.attacker import brute_force
3
 
4
- def run():
5
 
6
- brute_force()
7
 
8
  env = DeceptionEnv()
9
  env.reset()
10
 
 
 
11
  total_reward = 0
12
 
13
  _, r, _, _ = env.step("detect_attack")
 
1
  from env.env import DeceptionEnv
2
  from env.attacker import brute_force
3
 
 
4
 
5
+ def run():
6
 
7
  env = DeceptionEnv()
8
  env.reset()
9
 
10
+ brute_force()
11
+
12
  total_reward = 0
13
 
14
  _, r, _, _ = env.step("detect_attack")
tasks/test_tasks.py CHANGED
@@ -1,6 +1,6 @@
1
- from tasks.easy import run as easy
2
- from tasks.medium import run as medium
3
- from tasks.hard import run as hard
4
 
5
  print("Easy:", easy())
6
  print("Medium:", medium())
 
1
+ from tasks.easy.task import run as easy
2
+ from tasks.medium.task import run as medium
3
+ from tasks.hard.task import run as hard
4
 
5
  print("Easy:", easy())
6
  print("Medium:", medium())