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

來源:互聯網
上載者:User
什麼是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);    }   } }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.