-
sql 注入攻击
01.png
- b/s架构网站要对所有表单提交作数据有效性验证
- 拿用户登录来测试 sql 注入
- 在 apps/users/views.py 中,定义LoginUnSafeView 类视图来测试不用
django 提供的用户登录验证方法,而是手写一个简单的登录逻辑,不对数据进行验证
# apps/users/views.py
class LoginUnSafeView(View):
"""
不安全的登录类视图测试,
不使用 django 的 login 来验证数据,
"""
def get(self, request):
context = {}
return render(request, "login.html", context)
def post(self, request):
user_name = request.POST.get("username", "")
user_password = request.POST.get("password", "")
import pymysql
conn = pymysql.connect(host="127.0.0.1", port=3306, user="pythonic", passwd="pythonic", db="mxonline", charset="utf8")
cursor = conn.cursor()
sql_select = "select * from users_userprofile where email='{0}' and password='{1}'".format(user_name, user_password)
result = cursor.execute(sql_select)
for row in cursor.fetchall():
# 查询到用户, 执行用户登录的逻辑
pass
# 未查询到用户, 执行的代码
print("test")
- 配置 url
# apps/users/urls.py
from users.views import LoginUnSafeView
urlpatterns = [
...
path("login/", LoginUnSafeView.as_view(), name="login"),
]
-
输入数据库中存在的用户,正常流程可以进入用户登录逻辑
02.png
-
输入数据库中不存在的用户,不能进入用户登录逻辑
04.png
-
输入非法数据,可以绕过正常逻辑,进行sql注入,完成非法用户登录
06.png
- 总结,django 开发中,所有表单验证数据能用 django 内置方法验证的尽量采用 django 自带的验证方法,保证数据的安全有效性
-
xss 攻击
08.png
-
正常流程是用户访问网址传递参数 ?name='iphone6',但如果不做防范,黑客在参数中传递一段 js 脚本,可以做非法操作
09.png
-
csrf 攻击
12.png