国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

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

2024-07-10 13:30:02
字體:
供稿:網(wǎng)友
本文分享一個(gè)防SQL注入的過濾類SqlFilter,可以防止通過POST、GET和Cookies的注入。
 

什么是SQL注入?

我理解的sql注入就是一些人可以通過惡意的參數(shù)輸入,讓后臺執(zhí)行這段SQL,然后達(dá)到獲取數(shù)據(jù)或者破壞數(shù)據(jù)庫的目的!
舉個(gè)簡單的查詢例子,后臺sql是拼接的:select * from Test where name='+參數(shù)傳遞+';前臺頁面要求輸入name,那么黑客可以輸入: ';DROP TABLE Test;--   不要小瞧這一段SQL代碼:
select * from Test where name=' ';DROP TABLE Test;--';在SQL中是正確的,可執(zhí)行的,但是執(zhí)行后整個(gè)Test表都刪除了,網(wǎng)站崩潰!

最好的解決方法

最好的辦法就是不寫拼接SQL,改用參數(shù)化SQL,推薦新項(xiàng)目使用。這里不做介紹,感興趣的朋友可以自行搜索一下,本文介紹的方法適合老項(xiàng)目,就是沒有使用參數(shù)化SQL開發(fā)的程序。

使用過濾函數(shù)來過濾

將SQL一些危險(xiǎn)的關(guān)鍵字,還有注釋百分號以及分號這些根本在我們正常寫代碼的時(shí)候根本不會出現(xiàn)的字符都過濾掉,這樣能最大限度的保證SQL執(zhí)行是安全的,代碼如下:

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);    }  }}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 汕头市| 吴旗县| 乌鲁木齐县| 彩票| 郧西县| 阿瓦提县| 武强县| 通化县| 泗水县| 巩义市| 潞西市| 土默特左旗| 祥云县| 张家川| 襄樊市| 门头沟区| 南平市| 平舆县| 名山县| 县级市| 焉耆| 泰来县| 西充县| 淮南市| 南开区| 积石山| 罗源县| 聂拉木县| 新营市| 淮阳县| 睢宁县| 南丹县| 大姚县| 巩义市| 淮阳县| 桃园市| 双城市| 中方县| 德庆县| 隆尧县| 沙洋县|