简体   繁体   English

这个查询容易受到sql注入吗?

[英]Is this query vulnerable to sql injection?

$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. 我想知道是否可以使用SQL注入利用上述查询。 Is there any advanced SQL injection technique that could break sprintf for this particular query? 是否有任何高级SQL注入技术可以破坏这个特定查询的sprintf

I don't think it needs to be particularly advanced... try an input of 我不认为它需要特别先进...尝试输入

' OR 1 = 1 OR user='

In other words, you'll get SQL of: 换句话说,你会得到以下SQL:

select user from table where user='' OR 1 = 1 OR user=''

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. 底线是你应该使用参数化查询。

Using sprintf doesn't give you any more protection than using simple string concatenation . 使用sprintf不会比使用简单的字符串连接提供更多保护 The advantage of sprintf is just having it a little more readable than when to using simple PHP's string concatenation. sprintf的优势在于它比使用简单的PHP字符串连接时更具可读性。 But sprintf doesn't do any more than simple string concatenation when using the %s format: 但是当使用%s格式时, sprintf不会做简单的字符串连接:

$str = implode('', range("\x00", "\xFF"));        // string of characters from 0x00 – 0xFF
var_dump(sprintf("'%s'", $str) === "'".$str."'"); // true

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: 你需要使用逃避你想要插入数据的上下文特殊字符的函数(在这种情况下是MySQL中字符串声明 ,假设你正在使用MySQL),如**mysql_real_escape_string**

$myq = sprintf("select user from table where user='%s'", mysql_real_escape_string($_POST["user"]));

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. 需要转义\\x00, \\n, \\r, \\, ', "\\x1a ()不这样做sprintf()不对字符串进行修改,它只是扩展你给它的任何可变参数根据您指定的格式进入您提供的缓冲区。

If the strings ARE being transformed, its likely due to magic quotes (as Rob noted in Comments), not sprintf() . 如果字符串ARE被转换,可能是由于魔术引号 (如Rob在评论中所述),而不是sprintf() If that is the case, I highly recommend disabling them. 如果是这种情况,我强烈建议禁用它们。

when $_POST["user"] would equal "';SHUTDOWN;" 当$ _POST [“user”]等于“'; SHUTDOWN;” - what would happen? - 会发生什么?

Ahh here I come with the magic answer! 啊,我来到这个神奇的答案! :) :)
magic quotes do escaping for you! magic quotes为你逃脱!

So, you have to turn magic_quotes_gpc ini directive off 所以,你必须关闭magic_quotes_gpc ini指令
and then use mysql_real_escape_string as suggested. 然后按照建议使用mysql_real_escape_string。

Actually, turn off magic quotes. 实际上,关掉魔法引号。

In PHP, where it's appropriate, use filters: 在PHP中,在适当的地方使用过滤器:

$inUser = $_POST['user'];
$outUser = filter_var($inUser, FILTER_SANITIZE_STRING);

Filters strip out HTML tags and escape various characters. 过滤器删除HTML标记并转义各种字符。

In addition, you can let your database escape it for you: 此外,您可以让您的数据库为您逃脱:

$inUser = $_POST['user'];
$outUser = mysqli_real_escape_string($conn, $inUser);

This escapes MySQL specific special characters like double quotes, single quotes, etc. 这会逃脱MySQL特定的特殊字符,如双引号,单引号等。

Finally, you should use parameterized queries: 最后,您应该使用参数化查询:

$sql = "SELECT user FROM table WHERE user = ?";
$stmt = $pdo->prepare($sql);
$params = array($outUser);
$stmt->execute($params);

Parameterized queries automatically add the quotes around strings, etc., and have further restrictions that make SQL injections even more difficult. 参数化查询会自动在字符串等周围添加引号,并且还有进一步限制使SQL注入更加困难。

I use all three, in that order. 我按顺序使用这三个。

$_POST["user"] = "' or 1=1 or user='"

Yes. 是。

If somebody put in the following as the user in your form: 如果有人在您的表单中输入以下内容:

'; delete * from table

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM