.net通用防SQL注入漏洞程序(Global.asax方式)详细用法

来源:互联网 发布:淘宝微淘动态看不见的 编辑:程序博客网 时间:2024/05/18 02:48

在App_Code文件夹下建立SQLInjectionHelper.cs类

内容:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using System.Text.Regularexpression_r_r_r_rs;

/// <summary>
///SQLInjectionHelper 的摘要说明
/// </summary>
public class SQLInjectionHelper
{
    /// <summary>
    /// 获取Get的数据
    /// </summary>
    public static bool ValidUrlGetData()
    {
        bool result = false;

        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>
    /// 获取Post的数据
    /// </summary>
    public static bool ValidUrlPostData()
    {
        bool result = false;

        for (int i = 0; i < HttpContext.Current.Request.Form.Count; i++)
        {
            result = ValidData(HttpContext.Current.Request.Form[i].ToString());
            if (result)
            {
                //如果检测存在漏洞
                break;
            }
        }
        return result;
    }

    /// <summary>
    /// 验证是否存在注入代码
    /// </summary>
    /// <param name="inputData"></param>
    public static bool ValidData(string inputData)
    {
        //里面定义恶意字符集合
        string[] checkWord = { "and", "exec", "insert", "select", "delete", "update", "count", "from", "drop", "asc", "char", "*", "%", ";", ":", "/'", "/"", "chr", "mid", "master", "truncate", "char", "declare", "SiteName", "net user", "xp_cmdshell", "/add", "exec master.dbo.xp_cmdshell", "net localgroup administrators" };
        if (inputData == null || inputData == "")
        {
            return false;
        }
        else
        {
            foreach (string s in checkWord)
            {
                //验证inputData是否包含恶意集合
                if (inputData.ToString().ToLower().IndexOf(s) > -1)
                {
                    return true;
                }
                else
                {
                    continue;
                }
            }
            return false;
        }

    }
}


建立全局应用程序类:global.asax

内容:

<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e)
    {
        //在应用程序启动时运行的代码

    }
   
    void Application_End(object sender, EventArgs e)
    {
        //在应用程序关闭时运行的代码

    }
       
    void Application_Error(object sender, EventArgs e)
    { 
       

    }

    void Session_Start(object sender, EventArgs e)
    {
        //在新会话启动时运行的代码

    }

    void Session_End(object sender, EventArgs e)
    {
        //在会话结束时运行的代码。
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // InProc 时,才会引发 Session_End 事件。如果会话模式
        //设置为 StateServer 或 SQLServer,则不会引发该事件。

    }

    //在接收到一个应用程序请求时触发。对于一个请求来说,它是第一个被触发的事件,请求一般是用户输入的一个页面请求(URL)。
    void Application_BeginRequest(object sender, EventArgs e)
    {
        //检查是否有注入
        bool result = false;

        if (Request.RequestType.ToUpper() == "POST")
        {
            result = SQLInjectionHelper.ValidUrlPostData();//Post数据检查
        }
        else
        {
            result = SQLInjectionHelper.ValidUrlGetData();//Get数据检查
        }

        if (result)
        {
            Response.Write("您提交的数据有恶意字符!");
            Response.End();
        }
    }
      
</script>

 

在你的站点上测试就可以了

原创粉丝点击