[ASP.NET]ASP.NET 4.0 Extensible Request Validation

[ASP.NET]ASP.NET 4.0 Extensible Request Validation

之前在這篇中有提到這東西:[ASP.NET]ASP.NET 4.0 Overview-Core Services

ASP.NET的request validation主要是用來防堵XSS攻擊,然而XSS的攻擊手法非常多光靠ASP.NET內建的機制可能無法全部都檔到,若我們身處的環境比較複雜,我們可以透過自訂request validation logic來達到檢查每個request的效果。

然後MSDN上針對RequestValidator的說明:
RequestValidator Class

By default, ASP.NET does not validate requests until code explicitly requests a value from the request. (This is sometimes referred to as lazy validation.) For example, ASP.NET does not validate query string values until code accesses the QueryString collection. By default, ASP.NET also does not validate some types of request data, such as form values, cookies, the names of files that have been uploaded using HTTP, and the value of the RawUrl property.

The RequestValidator class is a base class that you can implement in order to provide custom request validation. By implementing this class, you can determine when validation occurs and what type of request data to perform validation on.

預設,ASP.NET本身不會檢查QueryString、Form、Cookie等內容的安全性,但我們可透過一些設定或者其他解決方案來協助我們做一些安全性的防堵,最常見的幾種方法我之前有大概提過:
[資訊安全]防範Cross-Site-Scripting(XSS)

[ASP.NET]防止跨網站(XSS)指令碼攻擊

[資訊安全]IE8.0的Anti-XSS機制

除了使用者的使用習慣外,程式的安全性控制也很重要,而IE瀏覽器本身也有防範的方法了,方法不嫌多,ASP.NET 4.0又多了一個可能的做法,就是透過RequestValidator去檢查每個Request的QueryString、Form、Cookie等內容是否有可能會造成安全性問題,範例如下:

Step1.新增一個Class MyRequestValidator
image 

Step2.修改MyRequestValidator繼承自RequestValidator,記得要using System.Web.Util;,然後override
IsValidRequestString


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Util;

namespace Validate
{
    /// <summary>
    /// Summary description for MyRequestValidator
    /// </summary>
    public class MyRequestValidator : RequestValidator
    {
        protected override bool IsValidRequestString(
              HttpContext context, string value,
              RequestValidationSource requestValidationSource,
                  string collectionKey,
              out int validationFailureIndex)
        {

            if (context.Request.QueryString["aa"].Contains("<script>"))
            {
                validationFailureIndex = 1;
                return false;
            }
            else
            {
                validationFailureIndex = 0;
                return true;
            }
        }
    }
}



Step3.在web.config的<Stenem.web>中加入下面這個設定,指定收到request時要由那個Type來進行處理。


<httpRuntime requestValidationType="Validate.MyRequestValidator" />


Step4.跑跑看吧,因為是我用QueryString做為我的判斷條件,所以錯誤訊息是在QueryString中偵測到潛在的安全性問題。
image 

若我改成以Session來做判斷:


            if (context.Session["AA"] == "AA")
            {
                validationFailureIndex = 1;
                return false;
            }
            else
            {
                validationFailureIndex = 0;
                return true;
            }



錯誤訊息就變這樣了,它會依不同的判斷條件來決定錯誤訊息:
image

再加上一些Error handling的技巧,就可以將這個功能串起來了,這個功能雖然還不錯,但使用上似乎沒那麼簡單,但這個解決方案確實有機會解決使用者端主動攻擊的XSS問題,但對於程式中自己寫出的XSS問題我還是比較習慣使用Anti-XssLibrary來處理這一類的問題。


參考資料:
RequestValidator Class
RequestValidator.IsValidRequestString Method

[資訊安全]防範Cross-Site-Scripting(XSS)
[ASP.NET]防止跨網站(XSS)指令碼攻擊
[資訊安全]IE8.0的Anti-XSS機制

游舒帆 (gipi)

探索原力Co-founder,曾任TutorABC協理與鼎新電腦總監,並曾獲選兩屆微軟最有價值專家 ( MVP ),離開職場後創辦探索原力,致力於協助青少年培養面對未來的能力。認為教育與組織育才其實息息相關,都是在為未來儲備能量,2018年起成立為期一年的專題課程《職涯躍升的關鍵24堂課》,為培養台灣未來的領袖而努力。