MySQLdb 防SQL注入,同时打印已执行的SQL
2015-08-01 14:57
615 查看
>>> 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 \\'\\' =\\'' "
>>> 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 \\'\\' =\\'' "
相关文章推荐
- mysql字符串查找(统计客源)
- mysql 日期格式化查询
- MyCli:支持自动补全和语法高亮的 MySQL 客户端
- mysql存储过程学习笔记
- 如何正确合理的建立MYSQL数据库索引
- mysql5.6新特性总结
- 新安装的MySQL必须调整的10项配置
- MySQL常用的索引
- mysql 保存表情符号
- MYSQL索引结构原理、性能分析与优化
- MySQL优化
- mysql 无法启动,错误1067,进程意外终止(特殊解决方法)
- mysql授权 远程访问授权
- MySQL实现操作表
- MySQL 5.6.13 解压版(zip版)安装配置方法
- MySQL中基本的用户和权限管理方法小结
- 操作MySql
- Mysql之查询、where、常用函数、having
- Mysql 5.7.7
- Mysql字符集以及校对规则