C#中的安全性原則

來源:互聯網
上載者:User
1.安全性

    .Net的安全性包括代碼和資料不被錯誤的使用或者被其他的程式破壞,這種安全機制是通過加強對Managed 程式碼的限制和保護來實施的(This security mechanism is implemented by strengthing restriction and protection for the managed code)。 .Net 程式需要請求他們操作, 有管理員設定的安全性原則進行判斷,如果有足夠的許可權,代碼將被執行;否則如果許可權不夠,將拋出SecurityException,代碼不被執行。許可權是通過資料簽名產生的,數位簽章包括了代碼從哪裡來,語言,public key token等資訊(permission is generated through digital signature or strong name, digital signature includes assembly's information, such as from which corperation,language,version, public key token).

    All of these concerns one definiton: Permission


2. 安全性原則Security policy
    安全性原則的執行是.NetManaged 程式碼安全的保證。
    我們可以通過修改對託管或者Unmanaged 程式碼的安全性原則,來實現對程式的安全訪問。
    有關安全性原則的所有library位於:    
    System.Security以及System.Security.Permission;

3.例子:對Unmanaged 程式碼的授權/禁止運行樣本
     1.禁止unmanaged code運行

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Security;
using System.Security.Permissions;

namespace Essence
{
    public class MyClass
    {
        [DllImport("user32.dll")]
        public static extern int MessageBox(uint hWnd, string lpText, string lpCaption, uint uType);
    }

    public  class MY
    {

        public static void CallUnmanagedCodeWithoutPermission()
        {
            SecurityPermission perm = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);
             perm.Deny();
            try
            {
                  MyClass.MessageBox(0, "Unmanaged code is executed with Permission!", "Security Policy Demo", 0);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
        }
  
    }
    class Program
    {
        static   void Main(string[] args)
        {
            MY.CallUnmanagedCodeWithoutPermission();
        }
    }
}

運行結果如下:拋出異常!



    2.允許unmanaged code 運行
    代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Security;
using System.Security.Permissions;

namespace Essence
{
    public class MyClass
    {
        [DllImport("user32.dll")]
        public static extern int MessageBox(uint hWnd, string lpText, string lpCaption, uint uType);
    }

    public  class MY
    {

        public static void CallUnmanagedCodeWithoutPermission()
        {
            SecurityPermission perm = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);
             perm.Assert();
            try
            {
                  MyClass.MessageBox(0, "Unmanaged code is executed with Permission!", "Security Policy Demo", 0);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
        }
  
    }
    class Program
    {
        static   void Main(string[] args)
        {
            MY.CallUnmanagedCodeWithoutPermission();
        }
    }
}

運行結果:運行unmanaged code, popup a window

4.安全性的進一步討論:
        目前為止,我們知道了.Net Framework提供了良好的安全驗證功能。對於managed code,我們可以採用strong name來對Assembly獲得digital signature的方法來進行安全性的驗證,防止DLL Hell的產生。 同時,對於unmanaged code,可以利用.Net 中的安全驗證機制使得unmanaged code不允許運行。

        簡言之,.Net安全系統防止了從網上下載有惡意的程式來保護電腦系統。但是,即使不觸發安全異常,這些安全檢查也是要付出代價的。那麼也可以在調用unmanged code的時候,使用特性SuppressUnmanagedCodeSecurity來跳過安全性檢查(前提是你對unmanaged code 足夠信任)。
例如:

using System;
using System.Security;
using System.Security.Permissions;
using System.Runtime.InteropServices;

class NativeMethods
{
    // 這是對Unmanaged 程式碼的調用。執行此方法需要
    // nmanagedCode 安全許可權。如果沒有此許可權,
    // 則調用此方法的嘗試將引發 SecurityException:
    /**//* 注意:SuppressUnmanagedCodeSecurityAttribute 禁止
       在運行時檢查 UnmanagedCode 許可權。一定要小心!*/
    [SuppressUnmanagedCodeSecurityAttribute()]
    [DllImport("msvcrt.dll")]
    internal static extern int puts(string str);
    [SuppressUnmanagedCodeSecurityAttribute()]
    [DllImport("msvcrt.dll")]
    internal static extern int _flushall();
}

這樣,在client 調用NativeMethods時候,不論是否有SecurityPermission,都將不起作用,因為它會跳過安全性檢查。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.