1

假设我有一个 Web 应用程序,它从用户那里获取输入并将其保存在数据库中。让我们进一步假设没有安全漏洞——它正确地转义了用户输入,使用绑定参数,等等。

从数据库中检索到的数据是否必须受到怀疑(即潜在的污染/恶意)?


示例(不确定结果,因为我不敢尝试)。这是数据库:

create table mytable (id int primary key, name varchar(50));

create table othertable (name varchar(50), xyz int, 
    ... `name` is an fk ...);

insert into mytable (id, name) values(1, '"abc"; drop table mytable;');

insert into othertable (name, xyz) values('"abc"; drop table mytable;', 45475);

然后我运行这个伪代码(例如,可能来自 PHP):

# run query 'select * from mytable where id = 1';

# put the `name` in $name

# run query 'select * from othertable where name = $name'
# $name is not escaped, no other precautions taken
4

2 回答 2

1

你必须再次逃脱。转义所做的只是说'SQL,这不是面向命令',它是数据的一部分'。因此,如果您转义“'one'”,SQL 将存储“\'one\'”,并输出...“'one'”。这意味着你必须再次逃离。

更好的是,不要使用常规的 mysql_ 函数,而是使用 mysqli_ 或 PDO 中的准备好的语句。我正在转移我自己的编程方法,因为它们消除了转义的需要。(基本思想是,不是发送一个必须用“就地”数据解析的查询字符串,而是发送一个带有占位符的查询字符串,然后告诉 SQL '还记得我之前给你的那个查询吗?使用值 X , Y, 和 Z。因此,这些值永远不会破坏实际查询的处理)

于 2011-11-28T18:22:29.600 回答
0

The vulnerability happens at the point where you substitute the $name. Always escape before substituting.

于 2011-11-28T17:36:03.040 回答