-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
264 lines (203 loc) · 8.36 KB
/
Copy pathapp.py
File metadata and controls
264 lines (203 loc) · 8.36 KB
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
#!/usr/bin/env python3
import os
import subprocess
import json
import re
import ipaddress
from flask import Flask, request, jsonify
from datetime import datetime
import psutil
app = Flask(__name__)
# Security settings
app.config['MAX_CONTENT_LENGTH'] = 1 * 1024 * 1024 # 1MB max request size
# MySQL config - enable if database available
MYSQL_ENABLED = False
DB_CONFIG = {
"host": "localhost",
"user": "testuser",
"password": "testpass",
"database": "testdb"
}
TELEMETRY_LOG = "telemetry.jsonl"
MAX_FILENAME_LENGTH = 100
def validate_ip_address(ip_str):
"""Validate IP address - prevent command injection"""
try:
ipaddress.ip_address(ip_str)
return True
except ValueError:
if re.match(r'^[a-zA-Z0-9.-]{1,255}$', ip_str):
return ip_str.count('.') <= 3
return False
def validate_filename(filename):
"""Validate filename - prevent path traversal"""
if not filename or len(filename) > MAX_FILENAME_LENGTH:
return False
return bool(re.match(r'^[a-zA-Z0-9._-]+$', filename))
def add_security_headers(response):
"""Add security headers to response"""
response.headers['X-Content-Type-Options'] = 'nosniff'
response.headers['X-Frame-Options'] = 'DENY'
response.headers['X-XSS-Protection'] = '1; mode=block'
return response
def get_process_metrics():
metrics = {
"cpu_percent": 0.0,
"memory_mb": 0.0,
"num_fds": 0,
"disk_read_mb": 0.0,
"disk_write_mb": 0.0,
"ctx_switches": 0,
"num_children": 0
}
try:
p = psutil.Process()
metrics["cpu_percent"] = p.cpu_percent(interval=0.1)
metrics["memory_mb"] = p.memory_info().rss / (1024 * 1024)
try:
metrics["num_fds"] = p.num_fds()
except:
metrics["num_fds"] = len(p.open_files())
io = p.io_counters()
metrics["disk_read_mb"] = io.read_bytes / (1024 * 1024)
metrics["disk_write_mb"] = io.write_bytes / (1024 * 1024)
ctx = p.num_ctx_switches()
metrics["ctx_switches"] = ctx.voluntary + ctx.involuntary
metrics["num_children"] = len(p.children())
except Exception:
pass
return metrics
def get_mysql_metrics():
metrics = {
"cpu_percent": 0.0,
"memory_mb": 0.0,
"num_fds": 0,
"disk_read_mb": 0.0,
"disk_write_mb": 0.0,
"ctx_switches": 0,
"num_children": 0
}
try:
for proc in psutil.process_iter(["name", "pid"]):
try:
name = proc.info["name"]
if name and ("mysql" in name.lower() or "mysqld" in name.lower()):
proc.cpu_percent(interval=0.1)
metrics["cpu_percent"] = proc.cpu_percent(interval=0.1)
metrics["memory_mb"] = proc.memory_info().rss / (1024 * 1024)
try:
metrics["num_fds"] = proc.num_fds()
except:
metrics["num_fds"] = len(proc.open_files())
io = proc.io_counters()
metrics["disk_read_mb"] = io.read_bytes / (1024 * 1024)
metrics["disk_write_mb"] = io.write_bytes / (1024 * 1024)
ctx = proc.num_ctx_switches()
metrics["ctx_switches"] = ctx.voluntary + ctx.involuntary
metrics["num_children"] = len(proc.children())
break
except (psutil.NoSuchProcess, psutil.AccessDenied):
continue
except Exception:
pass
return metrics
def log_telemetry(endpoint, status):
try:
flask_m = get_process_metrics()
mysql_m = get_mysql_metrics() if MYSQL_ENABLED else {}
telemetry = {
"timestamp": datetime.utcnow().isoformat(),
"endpoint": endpoint,
"status": status,
"remote_addr": request.remote_addr,
"flask_cpu": flask_m["cpu_percent"],
"flask_memory_mb": flask_m["memory_mb"],
"flask_fds": flask_m["num_fds"],
"flask_disk_read_mb": flask_m["disk_read_mb"],
"flask_disk_write_mb": flask_m["disk_write_mb"],
"flask_ctx_switches": flask_m["ctx_switches"],
"flask_children": flask_m["num_children"],
"mysql_cpu": mysql_m.get("cpu_percent", 0.0),
"mysql_memory_mb": mysql_m.get("memory_mb", 0.0),
"mysql_fds": mysql_m.get("num_fds", 0),
"mysql_disk_read_mb": mysql_m.get("disk_read_mb", 0.0),
"mysql_disk_write_mb": mysql_m.get("disk_write_mb", 0.0),
"mysql_ctx_switches": mysql_m.get("ctx_switches", 0),
"mysql_children": mysql_m.get("num_children", 0)
}
with open(TELEMETRY_LOG, "a") as f:
f.write(json.dumps(telemetry) + "\n")
except Exception as e:
print(f"Telemetry logging error: {e}")
@app.route("/login", methods=["POST"])
def login():
try:
username = request.form.get("username", "").strip()
password = request.form.get("password", "").strip()
# Input validation
if not username or not password:
log_telemetry("/login", "rejected_empty")
return jsonify({"status": "failure", "message": "Invalid credentials"}), 400
if len(username) > 100 or len(password) > 100:
log_telemetry("/login", "rejected_toolong")
return jsonify({"status": "failure", "message": "Credentials too long"}), 400
# Simulated login - always succeeds for IDS testing
log_telemetry("/login", "success")
response = jsonify({"status": "success", "message": "Login successful"})
return add_security_headers(response)
except Exception as e:
log_telemetry("/login", "error")
return jsonify({"status": "error", "message": "Internal error"}), 500
@app.route("/ping", methods=["GET"])
def ping():
try:
host = request.args.get("host", "127.0.0.1").strip()
# Input validation - prevent command injection
if not validate_ip_address(host):
log_telemetry("/ping", "rejected_invalid_host")
return jsonify({"status": "error", "message": "Invalid host"}), 400
# Use subprocess without shell=True for safety
import platform
if platform.system() == "Windows":
cmd = ["ping", "-n", "1", host]
else:
cmd = ["ping", "-c", "1", host]
result = subprocess.run(cmd, capture_output=True, text=True, timeout=5)
output = result.stdout[:500] if result.stdout else ""
log_telemetry("/ping", "success")
response = jsonify({"status": "success", "output": output, "returncode": result.returncode})
return add_security_headers(response)
except subprocess.TimeoutExpired:
log_telemetry("/ping", "timeout")
return jsonify({"status": "error", "message": "Ping timeout"}), 504
except Exception as e:
log_telemetry("/ping", "error")
return jsonify({"status": "error", "message": "Ping failed"}), 500
@app.route("/download", methods=["GET"])
def download():
try:
filename = request.args.get("file", "readme.txt").strip()
# Input validation - prevent path traversal
if not validate_filename(filename):
log_telemetry("/download", "rejected_invalid_filename")
return jsonify({"status": "error", "message": "Invalid filename"}), 400
# Simulated download response
log_telemetry("/download", "success")
response = jsonify({"status": "success", "content": f"File {filename} downloaded successfully"})
return add_security_headers(response)
except Exception as e:
log_telemetry("/download", "error")
return jsonify({"status": "error", "message": "Download failed"}), 500
@app.route("/health", methods=["GET"])
def health():
log_telemetry("/health", "success")
response = jsonify({"status": "healthy"})
return add_security_headers(response)
if __name__ == "__main__":
if not os.path.exists(TELEMETRY_LOG):
open(TELEMETRY_LOG, "w").close()
print("Starting IDS Flask application...")
print(f"Telemetry logging to: {TELEMETRY_LOG}")
print(f"Max request size: 1MB")
print(f"Security: Input validation enabled")
app.run(host="0.0.0.0", port=5000, debug=False, threaded=True)