2017-05-09 130 views
1

環境(?):有潛在危險的Request的值

IIS 8.5

.NET Framework版本:4.6.2(使用Web窗體)

的Windows Server 2012 R2

問題:

以下異常被報告:

BASE EXCEPTION: System.Web.HttpException (0x80004005): A potentially dangerous Request.Path value was detected from the client (?). 
    at System.Web.HttpRequest.ValidateInputIfRequiredByConfig() 
    at System.Web.HttpApplication.PipelineStepManager.ValidateHelper(HttpContext context) 

BASE EXCEPTION HRESUT: -2147467259 

EXCEPTION: System.Web.HttpException (0x80004005): A potentially dangerous Request.Path value was detected from the client (?). 
    at System.Web.HttpRequest.ValidateInputIfRequiredByConfig() 
    at System.Web.HttpApplication.PipelineStepManager.ValidateHelper(HttpContext context) 

在我們的日誌顯示的其他信息:

PATH_INFO 
/cities/index.aspx?locid=4163 
---- 
QUERY_STRING 
---- 
REMOTE_ADDR 
66.249.65.204 
---- 
REMOTE_HOST 
66.249.65.204 
---- 
REQUEST_METHOD 
GET 
---- 
SCRIPT_NAME 
/cities/index.aspx?locid=4163 
---- 
URL 
/cities/index.aspx?locid=4163 
---- 
HTTP_FROM 
googlebot(at)googlebot.com 
---- 
HTTP_USER_AGENT 
Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html) 

如果我剪切和粘貼在我的瀏覽器的路徑是什麼我不明白的是,呈現頁面就好了沒有錯誤。

問題:

  1. 爲什麼Googlebot的爬行網頁時產生這個錯誤,但是當我進入瀏覽器的路徑不會產生錯誤? (我發現奇怪的是,即使它存在,錯誤日誌也沒有顯示查詢字符串的值)。
  2. 爲什麼「?」性格被認爲有潛在危險

任何意見,將不勝感激,因爲我試圖瞭解這種特殊的「錯誤」是如何提出的,當路徑實際上是有效的。

在此先感謝。

回答

0

我漸漸明白,爲什麼查詢字符串並沒有顯示在我們的日誌什麼。 請求編碼「?「(%3F)將導致上述的異常被提出,例如:

/cities/index.aspx%3flocid=4163 

編碼%3F被解釋爲路徑的一部分,因此的異常」有潛在危險的Request的值爲從客戶端(?)檢測到「

當我在瀏覽器中輸入上面顯示的URL時 - 引發異常,日誌不包含查詢字符串,所以我只能假設一切正常,請求者在不應該編碼的時候;基本上破壞了URL的查詢字符串部分

我們也h ave requestValidationMode =「2.0」在system.web中,但不要使用requestPathInvalidCharacters(httpRuntime)設置。

1

從Asp.net 4.0+引入了一個嚴格的驗證,所以你看到的任何錯誤都可能是它的一部分。網址中存在一些可能導致XSS攻擊的危險字符。所以?就是其中之一。剩餘的字符如下:

< > * % & : \ ? 

也許有可能在web config是兩個解決方案

  1. ,你可以讓你的URL這些字符,或至少某些字符,可通過配置以下配置如下

    <system.web> <httpRuntime requestPathInvalidCharacters="&lt;,&gt;,*,%,&amp;,:,\,?" /> </system.web>

  2. 您可以回滾到asp.net 2.0中,具有以下配置

    <system.web> <httpRuntime requestValidationMode="2.0" /> </system.web>

0

如果您(或筆試者)訪問URL的ala https://domain.tld/<foobarhttps://domain.tld/</,則會發生同樣的情況。

即使啓用了自定義錯誤頁面,這將返回由IIS呈現的錯誤頁面,並且如果您在Application_Error中記錄錯誤,則可能發現日誌中充滿了來自掃描器/機器人等的噪音。

我發現一個相當簡單的解決方法是在Global.asax.cs的Application_Error中處理HttpException異常。這樣你就不需要用requestValidationMode來調整。

  1. 創建應用程序的根目錄下面的網頁:

    • 400.html - 對於IIS
    • 400.aspx - 爲ASP.NET
    • 404.html - 爲IIS
    • 404.aspx - 爲ASP.NET
    • 500.html - 對於IIS
    • 500.aspx - 爲ASP。 NET

      的.html文件有內容鼻翼這樣的:

      <!DOCTYPE html> 
      <html lang="en"> 
      <head> 
          <meta charset="utf-8" /> 
          <title>400 Bad request</title> 
      </head> 
      <body> 
          <h1>400 Bad request</h1> 
      </body> 
      </html> 
      

      的.aspx文件有內容鼻翼這樣的:

      <%@ Page Language="C#" %> 
      <% 
          Response.StatusCode = 400; 
          Server.Transfer("~/400.html"); 
      %> 
      

      確保您設置.aspx文件中適當的響應狀態代碼。

  2. 配置ASP。如下NET的自定義錯誤:

    <customErrors mode="RemoteOnly" redirectMode="ResponseRewrite" defaultRedirect="~/500.aspx"> 
        <error statusCode="400" redirect="~/400.aspx"/> 
        <error statusCode="404" redirect="~/404.aspx"/> 
        <error statusCode="500" redirect="~/500.aspx"/> 
    </customErrors> 
    
  3. 配置IIS的自定義錯誤如下:

    <httpErrors errorMode="DetailedLocalOnly"> 
    <remove statusCode="400"/> 
    <error statusCode="400" path="400.html" responseMode="File"/> 
    <remove statusCode="404"/> 
    <error statusCode="404" path="404.html" responseMode="File"/> 
    <remove statusCode="500"/> 
    <error statusCode="500" path="500.html" responseMode="File"/> 
    </httpErrors> 
    
  4. 調整的Global.asax.cs相應:

    protected void Application_Error(object sender, EventArgs e) 
    { 
        var lastError = Server.GetLastError(); 
        Server.ClearError(); 
    
        if (lastError.GetType() == typeof(HttpException)) 
        { 
         Response.StatusCode = 400; 
         Server.Transfer("400.html"); 
        } 
        else 
        { 
         Response.StatusCode = 500; 
         Server.Transfer("500.html"); 
    
         // logging 
        } 
    } 
    

的全文看http://benfoster.io/blog/aspnet-mvc-custom-error-pages如何設置自定義錯誤頁和https://msdn.microsoft.com/en-us/library/bb397417.aspx更多的錯誤處理。

相關問題