asp.net C# 全站防注入 利用Global.asax

全站防注入通用程序

在站点根目录下建立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,则不会引发该事件。

    }

    /// <summary>
    ///  替换所有注入的字符串,并记录注入时的信息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void Application_BeginRequest(object sender, EventArgs e)
    {
  // * * * * * * * * * * * * * * * * * * * * * * * *
  // 全局防注入
  // Author:依依秋寒
  // * * * * * * * * * * * * * * * * * * * * * * * *

        //在接收到一个应用程序请求时触发。
        string[] KeyWords = new string[] { ";", "'", "--", "xp_", "XP_", "xP_", "Xp_" };
        string[] safeKeys = "&#59;|&#39;|&#45;&#45;|&#120;&#112;&#95;|&#88;&#80;&#95;|&#120;&#80;&#95;|&#88;&#112;&#95;".Split('|');
        string QueryString = Server.UrlDecode(Request.QueryString.ToString());
        string url = Request.Url.AbsolutePath;
        //排除的扩展名
        string[] dotFileName = url.Split('.');
        string dotName = dotFileName[dotFileName.Length - 1];
        dotFileName = new string[] { "axd" };
        //出现被排除的扩展名时,直接退出
        foreach (string str in dotFileName)
        {
            if (str == dotName)
                return;
        }
        for(int i=0;i<KeyWords.Length ;i++)
        {
            string key = KeyWords[i];
            if (QueryString.Contains(key))
            {
                //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                //记录注入时的信息
                //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                string IntoRecord = System.DateTime.Now.ToString();
                IntoRecord += " " + Request.Url.Host;
                IntoRecord += " " + Request.RequestType;
                IntoRecord += " " + Request.Url.AbsolutePath;
                IntoRecord += " " + Server.UrlDecode(Request.QueryString.ToString());
                IntoRecord += " " + Request.UserHostAddress;
                IntoRecord += " " + Request.UserAgent;
                IntoRecord += "\r";

                try
                {
                    string path = Server.MapPath(@"/_IntoRecordLog/");
                    if (!System.IO.Directory.Exists(path))
                        System.IO.Directory.CreateDirectory(path);
                    System.IO.File.AppendAllText(path + DateTime.Now.ToString("yyyyMMdd") + ".log", IntoRecord, System.Text.Encoding.Default);
                }
                catch { }
                //替换注入的URL,并进行跳转
                QueryString = QueryString.Replace(key, safeKeys[i]);
                Response.Redirect(url + "?" + QueryString);
                Response.End();
            }
        }
    }
</script>

你可能感兴趣的:(asp.net)