asp.net防注入代碼

來源:互聯網
上載者:User
///  <summary>           /// 在 Application_BeginRequest中加入函數StartProcessRequest()         ///  </summary>           protected void Application_BeginRequest(Object sender, EventArgs e)        {                     StartProcessRequest();         } #region SQL注入式攻擊程式碼分析           / //  <summary>           /// 處理使用者提交的請求           ///  </summary>           private void StartProcessRequest()           {               try               {                   string getkeys = "";                   string sqlErrorPage = "/default.aspx";//如果有非法參數,轉向的錯誤提示頁面                   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);                               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 (getkeys == "__VIEWSTATE") continue;                           if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.Form[getkeys]))                           {                               System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage);                               System.Web.HttpContext.Current.Response.End();                           }                       }                   }                                  if (System.Web.HttpContext.Current.Request.Cookies != null)                   {                       for (int i = 0; i  < System.Web.HttpContext.Current.Request.Cookies.Count; i++)                       {                           getkeys = System.Web.HttpContext.Current.Request.Cookies.Keys;                          if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.Cookies[getkeys].ToString()))                           {                               System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage);                               System.Web.HttpContext.Current.Response.End();                           }                       }                   }               }               catch               {                   // 錯誤處理: 處理使用者提交資訊!               }           }           ///  <summary>           /// 分析使用者請求是否正常           ///  </summary>           ///  <param name="Str">傳入使用者提交資料  </param>           ///  <returns>返回是否含有SQL注入式攻擊代碼  </returns>           private bool ProcessSqlStr(string Str)           {               bool ReturnValue = true;              t ry               {                   if (Str.Trim() != "")                   {                       string SqlStr = "and |exec |insert |select |delete |update |count |* |chr |mid |master |truncate |char |declare";                       string[] anySqlStr = SqlStr.Split('|');                       f oreach (string ss in anySqlStr)                       {                           if (Str.ToLower().IndexOf(ss) >= 0)                           {                               ReturnValue = false;                               break;                           }                       }                   }               }               catch               {                   ReturnValue = false;               }              return ReturnValue;           }           #endregion [/pre]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.