C# 스테이션 간 스크립트 주입 방지 SXX

20615 단어
//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,        。

        WebContel_damu m_delsession = new WebContel_damu();
        m_delsession.CheckLogin_del(Session["user"].ToString());
    }

    void Application_BeginRequest(object sender, EventArgs e)
    {

        string q = "

, !
"
; if (Request.Cookies != null) { if (Nvs_Net_Safe.CookieData()) { Response.Write(q); Response.End(); } } if (Request.UrlReferrer != null) { if (Nvs_Net_Safe.referer()) { Response.Write(q); Response.End(); } } if (Request.RequestType.ToUpper() == "POST") { if (Nvs_Net_Safe.PostData()) { Response.Write(q); Response.End(); } } if (Request.RequestType.ToUpper() == "GET") { if (Nvs_Net_Safe.GetData()) { Response.Write(q); Response.End(); } } } </script>
//Nvs_Net_Safe       
using System;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;
public class Nvs_Net_Safe
{

   // private const string StrRegex = @"\b(and|or)\b.{1,6}?(=|>|
    private const string StrRegex = @"]+?style=[\w]+?:expression\(|\b(alert|confirm|prompt)\b|^\+/v(8|9)|]*?=[^>]*?[^>]*?>|\b(and|or)\b.{1,6}?(=|>|;
    public static bool PostData()
    {
        bool result = false;

        for (int i = 0; i < HttpContext.Current.Request.Form.Count; i++)
        {
            result = CheckData(HttpContext.Current.Request.Form[i].ToString());
            if (result)
            {
                break;
            }
        }
        return result;
    }

 
    public static bool GetData()
    {
        bool result = false;

        for (int i = 0; i < HttpContext.Current.Request.QueryString.Count; i++)
        {
            result = CheckData(HttpContext.Current.Request.QueryString[i].ToString());
            if (result)
            {
                break;
            }
        }
        return result;
    }


    public static bool CookieData()
    {
        bool result = false;
        for (int i = 0; i < HttpContext.Current.Request.Cookies.Count; i++)
        {
            result = CheckData(HttpContext.Current.Request.Cookies[i].Value.ToLower());
            if (result)
            {
                break;
            }
        }
        return result;
    
    }
    public static bool referer()
    {
        bool result = false;
        return result = CheckData(HttpContext.Current.Request.UrlReferrer.ToString());
    }

    public static bool CheckData(string inputData)
    {
        if (Regex.IsMatch(inputData, StrRegex))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

  
}

좋은 웹페이지 즐겨찾기