Python预编译语句防止SQL注入

"""
@author: zhangjun.xue
@time: 2019/12/18 22:20
@file: xue_test.py
@desc: 
"""

# Python预编译语句防止SQL注入

# 错误用法:
# 1 sql = "select id,type,name from xl_bugs where id = %s and type = %s" % (id, type)
# 2 cur.execute(sql)

# 这种用法就是常见的拼接字符串导致sql注入漏洞的产生。
# 看到这个突然想到上个礼拜drupal水滴的那个漏洞,其并不是预编译语句被绕过了。
# 而是在构造带入的预编译语句的时候拼接了用户输入字符串,还未带入查询的预编译语句已经被注入了,之后带入正确的参数,最后被注入了

# 正确用法:
# execute() 函数本身有接受sql语句参数位的,可以通过python自身的函数处理sql注入问题。
# 1 args = (id, type)
# 2 cur.execute('select id, type ,name from xl_bugs where id = %s and type = %s', args )
# 使用如此参数带入方式,python会自动过滤args中的特殊字符,制止SQL注入的产生。


# execute()函数本身就有接受SQL语句变量的参数位,只要正确的使用(直白一点就是:使用”逗号”,而不是”百分号”)就可以对传入的值进行correctly转义,从而避免SQL注入的发生。

# This is the qmark style:
# 1 cur.execute("insert into people values (?, ?)", (who, age))

# And this is the named style:
# 1 cur.execute("select * from people where name_last=:who and age=:age", {"who": who, "age": age})

# And 这个是模糊查询
# 1 conditions = {"name": "ming", "age": 18, "gender": "male"}
# 2 select_sql = "select * from people where name like concat('%', :name, '%') and age >= :age and gender = :gender"
# 3 cur.execute(select_sql, conditions)

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值