【问题标题】:simulating an API call by an external end user to an SQL Database模拟外部最终用户对 SQL 数据库的 API 调用
【发布时间】:2017-12-12 21:58:27
【问题描述】:

此脚本使用 JSON 输入作为参数,应从命令行运行。一个例子 可以运行为: python count_stock.py '{"productId":1}' 返回值 [(9,)] 我需要编写一个脚本,利用 count_stock.py 在 数据库。新脚本不能直接与 DB 通信,只能调用 count_stock.py

这是 count_stock.py 脚本

import sys, sqlite3, json
company_db_file = 'company_data.db'
conn = sqlite3.connect(company_db_file)
c = conn.cursor()
for arg in sys.argv[1:]:
    input_json = json.loads(arg)
    c.execute("SELECT amount FROM product WHERE id = " + str(input_json['productId']))
    sys.stdout.write(str(c.fetchall())+ "\n")
conn.commit()
conn.close()

【问题讨论】:

    标签: python sql-injection


    【解决方案1】:

    str 函数似乎没有在参数两边加上引号,因此您可以在命令行中传递您想要的任何 SQL。比如……

    python count_stock.py '{"productId":"1 or 1 = 1"}'
    

    这应该执行这条 SQL:

    SELECT amount FROM product WHERE id = 1 or 1 = 1
    

    此查询忽略“ID”过滤器并返回表中的所有行。

    【讨论】:

    • 还有两个表,我如何访问这些详细信息,我应该使用占位符吗? @大卫
    • 您也许可以传入“; select * from table”来对数据库执行多个查询。另一种选择是使用UNION 运算符将另一个表添加到结果中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2022-06-30
    • 2021-10-31
    • 2021-06-21
    相关资源
    最近更新 更多