首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

以 NULL DACL 解決 ASP.NET 與其他進程間 File Mapping 通訊權限問題

     編輯:關於ASP.NET

問題背景

近期開發一個採用 File Mapping 進行進程間通訊 (IPC) 的方案,旨在實現 Web Service 與自訂服務之間的安全隔離與狀態管理。測試發現,在一般 Windows 進程間通訊正常,但將客戶端移植至 ASP.NET 後,開啟 File Mapping 失敗,顯然是權限問題。

三種解決方案比較

  1. 強制 ASP.NET 以系統管理員身分執行:簡單但極不安全,不建議採用。
  2. 使用 NULL DACL 安全描述項:賦予核心物件對所有使用者的完全存取權。雖較方案一安全,但低權限攻擊者仍可操作核心物件,存在風險。
  3. 僅授予服務進程與 ASP.NET 進程特定權限:安全性最高,但實作複雜度較高。

NULL DACL C# 實作範例

以下提供可直接使用的 C# 程式碼,採用 P/Invoke 呼叫 Windows API 建立 NULL DACL 的 SecurityAttributes 類別:

[StructLayoutAttribute(LayoutKind.Sequential)]
public struct SECURITY_DESCRIPTOR
{
    public byte revision;
    public byte size;
    public short control;
    public IntPtr owner;
    public IntPtr group;
    public IntPtr sacl;
    public IntPtr dacl;
}

[StructLayout(LayoutKind.Sequential)]
public class SecurityAttributes : IDisposable
{
    [DllImport("advapi32.dll", SetLastError = true)]
    static extern bool SetSecurityDescriptorDacl(IntPtr sd, bool daclPresent, IntPtr dacl, bool daclDefaulted);

    [DllImport("advapi32.dll", SetLastError = true)]
    static extern bool InitializeSecurityDescriptor(IntPtr pSecurityDescriptor, uint dwRevision);

    private int nLength;
    private IntPtr lpSecurityDescriptor;
    private int bInheritHandle;

    public SecurityAttributes()
    {
        nLength = Marshal.SizeOf(typeof(SecurityAttributes));
        bInheritHandle = 1;

        SECURITY_DESCRIPTOR sd = new SECURITY_DESCRIPTOR();
        lpSecurityDescriptor = Marshal.AllocCoTaskMem(Marshal.SizeOf(sd));
        Marshal.StructureToPtr(sd, lpSecurityDescriptor, false);
        InitializeSecurityDescriptor(lpSecurityDescriptor, 1);
        SetSecurityDescriptorDacl(lpSecurityDescriptor, true, IntPtr.Zero, false);
    }

    public void Dispose()
    {
        lock (this)
        {
            if (lpSecurityDescriptor != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(lpSecurityDescriptor);
                lpSecurityDescriptor = IntPtr.Zero;
            }
        }
    }

    ~SecurityAttributes()
    {
        Dispose();
    }
}

使用方式

建立 File Mapping 時傳入此 SecurityAttributes 實例即可:

using (var sa = new SecurityAttributes())
{
    IntPtr handle = CreateFileMapping(
        IntPtr.Zero, sa, PageReadWrite, 0, size, name);
    // 後續操作...
}

後續規劃

方案三(精確授權特定進程)的完整實作將於後續文章補充。若專案對安全性要求極高,建議投入精力實作方案三。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved