django-waf 0.1.0__tar.gz
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.
- django_waf-0.1.0/LICENSE +21 -0
- django_waf-0.1.0/PKG-INFO +263 -0
- django_waf-0.1.0/README.md +223 -0
- django_waf-0.1.0/pyproject.toml +91 -0
- django_waf-0.1.0/setup.cfg +4 -0
- django_waf-0.1.0/src/django_waf.egg-info/PKG-INFO +263 -0
- django_waf-0.1.0/src/django_waf.egg-info/SOURCES.txt +51 -0
- django_waf-0.1.0/src/django_waf.egg-info/dependency_links.txt +1 -0
- django_waf-0.1.0/src/django_waf.egg-info/requires.txt +16 -0
- django_waf-0.1.0/src/django_waf.egg-info/top_level.txt +1 -0
- django_waf-0.1.0/src/icv_waf/__init__.py +5 -0
- django_waf-0.1.0/src/icv_waf/admin.py +306 -0
- django_waf-0.1.0/src/icv_waf/apps.py +12 -0
- django_waf-0.1.0/src/icv_waf/conf.py +84 -0
- django_waf-0.1.0/src/icv_waf/enums.py +69 -0
- django_waf-0.1.0/src/icv_waf/handlers.py +126 -0
- django_waf-0.1.0/src/icv_waf/management/__init__.py +0 -0
- django_waf-0.1.0/src/icv_waf/management/commands/__init__.py +0 -0
- django_waf-0.1.0/src/icv_waf/management/commands/icv_waf_detect_anomalies.py +63 -0
- django_waf-0.1.0/src/icv_waf/management/commands/icv_waf_generate_blocklist.py +80 -0
- django_waf-0.1.0/src/icv_waf/management/commands/icv_waf_prune_logs.py +52 -0
- django_waf-0.1.0/src/icv_waf/management/commands/icv_waf_sync_feed.py +73 -0
- django_waf-0.1.0/src/icv_waf/middleware.py +265 -0
- django_waf-0.1.0/src/icv_waf/migrations/0001_initial.py +569 -0
- django_waf-0.1.0/src/icv_waf/migrations/__init__.py +0 -0
- django_waf-0.1.0/src/icv_waf/models.py +555 -0
- django_waf-0.1.0/src/icv_waf/services/__init__.py +76 -0
- django_waf-0.1.0/src/icv_waf/services/anomaly_detector.py +269 -0
- django_waf-0.1.0/src/icv_waf/services/blocklist_generator.py +158 -0
- django_waf-0.1.0/src/icv_waf/services/challenge_service.py +303 -0
- django_waf-0.1.0/src/icv_waf/services/rate_limiter.py +116 -0
- django_waf-0.1.0/src/icv_waf/services/rule_engine.py +486 -0
- django_waf-0.1.0/src/icv_waf/services/threat_feed.py +329 -0
- django_waf-0.1.0/src/icv_waf/services/ua_analyser.py +136 -0
- django_waf-0.1.0/src/icv_waf/signals.py +36 -0
- django_waf-0.1.0/src/icv_waf/tasks.py +360 -0
- django_waf-0.1.0/src/icv_waf/templates/icv_waf/challenge.html +239 -0
- django_waf-0.1.0/src/icv_waf/templates/icv_waf/dashboard.html +226 -0
- django_waf-0.1.0/src/icv_waf/templates/icv_waf/partials/anomalies_panel.html +64 -0
- django_waf-0.1.0/src/icv_waf/templates/icv_waf/partials/anomaly_row_confirmed.html +25 -0
- django_waf-0.1.0/src/icv_waf/templates/icv_waf/partials/stats_panel.html +27 -0
- django_waf-0.1.0/src/icv_waf/templates/icv_waf/partials/top_blocked_panel.html +29 -0
- django_waf-0.1.0/src/icv_waf/testing/__init__.py +21 -0
- django_waf-0.1.0/src/icv_waf/testing/factories.py +131 -0
- django_waf-0.1.0/src/icv_waf/urls.py +41 -0
- django_waf-0.1.0/src/icv_waf/views.py +414 -0
- django_waf-0.1.0/tests/test_commands.py +418 -0
- django_waf-0.1.0/tests/test_handlers.py +356 -0
- django_waf-0.1.0/tests/test_middleware.py +622 -0
- django_waf-0.1.0/tests/test_models.py +609 -0
- django_waf-0.1.0/tests/test_services.py +2407 -0
- django_waf-0.1.0/tests/test_tasks.py +657 -0
- django_waf-0.1.0/tests/test_views.py +623 -0
django_waf-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nigel Copley
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: django-waf
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Self-hosted request filtering, bot management, and WAF middleware for Django — rate limiting, UA anomaly scoring, JS challenges, nginx blocklist generation, and collective threat feed.
|
|
5
|
+
Author: Nigel Copley
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/nigelcopley/django-waf
|
|
8
|
+
Project-URL: Documentation, https://github.com/nigelcopley/django-waf
|
|
9
|
+
Project-URL: Changelog, https://github.com/nigelcopley/django-waf/blob/main/CHANGELOG.md
|
|
10
|
+
Project-URL: Issue Tracker, https://github.com/nigelcopley/django-waf/issues
|
|
11
|
+
Project-URL: Source Code, https://github.com/nigelcopley/django-waf
|
|
12
|
+
Keywords: django,waf,bot-detection,rate-limiting,security,firewall
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Framework :: Django
|
|
15
|
+
Classifier: Framework :: Django :: 4.2
|
|
16
|
+
Classifier: Framework :: Django :: 5.0
|
|
17
|
+
Classifier: Framework :: Django :: 5.1
|
|
18
|
+
Classifier: Intended Audience :: Developers
|
|
19
|
+
Classifier: Programming Language :: Python :: 3
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Requires-Python: >=3.11
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Requires-Dist: Django>=4.2
|
|
26
|
+
Requires-Dist: django-redis>=5.4
|
|
27
|
+
Requires-Dist: httpx>=0.27
|
|
28
|
+
Provides-Extra: celery
|
|
29
|
+
Requires-Dist: celery>=5.3; extra == "celery"
|
|
30
|
+
Requires-Dist: django-celery-beat>=2.6; extra == "celery"
|
|
31
|
+
Provides-Extra: dev
|
|
32
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
33
|
+
Requires-Dist: pytest-django>=4.8; extra == "dev"
|
|
34
|
+
Requires-Dist: pytest-cov>=5.0; extra == "dev"
|
|
35
|
+
Requires-Dist: factory-boy>=3.3; extra == "dev"
|
|
36
|
+
Requires-Dist: ruff>=0.5.0; extra == "dev"
|
|
37
|
+
Requires-Dist: mypy>=1.10; extra == "dev"
|
|
38
|
+
Requires-Dist: django-stubs>=5.0; extra == "dev"
|
|
39
|
+
Dynamic: license-file
|
|
40
|
+
|
|
41
|
+
# django-waf
|
|
42
|
+
|
|
43
|
+
Self-hosted request filtering, bot management, and WAF middleware for Django.
|
|
44
|
+
|
|
45
|
+
Provides two-layer defence (nginx + Django middleware) with rate limiting,
|
|
46
|
+
user-agent anomaly scoring, JS proof-of-work challenges, nginx blocklist
|
|
47
|
+
generation, and collective threat feed integration — all configurable without
|
|
48
|
+
a reverse-proxy vendor.
|
|
49
|
+
|
|
50
|
+
## Features
|
|
51
|
+
|
|
52
|
+
- **Rate limiting** — sliding-window per-IP limits (burst, per-minute, per-5-min)
|
|
53
|
+
- **UA anomaly scoring** — heuristic detection of impossible OS/browser combos,
|
|
54
|
+
ancient versions, scraper libraries
|
|
55
|
+
- **JS proof-of-work challenges** — hashcash-style SHA-256 challenges for
|
|
56
|
+
suspicious clients (no CAPTCHAs, no third-party dependencies)
|
|
57
|
+
- **nginx blocklist generation** — exports `map`/`geo` blocks for C-level
|
|
58
|
+
filtering at < 0.01 ms latency
|
|
59
|
+
- **Anomaly detection** — auto-creates expiring rules for UA rotation, subnet
|
|
60
|
+
bursts, and challenge farms
|
|
61
|
+
- **Collective threat feed** — opt-in sync of anonymised threat intelligence
|
|
62
|
+
across deployments
|
|
63
|
+
- **Staff dashboard** — HTMX-powered real-time analytics with anomaly management
|
|
64
|
+
- **Fail-open design** — Redis outage never breaks the site
|
|
65
|
+
|
|
66
|
+
## Requirements
|
|
67
|
+
|
|
68
|
+
- Python >= 3.11
|
|
69
|
+
- Django >= 4.2
|
|
70
|
+
- Redis (via `django-redis >= 5.4`)
|
|
71
|
+
- `httpx >= 0.27` (for threat feed sync)
|
|
72
|
+
- Optional: `celery >= 5.3` (for scheduled tasks)
|
|
73
|
+
|
|
74
|
+
## Installation
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
pip install django-waf
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Add to `INSTALLED_APPS`:
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
INSTALLED_APPS = [
|
|
84
|
+
# ...
|
|
85
|
+
"icv_waf",
|
|
86
|
+
]
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Add the middleware — place it **after** `SecurityMiddleware` and **before**
|
|
90
|
+
other middleware so it can block requests early:
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
MIDDLEWARE = [
|
|
94
|
+
"django.middleware.security.SecurityMiddleware",
|
|
95
|
+
"icv_waf.middleware.WafMiddleware", # <-- here
|
|
96
|
+
"django.contrib.sessions.middleware.SessionMiddleware",
|
|
97
|
+
"django.middleware.common.CommonMiddleware",
|
|
98
|
+
# ...
|
|
99
|
+
]
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Include the URL routes for the challenge flow and staff dashboard:
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
# urls.py
|
|
106
|
+
from django.urls import include, path
|
|
107
|
+
|
|
108
|
+
urlpatterns = [
|
|
109
|
+
path("waf/", include("icv_waf.urls")),
|
|
110
|
+
# ...
|
|
111
|
+
]
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Configure a Redis cache backend (required for rate limiting):
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
CACHES = {
|
|
118
|
+
"default": {
|
|
119
|
+
"BACKEND": "django_redis.cache.RedisCache",
|
|
120
|
+
"LOCATION": "redis://127.0.0.1:6379/0",
|
|
121
|
+
"OPTIONS": {
|
|
122
|
+
"CLIENT_CLASS": "django_redis.client.DefaultClient",
|
|
123
|
+
},
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Run migrations:
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
python manage.py migrate icv_waf
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## Settings Reference
|
|
135
|
+
|
|
136
|
+
All settings are namespaced under `ICV_WAF_*` and have sensible defaults.
|
|
137
|
+
|
|
138
|
+
| Setting | Default | Description |
|
|
139
|
+
|---------|---------|-------------|
|
|
140
|
+
| `ICV_WAF_ENABLED` | `True` | Master switch — disable to pass all requests through |
|
|
141
|
+
| `ICV_WAF_EXEMPT_PATHS` | `["/static/", "/media/", "/health/", "/favicon.ico"]` | URL prefixes that bypass WAF evaluation |
|
|
142
|
+
| `ICV_WAF_TRUST_X_FORWARDED_FOR` | `False` | Trust `X-Forwarded-For` header for client IP extraction |
|
|
143
|
+
| `ICV_WAF_REDIS_ALIAS` | `"default"` | Django cache alias for Redis connections |
|
|
144
|
+
| `ICV_WAF_RATE_LIMIT_BURST` | `10` | Max requests per IP per second |
|
|
145
|
+
| `ICV_WAF_RATE_LIMIT_PER_MINUTE` | `120` | Max requests per IP per minute |
|
|
146
|
+
| `ICV_WAF_RATE_LIMIT_PER_5MIN` | `600` | Max requests per IP per 5 minutes |
|
|
147
|
+
| `ICV_WAF_CHALLENGE_DIFFICULTY` | `4` | Proof-of-work leading zero bits |
|
|
148
|
+
| `ICV_WAF_CHALLENGE_COOKIE_TTL` | `86400` | Seconds a solved-challenge cookie remains valid |
|
|
149
|
+
| `ICV_WAF_LOG_SAMPLE_RATE` | `0.01` | Fraction of allowed requests to log (0.0–1.0) |
|
|
150
|
+
| `ICV_WAF_LOG_RETENTION_DAYS` | `30` | Days to retain RequestLog entries |
|
|
151
|
+
| `ICV_WAF_ANOMALY_THRESHOLD_DISTINCT_UAS` | `20` | Distinct UAs per IP before triggering anomaly |
|
|
152
|
+
| `ICV_WAF_AUTO_RULE_EXPIRY_HOURS` | `24` | Hours before auto-generated rules expire |
|
|
153
|
+
| `ICV_WAF_NGINX_BLOCKLIST_PATH` | `"/etc/nginx/conf.d/icv-waf-blocklist.conf"` | Output path for nginx blocklist |
|
|
154
|
+
| `ICV_WAF_ACCESS_LOG_PATH` | `"/var/log/nginx/access.log"` | nginx access log path for parsing |
|
|
155
|
+
| `ICV_WAF_FEED_ENABLED` | `True` | Enable collective threat feed sync |
|
|
156
|
+
| `ICV_WAF_FEED_URL` | `"https://threats.icv.dev/v1/feed.json"` | Threat feed JSON endpoint |
|
|
157
|
+
| `ICV_WAF_FEED_MIN_CONFIDENCE` | `0.8` | Minimum confidence to import feed rules |
|
|
158
|
+
| `ICV_WAF_FEED_REPORT` | `False` | Report local detections back to feed (opt-in) |
|
|
159
|
+
| `ICV_WAF_FEED_REPORT_URL` | `"https://threats.icv.dev/v1/report"` | Telemetry reporting endpoint |
|
|
160
|
+
| `ICV_WAF_FEED_API_KEY` | `""` | API key for feed authentication |
|
|
161
|
+
|
|
162
|
+
## Celery Beat Schedule
|
|
163
|
+
|
|
164
|
+
If using Celery, configure the beat schedule for automated tasks:
|
|
165
|
+
|
|
166
|
+
```python
|
|
167
|
+
from celery.schedules import crontab
|
|
168
|
+
|
|
169
|
+
CELERY_BEAT_SCHEDULE = {
|
|
170
|
+
"icv-waf-generate-blocklist": {
|
|
171
|
+
"task": "icv_waf.tasks.generate_blocklist",
|
|
172
|
+
"schedule": crontab(minute="*/5"),
|
|
173
|
+
},
|
|
174
|
+
"icv-waf-detect-anomalies": {
|
|
175
|
+
"task": "icv_waf.tasks.detect_anomalies",
|
|
176
|
+
"schedule": crontab(minute="*/15"),
|
|
177
|
+
},
|
|
178
|
+
"icv-waf-parse-access-log": {
|
|
179
|
+
"task": "icv_waf.tasks.parse_access_log",
|
|
180
|
+
"schedule": crontab(minute="*/10"),
|
|
181
|
+
},
|
|
182
|
+
"icv-waf-expire-rules": {
|
|
183
|
+
"task": "icv_waf.tasks.expire_rules",
|
|
184
|
+
"schedule": crontab(minute="*/30"),
|
|
185
|
+
},
|
|
186
|
+
"icv-waf-update-ip-reputation": {
|
|
187
|
+
"task": "icv_waf.tasks.update_ip_reputation",
|
|
188
|
+
"schedule": crontab(hour="*/6", minute=0),
|
|
189
|
+
},
|
|
190
|
+
"icv-waf-prune-request-logs": {
|
|
191
|
+
"task": "icv_waf.tasks.prune_request_logs",
|
|
192
|
+
"schedule": crontab(hour=4, minute=0),
|
|
193
|
+
},
|
|
194
|
+
"icv-waf-sync-threat-feed": {
|
|
195
|
+
"task": "icv_waf.tasks.sync_threat_feed",
|
|
196
|
+
"schedule": crontab(hour=4, minute=30),
|
|
197
|
+
},
|
|
198
|
+
"icv-waf-report-threat-telemetry": {
|
|
199
|
+
"task": "icv_waf.tasks.report_threat_telemetry",
|
|
200
|
+
"schedule": crontab(hour=5, minute=0),
|
|
201
|
+
},
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
## Management Commands
|
|
206
|
+
|
|
207
|
+
| Command | Description |
|
|
208
|
+
|---------|-------------|
|
|
209
|
+
| `icv_waf_generate_blocklist` | Generate the nginx blocklist file (`--dry-run` to preview) |
|
|
210
|
+
| `icv_waf_detect_anomalies` | Run anomaly detectors and auto-create block rules (`--dry-run`) |
|
|
211
|
+
| `icv_waf_prune_logs` | Delete RequestLog entries older than retention period (`--dry-run`) |
|
|
212
|
+
| `icv_waf_sync_feed` | Fetch and import rules from the collective threat feed (`--dry-run`) |
|
|
213
|
+
|
|
214
|
+
## Dashboard
|
|
215
|
+
|
|
216
|
+
The staff dashboard is available at `/waf/dashboard/` for authenticated staff
|
|
217
|
+
users. It provides:
|
|
218
|
+
|
|
219
|
+
- Real-time traffic counters (allowed, blocked, challenged, throttled)
|
|
220
|
+
- Top 10 blocked IPs
|
|
221
|
+
- Auto-detected anomalies awaiting review
|
|
222
|
+
|
|
223
|
+
Superusers can **confirm** auto-generated rules (promoting them to permanent) or
|
|
224
|
+
**reject** them (deactivating) directly from the anomalies panel.
|
|
225
|
+
|
|
226
|
+
## Architecture
|
|
227
|
+
|
|
228
|
+
```
|
|
229
|
+
Client → nginx (C-level blocklist, < 0.01 ms)
|
|
230
|
+
→ Django WafMiddleware (dynamic analysis, < 0.5 ms)
|
|
231
|
+
→ Application views
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
The middleware evaluates requests in this order:
|
|
235
|
+
|
|
236
|
+
1. Exempt paths bypass
|
|
237
|
+
2. Master switch check
|
|
238
|
+
3. Staff/superuser bypass
|
|
239
|
+
4. Valid challenge cookie check
|
|
240
|
+
5. Allow rules → Block rules → Rate limits → UA scoring
|
|
241
|
+
6. Verdict dispatch (allow / block / challenge / throttle)
|
|
242
|
+
7. Sampled logging + signal emission
|
|
243
|
+
|
|
244
|
+
## Development
|
|
245
|
+
|
|
246
|
+
```bash
|
|
247
|
+
# Run tests
|
|
248
|
+
pytest
|
|
249
|
+
|
|
250
|
+
# Run tests with coverage
|
|
251
|
+
pytest --cov=src --cov-report=term-missing
|
|
252
|
+
|
|
253
|
+
# Lint
|
|
254
|
+
ruff check src/ tests/
|
|
255
|
+
ruff format src/ tests/
|
|
256
|
+
|
|
257
|
+
# Type check
|
|
258
|
+
mypy src/
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
## Licence
|
|
262
|
+
|
|
263
|
+
MIT
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
# django-waf
|
|
2
|
+
|
|
3
|
+
Self-hosted request filtering, bot management, and WAF middleware for Django.
|
|
4
|
+
|
|
5
|
+
Provides two-layer defence (nginx + Django middleware) with rate limiting,
|
|
6
|
+
user-agent anomaly scoring, JS proof-of-work challenges, nginx blocklist
|
|
7
|
+
generation, and collective threat feed integration — all configurable without
|
|
8
|
+
a reverse-proxy vendor.
|
|
9
|
+
|
|
10
|
+
## Features
|
|
11
|
+
|
|
12
|
+
- **Rate limiting** — sliding-window per-IP limits (burst, per-minute, per-5-min)
|
|
13
|
+
- **UA anomaly scoring** — heuristic detection of impossible OS/browser combos,
|
|
14
|
+
ancient versions, scraper libraries
|
|
15
|
+
- **JS proof-of-work challenges** — hashcash-style SHA-256 challenges for
|
|
16
|
+
suspicious clients (no CAPTCHAs, no third-party dependencies)
|
|
17
|
+
- **nginx blocklist generation** — exports `map`/`geo` blocks for C-level
|
|
18
|
+
filtering at < 0.01 ms latency
|
|
19
|
+
- **Anomaly detection** — auto-creates expiring rules for UA rotation, subnet
|
|
20
|
+
bursts, and challenge farms
|
|
21
|
+
- **Collective threat feed** — opt-in sync of anonymised threat intelligence
|
|
22
|
+
across deployments
|
|
23
|
+
- **Staff dashboard** — HTMX-powered real-time analytics with anomaly management
|
|
24
|
+
- **Fail-open design** — Redis outage never breaks the site
|
|
25
|
+
|
|
26
|
+
## Requirements
|
|
27
|
+
|
|
28
|
+
- Python >= 3.11
|
|
29
|
+
- Django >= 4.2
|
|
30
|
+
- Redis (via `django-redis >= 5.4`)
|
|
31
|
+
- `httpx >= 0.27` (for threat feed sync)
|
|
32
|
+
- Optional: `celery >= 5.3` (for scheduled tasks)
|
|
33
|
+
|
|
34
|
+
## Installation
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
pip install django-waf
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Add to `INSTALLED_APPS`:
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
INSTALLED_APPS = [
|
|
44
|
+
# ...
|
|
45
|
+
"icv_waf",
|
|
46
|
+
]
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Add the middleware — place it **after** `SecurityMiddleware` and **before**
|
|
50
|
+
other middleware so it can block requests early:
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
MIDDLEWARE = [
|
|
54
|
+
"django.middleware.security.SecurityMiddleware",
|
|
55
|
+
"icv_waf.middleware.WafMiddleware", # <-- here
|
|
56
|
+
"django.contrib.sessions.middleware.SessionMiddleware",
|
|
57
|
+
"django.middleware.common.CommonMiddleware",
|
|
58
|
+
# ...
|
|
59
|
+
]
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Include the URL routes for the challenge flow and staff dashboard:
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
# urls.py
|
|
66
|
+
from django.urls import include, path
|
|
67
|
+
|
|
68
|
+
urlpatterns = [
|
|
69
|
+
path("waf/", include("icv_waf.urls")),
|
|
70
|
+
# ...
|
|
71
|
+
]
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Configure a Redis cache backend (required for rate limiting):
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
CACHES = {
|
|
78
|
+
"default": {
|
|
79
|
+
"BACKEND": "django_redis.cache.RedisCache",
|
|
80
|
+
"LOCATION": "redis://127.0.0.1:6379/0",
|
|
81
|
+
"OPTIONS": {
|
|
82
|
+
"CLIENT_CLASS": "django_redis.client.DefaultClient",
|
|
83
|
+
},
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Run migrations:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
python manage.py migrate icv_waf
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Settings Reference
|
|
95
|
+
|
|
96
|
+
All settings are namespaced under `ICV_WAF_*` and have sensible defaults.
|
|
97
|
+
|
|
98
|
+
| Setting | Default | Description |
|
|
99
|
+
|---------|---------|-------------|
|
|
100
|
+
| `ICV_WAF_ENABLED` | `True` | Master switch — disable to pass all requests through |
|
|
101
|
+
| `ICV_WAF_EXEMPT_PATHS` | `["/static/", "/media/", "/health/", "/favicon.ico"]` | URL prefixes that bypass WAF evaluation |
|
|
102
|
+
| `ICV_WAF_TRUST_X_FORWARDED_FOR` | `False` | Trust `X-Forwarded-For` header for client IP extraction |
|
|
103
|
+
| `ICV_WAF_REDIS_ALIAS` | `"default"` | Django cache alias for Redis connections |
|
|
104
|
+
| `ICV_WAF_RATE_LIMIT_BURST` | `10` | Max requests per IP per second |
|
|
105
|
+
| `ICV_WAF_RATE_LIMIT_PER_MINUTE` | `120` | Max requests per IP per minute |
|
|
106
|
+
| `ICV_WAF_RATE_LIMIT_PER_5MIN` | `600` | Max requests per IP per 5 minutes |
|
|
107
|
+
| `ICV_WAF_CHALLENGE_DIFFICULTY` | `4` | Proof-of-work leading zero bits |
|
|
108
|
+
| `ICV_WAF_CHALLENGE_COOKIE_TTL` | `86400` | Seconds a solved-challenge cookie remains valid |
|
|
109
|
+
| `ICV_WAF_LOG_SAMPLE_RATE` | `0.01` | Fraction of allowed requests to log (0.0–1.0) |
|
|
110
|
+
| `ICV_WAF_LOG_RETENTION_DAYS` | `30` | Days to retain RequestLog entries |
|
|
111
|
+
| `ICV_WAF_ANOMALY_THRESHOLD_DISTINCT_UAS` | `20` | Distinct UAs per IP before triggering anomaly |
|
|
112
|
+
| `ICV_WAF_AUTO_RULE_EXPIRY_HOURS` | `24` | Hours before auto-generated rules expire |
|
|
113
|
+
| `ICV_WAF_NGINX_BLOCKLIST_PATH` | `"/etc/nginx/conf.d/icv-waf-blocklist.conf"` | Output path for nginx blocklist |
|
|
114
|
+
| `ICV_WAF_ACCESS_LOG_PATH` | `"/var/log/nginx/access.log"` | nginx access log path for parsing |
|
|
115
|
+
| `ICV_WAF_FEED_ENABLED` | `True` | Enable collective threat feed sync |
|
|
116
|
+
| `ICV_WAF_FEED_URL` | `"https://threats.icv.dev/v1/feed.json"` | Threat feed JSON endpoint |
|
|
117
|
+
| `ICV_WAF_FEED_MIN_CONFIDENCE` | `0.8` | Minimum confidence to import feed rules |
|
|
118
|
+
| `ICV_WAF_FEED_REPORT` | `False` | Report local detections back to feed (opt-in) |
|
|
119
|
+
| `ICV_WAF_FEED_REPORT_URL` | `"https://threats.icv.dev/v1/report"` | Telemetry reporting endpoint |
|
|
120
|
+
| `ICV_WAF_FEED_API_KEY` | `""` | API key for feed authentication |
|
|
121
|
+
|
|
122
|
+
## Celery Beat Schedule
|
|
123
|
+
|
|
124
|
+
If using Celery, configure the beat schedule for automated tasks:
|
|
125
|
+
|
|
126
|
+
```python
|
|
127
|
+
from celery.schedules import crontab
|
|
128
|
+
|
|
129
|
+
CELERY_BEAT_SCHEDULE = {
|
|
130
|
+
"icv-waf-generate-blocklist": {
|
|
131
|
+
"task": "icv_waf.tasks.generate_blocklist",
|
|
132
|
+
"schedule": crontab(minute="*/5"),
|
|
133
|
+
},
|
|
134
|
+
"icv-waf-detect-anomalies": {
|
|
135
|
+
"task": "icv_waf.tasks.detect_anomalies",
|
|
136
|
+
"schedule": crontab(minute="*/15"),
|
|
137
|
+
},
|
|
138
|
+
"icv-waf-parse-access-log": {
|
|
139
|
+
"task": "icv_waf.tasks.parse_access_log",
|
|
140
|
+
"schedule": crontab(minute="*/10"),
|
|
141
|
+
},
|
|
142
|
+
"icv-waf-expire-rules": {
|
|
143
|
+
"task": "icv_waf.tasks.expire_rules",
|
|
144
|
+
"schedule": crontab(minute="*/30"),
|
|
145
|
+
},
|
|
146
|
+
"icv-waf-update-ip-reputation": {
|
|
147
|
+
"task": "icv_waf.tasks.update_ip_reputation",
|
|
148
|
+
"schedule": crontab(hour="*/6", minute=0),
|
|
149
|
+
},
|
|
150
|
+
"icv-waf-prune-request-logs": {
|
|
151
|
+
"task": "icv_waf.tasks.prune_request_logs",
|
|
152
|
+
"schedule": crontab(hour=4, minute=0),
|
|
153
|
+
},
|
|
154
|
+
"icv-waf-sync-threat-feed": {
|
|
155
|
+
"task": "icv_waf.tasks.sync_threat_feed",
|
|
156
|
+
"schedule": crontab(hour=4, minute=30),
|
|
157
|
+
},
|
|
158
|
+
"icv-waf-report-threat-telemetry": {
|
|
159
|
+
"task": "icv_waf.tasks.report_threat_telemetry",
|
|
160
|
+
"schedule": crontab(hour=5, minute=0),
|
|
161
|
+
},
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## Management Commands
|
|
166
|
+
|
|
167
|
+
| Command | Description |
|
|
168
|
+
|---------|-------------|
|
|
169
|
+
| `icv_waf_generate_blocklist` | Generate the nginx blocklist file (`--dry-run` to preview) |
|
|
170
|
+
| `icv_waf_detect_anomalies` | Run anomaly detectors and auto-create block rules (`--dry-run`) |
|
|
171
|
+
| `icv_waf_prune_logs` | Delete RequestLog entries older than retention period (`--dry-run`) |
|
|
172
|
+
| `icv_waf_sync_feed` | Fetch and import rules from the collective threat feed (`--dry-run`) |
|
|
173
|
+
|
|
174
|
+
## Dashboard
|
|
175
|
+
|
|
176
|
+
The staff dashboard is available at `/waf/dashboard/` for authenticated staff
|
|
177
|
+
users. It provides:
|
|
178
|
+
|
|
179
|
+
- Real-time traffic counters (allowed, blocked, challenged, throttled)
|
|
180
|
+
- Top 10 blocked IPs
|
|
181
|
+
- Auto-detected anomalies awaiting review
|
|
182
|
+
|
|
183
|
+
Superusers can **confirm** auto-generated rules (promoting them to permanent) or
|
|
184
|
+
**reject** them (deactivating) directly from the anomalies panel.
|
|
185
|
+
|
|
186
|
+
## Architecture
|
|
187
|
+
|
|
188
|
+
```
|
|
189
|
+
Client → nginx (C-level blocklist, < 0.01 ms)
|
|
190
|
+
→ Django WafMiddleware (dynamic analysis, < 0.5 ms)
|
|
191
|
+
→ Application views
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
The middleware evaluates requests in this order:
|
|
195
|
+
|
|
196
|
+
1. Exempt paths bypass
|
|
197
|
+
2. Master switch check
|
|
198
|
+
3. Staff/superuser bypass
|
|
199
|
+
4. Valid challenge cookie check
|
|
200
|
+
5. Allow rules → Block rules → Rate limits → UA scoring
|
|
201
|
+
6. Verdict dispatch (allow / block / challenge / throttle)
|
|
202
|
+
7. Sampled logging + signal emission
|
|
203
|
+
|
|
204
|
+
## Development
|
|
205
|
+
|
|
206
|
+
```bash
|
|
207
|
+
# Run tests
|
|
208
|
+
pytest
|
|
209
|
+
|
|
210
|
+
# Run tests with coverage
|
|
211
|
+
pytest --cov=src --cov-report=term-missing
|
|
212
|
+
|
|
213
|
+
# Lint
|
|
214
|
+
ruff check src/ tests/
|
|
215
|
+
ruff format src/ tests/
|
|
216
|
+
|
|
217
|
+
# Type check
|
|
218
|
+
mypy src/
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## Licence
|
|
222
|
+
|
|
223
|
+
MIT
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "django-waf"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Self-hosted request filtering, bot management, and WAF middleware for Django — rate limiting, UA anomaly scoring, JS challenges, nginx blocklist generation, and collective threat feed."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.11"
|
|
12
|
+
authors = [{name = "Nigel Copley"}]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 3 - Alpha",
|
|
15
|
+
"Framework :: Django",
|
|
16
|
+
"Framework :: Django :: 4.2",
|
|
17
|
+
"Framework :: Django :: 5.0",
|
|
18
|
+
"Framework :: Django :: 5.1",
|
|
19
|
+
"Intended Audience :: Developers",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3.11",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
]
|
|
24
|
+
keywords = [
|
|
25
|
+
"django",
|
|
26
|
+
"waf",
|
|
27
|
+
"bot-detection",
|
|
28
|
+
"rate-limiting",
|
|
29
|
+
"security",
|
|
30
|
+
"firewall",
|
|
31
|
+
]
|
|
32
|
+
dependencies = [
|
|
33
|
+
"Django>=4.2",
|
|
34
|
+
"django-redis>=5.4",
|
|
35
|
+
"httpx>=0.27",
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
[project.urls]
|
|
39
|
+
Homepage = "https://github.com/nigelcopley/django-waf"
|
|
40
|
+
Documentation = "https://github.com/nigelcopley/django-waf"
|
|
41
|
+
Changelog = "https://github.com/nigelcopley/django-waf/blob/main/CHANGELOG.md"
|
|
42
|
+
"Issue Tracker" = "https://github.com/nigelcopley/django-waf/issues"
|
|
43
|
+
"Source Code" = "https://github.com/nigelcopley/django-waf"
|
|
44
|
+
|
|
45
|
+
[project.optional-dependencies]
|
|
46
|
+
celery = ["celery>=5.3", "django-celery-beat>=2.6"]
|
|
47
|
+
dev = [
|
|
48
|
+
"pytest>=8.0",
|
|
49
|
+
"pytest-django>=4.8",
|
|
50
|
+
"pytest-cov>=5.0",
|
|
51
|
+
"factory-boy>=3.3",
|
|
52
|
+
"ruff>=0.5.0",
|
|
53
|
+
"mypy>=1.10",
|
|
54
|
+
"django-stubs>=5.0",
|
|
55
|
+
]
|
|
56
|
+
|
|
57
|
+
[tool.setuptools.packages.find]
|
|
58
|
+
where = ["src"]
|
|
59
|
+
|
|
60
|
+
[tool.setuptools.package-data]
|
|
61
|
+
icv_waf = ["templates/**/*.html"]
|
|
62
|
+
|
|
63
|
+
[tool.pytest.ini_options]
|
|
64
|
+
DJANGO_SETTINGS_MODULE = "tests.settings"
|
|
65
|
+
pythonpath = ["."]
|
|
66
|
+
|
|
67
|
+
[tool.ruff]
|
|
68
|
+
target-version = "py311"
|
|
69
|
+
line-length = 120
|
|
70
|
+
|
|
71
|
+
[tool.ruff.lint]
|
|
72
|
+
select = ["E", "F", "I", "UP", "B", "SIM", "S"]
|
|
73
|
+
ignore = ["S101", "S105", "S106", "S107", "S110", "S311", "S607"]
|
|
74
|
+
|
|
75
|
+
[tool.ruff.lint.per-file-ignores]
|
|
76
|
+
"tests/**" = ["S"]
|
|
77
|
+
|
|
78
|
+
[tool.mypy]
|
|
79
|
+
python_version = "3.11"
|
|
80
|
+
plugins = ["mypy_django_plugin.main"]
|
|
81
|
+
strict = false
|
|
82
|
+
|
|
83
|
+
[tool.django-stubs]
|
|
84
|
+
django_settings_module = "tests.settings"
|
|
85
|
+
|
|
86
|
+
[tool.coverage.run]
|
|
87
|
+
source = ["src"]
|
|
88
|
+
omit = ["*/migrations/*", "*/testing/*"]
|
|
89
|
+
|
|
90
|
+
[tool.coverage.report]
|
|
91
|
+
fail_under = 90
|