简体   繁体   English

在 SQL 中必须转义哪些正则表达式字符?

[英]What regular expression characters have to be escaped in SQL?

To prevent SQL injection attack, the book "Building Scalable Web Sites" has a function to replace regular expression characters with escaped version:为防止SQL注入攻击,《Building Scalable Web Sites》一书中有一个用转义版本替换正则表达式字符的功能:

function db_escape_str_rlike($string) {
    preg_replace("/([().\[\]*^\$])/", '\\\$1', $string);
}

Does this function escape ( ) .此函数是否转义( ) . [ ] * ^ $ ? [ ] * ^ $ ? Why are only those characters escaped in SQL?为什么只有那些字符在 SQL 中被转义?

I found an excerpt from the book you mention , and found that the function is not for escaping to protect against SQL injection vulnerabilities.从你提到的那本书中找到了一个摘录,发现该功能不是为了防止SQL注入漏洞而进行的转义。 I assumed it was, and temporarily answered your question with that in mind.我认为是,并暂时考虑到这一点回答了您的问题。 I think other commenters are making the same assumption.我认为其他评论者也在做出同样的假设。

The function is actually about escaping characters that you want to use in regular expressions.该函数实际上是关于转义您要在正则表达式中使用的字符。 There are several characters that have special meaning in regular expressions, so if you want to search for those literal characters, you need to escape them (precede with a backslash).有几个字符在正则表达式中具有特殊含义,因此如果要搜索这些文字字符,则需要对它们进行转义(以反斜杠开头)。

This has little to do with SQL.这与 SQL 关系不大。 You would need to escape the same characters if you wanted to search for them literally using grep , sed , perl , vim , or any other program that uses regular expression searches.如果您想使用grepsedperlvim或任何其他使用正则表达式搜索的程序逐字搜索它们,则需要对相同的字符进行转义。

Unfortunately, active characters in sql databases is an open issue.不幸的是,sql 数据库中的活动字符是一个悬而未决的问题。 Each database vendor uses their own (mainly oracle's mysql, that uses \\ escape sequences)每个数据库供应商都使用自己的(主要是oracle的mysql,使用\\转义序列)

The official SQL way to escape a ' , which is the string delimiter used for values is to double the ' , as in '' .转义'的官方 SQL 方法(用于值的字符串分隔符)是将'加倍,如''

That should be the only way to ensure transparency in SQL statements, and the only way to introduce a proper ' into a string.这应该是确保 SQL 语句透明的唯一方法,也是将正确的'引入字符串的唯一方法。 As soon as any vendor admits \\' as a synonim of a quote, you are open to support all the extra escape sequences to delimit strings.一旦任何供应商承认\\'作为引号的同义词,您就可以支持所有额外的转义序列来分隔字符串。 Suppose you have:假设你有:

'Mac O''Connor' (should go into "Mac O'Connor" string)

and assume the only way to escape a ' is that... then you have to check the next char when you see a ' for a '' sequence and:并假设转义'的唯一方法是...然后当您看到'''序列时,您必须检查下一个字符,并且:

  • you get '' that you change into ' .你得到''你变成'
  • you get another, and you terminate the string literal and process the char as the first of the next token.你得到另一个,你终止字符串文字并将字符处理为下一个标记的第一个。

But if you admit \\ as escape also, then you have to check for \\' and for \\\\' , and \\\\\\' (this last one should be converted to \\' on input) etc. You can run into trouble if you don't detect special cases as但是如果你也承认\\作为转义符,那么你必须检查\\'\\\\'\\\\\\' (最后一个应该在输入时转换为\\' )等。如果你这样做,你可能会遇到麻烦不要将特殊情况检测为

  • \\'' (should the '' be processed as SQL mandates, or the first \\' is escaping the first ' and the second is the string end quote?) \\'' (应该将''作为 SQL 指令处理,还是第一个\\'转义第一个'而第二个是字符串结束引号?)
  • \\\\'' (should the \\\\ be converted into a single \\ then the ' should be the string terminator, or do we have to switch to SQL way of encoding and consider '' as a single quote?) \\\\'' (应该将\\\\转换为单个\\然后'应该是字符串终止符,还是我们必须切换到 SQL 编码方式并将''视为单引号?)

etc.等等。

You have to check your database documentation to see if \\ as escape characters affect only the encoding of special characters (like control characters or the like) and also affects the interpretation of the quote character or simply doesn't, and you have to escape ' the other way.您必须检查您的数据库文档以查看\\作为转义字符是否仅影响特殊字符(如控制字符等)的编码,并且还影响引号字符的解释或根本不影响,并且您必须转义'另一种方法。

That is the reason for the vendors to include functions to do the escape/unescape of character literals into values to be embedded in a SQL statement.这就是供应商包含将字符文字转义/取消转义到要嵌入到 SQL 语句中的值的函数的原因。 The idea of the attackers is to include (if you don't properly do) escape sequences into the data they post to you to see if that allows them to modify the text of the sql command to simply add a semicolon ;攻击者的想法是(如果您没有正确执行)转义序列到他们发布给您的数据中,以查看是否允许他们修改 sql 命令的文本以简单地添加分号; and write a complete sql statement that allows them to access freely your database.并编写一个完整的 sql 语句,允许他们自由访问您的数据库。

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

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