此查询是否容易受到 SQL 注入攻击?
$myq = sprintf("select user from table where user='%s'", $_POST["user"]);
我想知道是否可以使用 SQL 注入来利用上述查询。是否有任何高级 SQL 注入技术可以破坏此特定查询的 sprintf ?
$myq = sprintf("select user from table where user='%s'", $_POST["user"]);
I would like to know if the above query can be exploited using SQL injection. Is there any advanced SQL injection technique that could break sprintf
for this particular query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我不认为它需要特别高级...尝试输入
换句话说,您将得到以下 SQL:
这看起来像您真正想要执行的查询吗? (现在考虑它删除表或类似内容的可能性。)
底线是您应该使用参数化查询。
I don't think it needs to be particularly advanced... try an input of
In other words, you'll get SQL of:
Does that look like a query you really want to execute? (Now consider the possibility of it dropping tables instead, or something similar.)
The bottom line is that you should be using a parameterised query.
是的,我想说你那里有一个潜在的问题:)
你需要转义:
\x00、\n、\r、\、'、"
和\x1a
。sprintf() 不会这样做,sprintf()
不会对字符串进行任何修改,它只是根据您指定的格式将您提供的任何可变参数扩展到您提供的缓冲区中。如果字符串正在被转换,则可能是由于 魔法引号(如Rob 在评论中指出),而不是
sprintf()
如果是这种情况,我强烈建议禁用它们。Yes, I'd say you have a potential problem there :)
You need to escape:
\x00, \n, \r, \, ', "
and\x1a
. sprintf() does not do that,sprintf()
does no modification to strings, it just expands whatever variadic arguments that you give it into the buffer that you provide according to the format that you specify.If the strings ARE being transformed, its likely due to magic quotes (as Rob noted in Comments), not
sprintf()
. If that is the case, I highly recommend disabling them.使用
sprintf
不会为您提供比使用简单字符串更多的保护连接。sprintf
的优点只是比使用简单 PHP 的字符串连接更具可读性。但是,在使用%s
格式时,sprintf
只执行简单的字符串连接:您需要使用转义要插入数据的上下文特殊字符的函数(在本例中是 MySQL 中的 字符串声明 ,假设您使用的是 MySQL),例如
**mysql_real_escape_string**
的作用:Using
sprintf
doesn’t give you any more protection than using simple string concatenation. The advantage ofsprintf
is just having it a little more readable than when to using simple PHP’s string concatenation. Butsprintf
doesn’t do any more than simple string concatenation when using the%s
format:You need to use functions that escape the contextual special characters you want to insert your data into (in this case a string declaration in MySQL, supposing you’re using MySQL) like
**mysql_real_escape_string**
does:当 $_POST["user"] 等于 "';SHUTDOWN;" 时- 会发生什么?
when $_POST["user"] would equal "';SHUTDOWN;" - what would happen?
实际上,关闭魔术引号。
在 PHP 中,在适当的情况下使用过滤器:
过滤器去除 HTML 标签并转义各种字符。
此外,你可以让你的数据库为你转义它:
这会转义 MySQL 特定的特殊字符,如双引号、单引号等。
最后,你应该使用参数化查询:
参数化查询会自动在字符串等周围添加引号,并且具有进一步的限制使 SQL 注入变得更加困难。
我按顺序使用这三个。
Actually, turn off magic quotes.
In PHP, where it's appropriate, use filters:
Filters strip out HTML tags and escape various characters.
In addition, you can let your database escape it for you:
This escapes MySQL specific special characters like double quotes, single quotes, etc.
Finally, you should use parameterized queries:
Parameterized queries automatically add the quotes around strings, etc., and have further restrictions that make SQL injections even more difficult.
I use all three, in that order.
啊,我带来了神奇的答案! :)
神奇的引号
可以为你转义!所以,你必须关闭 magic_quotes_gpc ini 指令
然后按照建议使用 mysql_real_escape_string 。
Ahh here I come with the magic answer! :)
magic quotes
do escaping for you!So, you have to turn magic_quotes_gpc ini directive off
and then use mysql_real_escape_string as suggested.
是的。
如果有人在您的表单中以用户身份输入以下内容:
Yes.
If somebody put in the following as the user in your form: