IIS 7新增了虛擬帳戶(Virtual Accounts)的概念,即每個應用程式集區(App Pool)都有一個專屬的IIS APPPOOL\AppPoolName虛擬帳號,便於更精準地控管網站相關檔案及其他資源的存取權限。(關於虛擬帳號概念,可參考保哥的詳細介紹: 介紹 IIS 7.5 的應用程式集區與新增的「虛擬帳戶」特性)

權限管控的最高指導原則在力求"在維持可運作的前題下,開放最少權限",因此拔除網站目錄的預設NTFS權限理,只授權給最少的必要帳號,將能提高安全性。不過,IIS 7"最小權限"的範圍為何?

找了一些探討IIS權限文章,沒找到關於NTFS權限需求較直接明確的說明,最後只依理論跟實驗結果整理出一點心得。(如果有人看過明確的文件說明,歡迎分享)

我用以下程式偵測ASP.NET執行身分的相關資訊:

<%@ Page Language="C#" %>
<script runat="server">
    void Page_Load(object sender, EventArgs e)
    {
        Response.Write("<li>Request.IsAuthenticated=" + 
            Request.IsAuthenticated);
        Response.Write("<li>Request.LogonUserIdentity.Name=" + 
            Request.LogonUserIdentity.Name);
        Response.Write("<li>WindowsIdentity.GetCurrent().Name=" +
            System.Security.Principal.WindowsIdentity.GetCurrent().Name);
    }
</script>

在匿名存取時結果如下:

  • Request.IsAuthenticated=False
  • Request.LogonUserIdentity.Name=NT AUTHORITY\IUSR
  • WindowsIdentity.GetCurrent().Name=IIS APPPOOL\JEFFPool
  • 啟用Windows Authentication時則為:

  • Request.IsAuthenticated=True
  • Request.LogonUserIdentity.Name=MyDoamin\MyAccount
  • WindowsIdentity.GetCurrent().Name=IIS APPPOOL\JEFFPool
  • 要透過IIS執行程式,帳號必須對網站資料夾至少有讀取與執行(Read & execute)、列出資料夾內容(List folder contents)等權限。原本以為只需要開放給IIS APPPOOL\JEFFPool帳號就好,但實測發現在上述兩種情況下,還分別需要授與權限給NT AUTHORITY\IUSR及MyDoamin\MyAccount,不然就會產生拒絕存取錯誤。

    透過Process Monitor我觀察到w3wp.exe確實是以IIS APPPOOL\JEFFPool身分執行,但在執行showid.aspx時偷偷調閱了aspx的檔案權限設定,推測(純屬推測)其目的是用來比對Request.LogonUserIdentity的使用者在showid.aspx的NTFS權限清單中是否具有讀取執行權。因此Request.LogonUserIdentity跟IIS APPPOOL\JEFFPool兩個帳號都要有權限才不會產生存取錯誤。

    最後我整理的結論為:

    • 匿名存取(或表單驗證, Form Authentcation)時,網站資料夾的最少授權對象為NT AUTHORITY\IUSR及IIS APPPOOL\YourPoolName;
    • Windows驗證時,除IIS APPPOOL\YourPoolName外,要再指定為Authenticated Users群組、Domain Users群組,或是可登入使用者所屬的特定群組三者之一。

    PS: 當啟用ASP.NET模擬(Impersonation)時,會影響ASP.NET的執行身份(可由WindowsIdentity.GetCurrent()觀察),授權對象需再自行調整。


    Comments

    Be the first to post a comment

    Post a comment