spidey121 commited on
Commit
e927afb
·
1 Parent(s): 0e82b9f

fix round 1

Browse files
Files changed (3) hide show
  1. env/attacker.py +57 -11
  2. env/env.py +2 -2
  3. env/fake_server.py +47 -7
env/attacker.py CHANGED
@@ -1,51 +1,97 @@
1
  import requests
2
  import time
 
3
 
4
  TARGET = "http://127.0.0.1:7860"
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
- # ---------------- 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()
 
1
  import requests
2
  import time
3
+ import random
4
 
5
  TARGET = "http://127.0.0.1:7860"
6
 
7
+ # Multiple attacker IPs
8
+ ATTACKER_IPS = [
9
+ "192.168.1.10",
10
+ "192.168.1.11",
11
+ "192.168.1.12",
12
+ "10.0.0.5",
13
+ "172.16.0.3"
14
+ ]
15
+
16
 
17
  def brute_force():
18
+ ip = random.choice(ATTACKER_IPS)
19
+
20
  for i in range(5):
21
  requests.post(
22
  f"{TARGET}/login",
23
  data={
24
  "username": "admin",
25
  "password": "wrong"
26
+ },
27
+ headers={"X-Forwarded-For": ip}
28
  )
29
+ time.sleep(0.3)
30
 
31
 
32
  def port_scan():
33
+ ip = random.choice(ATTACKER_IPS)
34
+
35
  endpoints = ["/admin", "/config", "/backup"]
36
 
37
  for ep in endpoints:
38
+ requests.get(
39
+ f"{TARGET}{ep}",
40
+ headers={"X-Forwarded-For": ip}
41
+ )
42
 
43
 
44
  def credential_stuffing():
45
+ ip = random.choice(ATTACKER_IPS)
46
+
47
  passwords = ["admin", "password", "123456"]
48
 
49
  for p in passwords:
50
  requests.post(
51
  f"{TARGET}/login",
52
+ data={
53
+ "username": "admin",
54
+ "password": p
55
+ },
56
+ headers={"X-Forwarded-For": ip}
57
  )
58
 
59
 
 
 
60
  def sql_injection():
61
+ ip = random.choice(ATTACKER_IPS)
62
 
63
+ payloads = [
64
+ "' OR '1'='1",
65
+ "' OR 1=1 --",
66
+ "' UNION SELECT * FROM users --"
67
+ ]
68
 
69
+ for payload in payloads:
70
+ requests.post(
71
+ f"{TARGET}/login",
72
+ data={
73
+ "username": payload,
74
+ "password": payload
75
+ },
76
+ headers={"X-Forwarded-For": ip}
77
+ )
78
 
 
 
79
 
80
+ def directory_traversal():
81
+ ip = random.choice(ATTACKER_IPS)
82
+
83
+ paths = [
84
+ "/../../etc/passwd",
85
+ "/../config",
86
+ "/../../backup"
87
+ ]
88
+
89
+ for path in paths:
90
+ requests.get(
91
+ f"{TARGET}{path}",
92
+ headers={"X-Forwarded-For": ip}
93
+ )
94
 
 
95
 
96
  def simulate_attack():
97
  brute_force()
env/env.py CHANGED
@@ -70,10 +70,10 @@ class DeceptionEnv:
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
 
70
  fake_database()
71
  reward += 0.20
72
 
73
+ # Block attacker (multi attacker support)
74
  elif action == "block_ip":
75
  if logs.get("suspicious_ips"):
76
+ ip = logs["suspicious_ips"][-1] # latest attacker
77
  block_attacker(ip)
78
  reward += 0.50
79
  self.done = True
env/fake_server.py CHANGED
@@ -14,11 +14,16 @@ logs = {
14
  "requests": []
15
  }
16
 
 
 
 
 
 
17
  # ---------------- Login Attack ----------------
18
 
19
  @app.route("/login", methods=["POST"])
20
  def login():
21
- ip = request.remote_addr
22
  username = request.form.get("username")
23
  password = request.form.get("password")
24
 
@@ -29,6 +34,14 @@ def login():
29
  "time": time.time()
30
  })
31
 
 
 
 
 
 
 
 
 
32
  if password == "admin123":
33
  return jsonify({"status": "success"})
34
  else:
@@ -44,7 +57,7 @@ def login():
44
 
45
  @app.route("/scan", methods=["GET"])
46
  def scan():
47
- ip = request.remote_addr
48
 
49
  logs["port_scans"] += 1
50
 
@@ -60,11 +73,11 @@ def scan():
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,
@@ -82,7 +95,7 @@ def sql():
82
 
83
  @app.route("/download")
84
  def download():
85
- ip = request.remote_addr
86
 
87
  logs["requests"].append({
88
  "ip": ip,
@@ -96,23 +109,50 @@ def download():
96
  return jsonify({"status": "directory traversal attempt"})
97
 
98
 
99
- # Common scan endpoints
100
 
101
  @app.route("/admin")
102
  def admin():
 
 
103
  logs["port_scans"] += 1
 
 
 
 
 
 
 
104
  return "Forbidden", 403
105
 
106
 
107
  @app.route("/config")
108
  def config():
 
 
109
  logs["port_scans"] += 1
 
 
 
 
 
 
 
110
  return "Forbidden", 403
111
 
112
 
113
  @app.route("/backup")
114
  def backup():
 
 
115
  logs["port_scans"] += 1
 
 
 
 
 
 
 
116
  return "Forbidden", 403
117
 
118
 
@@ -229,4 +269,4 @@ def run_server():
229
  port=7860,
230
  debug=False,
231
  use_reloader=False
232
- )
 
14
  "requests": []
15
  }
16
 
17
+ # Helper to extract attacker IP
18
+ def get_ip():
19
+ return request.headers.get("X-Forwarded-For", request.remote_addr)
20
+
21
+
22
  # ---------------- Login Attack ----------------
23
 
24
  @app.route("/login", methods=["POST"])
25
  def login():
26
+ ip = get_ip()
27
  username = request.form.get("username")
28
  password = request.form.get("password")
29
 
 
34
  "time": time.time()
35
  })
36
 
37
+ # Detect SQL injection
38
+ if "'" in str(username) or "--" in str(username):
39
+ logs["requests"].append({
40
+ "ip": ip,
41
+ "type": "sql_injection",
42
+ "time": time.time()
43
+ })
44
+
45
  if password == "admin123":
46
  return jsonify({"status": "success"})
47
  else:
 
57
 
58
  @app.route("/scan", methods=["GET"])
59
  def scan():
60
+ ip = get_ip()
61
 
62
  logs["port_scans"] += 1
63
 
 
73
  return jsonify({"status": "scan detected"})
74
 
75
 
76
+ # ---------------- SQL Injection Endpoint ----------------
77
 
78
  @app.route("/sql")
79
  def sql():
80
+ ip = get_ip()
81
 
82
  logs["requests"].append({
83
  "ip": ip,
 
95
 
96
  @app.route("/download")
97
  def download():
98
+ ip = get_ip()
99
 
100
  logs["requests"].append({
101
  "ip": ip,
 
109
  return jsonify({"status": "directory traversal attempt"})
110
 
111
 
112
+ # ---------------- Common Scan Endpoints ----------------
113
 
114
  @app.route("/admin")
115
  def admin():
116
+ ip = get_ip()
117
+
118
  logs["port_scans"] += 1
119
+
120
+ logs["requests"].append({
121
+ "ip": ip,
122
+ "type": "port_scan",
123
+ "time": time.time()
124
+ })
125
+
126
  return "Forbidden", 403
127
 
128
 
129
  @app.route("/config")
130
  def config():
131
+ ip = get_ip()
132
+
133
  logs["port_scans"] += 1
134
+
135
+ logs["requests"].append({
136
+ "ip": ip,
137
+ "type": "port_scan",
138
+ "time": time.time()
139
+ })
140
+
141
  return "Forbidden", 403
142
 
143
 
144
  @app.route("/backup")
145
  def backup():
146
+ ip = get_ip()
147
+
148
  logs["port_scans"] += 1
149
+
150
+ logs["requests"].append({
151
+ "ip": ip,
152
+ "type": "port_scan",
153
+ "time": time.time()
154
+ })
155
+
156
  return "Forbidden", 403
157
 
158
 
 
269
  port=7860,
270
  debug=False,
271
  use_reloader=False
272
+ )