问题描述
我一直在向我的同事和这里讨论在SQL查询中使用参数的好处,特别是在.NET应用程序中。我甚至已经承诺他们可以免疫SQL注入攻击。
I've been preaching both to my colleagues and here on SO about the goodness of using parameters in SQL queries, especially in .NET applications. I've even gone so far as to promise them as giving immunity against SQL injection attacks.
但我开始怀疑这是否真的是真的。有没有任何已知的SQL注入攻击将是成功的参数化查询?例如,您可以发送一个字符串导致服务器上的缓冲区溢出?
But I'm starting to wonder if this really is true. Are there any known SQL injection attacks that will be successfull against a parameterized query? Can you for example send a string that causes a buffer overflow on the server?
当然还有其他一些考虑要做,以确保Web应用程序是安全的用户输入和所有东西),但现在我在思考SQL注入。我特别感兴趣的是对MsSQL 2005和2008的攻击,因为它们是我的主数据库,但所有数据库都很有趣。
There are of course other considerations to make to ensure that a web application is safe (like sanitizing user input and all that stuff) but now I am thinking of SQL injections. I'm especially interested in attacks against MsSQL 2005 and 2008 since they are my primary databases, but all databases are interesting.
编辑:澄清我的意思是参数和参数化查询。通过使用参数,我的意思是使用变量,而不是在字符串中构建sql查询。
因此,而不是这样:
To clarify what I mean by parameters and parameterized queries. By using parameters I mean using "variables" instead of building the sql query in a string.
So instead of doing this:
SELECT * FROM Table WHERE Name = 'a name'
SELECT * FROM Table WHERE Name = @Name
,然后在查询/命令对象上设置@Name参数的值。
and then set the value of the @Name parameter on the query / command object.
推荐答案
占位符足以防止注射。你可能仍然打开缓冲区溢出,但这是一个完全不同的攻击从SQL注入(攻击矢量将不是SQL语法,但二进制)。由于传递的参数都将被正确转义,因此攻击者没有任何方式传递将被视为活SQL的数据。
Placeholders are enough to prevent injections. You might still be open to buffer overflows, but that is a completely different flavor of attack from an SQL injection (the attack vector would not be SQL syntax but binary). Since the parameters passed will all be escaped properly, there isn't any way for an attacker to pass data that will be treated like "live" SQL.
您不能使用占位符内的函数,并且不能将占位符用作列或表名,因为它们将作为字符串文字转义和引用。
You can't use functions inside placeholders, and you can't use placeholders as column or table names, because they are escaped and quoted as string literals.
但是,如果您在动态查询中使用参数作为字符串连接的一部分,因为你的字符串不会被转义,而是字面量。使用其他类型的参数(如整数)是安全的。
However, if you use parameters as part of a string concatenation inside your dynamic query, you are still vulnerable to injection, because your strings will not be escaped but will be literal. Using other types for parameters (such as integer) is safe.
也就是说,如果你使用输入来设置 security_level
,那么有人可能只是在系统中创建自己的管理员,并拥有一个免费的。但是这只是基本的输入验证,并且与SQL注入无关。
That said, if you're using use input to set the value of something like security_level
, then someone could just make themselves administrators in your system and have a free-for-all. But that's just basic input validation, and has nothing to do with SQL injection.
这篇关于参数是否真的足以防止Sql注入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!