File size: 5,476 Bytes
4431575
 
6971e8f
 
 
 
4431575
 
 
 
 
6971e8f
4431575
 
 
 
e927afb
 
 
 
 
6971e8f
4431575
 
 
e927afb
4431575
 
 
 
 
6971e8f
4431575
 
 
 
e927afb
 
 
 
 
 
 
 
4431575
 
 
 
 
 
 
 
 
 
 
6971e8f
4431575
6971e8f
 
e927afb
4431575
6971e8f
c3e6acc
 
6971e8f
c3e6acc
 
 
6971e8f
 
 
 
 
 
 
e927afb
5b93dcc
 
 
e927afb
5b93dcc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e927afb
5b93dcc
 
 
 
 
 
 
 
 
 
 
 
 
e927afb
6971e8f
 
 
e927afb
 
6971e8f
e927afb
 
 
 
 
 
 
c3e6acc
 
 
 
 
e927afb
 
6971e8f
e927afb
 
 
 
 
 
 
c3e6acc
 
 
 
 
e927afb
 
6971e8f
e927afb
 
 
 
 
 
 
c3e6acc
6361dcc
afefc3b
6971e8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b93dcc
 
6971e8f
 
 
 
 
b78cf55
6971e8f
 
5b93dcc
6971e8f
 
 
 
 
 
5b93dcc
ddc819a
 
 
 
6971e8f
5b93dcc
 
6971e8f
8cf7800
6971e8f
 
8cf7800
 
 
 
 
6971e8f
 
 
5b93dcc
6971e8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b93dcc
 
 
 
 
 
 
6971e8f
 
afefc3b
 
6971e8f
afefc3b
 
 
 
5b93dcc
 
 
afefc3b
 
6971e8f
08690d1
59fe91e
6971e8f
59fe91e
6971e8f
 
 
59fe91e
 
6971e8f
 
 
 
 
 
 
 
e927afb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
from flask import Flask, request, jsonify
import time
import logging

log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)

app = Flask(__name__)

logs = {
    "failed_logins": 0,
    "port_scans": 0,
    "suspicious_ips": [],
    "requests": []
}

# Helper to extract attacker IP
def get_ip():
    return request.headers.get("X-Forwarded-For", request.remote_addr)


# ---------------- Login Attack ----------------

@app.route("/login", methods=["POST"])
def login():
    ip = get_ip()
    username = request.form.get("username")
    password = request.form.get("password")

    logs["requests"].append({
        "ip": ip,
        "type": "login_attempt",
        "username": username,
        "time": time.time()
    })

    # Detect SQL injection
    if "'" in str(username) or "--" in str(username):
        logs["requests"].append({
            "ip": ip,
            "type": "sql_injection",
            "time": time.time()
        })

    if password == "admin123":
        return jsonify({"status": "success"})
    else:
        logs["failed_logins"] += 1

        if ip not in logs["suspicious_ips"]:
            logs["suspicious_ips"].append(ip)

        return jsonify({"status": "failed"})


# ---------------- Port Scan ----------------

@app.route("/scan", methods=["GET"])
def scan():
    ip = get_ip()

    logs["port_scans"] += 1

    logs["requests"].append({
        "ip": ip,
        "type": "port_scan",
        "time": time.time()
    })

    if ip not in logs["suspicious_ips"]:
        logs["suspicious_ips"].append(ip)

    return jsonify({"status": "scan detected"})


# ---------------- SQL Injection Endpoint ----------------

@app.route("/sql")
def sql():
    ip = get_ip()

    logs["requests"].append({
        "ip": ip,
        "type": "sql_injection",
        "time": time.time()
    })

    if ip not in logs["suspicious_ips"]:
        logs["suspicious_ips"].append(ip)

    return jsonify({"status": "sql injection detected"})


# ---------------- Directory Traversal ----------------

@app.route("/download")
def download():
    ip = get_ip()

    logs["requests"].append({
        "ip": ip,
        "type": "directory_traversal",
        "time": time.time()
    })

    if ip not in logs["suspicious_ips"]:
        logs["suspicious_ips"].append(ip)

    return jsonify({"status": "directory traversal attempt"})


# ---------------- Common Scan Endpoints ----------------

@app.route("/admin")
def admin():
    ip = get_ip()

    logs["port_scans"] += 1

    logs["requests"].append({
        "ip": ip,
        "type": "port_scan",
        "time": time.time()
    })

    return "Forbidden", 403


@app.route("/config")
def config():
    ip = get_ip()

    logs["port_scans"] += 1

    logs["requests"].append({
        "ip": ip,
        "type": "port_scan",
        "time": time.time()
    })

    return "Forbidden", 403


@app.route("/backup")
def backup():
    ip = get_ip()

    logs["port_scans"] += 1

    logs["requests"].append({
        "ip": ip,
        "type": "port_scan",
        "time": time.time()
    })

    return "Forbidden", 403


# ---------------- Logs ----------------

@app.route("/logs", methods=["GET"])
def get_logs():
    return jsonify(logs)


# ---------------- State API ----------------

@app.route("/state")
def state():
    return jsonify({
        "failed_logins": logs["failed_logins"],
        "port_scans": logs["port_scans"],
        "suspicious_ips": logs["suspicious_ips"],
        "total_requests": len(logs["requests"]),
        "attack_types": [r["type"] for r in logs["requests"]]
    })


# ---------------- Reset API ----------------

@app.route("/reset", methods=["GET", "POST"])
def reset():
    global logs

    logs = {
        "failed_logins": 0,
        "port_scans": 0,
        "suspicious_ips": [],
        "requests": []
    }

    return jsonify({
        "status": "reset",
        "success": True
    }), 200


# ---------------- Step API ----------------

@app.route("/step", methods=["GET", "POST"])
def step():

    if request.method == "POST":
        data = request.get_json(silent=True) or {}
        action = data.get("action", None)
    else:
        action = request.args.get("action")

    reward = 0.0
    done = False

    if action == "detect_bruteforce":
        if logs["failed_logins"] > 3:
            reward = 0.4

    elif action == "detect_portscan":
        if logs["port_scans"] > 0:
            reward = 0.3

    elif action == "mitigate_attack":
        if len(logs["suspicious_ips"]) > 0:
            reward = 1.0
            done = True

    return jsonify({
        "state": logs,
        "reward": reward,
        "done": done,
        "info": {}
    })


# ---------------- Health Check ----------------

@app.route("/health")
def health():
    return jsonify({"status": "ok"})


# ---------------- Status ----------------

@app.route("/status")
def status():
    return jsonify({
        "environment": "AI Cyber Deception",
        "attacks": [
            "brute_force",
            "port_scan",
            "credential_stuffing",
            "sql_injection",
            "directory_traversal"
        ],
        "status": "running"
    })


# ---------------- Home ----------------

@app.route("/")
def home():
    return "AI Cyber Deception Server Running"


# ---------------- Run Server ----------------

def run_server():
    app.run(
        host="0.0.0.0",
        port=7860,
        debug=False,
        use_reloader=False
    )