麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > .NET > 正文

ASP.NET過濾類SqlFilter,防止SQL注入

2024-07-10 13:30:02
字體:
來源:轉載
供稿:網友
本文分享一個防SQL注入的過濾類SqlFilter,可以防止通過POST、GET和Cookies的注入。
 

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产精品一区二区手机在线观看 | 国产一级毛片高清视频 | 亚洲精品无码不卡在线播放he | 久久久久99999 | 国产乱free国语对白 | 少妇淫片免费一级毛片 | 在线看免费观看av | 久久国产精品系列 | 免费国产人成网站 | 艹男人的日日夜夜 | 偿还的影视高清在线观看 | 欧美一级二级毛片视频 | 欧美另类综合 | 日本人乱人乱亲乱色视频观看 | 国产精品视频2021 | 国产在线一级视频 | 国产妇女乱码一区二区三区 | 视频一区二区三区在线播放 | 3344永久免费 | 久久草在线看 | 一区二区久久电影 | 国产午夜精品久久久 | 国产一区二区三区在线观看视频 | 精品一区二区久久久久久久网精 | 亚洲5区 | 一本到免费视频 | 亚洲视频成人在线 | 精品中文字幕在线播放 | 国产成年人网站 | 久久性生活免费视频 | a视频在线免费观看 | tube69xxxxxhd| 夜夜夜精品视频 | 午夜久久久精品一区二区三区 | 久草视频在线资源 | 久久蜜桃香蕉精品一区二区三区 | 欧美成人精品一区二区 | 亚洲小视频网站 | 国产羞羞视频 | 在线播放亚洲 | 92看片淫黄大片欧美看国产片 |