且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何避免SQL注入攻击?

更新时间:2022-11-27 15:29:05

避免SQL注入攻击的正确方法不是简单地禁止某些概念字符,使用参数化的SQL。你可以在这里找到一些关于参数化SQL主题的好文章:



http://www.codinghorror.com/blog/archives/000275.html



http://www.codeproject.com/KB/database/ParameterizingAdHocSQL.aspx



现在你提到你正在使用ASP.net,我可以给你一些链接,专门处理ASP中的SQL注入。



http://en.csharp-online.net/ASP .NET_Security_Hacks%E2%80%94Avoiding_SQL_Injection
http://www.codeproject.com/KB/aspnet/SQL_Injection_.aspx?msg=3209511



这是一篇有关制作ASP的更一般的文章更安全:
http://www.codeproject.com/KB /web-security/Securing_ASP_NET_Apps.aspx



当然,MSDN文章SQL注入:
http://msdn.microsoft.com/en-us/library/ms998271.aspx


Yesterday I was speaking with a developer, and he mentioned something about restricting the insertions on database field, like, strings such as -- (minus minus).

At the same type, what I know is that is a good approach to escape HTML chars like <, > etc. Not --. Is this true? Do I have to worry about --, ++? Is it more like a myth or old stuff?


Update

Thanks a lot for all the answers, it's easy to understand like that since I'm kind of new to all of this. Well, to be more specific in this case our discussion was about and C# ASP.NET MVC website we're developing, so there's a complex open an account form in there with important information, so I'm not sure if MVC using Linq to interface with database already comes with this kind of protection or not. So if anyone could provides some hints about it, it would be great. Thanks again

The proper way to avoid SQL Injection attacks is not so simply disallow certain problamatic characters, but rather to use paramaterized SQL. You can find some good articles on the subject of paramaterized SQL here:

http://www.codinghorror.com/blog/archives/000275.html

http://www.codeproject.com/KB/database/ParameterizingAdHocSQL.aspx

Now that you mentioned that you are working with ASP.net I can give you some links that deal specifically with SQL Injection in ASP.

http://en.csharp-online.net/ASP.NET_Security_Hacks%E2%80%94Avoiding_SQL_Injection http://www.codeproject.com/KB/aspnet/SQL_Injection_.aspx?msg=3209511

Here is a more general article on making your ASP more secure: http://www.codeproject.com/KB/web-security/Securing_ASP_NET_Apps.aspx

And, of course the MSDN article on SQL injection: http://msdn.microsoft.com/en-us/library/ms998271.aspx