- #region 防止sql注入式攻击(可用于UI层控制)
-
- ///
- /// 判断字符串中是否有SQL攻击代码
- ///
- /// 传入用户提交数据
- /// true-安全;false-有注入攻击现有;
- public bool ProcessSqlStr( string inputString)
- {
- 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 " ;
- try
- {
- if ((inputString != null ) && (inputString != String.Empty))
- {
- string str_Regex = @" \b( " + SqlStr + @" )\b " ;
-
- Regex Regex = new Regex(str_Regex, RegexOptions.IgnoreCase);
- // string s = Regex.Match(inputString).Value;
- if ( true == Regex.IsMatch(inputString))
- return false ;
-
- }
- }
- catch
- {
- return false ;
- }
- return true ;
- }
- ///
- /// 处理用户提交的请求,校验sql注入式攻击,在页面装置时候运行
- /// System.Configuration.ConfigurationSettings.AppSettings["ErrorPage"].ToString(); 为用户自定义错误页面提示地址,
- /// 在Web.Config文件时里面添加一个 ErrorPage 即可
- ///
- ///
- ///
- public void ProcessRequest()
- {
- try
- {
- string getkeys = "" ;
- string sqlErrorPage = System.Configuration.ConfigurationSettings.AppSettings[ " ErrorPage " ].ToString();
- if (System.Web.HttpContext.Current.Request.QueryString != null )
- {
- for ( int i = 0 ; i < System.Web.HttpContext.Current.Request.QueryString.Count; i ++ )
- {
- getkeys = System.Web.HttpContext.Current.Request.QueryString.Keys;
- if ( ! ProcessSqlStr(System.Web.HttpContext.Current.Request.QueryString[getkeys]))
- {
- System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage + " ?errmsg= " + getkeys + " 有SQL攻击嫌疑! " );
- System.Web.HttpContext.Current.Response.End();
- }
- }
- }
- if (System.Web.HttpContext.Current.Request.Form != null )
- {
- for ( int i = 0 ; i < System.Web.HttpContext.Current.Request.Form.Count; i ++ )
- {
- getkeys = System.Web.HttpContext.Current.Request.Form.Keys;
- if ( ! ProcessSqlStr(System.Web.HttpContext.Current.Request.Form[getkeys]))
- {
- System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage + " ?errmsg= " + getkeys + " 有SQL攻击嫌疑! " );
- System.Web.HttpContext.Current.Response.End();
- }
- }
- }
- }
- catch
- {
- // 错误处理: 处理用户提交信息!
- }
- }
- #endregion
Kopēt kodu Mūsu risinājums ir: 1. Pirmkārt, ievadot lietotāja saskarni, lai kontrolētu datu veidu un garumu, lai novērstu SQL injekcijas uzbrukumus, sistēma nodrošina funkciju, lai atklātu injekcijas uzbrukumus, tiklīdz injekcijas uzbrukums ir atklāts, datus nevar iesniegt; 2. Biznesa loģikas slāņa kontrole, bloķējot SQL atslēgvārdus noteiktā veidā metodes iekšienē un pēc tam pārbaudot datu garumu, lai nodrošinātu, ka, iesniedzot SQL, nebūs SQL datu bāzes injekcijas uzbrukuma koda; Tomēr pēc šīs apstrādes maskētās rakstzīmes tiek atjaunotas, kad tiek izvadīta lietotāja saskarne. Tāpēc sistēma nodrošina funkcijas, lai aizsargātu rakstzīmes un funkcijas, lai atjaunotu rakstzīmes. 3. Datu piekļuves slānī lielākajai daļai datu piekļūst saglabātās procedūras, un saglabātie procedūras parametri tiek piekļūsti, kad tie tiek izsaukti, kas arī novērsīs injekcijas uzbrukumus.
|