spidey121 commited on
Commit
502d891
·
1 Parent(s): 2e2490d

resolve merge conflict

Browse files
Files changed (4) hide show
  1. env/attacker.py +3 -10
  2. env/deception.py +0 -6
  3. env/env.py +1 -3
  4. inference.py +17 -10
env/attacker.py CHANGED
@@ -5,39 +5,32 @@ TARGET = "http://127.0.0.1:7860"
5
 
6
 
7
  def brute_force():
8
- print("Starting brute force attack...")
9
  for i in range(5):
10
- response = requests.post(
11
  f"{TARGET}/login",
12
  data={
13
  "username": "admin",
14
  "password": "wrong"
15
  }
16
  )
17
- print("Attempt:", i + 1, response.json())
18
  time.sleep(0.5)
19
 
20
 
21
  def port_scan():
22
- print("Starting port scan...")
23
  endpoints = ["/admin", "/config", "/backup"]
24
 
25
  for ep in endpoints:
26
- response = requests.get(f"{TARGET}{ep}")
27
- print("Scan:", ep, response.status_code)
28
 
29
 
30
  def credential_stuffing():
31
- print("Starting credential stuffing...")
32
-
33
  passwords = ["admin", "password", "123456"]
34
 
35
  for p in passwords:
36
- response = requests.post(
37
  f"{TARGET}/login",
38
  data={"username": "admin", "password": p}
39
  )
40
- print("Attempt:", p, response.json())
41
 
42
 
43
  def simulate_attack():
 
5
 
6
 
7
  def brute_force():
 
8
  for i in range(5):
9
+ requests.post(
10
  f"{TARGET}/login",
11
  data={
12
  "username": "admin",
13
  "password": "wrong"
14
  }
15
  )
 
16
  time.sleep(0.5)
17
 
18
 
19
  def port_scan():
 
20
  endpoints = ["/admin", "/config", "/backup"]
21
 
22
  for ep in endpoints:
23
+ requests.get(f"{TARGET}{ep}")
 
24
 
25
 
26
  def credential_stuffing():
 
 
27
  passwords = ["admin", "password", "123456"]
28
 
29
  for p in passwords:
30
+ requests.post(
31
  f"{TARGET}/login",
32
  data={"username": "admin", "password": p}
33
  )
 
34
 
35
 
36
  def simulate_attack():
env/deception.py CHANGED
@@ -1,7 +1,5 @@
1
  def deploy_honeypot():
2
 
3
- print("Honeypot deployed")
4
-
5
  return {
6
  "action": "honeypot",
7
  "status": "deployed"
@@ -10,8 +8,6 @@ def deploy_honeypot():
10
 
11
  def fake_database():
12
 
13
- print("Fake database exposed")
14
-
15
  return {
16
  "action": "fake_db",
17
  "status": "active"
@@ -20,8 +16,6 @@ def fake_database():
20
 
21
  def block_attacker(ip):
22
 
23
- print(f"Blocked attacker: {ip}")
24
-
25
  return {
26
  "action": "block",
27
  "ip": ip
 
1
  def deploy_honeypot():
2
 
 
 
3
  return {
4
  "action": "honeypot",
5
  "status": "deployed"
 
8
 
9
  def fake_database():
10
 
 
 
11
  return {
12
  "action": "fake_db",
13
  "status": "active"
 
16
 
17
  def block_attacker(ip):
18
 
 
 
19
  return {
20
  "action": "block",
21
  "ip": ip
env/env.py CHANGED
@@ -6,7 +6,7 @@ SERVER = "http://127.0.0.1:7860"
6
 
7
  class DeceptionEnv:
8
 
9
- def __init__(self):
10
  self.state = {}
11
  self.done = False
12
 
@@ -28,7 +28,6 @@ class DeceptionEnv:
28
  # Detect brute force
29
  if action == "detect_attack":
30
  if failed_logins > 3:
31
- print("Brute force detected")
32
  reward += 0.2
33
  else:
34
  reward -= 0.1
@@ -37,7 +36,6 @@ class DeceptionEnv:
37
  if action == "detect_attack":
38
  for r in requests_log:
39
  if isinstance(r, dict) and r.get("type") == "port_scan":
40
- print("Port scan detected")
41
  reward += 0.2
42
  break
43
 
 
6
 
7
  class DeceptionEnv:
8
 
9
+ def _init_(self):
10
  self.state = {}
11
  self.done = False
12
 
 
28
  # Detect brute force
29
  if action == "detect_attack":
30
  if failed_logins > 3:
 
31
  reward += 0.2
32
  else:
33
  reward -= 0.1
 
36
  if action == "detect_attack":
37
  for r in requests_log:
38
  if isinstance(r, dict) and r.get("type") == "port_scan":
 
39
  reward += 0.2
40
  break
41
 
inference.py CHANGED
@@ -6,20 +6,20 @@ from openai import OpenAI
6
 
7
  random.seed(42)
8
 
 
9
  from env.fake_server import run_server
10
  threading.Thread(target=run_server, daemon=True).start()
11
-
12
  time.sleep(3)
 
13
  from env.env import DeceptionEnv
14
  from env.attacker import simulate_attack
15
 
16
-
17
  API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
18
  MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-7B-Instruct")
19
  HF_TOKEN = os.getenv("HF_TOKEN")
20
 
21
  if not HF_TOKEN:
22
- print("[ERROR] HF_TOKEN not set. Please set your Hugging Face token.")
23
  exit(1)
24
 
25
  client = OpenAI(
@@ -28,17 +28,20 @@ client = OpenAI(
28
  )
29
 
30
  env = DeceptionEnv()
31
- state = env.reset() # reset only once
32
 
33
  print("[START] task=ai-deception env=cyber-security model=AI-agent", flush=True)
34
 
35
  rewards = []
36
  history = []
37
 
38
- for step in range(1, 4):
39
 
40
  simulate_attack()
41
 
 
 
 
42
  summary = {
43
  "failed_logins": state["failed_logins"],
44
  "port_scans": state.get("port_scans", 0),
@@ -54,7 +57,7 @@ Previous actions:
54
  Current summary:
55
  {summary}
56
 
57
- STRICT RULES (must follow exactly):
58
 
59
  1. If no previous action → detect_attack
60
  2. If last action == detect_attack → deploy_honeypot
@@ -77,7 +80,7 @@ block_ip
77
 
78
  action = response.choices[0].message.content.strip()
79
 
80
- # deterministic fallback
81
  if history:
82
  last = history[-1]
83
  if last == "detect_attack":
@@ -95,11 +98,15 @@ block_ip
95
  f"done={str(done).lower()} error=null",
96
  flush=True
97
  )
 
 
 
 
 
98
  score = min(sum(rewards), 1.0)
99
 
100
  print(
101
- f"[END] success=true steps=3 score={score:.2f} rewards={','.join(f'{r:.2f}' for r in rewards)}",
 
102
  flush=True
103
  )
104
- while True:
105
- time.sleep(60)
 
6
 
7
  random.seed(42)
8
 
9
+ # Start server
10
  from env.fake_server import run_server
11
  threading.Thread(target=run_server, daemon=True).start()
 
12
  time.sleep(3)
13
+
14
  from env.env import DeceptionEnv
15
  from env.attacker import simulate_attack
16
 
 
17
  API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
18
  MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-7B-Instruct")
19
  HF_TOKEN = os.getenv("HF_TOKEN")
20
 
21
  if not HF_TOKEN:
22
+ print("[ERROR] HF_TOKEN not set.")
23
  exit(1)
24
 
25
  client = OpenAI(
 
28
  )
29
 
30
  env = DeceptionEnv()
31
+ state = env.reset()
32
 
33
  print("[START] task=ai-deception env=cyber-security model=AI-agent", flush=True)
34
 
35
  rewards = []
36
  history = []
37
 
38
+ for step in range(1, 10):
39
 
40
  simulate_attack()
41
 
42
+ # UPDATE STATE BEFORE DECISION (important fix)
43
+ state = env.state
44
+
45
  summary = {
46
  "failed_logins": state["failed_logins"],
47
  "port_scans": state.get("port_scans", 0),
 
57
  Current summary:
58
  {summary}
59
 
60
+ STRICT RULES:
61
 
62
  1. If no previous action → detect_attack
63
  2. If last action == detect_attack → deploy_honeypot
 
80
 
81
  action = response.choices[0].message.content.strip()
82
 
83
+ # fallback logic
84
  if history:
85
  last = history[-1]
86
  if last == "detect_attack":
 
98
  f"done={str(done).lower()} error=null",
99
  flush=True
100
  )
101
+
102
+ if done:
103
+ break
104
+
105
+ # FINAL SCORE FIX
106
  score = min(sum(rewards), 1.0)
107
 
108
  print(
109
+ f"[END] success=true steps={len(rewards)} "
110
+ f"score={score:.2f} rewards={','.join(f'{r:.2f}' for r in rewards)}",
111
  flush=True
112
  )