aiwaf 0.1.9.0.4__py3-none-any.whl → 0.1.9.0.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of aiwaf might be problematic. Click here for more details.
- aiwaf/__init__.py +1 -1
- aiwaf/blacklist_manager.py +17 -4
- aiwaf/management/commands/add_exemption.py +30 -0
- aiwaf/management/commands/clear_cache.py +18 -0
- aiwaf/management/commands/diagnose_blocking.py +96 -0
- aiwaf/management/commands/setup_models.py +35 -0
- aiwaf/management/commands/test_exemption.py +120 -0
- aiwaf/management/commands/test_exemption_fix.py +54 -0
- aiwaf/middleware.py +28 -16
- aiwaf/middleware_logger.py +66 -106
- aiwaf/models.py +28 -1
- aiwaf/storage.py +166 -360
- aiwaf/trainer.py +0 -12
- {aiwaf-0.1.9.0.4.dist-info → aiwaf-0.1.9.0.6.dist-info}/METADATA +30 -27
- aiwaf-0.1.9.0.6.dist-info/RECORD +32 -0
- aiwaf/management/commands/debug_csv.py +0 -155
- aiwaf-0.1.9.0.4.dist-info/RECORD +0 -27
- {aiwaf-0.1.9.0.4.dist-info → aiwaf-0.1.9.0.6.dist-info}/WHEEL +0 -0
- {aiwaf-0.1.9.0.4.dist-info → aiwaf-0.1.9.0.6.dist-info}/licenses/LICENSE +0 -0
- {aiwaf-0.1.9.0.4.dist-info → aiwaf-0.1.9.0.6.dist-info}/top_level.txt +0 -0
aiwaf/middleware_logger.py
CHANGED
|
@@ -1,44 +1,39 @@
|
|
|
1
1
|
# aiwaf/middleware_logger.py
|
|
2
2
|
|
|
3
|
-
import os
|
|
4
|
-
import csv
|
|
5
3
|
import time
|
|
6
4
|
from datetime import datetime
|
|
7
5
|
from django.conf import settings
|
|
8
6
|
from django.utils.deprecation import MiddlewareMixin
|
|
7
|
+
from django.utils import timezone
|
|
9
8
|
from .utils import get_ip
|
|
10
9
|
|
|
10
|
+
# Defer model imports to avoid AppRegistryNotReady during Django app loading
|
|
11
|
+
RequestLog = None
|
|
12
|
+
|
|
13
|
+
def _import_models():
|
|
14
|
+
"""Import Django models only when needed and apps are ready."""
|
|
15
|
+
global RequestLog
|
|
16
|
+
|
|
17
|
+
if RequestLog is not None:
|
|
18
|
+
return # Already imported
|
|
19
|
+
|
|
20
|
+
try:
|
|
21
|
+
from django.apps import apps
|
|
22
|
+
if apps.ready and apps.is_installed('aiwaf'):
|
|
23
|
+
from .models import RequestLog
|
|
24
|
+
except (ImportError, RuntimeError, Exception):
|
|
25
|
+
# Keep models as None if can't import
|
|
26
|
+
pass
|
|
27
|
+
|
|
11
28
|
class AIWAFLoggerMiddleware(MiddlewareMixin):
|
|
12
29
|
"""
|
|
13
|
-
Middleware that logs requests to
|
|
30
|
+
Middleware that logs requests to Django models for AI-WAF training.
|
|
14
31
|
Acts as a fallback when main access logs are unavailable.
|
|
15
32
|
"""
|
|
16
33
|
|
|
17
34
|
def __init__(self, get_response):
|
|
18
35
|
super().__init__(get_response)
|
|
19
|
-
self.log_file = getattr(settings, "AIWAF_MIDDLEWARE_LOG", "aiwaf_requests.log")
|
|
20
|
-
self.csv_format = getattr(settings, "AIWAF_MIDDLEWARE_CSV", True)
|
|
21
36
|
self.log_enabled = getattr(settings, "AIWAF_MIDDLEWARE_LOGGING", False)
|
|
22
|
-
|
|
23
|
-
# CSV file path (if using CSV format)
|
|
24
|
-
if self.csv_format and self.log_enabled:
|
|
25
|
-
self.csv_file = self.log_file.replace('.log', '.csv')
|
|
26
|
-
self._ensure_csv_header()
|
|
27
|
-
|
|
28
|
-
def _ensure_csv_header(self):
|
|
29
|
-
"""Ensure CSV file has proper header row"""
|
|
30
|
-
if not os.path.exists(self.csv_file):
|
|
31
|
-
# Create directory if it doesn't exist
|
|
32
|
-
csv_dir = os.path.dirname(self.csv_file)
|
|
33
|
-
if csv_dir and not os.path.exists(csv_dir):
|
|
34
|
-
os.makedirs(csv_dir, exist_ok=True)
|
|
35
|
-
|
|
36
|
-
with open(self.csv_file, 'w', newline='', encoding='utf-8') as f:
|
|
37
|
-
writer = csv.writer(f)
|
|
38
|
-
writer.writerow([
|
|
39
|
-
'timestamp', 'ip_address', 'method', 'path', 'status_code',
|
|
40
|
-
'response_time', 'user_agent', 'referer', 'content_length'
|
|
41
|
-
])
|
|
42
37
|
|
|
43
38
|
def process_request(self, request):
|
|
44
39
|
"""Store request start time"""
|
|
@@ -46,7 +41,7 @@ class AIWAFLoggerMiddleware(MiddlewareMixin):
|
|
|
46
41
|
return None
|
|
47
42
|
|
|
48
43
|
def process_response(self, request, response):
|
|
49
|
-
"""Log the completed request"""
|
|
44
|
+
"""Log the completed request to Django model"""
|
|
50
45
|
if not self.log_enabled:
|
|
51
46
|
return response
|
|
52
47
|
|
|
@@ -54,116 +49,81 @@ class AIWAFLoggerMiddleware(MiddlewareMixin):
|
|
|
54
49
|
start_time = getattr(request, '_aiwaf_start_time', time.time())
|
|
55
50
|
response_time = time.time() - start_time
|
|
56
51
|
|
|
57
|
-
#
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
52
|
+
# Import models and log to database
|
|
53
|
+
_import_models()
|
|
54
|
+
if RequestLog is not None:
|
|
55
|
+
try:
|
|
56
|
+
RequestLog.objects.create(
|
|
57
|
+
ip_address=get_ip(request),
|
|
58
|
+
method=request.method,
|
|
59
|
+
path=request.path[:500], # Truncate long paths
|
|
60
|
+
status_code=response.status_code,
|
|
61
|
+
response_time=response_time,
|
|
62
|
+
user_agent=request.META.get('HTTP_USER_AGENT', '')[:2000], # Truncate long user agents
|
|
63
|
+
referer=request.META.get('HTTP_REFERER', '')[:500], # Truncate long referers
|
|
64
|
+
content_length=response.get('Content-Length', '-'),
|
|
65
|
+
timestamp=timezone.now()
|
|
66
|
+
)
|
|
67
|
+
except Exception as e:
|
|
68
|
+
# Fail silently to avoid breaking the application
|
|
69
|
+
pass
|
|
74
70
|
|
|
75
71
|
return response
|
|
76
|
-
|
|
77
|
-
def _log_to_csv(self, data):
|
|
78
|
-
"""Write log entry to CSV file"""
|
|
79
|
-
try:
|
|
80
|
-
# Ensure directory exists before writing
|
|
81
|
-
csv_dir = os.path.dirname(self.csv_file)
|
|
82
|
-
if csv_dir and not os.path.exists(csv_dir):
|
|
83
|
-
os.makedirs(csv_dir, exist_ok=True)
|
|
84
|
-
|
|
85
|
-
with open(self.csv_file, 'a', newline='', encoding='utf-8') as f:
|
|
86
|
-
writer = csv.writer(f)
|
|
87
|
-
writer.writerow([
|
|
88
|
-
data['timestamp'], data['ip_address'], data['method'],
|
|
89
|
-
data['path'], data['status_code'], data['response_time'],
|
|
90
|
-
data['user_agent'], data['referer'], data['content_length']
|
|
91
|
-
])
|
|
92
|
-
except Exception as e:
|
|
93
|
-
# Fail silently to avoid breaking the application
|
|
94
|
-
pass
|
|
95
|
-
|
|
96
|
-
def _log_to_text(self, data):
|
|
97
|
-
"""Write log entry in common log format"""
|
|
98
|
-
try:
|
|
99
|
-
# Common Log Format with response time
|
|
100
|
-
log_line = f'{data["ip_address"]} - - [{data["timestamp"]}] "{data["method"]} {data["path"]} HTTP/1.1" {data["status_code"]} {data["content_length"]} "{data["referer"]}" "{data["user_agent"]}" response-time={data["response_time"]}\n'
|
|
101
|
-
|
|
102
|
-
with open(self.log_file, 'a', encoding='utf-8') as f:
|
|
103
|
-
f.write(log_line)
|
|
104
|
-
except Exception as e:
|
|
105
|
-
# Fail silently to avoid breaking the application
|
|
106
|
-
pass
|
|
107
72
|
|
|
108
73
|
|
|
109
|
-
class
|
|
74
|
+
class AIWAFModelLogParser:
|
|
110
75
|
"""
|
|
111
|
-
Parser for AI-WAF
|
|
76
|
+
Parser for AI-WAF Django model logs that converts them to the format expected by trainer.py
|
|
112
77
|
"""
|
|
113
78
|
|
|
114
79
|
@staticmethod
|
|
115
|
-
def
|
|
80
|
+
def parse_model_logs():
|
|
116
81
|
"""
|
|
117
|
-
Parse
|
|
82
|
+
Parse Django model logs and return records in the format expected by trainer.py
|
|
118
83
|
Returns list of dictionaries with keys: ip, timestamp, path, status, referer, user_agent, response_time
|
|
119
84
|
"""
|
|
120
85
|
records = []
|
|
121
86
|
|
|
122
|
-
|
|
87
|
+
_import_models()
|
|
88
|
+
if RequestLog is None:
|
|
123
89
|
return records
|
|
124
90
|
|
|
125
91
|
try:
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
'response_time': float(row['response_time'])
|
|
141
|
-
}
|
|
142
|
-
records.append(record)
|
|
143
|
-
except (ValueError, KeyError) as e:
|
|
144
|
-
# Skip malformed rows
|
|
145
|
-
continue
|
|
92
|
+
# Get all request logs
|
|
93
|
+
logs = RequestLog.objects.all().order_by('-timestamp')
|
|
94
|
+
|
|
95
|
+
for log in logs:
|
|
96
|
+
record = {
|
|
97
|
+
'ip': str(log.ip_address),
|
|
98
|
+
'timestamp': log.timestamp,
|
|
99
|
+
'path': log.path,
|
|
100
|
+
'status': str(log.status_code),
|
|
101
|
+
'referer': log.referer if log.referer else '-',
|
|
102
|
+
'user_agent': log.user_agent if log.user_agent else '-',
|
|
103
|
+
'response_time': log.response_time
|
|
104
|
+
}
|
|
105
|
+
records.append(record)
|
|
146
106
|
except Exception as e:
|
|
147
|
-
# Return empty list if
|
|
107
|
+
# Return empty list if models can't be accessed
|
|
148
108
|
pass
|
|
149
109
|
|
|
150
110
|
return records
|
|
151
111
|
|
|
152
112
|
@staticmethod
|
|
153
|
-
def get_log_lines_for_trainer(
|
|
113
|
+
def get_log_lines_for_trainer():
|
|
154
114
|
"""
|
|
155
|
-
Convert
|
|
115
|
+
Convert Django model logs to format compatible with trainer.py's _read_all_logs()
|
|
156
116
|
Returns list of log line strings
|
|
157
117
|
"""
|
|
158
|
-
records =
|
|
118
|
+
records = AIWAFModelLogParser.parse_model_logs()
|
|
159
119
|
log_lines = []
|
|
160
120
|
|
|
161
121
|
for record in records:
|
|
162
|
-
# Convert
|
|
122
|
+
# Convert to common log format that trainer.py expects
|
|
163
123
|
timestamp_str = record['timestamp'].strftime('%d/%b/%Y:%H:%M:%S +0000')
|
|
164
|
-
content_length = '-' # We don't track this in
|
|
124
|
+
content_length = '-' # We don't track this in detail
|
|
165
125
|
|
|
166
|
-
log_line = f'{record["ip"]} - - [{timestamp_str}] "GET {record["path"]} HTTP/1.1" {record["status"]} {content_length} "{record["referer"]}" "{record["user_agent"]}" response-time={record["response_time"]:.3f}'
|
|
126
|
+
log_line = f'{record["ip"]} - - [{timestamp_str}] "{record.get("method", "GET")} {record["path"]} HTTP/1.1" {record["status"]} {content_length} "{record["referer"]}" "{record["user_agent"]}" response-time={record["response_time"]:.3f}'
|
|
167
127
|
log_lines.append(log_line)
|
|
168
128
|
|
|
169
129
|
return log_lines
|
aiwaf/models.py
CHANGED
|
@@ -43,4 +43,31 @@ class IPExemption(models.Model):
|
|
|
43
43
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
44
44
|
|
|
45
45
|
def __str__(self):
|
|
46
|
-
return f"{self.ip_address} (Exempted: {self.reason})"
|
|
46
|
+
return f"{self.ip_address} (Exempted: {self.reason})"
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
# Model to store request logs for AI-WAF training
|
|
50
|
+
class RequestLog(models.Model):
|
|
51
|
+
ip_address = models.GenericIPAddressField(db_index=True)
|
|
52
|
+
method = models.CharField(max_length=10)
|
|
53
|
+
path = models.CharField(max_length=500)
|
|
54
|
+
status_code = models.IntegerField()
|
|
55
|
+
response_time = models.FloatField()
|
|
56
|
+
user_agent = models.TextField(blank=True, default="")
|
|
57
|
+
referer = models.CharField(max_length=500, blank=True, default="")
|
|
58
|
+
content_length = models.CharField(max_length=20, blank=True, default="-")
|
|
59
|
+
timestamp = models.DateTimeField(auto_now_add=True)
|
|
60
|
+
|
|
61
|
+
class Meta:
|
|
62
|
+
verbose_name = "Request Log"
|
|
63
|
+
verbose_name_plural = "Request Logs"
|
|
64
|
+
indexes = [
|
|
65
|
+
models.Index(fields=["ip_address"]),
|
|
66
|
+
models.Index(fields=["timestamp"]),
|
|
67
|
+
models.Index(fields=["status_code"]),
|
|
68
|
+
models.Index(fields=["method"]),
|
|
69
|
+
]
|
|
70
|
+
ordering = ['-timestamp']
|
|
71
|
+
|
|
72
|
+
def __str__(self):
|
|
73
|
+
return f"{self.ip_address} {self.method} {self.path} - {self.status_code}"
|