架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 14938|回复: 0

[WinForm] C#.NET防止SQL注入式攻击

[复制链接]
发表于 2015-4-19 23:32:01 | 显示全部楼层 |阅读模式
  1. #region  防止sql注入式攻击(可用于UI层控制)
  2.   
  3.    ///
  4.    ///  判断字符串中是否有SQL攻击代码
  5.    ///
  6.    ///  传入用户提交数据
  7.    ///  true-安全;false-有注入攻击现有;
  8.    public   bool  ProcessSqlStr( string  inputString)
  9.      {
  10.       string  SqlStr  =   @"
  11. 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
  12. +localgroup +administrators " ;
  13.         try
  14.           {
  15.             if  ((inputString  !=   null )  &&  (inputString  !=  String.Empty))
  16.               {
  17.                 string  str_Regex  =   @" \b( "   +  SqlStr  +   @" )\b " ;
  18.   
  19.                Regex Regex  =   new  Regex(str_Regex, RegexOptions.IgnoreCase);
  20.                 // string s = Regex.Match(inputString).Value;
  21.                 if  ( true   ==  Regex.IsMatch(inputString))
  22.                     return   false ;
  23.   
  24.           }
  25.       }
  26.        catch
  27.          {
  28.            return   false ;
  29.       }
  30.        return   true ;
  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.                for  ( int  i  =   0 ; i  <  System.Web.HttpContext.Current.Request.QueryString.Count; i ++ )
  48.                 {
  49.                   getkeys  =  System.Web.HttpContext.Current.Request.QueryString.Keys;
  50.                     if  ( ! ProcessSqlStr(System.Web.HttpContext.Current.Request.QueryString[getkeys]))
  51.                       {
  52.                        System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage  +   " ?errmsg= "   +  getkeys  +   " 有SQL攻击嫌疑! " );
  53.                      System.Web.HttpContext.Current.Response.End();
  54.                  }
  55.              }
  56.          }
  57.           if  (System.Web.HttpContext.Current.Request.Form  !=   null )
  58.             {
  59.                for  ( int  i  =   0 ; i  <  System.Web.HttpContext.Current.Request.Form.Count; i ++ )
  60.                   {
  61.                    getkeys  =  System.Web.HttpContext.Current.Request.Form.Keys;
  62.                     if  ( ! ProcessSqlStr(System.Web.HttpContext.Current.Request.Form[getkeys]))
  63.                       {
  64.                        System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage  +   " ?errmsg= "   +  getkeys  +   " 有SQL攻击嫌疑! " );
  65.                        System.Web.HttpContext.Current.Response.End();
  66.                    }
  67.                }
  68.            }
  69.        }
  70.         catch
  71.           {
  72.             //  错误处理: 处理用户提交信息!
  73.        }
  74.    }
  75.    #endregion
复制代码
我们的解决方式是:
1、首先在UI录入时,要控制数据的类型和长度、防止SQL注入式攻击,系统提供检测注入式攻击的函数,一旦检测出注入式攻击,该数据即不能提交;
2、业务逻辑层控制,通过在方法内部将SQL关键字用一定的方法屏蔽掉,然后检查数据长度,保证提交SQL时,不会有SQL数据库注入式攻击代码;但是这样处理后,要求UI输出时将屏蔽的字符还原。因此系统提供屏蔽字符 的函数和还原字符的函数。
3、在数据访问层,绝大多数采用存储过程访问数据,调用时以存储过程参数的方式访问,也会很好的防止注入式攻击。







上一篇:ASP.NET性能优化之构建自定义文件缓存
下一篇:c#和asp.net防止SQL注入过滤方法
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|手机版|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2025-6-17 14:00

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表