﻿# BlogEngine 1.3.0.0 出現嚴重安全性漏洞

如果有人使用 BlogEngine 1.3.0.0 版本的話，可能需要「立即」將你的版本升級至 [BlogEngine 1.3.1.0 版](https://www.codeplex.com/Release/ProjectReleases.aspx?ProjectName=blogengine&ReleaseId=7016)，因為 1.3.0.0 有個嚴重的安全性漏洞可以將該網站所有使用者的帳號、密碼下載。這問題連 [BlogEngine.NET 官方部落格](http://www.dotnetblogengine.net/) 都被駭了。

我原本不想將這個漏洞的細節說的太清楚的，因為說的越清楚可能就會有越多人遭殃，不過這應該也是一場機會教育吧，所以我還是多說明一些好了，反正有使用 BlogEngine 1.3.0.0  Blogger 趕快升級就是了，如果自己改過很多程式的話，其實只要更新 DotNetSlave.BusinessLogic\\Web\\HttpHandlers\\JavaScriptHandler.cs 這支程式就可以了，在這支程式的 RetrieveLocalScript 方法改掉即可。

原本的程式碼是這樣：

```
private static string RetrieveLocalScript(string file)
{
    string path = HttpContext.Current.Server.MapPath(file);
    string script = null;

    if (File.Exists(path))
    {
        using (StreamReader reader = new StreamReader(path))
        {
            script = reader.ReadToEnd();
            script = StripWhitespace(script);
            HttpContext.Current.Cache.Insert(file, script, new CacheDependency(path));
        }
    }

    return script;
}
```

然而在這個方法前面多加上幾行即可：

```
private static string RetrieveLocalScript(string file)
{
    if (!file.EndsWith(".js", StringComparison.OrdinalIgnoreCase))
    {
        throw new System.Security.SecurityException("No access");
    }

    string path = HttpContext.Current.Server.MapPath(file);
    string script = null;
    
    using (StreamReader reader = new StreamReader(path))
    {
        script = reader.ReadToEnd();
        HttpContext.Current.Cache.Insert(file, script, new CacheDependency(path));
    }

    return script;
}
```

這問題主要是出在 js.axd 這支 HttpHandler，因為沒有將下載的路徑處理好，導致可以讓任何人透過這支Handler下載網站上任意檔案，包括 web.config 與網站內所有任意檔案，這問題我們在之前也犯過一次，但那一次發現就已經很震驚了，因為所有定義在 web.config 的資料庫連線參數、密碼都被看光光，不得已只好將所有帳號、密碼全部重設！

若有人剛開始寫 HttpHandler，並且是用 HttpHandler 下載檔案的人，一定要特別特別留意這個問題，多做一些檔名的判斷才能防止漏洞產生，例如說檔名中不能任意指定其他路徑名稱，你可以多判斷檔名中是否有 / 或 .. 等字元的出現。

剛剛花了兩個小時小心翼翼的將版本升級，因為我自己 Patch 了不少原本就有的 Bug，真怕升級後問題又出現，所以也把每一支更新的程式都看過一遍，還好我有用 Subversion 做版本管理，不然每次升級都要看完所有程式碼那還真的要熬夜了。

另外，我也透過 [LogParser](http://www.microsoft.com/downloads/details.aspx?FamilyID=890cd06b-abf8-4c25-91b2-f8d975cf8c07&displaylang=en) 工具幫我找出到目前為止有幾個人透過這個漏洞看到我的密碼了（還好我發現的早沒人看過），在此分享我的 LogPaser 指令，方便部落客查一下 IIS 的 Log，以進行確認有沒有人看過你的密碼了，如果有人看過了就趕快換密碼吧！以下是查詢的步驟：

1.  先下載安裝 [LogParser](http://www.microsoft.com/downloads/details.aspx?FamilyID=890cd06b-abf8-4c25-91b2-f8d975cf8c07&displaylang=en)
2.  進入命列提示字元 ( 開始 -> 執行 -> cmd.exe )
3.  切換至你網站的 IIS Log 目錄，例如：C:\\WINDOWS\\system32\\LogFiles\\W3SVC1137020394
4.  執行 LogParser 指令將篩選過的資料匯出：  
    LogParser "SELECT date, time, cs-uri-stem, cs-uri-query, c-ip, cs(User-Agent) FROM \*.log WHERE cs-uri-stem like '%js.axd%' and cs-uri-query like '%users.xml%'" -i:W3C -o:CSV  
      
    輸出的結果大概長這樣，如果有可疑的IP那就是中標啦! （P.S. 底下的IP是我亂打的）：  
    
    ```
    2008-04-15,13:54:05,/js.axd,path=/app_data/users.xml,127.198.10.85
    2008-04-15,13:54:19,/js.axd,path=/app_data/users.xml,129.218.2.1
    2008-04-15,14:17:46,/js.axd,path=/app_data/users.xml,182.232.123.123
    
    Statistics:
    -----------
    Elements processed: 310655
    Elements output:    4
    Execution time:     8.72 seconds
    ```
    

**相關連結**

-   [Critical Security Patch Available](https://web.archive.org/web/20160603034647/http://www.dotnetblogengine.net/post/Critical-Security-Patch-Available.aspx)  
    
-   [High Risk: Security Vulnerability in BE Javascript handler](http://www.codeplex.com/blogengine/WorkItem/View.aspx?WorkItemId=5979)  
    
-   [BlogEngine.NET Intranet Hacking](https://web.archive.org/web/20150417080121/http://ha.ckers.org:80/blog/20080412/blogenginenet-intranet-hacking)  
    
-   [BlogEngine Vulnerability Exposed](https://web.archive.org/web/20080820020638/http://www.dscoduc.com/post/2008/04/BlogEngine-Vulnerability-Exposed.aspx)  
    
-   [下載 BlogEngine 1.3.1.0 版](https://www.codeplex.com/Release/ProjectReleases.aspx?ProjectName=blogengine&ReleaseId=7016)  
    
-   [LogParser 2.2](http://www.microsoft.com/downloads/details.aspx?FamilyID=890cd06b-abf8-4c25-91b2-f8d975cf8c07&displaylang=en)
