我意识到,在构建包含用户输入的查询时,参数化SQL查询是清理用户输入的最佳方式,但我想知道接受用户输入、转义任何单引号并用单引号包围整个字符串有什么错.以下是代码:
sSanitizedInput = "'" & Replace(sInput, "'", "''") & "'"
用户输入的任何单引号都将替换为双单引号,这将消除用户结束字符串的能力,因此他们可能键入的任何其他内容,如分号、百分号等,都将是字符串的一部分,而不是作为命令的一部分实际执行.
我们使用的是Microsoft SQL Server 2000,我认为单引号是唯一的字符串分隔符,也是转义字符串分隔符的唯一方法,因此无法执行用户键入的任何内容.
我看不出有任何方法可以对它发起SQL注入攻击,但我意识到,如果它像我认为的那样防弹,其他人可能已经想到了,这将是一种常见的做法.
这个代码怎么了?有没有办法让SQL注入攻击通过这种净化技术?利用这种技术的示例用户输入将非常有用.
更新:
I still don't know of any way to effectively launch a SQL injection attack against this code. A few people suggested that a backslash would escape one single-quote and leave the other to end the string so that the rest of the string would be executed as part of the SQL command, and I realize that this method would work to inject SQL into a MySQL database, but in SQL Server 2000 the only way (that I've been able to find) to escape a single-quote is with another single-quote; backslashes won't do it.
除非有办法停止单引号的转义,否则不会执行用户输入的其余部分,因为它们都将被视为一个连续字符串.
我知道有更好的方法来净化输入,但我真的更感兴趣的是了解为什么我上面提供的方法不起作用.如果有人知道针对这种净化方法发起SQL注入攻击的具体方法,我很乐意看到.