题
当用户的输入插入MySQL查询时,用户输入中应该替换哪些危险字符?我知道引号,双引号,\ r和\ n。还有其他人吗?
(我没有选择使用接受参数的智能连接器,因此我必须自己构建查询,这将以多种编程语言实现,包括一些模糊不清的解决方案例如PHP中的 mysql_real_escape_string
无效)
解决方案
mysql_real_escape_string()来自mysql.com docs:
将from中的字符串编码为转义的SQL字符串,并考虑连接的当前字符集。结果放在to中,并附加一个终止空字节。编码的字符是NUL(ASCII 0),“ \ n”,“ \ r”,“ \”,“'”,& #8220;"”和Control-Z(参见第8.1节“文字值”)。 (严格地说,MySQL只需要反斜杠,并且用于引用查询中字符串的引号字符将被转义。此函数引用其他字符以使它们更容易在日志文件中读取。)
mysql_real_escape_string()可识别字符集,因此复制其所有功能(尤其是针对多字节攻击问题)并不是一件容易的工作。
来自 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
其他提示
您需要支持哪些语言?使用语言的内置清理比编写自己的清理要好得多。
编辑:在php.net上查看 mysql_real_escape_string
:
mysql_real_escape_string()
调用MySQL的库函数mysql_real_escape_string
,它将反斜杠添加到以下字符:\ x00
,\ n
,\ r
,\
,'
,"
和\ x1a