使用 HttpModule实现sql防注入

sql注入是被谈的很多的一个话题,有很多的方法能够实现sql的防注入,在这里就简单说一下如果使用HttpModule来实现sql的防注入。

    在项目中添加一个类让其实现IHttpModule接口。IHttpModule接口有两个方法 Init 和 Dispose。然后在Init方法中来订阅

    AcquireRequestState事件。

 

    为什么是AcquireRequestState 事件而不是Begin_Request呢 ,因为在Begin_Request执行的时候还没有加载session状态,而在处理的时侯

可能会用到session。

    在AcquireRequestState 事件中我们就要进行相应的处理了,思路如下,一般网站提交数据只有两个地方,表单和url,所有就在该事件中将从这两处

提交的数据截取,判断是否有一些危险字符,然后做相应处理。代码如下

  1.    1. private void context_AcquireRequestState(object sender, EventArgs e)
  2.    2.     {
  3.    3.         HttpContext context = ((HttpApplication)sender).Context;
  4.    4.
  5.    5.         try
  6.    6.         {
  7.    7.             string getkeys = string.Empty;
  8.    8.             string sqlErrorPage = "~/Error.aspx";//转向的错误提示页面 
  9.    9.             string keyvalue = string.Empty;
  10.   10.
  11.   11.             string requestUrl = context.Request.Path.ToString();
  12.   12.             //url提交数据
  13.   13.             if (context.Request.QueryString != null)
  14.   14.             {
  15.   15.                 for (int i = 0; i < context.Request.QueryString.Count; i++)
  16.   16.                 {
  17.   17.                     getkeys = context.Request.QueryString.Keys[i];
  18.   18.                     keyvalue = context.Server.UrlDecode(context.Request.QueryString[getkeys]);
  19.   19.
  20.   20.                     if (!ProcessSqlStr(keyvalue))
  21.   21.                     {
  22.   22.                         context.Response.Redirect(sqlErrorPage);
  23.   23.                         context.Response.End();
  24.   24.                         break;
  25.   25.                     }
  26.   26.                 }
  27.   27.             }
  28.   28.             //表单提交数据
  29.   29.             if (context.Request.Form != null)
  30.   30.             {
  31.   31.                 for (int i = 0; i < context.Request.Form.Count; i++)
  32.   32.                 {
  33.   33.                     getkeys = context.Request.Form.Keys[i];
  34.   34.                     keyvalue = context.Server.HtmlDecode(context.Request.Form[i]);//[getkeys];
  35.   35.                     if (getkeys == "__VIEWSTATE"continue;
  36.   36.                     if (!ProcessSqlStr(keyvalue))
  37.   37.                     {
  38.   38.                         context.Response.Redirect(sqlErrorPage);
  39.   39.                         context.Response.End();
  40.   40.                         break;
  41.   41.                     }
  42.   42.                 }
  43.   43.             }
  44.   44.         }
  45.   45.         catch (Exception ex)
  46.   46.         {
  47.   47.         }
  48.   48.     }
上面的代码只是做了简单的处理,当然也可以在事件中将输入非法关键字的用户ip ,操作页面的url,时间等信息记录在数据库中或是记录在日志中。

而且还用到了一个叫ProcessSqlStr的方法,这个方法就是用来处理字符串的,判断是否合法,如下

  1.    1.  private bool ProcessSqlStr(string str)
  2.    2.     {
  3.    3.         bool returnValue = true;
  4.    4.         try
  5.    5.         {
  6.    6.             if (str.Trim() != "")
  7.    7.             {
  8.    8.                 //string sqlStr = ConfigurationManager.AppSettings["FilterSql"].Trim();
  9.    9.                 string sqlStr = "declare |exec|varchar|cursor|begin|open |drop |creat |select |truncate";
  10.   10.
  11.   11.                 string[] sqlStrs = sqlStr.Split('|');
  12.   12.                 foreach (string ss in sqlStrs)
  13.   13.                 {
  14.   14.                     if (str.ToLower().IndexOf(ss) >= 0)
  15.   15.                     {
  16.   16.                         m_sqlstr = ss;
  17.   17.                         returnValue = false;
  18.   18.                         break;
  19.   19.                     }
  20.   20.                 }
  21.   21.             }
  22.   22.         }
  23.   23.         catch
  24.   24.         {
  25.   25.             returnValue = false;
  26.   26.         }
  27.   27.         return returnValue;
  28.   28.     }
  29. 到这儿类就写好了,再在web.config中做相应的配置就大功告成
    1. #   <httpModules>
    2. #       <add type="HttpModule" name="HttpModule"/>
    3. #   </httpModules>


        


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值