码迷,mamicode.com
首页 > 数据库 > 详细

浅谈C#.NET防止SQL注入式攻击

时间:2016-06-20 00:46:15      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:

1技术分享#region 防止sql注入式攻击(可用于UI层控制)
  2技术分享
  3技术分享/// 
  4技术分享/// 判断字符串中是否有SQL攻击代码
  5技术分享/// 
  6技术分享/// 传入用户提交数据
  7技术分享/// true-安全;false-有注入攻击现有;
  8技术分享public bool ProcessSqlStr(string inputString)
  9技术分享{
 10技术分享    string SqlStr = @"and|or|exec|execute|insert|select|delete|update|alter|create|drop|count|\*|chr|char|asc|mid|substring|master|truncate|declare|xp_cmdshell|restore|backup|net +user|net +localgroup +administrators";
 11技术分享    try
 12技术分享    {
 13技术分享        if ((inputString != null) && (inputString != String.Empty))
 14技术分享        {
 15技术分享            string str_Regex = @"\b(" + SqlStr + @")\b";
 16技术分享
 17技术分享            Regex Regex = new Regex(str_Regex, RegexOptions.IgnoreCase);
 18技术分享            //string s = Regex.Match(inputString).Value; 
 19技术分享            if (true == Regex.IsMatch(inputString))
 20技术分享                return false;
 21技术分享
 22技术分享        }
 23技术分享    }
 24技术分享    catch
 25技术分享    {
 26技术分享        return false;
 27技术分享    }
 28技术分享    return true;
 29技术分享}
 30技术分享
 31技术分享
 32技术分享/// 
 33技术分享/// 处理用户提交的请求,校验sql注入式攻击,在页面装置时候运行
 34技术分享/// System.Configuration.ConfigurationSettings.AppSettings["ErrorPage"].ToString(); 为用户自定义错误页面提示地址,
 35技术分享/// 在Web.Config文件时里面添加一个 ErrorPage 即可
 36技术分享/// 
 37技术分享///     
 38技术分享/// 
 39技术分享public void ProcessRequest()
 40技术分享{
 41技术分享    try
 42技术分享    {
 43技术分享        string getkeys = "";
 44技术分享        string sqlErrorPage = System.Configuration.ConfigurationSettings.AppSettings["ErrorPage"].ToString();
 45技术分享        if (System.Web.HttpContext.Current.Request.QueryString != null)
 46技术分享        {
 47技术分享
 48技术分享            for (int i = 0; i < System.Web.HttpContext.Current.Request.QueryString.Count; i++)
 49技术分享            {
 50技术分享                getkeys = System.Web.HttpContext.Current.Request.QueryString.Keys[i];
 51技术分享                if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.QueryString[getkeys]))
 52技术分享                {
 53技术分享                    System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage + "?errmsg=" + getkeys + "有SQL攻击嫌疑!");
 54技术分享                    System.Web.HttpContext.Current.Response.End();
 55技术分享                }
 56技术分享            }
 57技术分享        }
 58技术分享        if (System.Web.HttpContext.Current.Request.Form != null)
 59技术分享        {
 60技术分享            for (int i = 0; i < System.Web.HttpContext.Current.Request.Form.Count; i++)
 61技术分享            {
 62技术分享                getkeys = System.Web.HttpContext.Current.Request.Form.Keys[i];
 63技术分享                if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.Form[getkeys]))
 64技术分享                {
 65技术分享                    System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage + "?errmsg=" + getkeys + "有SQL攻击嫌疑!");
 66技术分享                    System.Web.HttpContext.Current.Response.End();
 67技术分享                }
 68技术分享            }
 69技术分享        }
 70技术分享    }
 71技术分享    catch
 72技术分享    {
 73技术分享        // 错误处理: 处理用户提交信息!
 74技术分享    }
 75技术分享}
 76技术分享#endregion
 77技术分享
 78技术分享
 79技术分享
 80技术分享
 81技术分享#region 转换sql代码(也防止sql注入式攻击,可以用于业务逻辑层,但要求UI层输入数据时候进行解码)
 82技术分享/// 
 83技术分享/// 提取字符固定长度
 84技术分享/// 
 85技术分享/// 
 86技术分享/// 
 87技术分享/// 
 88技术分享public string CheckStringLength(string inputString, Int32 maxLength)
 89技术分享{
 90技术分享    if ((inputString != null) && (inputString != String.Empty))
 91技术分享    {
 92技术分享        inputString = inputString.Trim();
 93技术分享
 94技术分享        if (inputString.Length > maxLength)
 95技术分享            inputString = inputString.Substring(0, maxLength);
 96技术分享    }
 97技术分享    return inputString;
 98技术分享}
 99技术分享
100技术分享/// 
101技术分享/// 将输入字符串中的sql敏感字,替换成"[敏感字]",要求输出时,替换回来
102技术分享/// 
103技术分享/// 
104技术分享/// 
105技术分享public string MyEncodeInputString(string inputString)
106技术分享{
107技术分享    //要替换的敏感字
108技术分享    string SqlStr = @"and|or|exec|execute|insert|select|delete|update|alter|create|drop|count|\*|chr|char|asc|mid|substring|master|truncate|declare|xp_cmdshell|restore|backup|net +user|net +localgroup +administrators";
109技术分享    try
110技术分享    {
111技术分享        if ((inputString != null) && (inputString != String.Empty))
112技术分享        {
113技术分享            string str_Regex = @"\b(" + SqlStr + @")\b";
114技术分享
115技术分享            Regex Regex = new Regex(str_Regex, RegexOptions.IgnoreCase);
116技术分享            //string s = Regex.Match(inputString).Value; 
117技术分享            MatchCollection matches = Regex.Matches(inputString);
118技术分享            for (int i = 0; i < matches.Count; i++)
119技术分享                inputString = inputString.Replace(matches[i].Value, "[" + matches[i].Value + "]");
120技术分享
121技术分享        }
122技术分享    }
123技术分享    catch
124技术分享    {
125技术分享        return "";
126技术分享    }
127技术分享    return inputString;
128技术分享
129技术分享}
130技术分享
131技术分享/// 
132技术分享/// 将已经替换成的"[敏感字]",转换回来为"敏感字"
133技术分享/// 
134技术分享/// 
135技术分享/// 
136技术分享public string MyDecodeOutputString(string outputstring)
137技术分享{
138技术分享    //要替换的敏感字
139技术分享    string SqlStr = @"and|or|exec|execute|insert|select|delete|update|alter|create|drop|count|\*|chr|char|asc|mid|substring|master|truncate|declare|xp_cmdshell|restore|backup|net +user|net +localgroup +administrators";
140技术分享    try
141技术分享    {
142技术分享        if ((outputstring != null) && (outputstring != String.Empty))
143技术分享        {
144技术分享            string str_Regex = @"\[\b(" + SqlStr + @")\b\]";
145技术分享            Regex Regex = new Regex(str_Regex, RegexOptions.IgnoreCase);
146技术分享            MatchCollection matches = Regex.Matches(outputstring);
147技术分享            for (int i = 0; i < matches.Count; i++)
148技术分享                outputstring = outputstring.Replace(matches[i].Value, matches[i].Value.Substring(1, matches[i].Value.Length - 2));
149技术分享
150技术分享        }
151技术分享    }
152技术分享    catch
153技术分享    {
154技术分享        return "";
155技术分享    }
156技术分享    return outputstring;
157技术分享}
158技术分享#endregion

我们的解决方式是:
1、首先在UI录入时,要控制数据的类型和长度、防止SQL注入式攻击,系统提供检测注入式攻击的函数,一旦检测出注入式攻击,该数据即不能提交;
2、业务逻辑层控制,通过在方法内部将SQL关键字用一定的方法屏蔽掉,然后检查数据长度,保证提交SQL时,不会有SQL数据库注入式攻击代码;但是这样处理后,要求UI输出时将屏蔽的字符还原。因此系统提供屏蔽字符 的函数和还原字符的函数。
3、在数据访问层,绝大多数采用存储过程访问数据,调用时以存储过程参数的方式访问,也会很好的防止注入式攻击。

浅谈C#.NET防止SQL注入式攻击

标签:

原文地址:http://www.cnblogs.com/BrokenIce/p/5599182.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!