在Global.asax檔案裡實現通用防SQL注入漏洞程式

來源:互聯網
上載者:User

標籤:

首先,建立一個SQLInjectionHelper類完成惡意代碼的檢查

代碼如下:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Text.RegularExpressions; /// <summary>///SQLInjectionHelper 的摘要說明/// </summary>public class SQLInjectionHelper{    /// <summary>    /// 擷取Post的資料    /// </summary>    /// <param name="request"></param>    /// <returns></returns>    public static bool ValidUrlData(string request)    {        bool result = false;        if (request == "POST")        {            for (int i = 0; i < HttpContext.Current.Request.Form.Count; i++)            {                result = ValidData(HttpContext.Current.Request.Form[i].ToString());                if (result)                {                    break;                }            }        }        else        {            for (int i = 0; i < HttpContext.Current.Request.QueryString.Count; i++)            {                result = ValidData(HttpContext.Current.Request.QueryString[i].ToString());                if (result)                {                    break;                }            }        }        return result;    }     /// <summary>    /// 驗證是否存在注入代碼    /// </summary>    /// <param name="inputData"></param>    /// <returns></returns>    private static bool ValidData(string inputData)    {        //驗證inputData是否包含惡意集合        if (Regex.IsMatch(inputData, GetRegexString()))        {            return true;        }        else        {            return false;        }    }     /// <summary>    /// 擷取Regex    /// </summary>    /// <returns></returns>    private static string GetRegexString()    {        //構造SQL的注入關鍵字符        string[] strChar = { "and", "exec", "insert", "select", "update", "delete", "count", "from", "drop", "asc", "or", "char", "%", ";", ":", "\‘", "\"", "-", "chr", "master", "mid", "truncate", "declare", "char", "SiteName", "/add", "xp_cmdshell", "net user", "net localgroup administrators", "exec master.dbo.xp_cmdshell" };        string str_Regex = ".*(";        for (int i = 0; i < strChar.Length - 1; i++)        {            str_Regex += strChar[i] + "|";        }        str_Regex += strChar[strChar.Length - 1] + ").*";        return str_Regex;    }}

  

有此類後即可使用Global.asax中的Application_BeginRequest(object sender, EventArgs e)事件來實現表單或者URL提交資料的擷取,擷取後傳給SQLInjectionHelper類ValidUrlData方法來完成檢查

代碼如下

protected void Application_BeginRequest(object sender, EventArgs e)    {        bool result = false;        result = SQLInjectionHelper.ValidUrlData(Request.RequestType.ToUpper());        if (result)        {            Response.Write("您提交的資料有惡意字元");            Response.End();        }    }

  

下面以一個小程式測試:

建立一個頁面,如下

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title></head><body>    <form id="form1" runat="server">    <div>           <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>        <br />        <asp:Button ID="btnPost" runat="server" Text="擷取Post資料"            onclick="btnPost_Click" />       </div>    <asp:Button ID="btnGet" runat="server" Text="擷取Get資料" onclick="btnGet_Click" />    </form></body></html>

  

 

分別添加單擊事件,如下

protected void btnPost_Click(object sender, EventArgs e)    {     }    protected void btnGet_Click(object sender, EventArgs e)    {        Response.Redirect("Default.aspx?a=1&b=2&c=3");    }

  

在文字框中輸入非法字串,無論post請求還是get請求,都會被防SQL注入程式所截獲

                      圖1 測試防SQL注入程式的頁面

                               圖2 錯誤資訊

在Global.asax檔案裡實現通用防SQL注入漏洞程式

聯繫我們

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