zoukankan      html  css  js  c++  java
  • C#中的安全策略

    1.安全性
        .Net的安全性包括代码和数据不被错误的使用或者被其他的程序破坏,这种安全机制是通过加强对托管代码的限制和保护来实施的(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
        安全策略的执行是.Net托管代码安全的保证。
        我们可以通过修改对托管或者非托管代码的安全策略,来实现对程序的安全访问。
        有关安全策略的所有library位于:    
        System.Security以及System.Security.Permission;


    3.例子:对非托管代码的授权/禁止运行示例
         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
    {
        
    // 这是对非托管代码的调用。执行此方法需要
        
    // 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,都将不起作用,因为它会跳过安全性检查。
  • 相关阅读:
    拟阵交
    HEOI2021退役记
    退役划水一
    上下界网络流学习笔记
    扩展卢卡斯学习笔记
    扩展中国剩余定理(EXCRT)学习笔记
    插头DP学习笔记
    如何优雅地生成仙人掌图
    Powerful Number 筛学习笔记
    边分治学习笔记
  • 原文地址:https://www.cnblogs.com/Winston/p/1207661.html
Copyright © 2011-2022 走看看