-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathids_log_generator.py
More file actions
282 lines (233 loc) · 9.53 KB
/
Copy pathids_log_generator.py
File metadata and controls
282 lines (233 loc) · 9.53 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env python3
"""
IDS Log Generator and Attack Simulator
Generates realistic logs from web, database, and email servers
Feeds them to the IDS for threat detection
"""
import requests
import subprocess
import time
import random
from datetime import datetime, timedelta
import json
BASE_URL = 'http://localhost:5001'
# ==================== LOG COLLECTION ====================
def get_nginx_logs(container_name):
"""Collect Nginx access logs from a container"""
try:
result = subprocess.run(
['docker', 'exec', container_name, 'cat', '/var/log/nginx/access.log'],
capture_output=True,
text=True,
timeout=5
)
return result.stdout.split('\n') if result.returncode == 0 else []
except Exception as e:
print(f"❌ Error getting Nginx logs from {container_name}: {e}")
return []
def get_mysql_logs(container_name):
"""Collect MySQL query logs from a container"""
try:
result = subprocess.run(
['docker', 'exec', container_name, 'bash', '-c',
'mysql -u root -proot -e "SELECT * FROM mysql.general_log LIMIT 50;" 2>/dev/null'],
capture_output=True,
text=True,
timeout=5
)
return result.stdout.split('\n') if result.returncode == 0 else []
except Exception as e:
print(f"❌ Error getting MySQL logs from {container_name}: {e}")
return []
def get_email_logs(container_name):
"""Collect email server logs"""
try:
result = subprocess.run(
['docker', 'logs', container_name],
capture_output=True,
text=True,
timeout=5
)
return result.stdout.split('\n') if result.returncode == 0 else []
except Exception as e:
print(f"❌ Error getting email logs from {container_name}: {e}")
return []
# ==================== LOG ANALYSIS ====================
def analyze_web_logs():
"""Collect and analyze web server logs"""
print("\n📡 Analyzing Web Server Logs...")
print(" " + "─" * 60)
for container in ['web-server-1', 'web-server-2']:
logs = get_nginx_logs(container)
valid_logs = [log for log in logs if log.strip()]
if valid_logs:
print(f" Found {len(valid_logs)} log lines from {container}")
# Analyze each log
analyzed = 0
for log in valid_logs[-10:]: # Analyze last 10 logs
try:
response = requests.post(
f'{BASE_URL}/api/ids/analyze',
json={'type': 'web', 'log_line': log},
timeout=5
)
if response.status_code == 200:
data = response.json()
if data.get('alert'):
print(f" 🚨 ALERT: {data['alert']['alert_type']} - {data['alert']['attack_type']}")
analyzed += 1
except:
pass
print(f" ✅ Analyzed {analyzed} web logs")
def analyze_db_logs():
"""Collect and analyze database logs"""
print("\n🗄️ Analyzing Database Logs...")
print(" " + "─" * 60)
logs = get_mysql_logs('db-server-1')
valid_logs = [log for log in logs if log.strip() and 'SELECT' in log.upper()]
if valid_logs:
print(f" Found {len(valid_logs)} database queries")
# Analyze queries
analyzed = 0
for query in valid_logs[-10:]:
try:
response = requests.post(
f'{BASE_URL}/api/ids/analyze',
json={'type': 'db', 'log_line': query},
timeout=5
)
if response.status_code == 200:
data = response.json()
if data.get('alert'):
print(f" 🚨 ALERT: {data['alert']['alert_type']}")
analyzed += 1
except:
pass
print(f" ✅ Analyzed {analyzed} database logs")
else:
print(" ℹ️ No database queries found in logs")
def analyze_email_logs():
"""Collect and analyze email server logs"""
print("\n📧 Analyzing Email Server Logs...")
print(" " + "─" * 60)
logs = get_email_logs('email-server-1')
valid_logs = [log for log in logs if log.strip()]
if valid_logs:
print(f" Found {len(valid_logs)} email server events")
# Analyze logs
analyzed = 0
for log in valid_logs[-10:]:
try:
response = requests.post(
f'{BASE_URL}/api/ids/analyze',
json={'type': 'email', 'log_line': log},
timeout=5
)
if response.status_code == 200:
analyzed += 1
except:
pass
print(f" ✅ Analyzed {analyzed} email logs")
# ==================== ATTACK SIMULATION ====================
def generate_and_test_attacks():
"""Generate attack simulations and feed them to IDS"""
print("\n🎯 Generating and Testing Attack Simulations...")
print(" " + "─" * 60)
attack_configs = [
('sql_injection', 'db', 5),
('xss', 'web', 5),
('brute_force', 'web', 3),
('path_traversal', 'web', 3),
]
total_alerts = 0
for attack_type, target_type, count in attack_configs:
print(f"\n 🔴 {attack_type.upper()} Attacks:")
# Generate attacks
try:
response = requests.post(
f'{BASE_URL}/api/ids/test-attack',
json={'attack_type': attack_type, 'count': count},
timeout=5
)
if response.status_code != 200:
print(f" ❌ Failed to generate {attack_type}")
continue
data = response.json()
simulations = data.get('simulations', [])
print(f" Generated {len(simulations)} payloads")
# Analyze attacks
alerts_created = 0
for sim in simulations:
try:
response = requests.post(
f'{BASE_URL}/api/ids/analyze',
json={'type': target_type, 'log_line': sim},
timeout=5
)
if response.status_code == 200:
result = response.json()
if result.get('alert'):
alerts_created += 1
alert = result['alert']
confidence = alert.get('confidence', 0)
print(f" ✅ {alert['severity']}: {alert['attack_type']} (Conf: {confidence:.2%})")
except:
pass
total_alerts += alerts_created
print(f" → {alerts_created} alerts created from {attack_type}")
except Exception as e:
print(f" ❌ Error: {e}")
return total_alerts
# ==================== MAIN ====================
def main():
"""Main execution"""
print("\n" + "╔" + "═" * 68 + "╗")
print("║" + " " * 15 + "IDS LOG GENERATOR AND ATTACK SIMULATOR" + " " * 13 + "║")
print("╚" + "═" * 68 + "╝")
print(f"\n⏰ Starting at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print(f"🎯 Target: {BASE_URL}")
start_time = time.time()
try:
# Step 1: Collect and analyze real logs
print("\n" + "─" * 70)
print("PHASE 1: REAL LOG ANALYSIS")
print("─" * 70)
analyze_web_logs()
analyze_db_logs()
analyze_email_logs()
# Step 2: Generate and test attacks
print("\n" + "─" * 70)
print("PHASE 2: ATTACK SIMULATION AND DETECTION")
print("─" * 70)
alerts = generate_and_test_attacks()
# Step 3: Get final IDS status
print("\n" + "─" * 70)
print("PHASE 3: FINAL IDS STATUS")
print("─" * 70)
response = requests.get(f'{BASE_URL}/api/ids/status', timeout=5)
if response.status_code == 200:
data = response.json()
stats = data['statistics']
print(f"\n📊 IDS Statistics:")
print(f" Total Alerts: {stats['total_alerts']}")
print(f" Alert Types: {len(stats['alert_types'])}")
for alert_type, count in stats.get('alert_types', {}).items():
print(f" • {alert_type}: {count}")
# Get alerts
response = requests.get(f'{BASE_URL}/api/ids/alerts?limit=10', timeout=5)
if response.status_code == 200:
alerts_data = response.json()
alert_list = alerts_data.get('alerts', [])
if alert_list:
print(f"\n🚨 Recent Alerts:")
for alert in alert_list[-5:]:
print(f" {alert['timestamp']}: {alert['severity']} - {alert['alert_type']}")
elapsed = time.time() - start_time
print(f"\n✅ Complete! Took {elapsed:.2f} seconds")
print(f"📡 Total Alerts Detected: {alerts}")
except Exception as e:
print(f"\n❌ Error: {e}")
import traceback
traceback.print_exc()
if __name__ == '__main__':
main()