首页 > 数据库技术 > 详细

MySQLdb 防SQL注入,同时打印已执行的SQL

时间:2015-08-01 19:10:57      阅读:334      评论:0      收藏:0      [点我收藏+]

>>> import MySQLdb

>>> conn=MySQLdb.connect(user=‘root‘,passwd=‘root‘)

>>> cur=conn.cursor()

>>> sql = "select user from mysql.user where user=‘%s‘ and password = ‘%s‘ ";

>>> cur.execute(sql % (‘aaa‘,‘aaa‘))

0L

>>> cur.execute(sql % ("aaa","aaa‘ or ‘‘ =‘"))  #SQL注入

3L

>>> cur._executed  #打印刚执行的SQL

"select user from mysql.user where user=‘aaa‘ and password = ‘aaa‘ or ‘‘ =‘‘ "  

>>> cur.fetchall()

((‘root‘,), (‘root‘,), (‘root‘,))

>>> sql="select user from mysql.user where user=%s and password = %s "

>>> cur.execute(sql,("aaa","aaa‘ or ‘‘ =‘"))  #SQL注入失败。将变量作为 execute 的参数传入,execute函数会自动进行转义,需要注意的是,所有的占位符都是%s, %s 两边不需要引号,execute会自动根据你传如参数的类型判断是否添加引号。

0L

>>> cur._executed

"select user from mysql.user where user=‘aaa‘ and password = ‘aaa\\‘ or \\‘\\‘ =\\‘‘ "



MySQLdb 防SQL注入,同时打印已执行的SQL

原文:http://youmeng123.blog.51cto.com/1420398/1680799

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!