mysql_real_escape_string() 是否完全防止 SQL 注入?

发布于 2024-07-29 07:51:10 字数 486 浏览 1 评论 0原文

http://www.justinshattuck .com/2007/01/18/mysql-injection-cheat-sheet/?akst_action=share-this ,有一个部分声称您可以使用某些亚洲字符编码绕过 mysql_real_escape_string

使用 BIG5 或 GBK 绕过 mysql_real_escape_string()

“注入字符串”
に关する追加情报:

以上字符为中文Big5

这是真的吗? 如果是这样,如果您无法访问准备好的声明,您将如何保护您的网站免受这种情况的影响?

On http://www.justinshattuck.com/2007/01/18/mysql-injection-cheat-sheet/?akst_action=share-this , there is a section that claims you can bypass mysql_real_escape_string with certain Asian character encodings

Bypassing mysql_real_escape_string() with BIG5 or GBK

"injection string"
に関する追加情報:

the above chars are Chinese Big5

Is this really true? And if so, how would you protect your website against this, if you had no access to prepared statements?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终陌 2024-08-05 07:51:10

根据 Stefan Esser 的说法,“使用 SET NAMES 时,mysql_real_escape_string() [is] 不安全。”

他的解释是,来自他的博客

SET NAMES 通常用于将编码从默认编码切换为应用程序需要的编码。
这是通过 mysql_real_escape_string 不知道的方式完成的。 这意味着如果您切换到某种允许反斜杠作为第 2 个第 3 个第 4 个…字节的多字节编码,您会遇到麻烦,因为 mysql_real_escape_string 无法正确转义。 UTF-8 是安全的...

更改编码的安全方法是mysql_set_charset,但这仅在新的 PHP 版本中可用

不过,他确实提到 UTF-8 是安全的。

According to Stefan Esser, "mysql_real_escape_string() [is] not safe when SET NAMES is used."

His explanation, from his blog:

SET NAMES is usually used to switch the encoding from what is default to what the application needs.
This is done in a way that mysql_real_escape_string doesn’t know about this. This means if you switch to some multi byte encoding that allows backslash as 2nd 3rd 4th… byte you run into trouble, because mysql_real_escape_string doesn’t escape correctly. UTF-8 is safe…

Safe way to change encoding is mysql_set_charset, but that is only available in new PHP versions

He does mention that UTF-8 is safe, though.

暗喜 2024-08-05 07:51:10

这是一个 MySQL 服务器错误,据报道早在 2006 年 5 月就已修复。

请参阅:

该错误已在 MySQL 4.1.20、5.0.22、5.1.11 中修复。

如果您使用 4.1.x、5.0.x 或 5.1.x,请确保您至少已升级到次要版本号。

作为解决方法,您还可以启用 SQL 模式 NO_BACKSLASH_ESCAPES 禁用反斜杠作为引号转义字符。

This is a MySQL server bug that was reportedly fixed way back in May 2006.

See:

  • MySQL bug #8303: String literals with multi-byte characters containing \ are lexed incorrectly
  • MySQL Bug #8317: Character set introducer in query fails to override connection character set
  • MySQL Bug #8378: String escaped incorrectly with client character set 'gbk'
  • MySQL 5.1.11 changelog.

The bug was reported fixed in MySQL 4.1.20, 5.0.22, 5.1.11.

If you use 4.1.x, 5.0.x, or 5.1.x, make sure you have upgraded at least to the minor revision numbers.

As a workaround, you can also enable the SQL mode NO_BACKSLASH_ESCAPES which disables backslash as a quote-escape character.

夜深人未静 2024-08-05 07:51:10

我很确定只有当您使用 SQL 更改 char 编码时它才不起作用。

I'm pretty sure it only doesn't work if you use SQL to change the char encoding.

自此以后,行同陌路 2024-08-05 07:51:10

正如其他人所证明的,mysql_real_escape_string() 在模糊的边缘情况下可以被绕过。 这是绕过转义逻辑的一种已知策略,但可能还有其他尚未发现的未知漏洞。

准备好的语句在实际使用时而不是由 PDO 驱动程序模拟时,可证明是安全的(至少在 SQL 注入方面),因为它们解决了应用程序安全性的基本问题:它们将数据与对数据进行操作的指令。 它们以单独的数据包发送; 参数化值永远不会污染查询字符串。

都2015年了,别再逃避和串联了。 您仍然应该根据应用程序(和业务)逻辑验证您的输入,但只需使用准备好的语句。

As others have demonstrated, mysql_real_escape_string() can be bypassed in obscure edge cases. That's one known strategy for bypassing the escaping logic, but there could be other unknown vulnerabilities that have not been discovered yet.

The simple and effective way to prevent SQL injection in PHP is to use prepared statements where you can, and a very strict whitelist where you can't.

Prepared statements, when actually used and not emulated by the PDO driver, are provably secure (at least with regards to SQL injection), as they solve a fundamental problem with application security: they separate the data from the instructions that operate on the data. They're sent in separate packets; the parameterized values never have a chance to taint the query string.

It's 2015. Don't escape and concatenate anymore. You should still validate your inputs according to application (and business) logic, but just use prepared statements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文