无为清净楼资源网 Design By www.qnjia.com
什么是SQL注入?
我理解的sql注入就是一些人可以通过恶意的参数输入,让后台执行这段SQL,然后达到获取数据或者破坏数据库的目的!
举个简单的查询例子,后台sql是拼接的:select * from Test where name='+参数传递+';前台页面要求输入name,那么黑客可以输入: ';DROP TABLE Test;-- 不要小瞧这一段SQL代码:
select * from Test where name=' ';DROP TABLE Test;--';在SQL中是正确的,可执行的,但是执行后整个Test表都删除了,网站崩溃!
最好的解决方法
最好的办法就是不写拼接SQL,改用参数化SQL,推荐新项目使用。这里不做介绍,感兴趣的朋友可以自行搜索一下,本文介绍的方法适合老项目,就是没有使用参数化SQL开发的程序。
使用过滤函数来过滤
将SQL一些危险的关键字,还有注释百分号以及分号这些根本在我们正常写代码的时候根本不会出现的字符都过滤掉,这样能最大限度的保证SQL执行是安全的,代码如下:
public class SqlFilter { public static void Filter() { string fileter_sql = "execute,exec,select,insert,update,delete,create,drop,alter,exists,table,sysobjects,truncate,union,and,order,xor,or,mid,cast,where,asc,desc,xp_cmdshell,join,declare,nvarchar,varchar,char,sp_oacreate,wscript.shell,xp_regwrite,',%,;,--"; try { // -----------------------防 Post 注入----------------------- if (HttpContext.Current.Request.Form != null) { PropertyInfo isreadonly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic); //把 Form 属性改为可读写 isreadonly.SetValue(HttpContext.Current.Request.Form, false, null); for (int k = 0; k < System.Web.HttpContext.Current.Request.Form.Count; k++) { string getsqlkey = HttpContext.Current.Request.Form.Keys[k]; string sqlstr = HttpContext.Current.Request.Form[getsqlkey]; string[] replace_sqls = fileter_sql.Split(','); foreach (string replace_sql in replace_sqls) { sqlstr = Regex.Replace(sqlstr, replace_sql, "", RegexOptions.IgnoreCase); } HttpContext.Current.Request.Form[getsqlkey] = sqlstr; } } // -----------------------防 GET 注入----------------------- if (HttpContext.Current.Request.QueryString != null) { PropertyInfo isreadonly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic); //把 QueryString 属性改为可读写 isreadonly.SetValue(HttpContext.Current.Request.QueryString, false, null); for (int k = 0; k < System.Web.HttpContext.Current.Request.QueryString.Count; k++) { string getsqlkey = HttpContext.Current.Request.QueryString.Keys[k]; string sqlstr = HttpContext.Current.Request.QueryString[getsqlkey]; string[] replace_sqls = fileter_sql.Split(','); foreach (string replace_sql in replace_sqls) { sqlstr = Regex.Replace(sqlstr, replace_sql, "", RegexOptions.IgnoreCase); } HttpContext.Current.Request.QueryString[getsqlkey] = sqlstr; } } // -----------------------防 Cookies 注入----------------------- if (HttpContext.Current.Request.Cookies != null) { PropertyInfo isreadonly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic); //把 Cookies 属性改为可读写 isreadonly.SetValue(HttpContext.Current.Request.Cookies, false, null); for (int k = 0; k < System.Web.HttpContext.Current.Request.Cookies.Count; k++) { string getsqlkey = HttpContext.Current.Request.Cookies.Keys[k]; string sqlstr = HttpContext.Current.Request.Cookies[getsqlkey].Value; string[] replace_sqls = fileter_sql.Split(','); foreach (string replace_sql in replace_sqls) { sqlstr = Regex.Replace(sqlstr, replace_sql, "", RegexOptions.IgnoreCase); } HttpContext.Current.Request.Cookies[getsqlkey].Value = sqlstr; } } } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
标签:
SQL注入,ASP.NET过滤
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年09月27日
2024年09月27日
- 梁静茹.2007-崇拜【相信音乐】【WAV+CUE】
- 金佩姗.1997-喜新恋旧【现代派】【WAV+CUE】
- 梁静茹.2007-崇拜【相信音乐】【WAV+CUE】
- 陈晓东.1997-在乎你感受(EP)【宝丽金】【WAV+CUE】
- 纯音入心系列纯音乐《云水禅心 - 佛曲古筝礼赞1-2》1CD[MP3][190MB]
- 纯音入心系列纯音乐《精心传神-古筝》1CD[MP3][363.1MB]
- 纯音入心系列纯音乐《琴瑟古风之古筝精选曲》1CD[MP3][354.1MB]
- 陈晓东.1997-在乎你感受(EP)【宝丽金】【WAV+CUE】
- 黄凯芹.1990-给你留念(2024环球MQA-UHQCD限量版)【环球】【WAV+CUE】
- 张学友.1999-有个人【宝丽金】【WAV+CUE】
- 纯音入心系列纯音乐《古筝佛曲》1CD[MP3][411.1MB]
- 纯音入心系列纯音乐《古筝世界名曲》1CD[MP3][38.9MB]
- 纯音入心系列纯音乐《古筝新奏-菩提妙音》1CD[MP3][112.3MB]
- 陈思安.1993-怀念福建金曲5CD【歌丽美】【WAV+CUE】
- 张学友.1999-有个人【宝丽金】【WAV+CUE】