如何避免 ASP.NET 应用程序中的 SQL 注入攻击?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/305044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I avoid SQL injection attacks in my ASP.NET application?
提问by balaweblog
I need to avoid being vulnerable to SQL injection in my ASP.NET application. How might I accomplish this?
我需要避免在我的 ASP.NET 应用程序中受到 SQL 注入的影响。我怎样才能做到这一点?
回答by Tomalak
Even though your question is very generic, a few rules always apply:
尽管您的问题非常笼统,但始终适用一些规则:
- Use parameterized queries (
SqlCommand
withSqlParameter
) and put user input into parameters. - Don't build SQL strings out of unchecked user input.
- Don't assume you can build a sanitizing routine that can check user input for every kind of malformedness. Edge cases are easily forgotten. Checking numeric input may be simple enough to get you on the safe side, but for string input just use parameters.
- Check for second-level vulnerabilites - don't build SQL query strings out of SQL table values if these values consist of user input.
- Use stored procedures to encapsulate database operations.
- 使用参数化查询(
SqlCommand
withSqlParameter
)并将用户输入放入参数中。 - 不要从未经检查的用户输入中构建 SQL 字符串。
- 不要假设您可以构建一个可以检查用户输入的各种格式错误的清理例程。边缘情况很容易被遗忘。检查数字输入可能很简单,可以让您安全起见,但对于字符串输入,只需使用参数即可。
- 检查二级漏洞 - 如果这些值包含用户输入,则不要从 SQL 表值中构建 SQL 查询字符串。
- 使用存储过程封装数据库操作。
回答by Vinko Vrsalovic
Use Prepared Statements(link to an ASP.NET tutorial that uses prepared statements in the 'To add nodes for products' section). that's all there is to it.
使用准备好的语句(链接到“为产品添加节点”部分中使用准备好的语句的 ASP.NET 教程)。这里的所有都是它的。
Well, that or use an ORM, like Linq to SQLor NHibernate, they internally use prepared statements.
好吧,或者使用 ORM,例如Linq to SQL或NHibernate,它们在内部使用准备好的语句。
回答by Rune Grimstad
Use parameters! It really is that simple :-)
使用参数!真的就是这么简单:-)
Create your queries like this (for MS Sql server with C#):
像这样创建您的查询(对于使用 C# 的 MS Sql 服务器):
SqlCommand getPersons = new SqlCommand("SELECT * FROM Table WHERE Name = @Name", conn);
Here @Name is the parameter where you want to avoid sql injection and conn is an SqlConnection object. Then to add the parameter value you do the following:
这里@Name 是要避免 sql 注入的参数,conn 是一个 SqlConnection 对象。然后要添加参数值,请执行以下操作:
getPersons.Parameters.AddWithValue("@Name", theName);
Here theName is a variable that contains the name you are searching for.
这里 theName 是一个变量,其中包含您要搜索的名称。
Now it should be impossible to do any sql injections on that query.
现在应该不可能对该查询进行任何 sql 注入。
Since it is this simple there is no reason not to use parameters.
既然如此简单,就没有理由不使用参数。
回答by kevchadders
Never trust user input- Validate all textbox entries using validation controls, regular expressions, code, and so on
从不信任用户输入- 使用验证控件、正则表达式、代码等验证所有文本框条目
Never use dynamic SQL- Use parameterized SQL or stored procedures
永远不要使用动态 SQL- 使用参数化 SQL 或存储过程
Never connect to a database using an admin-level account- Use a limited access account to connect to the database
切勿使用管理员级别的帐户连接到数据库- 使用受限访问帐户连接到数据库
Don't store secrets in plain text- Encrypt or hash passwords and other sensitive data; you should also encrypt connection strings
不要以纯文本形式存储机密- 加密或散列密码和其他敏感数据;您还应该加密连接字符串
Exceptions should divulge minimal information- Don't reveal too much information in error messages; use customErrors to display minimal information in the event of unhandled error; set debug to false
异常应该泄露最少的信息——不要在错误消息中泄露太多信息;使用 customErrors 在出现未处理的错误时显示最少的信息;将调试设置为 false
Useful link on MSDN Stop SQL Injection
MSDN 上的有用链接停止 SQL 注入
回答by Brian Schmitt
SQL injection occurs because the query to the database is being constructed in real time, for example:
SQL注入是因为对数据库的查询是实时构建的,例如:
SELECT * From Table1 WHERE " + UserInput
UserInput
may be malicious and contain other statements that you do not intend.
UserInput
可能是恶意的,并包含您不想要的其他陈述。
To avoid it, you need to avoid concatenating your query together.
为避免这种情况,您需要避免将查询连接在一起。
You can accomplish this by using parametrized queries - check out the DBCommand
object for your particular DB flavor.
您可以通过使用参数化查询来实现这一点 - 检查DBCommand
您的特定数据库风格的对象。
回答by Max
Scott Guthrie posted a decent little articleabout this a while back. In it, he offers 5 suggestions for protecting yourself:
Scott Guthrie 不久前发布了一篇关于此的不错的小文章。在其中,他提供了 5 条保护自己的建议:
Don't construct dynamic SQL Statements without using a type-safe parameter encoding mechanism.[...]
Always conduct a security review of your application before ever put it in production, and establish a formal security process to review all code anytime you make updates.[...]
Never store sensitive data in clear-text within a database.[...]
Ensure you write automation unit tests that specifically verify your data access layer and application against SQL Injection attacks.[...]
Lock down your database to only grant the web application accessing it the minimal set of permissions that it needs to function.[...]
不要在不使用类型安全参数编码机制的情况下构造动态 SQL 语句。[...]
在将应用程序投入生产之前,始终对其进行安全,并建立正式的安全流程以在您进行更新时所有代码。[...]
切勿将敏感数据以明文形式存储在数据库中。[...]
确保您编写自动化单元测试,专门验证您的数据访问层和应用程序免受 SQL 注入攻击。[...]
锁定您的数据库,只授予访问它的 Web 应用程序运行所需的最小权限集。[...]
He does a decent job of explaining whythese are important, and links to several other resources as well...
他很好地解释了为什么这些很重要,并且还链接到了其他一些资源......
回答by Gertjan
Use parametrized queries and/or stored procedures and parse your parameters via SQL parameters. Nevergenerate SQL code by concatenating strings. Also do some reading about SQL injection and about writing secure code, because preventing SQL injection is only a small part of security. There is many more (like XSS - Cross Site Scripting). If a hacker wants to compromise your site/application he will look for more then only SQL injection.
使用参数化查询和/或存储过程并通过 SQL 参数解析您的参数。切勿通过连接字符串来生成 SQL 代码。还要阅读一些有关 SQL 注入和编写安全代码的文章,因为防止 SQL 注入只是安全性的一小部分。还有更多(例如 XSS - 跨站点脚本)。如果黑客想要破坏您的站点/应用程序,他会寻找更多的 SQL 注入。
回答by James
NEVER trust user input, always validate it, and use sql parameters. Should be enough basis to prevent SQL injection.
永远不要相信用户输入,总是验证它,并使用 sql 参数。应该有足够的基础来防止SQL注入。
回答by Eric J.
Always use only parameterized queries.
始终仅使用参数化查询。
回答by IrishChieftain
Hopefully, this will help:
希望这会有所帮助:
http://www.codersbarn.com/post/2008/11/01/ASPNET-Data-Input-Validation.aspx
http://www.codersbarn.com/post/2008/11/01/ASPNET-Data-Input-Validation.aspx
The short answer is to use parameterized queries.
简短的回答是使用参数化查询。
Anthony :-) www.codersbarn.com
安东尼 :-) www.codersbarn.com