清理 MySQL 用户参数

发布于 2024-07-07 01:46:42 字数 201 浏览 8 评论 0原文

当用户的输入将被插入到 MySQL 查询中时,用户输入中应该替换哪些危险字符? 我知道引号、双引号、\r 和 \n。 还有其他吗?
(我无法选择使用接受参数的智能连接器,因此我必须自己构建查询,这将用多种编程语言实现,包括一些晦涩难懂的语言,因此解决方案例如 PHP 中的 mysql_real_escape_string 无效)

What are the dangerous characters that should be replaced in user input when the users' input will be inserted in a MySQL query? I know about quotes, double quotes, \r and \n. Are there others?
(I don't have the option of using a smart connector that accepts parameters so I have to build the query myself and this will be implemented in multiple programming languages, including some obscure ones so solutions such as mysql_real_escape_string in PHP are not valid)

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

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

发布评论

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

评论(2

沙沙粒小 2024-07-14 01:46:42

mysql_real_escape_string() 来自 mysql。 com 文档:

考虑到连接的当前字符集,from 中的字符串被编码为转义 SQL 字符串。 结果被放入 to 中并附加一个终止空字节。 编码的字符为 NUL (ASCII 0)、“\n”、“\r”、“\”、“'”、“"” 和 Control-Z(请参见第 8.1 节“文字值”)。(严格来说, MySQL 仅要求转义反斜杠和用于在查询中引用字符串的引号字符。此函数引用其他字符以使它们更易于在日志文件中读取。)


mysql_real_escape_string()

> 具有字符集感知能力 addslashes_dont_call_it_a_comeback.html" rel="nofollow noreferrer">http://cognifty.com/blog.entry/id=6/addslashes_dont_call_it_a_comeback.html:

AS = addslashes()  
MRES = mysql_real_escape_string()
ACS = addcslashes() //called with "\\\000\n\r'\"\032%_"

Feature                                         AS     MRES    ACS
escapes quote, double quote, and backslash      yes    yes     yes
escapes LIKE modifiers: underscore, percent     no     no      yes
escapes with single quotes instead of backslash no     yes*1   no
character-set aware                             no     yes*2   no
prevents multi-byte attacks                     no     yes*3   no

mysql_real_escape_string() from mysql.com docs:

The string in from is encoded to an escaped SQL string, taking into account the current character set of the connection. The result is placed in to and a terminating null byte is appended. Characters encoded are NUL (ASCII 0), “\n”, “\r”, “\”, “'”, “"”, and Control-Z (see Section 8.1, “Literal Values”). (Strictly speaking, MySQL requires only that backslash and the quote character used to quote the string in the query be escaped. This function quotes the other characters to make them easier to read in log files.)


mysql_real_escape_string() is character set aware, so replicating all its abilities (especially against multi-byte attack issues) is not a small amount of work.

From http://cognifty.com/blog.entry/id=6/addslashes_dont_call_it_a_comeback.html:

AS = addslashes()  
MRES = mysql_real_escape_string()
ACS = addcslashes() //called with "\\\000\n\r'\"\032%_"

Feature                                         AS     MRES    ACS
escapes quote, double quote, and backslash      yes    yes     yes
escapes LIKE modifiers: underscore, percent     no     no      yes
escapes with single quotes instead of backslash no     yes*1   no
character-set aware                             no     yes*2   no
prevents multi-byte attacks                     no     yes*3   no

另类 2024-07-14 01:46:42

您需要支持哪些语言? 使用语言的内置清理比编写自己的清理要好得多。

编辑:查看 php.net 上的 mysql_real_escape_string

mysql_real_escape_string() 调用 MySQL 的库函数 mysql_real_escape_string,该函数在以下字符前面添加反斜杠:\x00\n< /code>、\r\'"\x1a .


What languages do you need to support? It is much better to use a language's built-in sanitization than to write your own.

Edit: Looking at mysql_real_escape_string on php.net:

mysql_real_escape_string() calls MySQL's library function mysql_real_escape_string, which prepends backslashes to the following characters: \x00, \n, \r, \, ', " and \x1a.

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