欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET过滤类SqlFilter,防止SQL注入 原创

程序员文章站 2022-06-23 18:33:43
什么是sql注入? 我理解的sql注入就是一些人可以通过恶意的参数输入,让后台执行这段sql,然后达到获取数据或者破坏数据库的目的! 举个简单的查询例子,后台sql是拼...

什么是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);
    }

  }

}