搜索关键字:
- python mysqldb anti sql injection
参考链接:
- http://bobby-tables.com/python.html
- https://wiki.python.org/moin/DbApiFaq
- https://www.python.org/dev/peps/pep-0249/#paramstyle
- https://docs.python.org/2/library/sqlite3.html#sqlite3.Cursor.execute
- =
- http://stackoverflow.com/questions/7929364/python-best-practice-and-securest-to-connect-to-mysql-and-execute-queries
- http://stackoverflow.com/questions/3617052/escape-string-python-for-mysql
- http://stackoverflow.com/questions/775296/python-mysql-with-variables
- =
- [Python]Python预编译语句防止SQL注入
- =
- SQL Injection Wiki | References
- =
- http://stackoverflow.com/questions/9059027/if-i-receive-post-data-with-flask-put-that-data-into-a-wtforms-form-and-it-succ
参考解答:
Using the Python DB API, don’t do this:
# Do NOT do it this way. cmd = "update people set name='%s' where id='%s'" % (name, id) curs.execute(cmd)
Instead, do this:
cmd = "update people set name=%s where id=%s" curs.execute(cmd, (name, id))
Note that the placeholder syntax depends on the database you are using.
'qmark' Question mark style, e.g. '...WHERE name=?' 'numeric' Numeric, positional style, e.g. '...WHERE name=:1' 'named' Named style, e.g. '...WHERE name=:name' 'format' ANSI C printf format codes, e.g. '...WHERE name=%s' 'pyformat' Python extended format codes, e.g. '...WHERE name=%(name)s'
The values for the most common databases are:
>>> import MySQLdb; print MySQLdb.paramstyle format >>> import psycopg2; print psycopg2.paramstyle pyformat >>> import sqlite3; print sqlite3.paramstyle qmark
So if you are using MySQL or PostgreSQL, use %s
(even for numbers and other non-string values!) and if you are using SQLite use ?
==
How do I pass parameters to the cursor.execute method?
Don’t use the ‘%’ concatenation operator, pass them as a series of extra parameters. For instance
>>> cursor.execute("SELECT * FROM my_table WHERE my_column = '%s'" % "column_value")
May do what you want, but more by accident than design. If you change it to;
>>> cursor.execute("SELECT * FROM my_table WHERE my_column = %s", "column_value")
Then the DB-API module will make sure your value is correctly escaped and turned into an object appropriate for the database.
==
execute()函数本身就有接受SQL语句变量的参数位,只要正确的使用(直白一点就是:使用”逗号”,而不是”百分号”)就可以对传入的值进行correctly转义,从而避免SQL注入的发生。
==
execute(sql[, parameters])
Executes an SQL statement. The SQL statement may be parameterized (i. e. placeholders instead of SQL literals). The sqlite3module supports two kinds of placeholders: question marks (qmark style) and named placeholders (named style).
Here’s an example of both styles:
import sqlite3 con = sqlite3.connect(":memory:") cur = con.cursor() cur.execute("create table people (name_last, age)") who = "Yeltsin" age = 72 # This is the qmark style: cur.execute("insert into people values (?, ?)", (who, age)) # And this is the named style: cur.execute("select * from people where name_last=:who and age=:age", {"who": who, "age": age}) print cur.fetchone()
execute() will only execute a single SQL statement. If you try to execute more than one statement with it, it will raise a Warning. Use executescript() if you want to execute multiple SQL statements with one call.
=EOF=
《 “Python中的防SQL注入” 》 有 7 条评论
sqlmap对所有目标及其所有旁站批量自动化sqli
https://github.com/3xp10it/MyToolKit
SQL注入Wiki
https://sqlwiki.netspi.com/
SQL注入WIKI
http://sqlwiki.radare.cn/#/
SQL和NoSQL注入浅析(上、下)
https://mp.weixin.qq.com/s/LsqQo_04ROuf2_wLrBRRZQ
https://mp.weixin.qq.com/s/aaLdXIbMu_WVq8E65OAQsQ
简单分析SQL注入语义分析库Libinjection
http://www.freebuf.com/articles/web/170930.html
https://github.com/client9/libinjection
https://www.client9.com/tags/security/
sqlmap 内核分析 I: 基础流程
https://zhuanlan.zhihu.com/p/43242220
sqlmap 内核分析 II: 核心原理-页面相似度算法实践
https://zhuanlan.zhihu.com/p/44157153
https://bobby-tables.com/python.html
`
cmd = “update people set name=%s where id=%s”
curs.execute(cmd, (name, id))
# 如果你使用的是 MySQL/PostgreSQL 请一直使用 %s 作为占位符,即便对应的字段是 整型或是其它非字符 类型的值。
`
Python MySQLdb WHERE SQL LIKE
https://stackoverflow.com/questions/10903497/python-mysqldb-where-sql-like
`
# 不要在占位符那里用 % 拼接,而是在传入的参数那里提前拼接,然后用防注入的方式传入
c.execute(“SELECT * FROM data WHERE params LIKE %s LIMIT 1”, (“%” + param + “%”,))
`