温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

9Python全站之路系列之MySQL SL注入

发布时间:2020-08-25 13:49:57 来源:网络 阅读:480 作者:Adlereden 栏目:MySQL数据库

Python全栈之路系列之MySQL SQL注入


SQL注入是一种代码注入技术,过去常常用于***数据驱动性的应用,比如将恶意的SQL代码注入到特定字段用于实施******等。

SQL注入的成功必须借助应用程序的安全漏洞,例如用户输入没有经过正确地过滤(针对某些特定字符串)或者没有特别强调类型的时候,都容易造成异常地执行SQL语句。

SQL注入是网站***中最常用的***技术,但是其实SQL注入可以用来***所有的SQL数据库。


SQL注入的实现

创建SQLdb数据库

CREATE DATABASE SQLdb;

创建user_info

CREATE TABLE `user_info` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(32) DEFAULT NULL,
  `password` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

插入一条用户数据

测试的用户名是ansheng,密码as

insert into user_info(username,password) values("ansheng","as");

Python代码

app.py文件

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import tornado.ioloop
import tornado.web
import pymysql

class LoginHandler(tornado.web.RequestHandler):
    def get(self, *args, **kwargs):
        self.render('login.html')
    def post(self, *args, **kwargs):
        username = self.get_argument('username', None)
        pwd = self.get_argument('pwd', None)
        conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='as', db='sqldb')
        cursor = conn.cursor()
        temp = "select username from user_info where username='%s' and password = '%s'" %(username, pwd,)
        effect_row = cursor.execute(temp)
        result = cursor.fetchone()
        conn.commit()
        cursor.close()
        conn.close()
        if result:
            self.write('登录成功')
        else:
            self.write('登录失败')
            
application = tornado.web.Application([
    (r"/login", LoginHandler),
])


if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

HTML代码

login.htmlapp.py文件在同级

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/login" method="post">
    <input type="text" name="username" placeholder="用户名" />
    <input type="text" name="pwd" placeholder="密码" />
    <input type="submit" />
</form>
</body>
</html>

演示效果

打开浏览器,输入地址http://127.0.0.1:8888/login

填写内容如下:

用户名:asas ' or 1 = 1-- asd
密码:随便填写一串字母

如图:

9Python全站之路系列之MySQL SL注入

当点击提交的时候是否会跳转到登陆成功页面?如果你的代码和我一样,那么就会跳转到登陆成页面

为什么出现这种问题?

出现这个问题的主要原因就是因为我们使用了字符串拼接的方式来进行SQL指令的拼接。

SQL指令拼接代码

temp = "select username from user_info where username='%s' and password = '%s'" %(username, pwd,)

这是一个正常的SQL拼接出来的结果

select username from user_info where username='ansheng' and password = 'as'

这是一个非正常的SQL拼接出来的结果

select username from user_info where username='asas' or 1 = 1  -- asd' and password = 's'

聪明的你是否已经看到其中的玄机了呢?--

如何防止?

通过Pythonpymysql模块来进行SQL的执行,在pymysql模块内部会自动把”'“(单引号做一个特殊的处理,来预防上述的错误

......
effect_row = cursor.execute("select username from user_info where username='%s' and password = '%s'", (username, pwd))
......

#Python全栈之路 #Sql注入


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI