栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > asp

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

asp 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ASP.NET过滤类SqlFilter,防止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);
    }

  }

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/56523.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号