首页 > 解决方案 > 如何在 Python 中测试 Bobby Tables SQL 注入?

问题描述

我正在尝试在 Python 3.x 中从这张图片中测试 SQL 注入: 在此处输入图像描述

来源:https ://xkcd.com/327/

Python SQLite3 库文档页面https://docs.python.org/3/library/sqlite3.html中提到了这个 SQL 注入示例。我尝试在 python 代码中对此进行测试,但收到警告:“sqlite3.Warning:您一次只能执行一个语句。” 他们是否已经修复了这个漏洞?难道我做错了什么?

代码片段:

import sqlite3

conn = sqlite3.connect('example.db')
c = conn.cursor()

c.execute('''CREATE TABLE test(firstname text, lastname text)''')
c.execute("INSERT INTO test VALUES ('John','Doe')")

firstname = "John'; DROP TABLE test;"
c.execute("SELECT * FROM test WHERE firstname = '%s'" % firstname)
for i in c:
    print(i)

conn.commit()
conn.close()

结果:

Traceback (most recent call last):
  File "C:\test.py", line 10, in <module>
    c.execute("SELECT * FROM test WHERE firstname = '%s'" % firstname)
sqlite3.Warning: You can only execute one statement at a time.

标签: pythonsqlpython-3.xsqlite

解决方案


c.execute(...) 只允许一个语句。使用 c.executescript(...) 或 c.executemany(...)。


推荐阅读