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

 

  1 ContractedBlock.gif ExpandedBlockStart.gif 防止sql注入式攻击(可用于UI层控制) #region 防止sql注入式攻击(可用于UI层控制)
  2
  3ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// 
  4/// 判断字符串中是否有SQL攻击代码
  5/// 
  6/// 传入用户提交数据
  7/// true-安全;false-有注入攻击现有;

  8public bool ProcessSqlStr(string inputString)
  9ExpandedSubBlockStart.gifContractedSubBlock.gif{
 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
 12ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 13        if ((inputString != null&& (inputString != String.Empty))
 14ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 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
 25ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 26        return false;
 27    }

 28    return true;
 29}

 30
 31
 32ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// 
 33/// 处理用户提交的请求,校验sql注入式攻击,在页面装置时候运行
 34/// System.Configuration.ConfigurationSettings.AppSettings["ErrorPage"].ToString(); 为用户自定义错误页面提示地址,
 35/// 在Web.Config文件时里面添加一个 ErrorPage 即可
 36/// 
 37///     
 38/// 

 39public void ProcessRequest()
 40ExpandedSubBlockStart.gifContractedSubBlock.gif{
 41    try
 42ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 43        string getkeys = "";
 44        string sqlErrorPage = System.Configuration.ConfigurationSettings.AppSettings["ErrorPage"].ToString();
 45        if (System.Web.HttpContext.Current.Request.QueryString != null)
 46ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 47
 48            for (int i = 0; i < System.Web.HttpContext.Current.Request.QueryString.Count; i++)
 49ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 50                getkeys = System.Web.HttpContext.Current.Request.QueryString.Keys[i];
 51                if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.QueryString[getkeys]))
 52ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 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)
 59ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 60            for (int i = 0; i < System.Web.HttpContext.Current.Request.Form.Count; i++)
 61ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 62                getkeys = System.Web.HttpContext.Current.Request.Form.Keys[i];
 63                if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.Form[getkeys]))
 64ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 65                    System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage + "?errmsg=" + getkeys + "有SQL攻击嫌疑!");
 66                    System.Web.HttpContext.Current.Response.End();
 67                }

 68            }

 69        }

 70    }

 71    catch
 72ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 73        // 错误处理: 处理用户提交信息!
 74    }

 75}

 76#endregion

 77
 78
 79
 80
 81 ContractedBlock.gifExpandedBlockStart.gif 转换sql代码(也防止sql注入式攻击,可以用于业务逻辑层,但要求UI层输入数据时候进行解码) #region 转换sql代码(也防止sql注入式攻击,可以用于业务逻辑层,但要求UI层输入数据时候进行解码)
 82ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// 
 83/// 提取字符固定长度
 84/// 
 85/// 
 86/// 
 87/// 

 88public string CheckStringLength(string inputString, Int32 maxLength)
 89ExpandedSubBlockStart.gifContractedSubBlock.gif{
 90    if ((inputString != null&& (inputString != String.Empty))
 91ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 92        inputString = inputString.Trim();
 93
 94        if (inputString.Length > maxLength)
 95            inputString = inputString.Substring(0, maxLength);
 96    }

 97    return inputString;
 98}

 99
100ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// 
101/// 将输入字符串中的sql敏感字,替换成"[敏感字]",要求输出时,替换回来
102/// 
103/// 
104/// 

105public string MyEncodeInputString(string inputString)
106ExpandedSubBlockStart.gifContractedSubBlock.gif{
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
110ExpandedSubBlockStart.gifContractedSubBlock.gif    {
111        if ((inputString != null&& (inputString != String.Empty))
112ExpandedSubBlockStart.gifContractedSubBlock.gif        {
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
124ExpandedSubBlockStart.gifContractedSubBlock.gif    {
125        return "";
126    }

127    return inputString;
128
129}

130
131ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// 
132/// 将已经替换成的"[敏感字]",转换回来为"敏感字"
133/// 
134/// 
135/// 

136public string MyDecodeOutputString(string outputstring)
137ExpandedSubBlockStart.gifContractedSubBlock.gif{
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
141ExpandedSubBlockStart.gifContractedSubBlock.gif    {
142        if ((outputstring != null&& (outputstring != String.Empty))
143ExpandedSubBlockStart.gifContractedSubBlock.gif        {
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
153ExpandedSubBlockStart.gifContractedSubBlock.gif    {
154        return "";
155    }

156    return outputstring;
157}

158#endregion

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

转载:http://blog.csdn.net/yufangbo/archive/2008/07/21/2685589.aspx

转载于:https://www.cnblogs.com/X-Jonney/archive/2009/07/05/1517309.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值