zoukankan      html  css  js  c++  java
  • Python预编译语句防止SQL注入

    这个月太忙,最近不太太平,我的愿望是世界和平!

    ==================================

    今天也在找python的预编译,早上写的sql是拼接来构成的。于是找了2篇文章,还不错,分享一下大家学习。

    ps:直接引用别人的话了,因为他们说的已经很好了。

    错误用法:

    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注入的发生。

    example:

     1 import sqlite3
     2  
     3 con = sqlite3.connect(":memory:")
     4 cur = con.cursor()
     5 cur.execute("create table people (name_last, age)")
     6  
     7 who = "Yeltsin"
     8 age = 72
     9  
    10 # This is the qmark style:
    11 cur.execute("insert into people values (?, ?)", (who, age))
    12  
    13 # And this is the named style:
    14 cur.execute("select * from people where name_last=:who and age=:age", {"who": who, "age": age})
    15  
    16 print cur.fetchone()

    本文参考信息:

    http://xlixli.net/?p=377

    https://crazyof.me/blog/archives/2224.html

  • 相关阅读:
    权限管理
    Linux常用命令
    SSM的开发步骤分析
    03每日课后作业面试题汇总
    Redis常用命令
    大觅网07day
    消息队列面试题
    bzoj 3745: [Coci2015]Norma
    Codeforces 343E Pumping Stations
    UOJ #236. 【IOI2016】railroad
  • 原文地址:https://www.cnblogs.com/sevck/p/6733702.html
Copyright © 2011-2022 走看看