2009-08-31 32 views
8

Tôi hỏi một thời gian trước làm thế nào để hạn chế bổ sung truy cập (Tôi muốn ngăn chặn chúng từ văn bản cho đĩa hoặc mạng) và tôi đã nói với sử dụng AppDomain. Tôi đã tìm kiếm và thử và thất bại về cách làm việc này.Hạn chế truy cập plugin để hệ thống tập tin và mạng thông qua appdomain

bất cứ ai có thể cung cấp một số thông tin vì vậy tôi có thể bắt đầu, chỉ cần đặt làm một AppDomain điều đó không cho phép bằng văn bản cho các tập tin hoặc mạng.

Trả lời

5

Tôi đoán đây là những gì bạn cần, nếu tôi hiểu chính xác quan điểm của bạn.

System.Security.PermissionSet ps = 
    new System.Security.PermissionSet(System.Security.Permissions.PermissionState.None); 
ps.AddPermission(new System.Security.Permissions.FileIOPermission(System.Security.Permissions.FileIOPermissionAccess.NoAccess, "C:\\")); 
System.Security.Policy.PolicyLevel pl = System.Security.Policy.PolicyLevel.CreateAppDomainLevel(); 
pl.RootCodeGroup.PolicyStatement = new System.Security.Policy.PolicyStatement(ps); 
AppDomain.CurrentDomain.SetAppDomainPolicy(pl); 
System.Reflection.Assembly myPluginAssembly = AppDomain.CurrentDomain.Load("MyPluginAssembly"); 

Điều này có ý nghĩa hơn không?

Chú ý rằng bạn có thể cung cấp một loạt các chuỗi có chứa có các đường dẫn mà bạn không muốn các plugin để có thể truy cập. Bạn có thể cung cấp nếu khi khởi tạo phiên bản mới của lớp FileIOPermission.

Hãy cho tôi biết nếu điều này có ích. :-)

+0

Điều đó có vẻ là exacly những gì im sau, tôi nhận được Tên lắp ráp hoặc codebase đã cho không hợp lệ. (Ngoại lệ từ HRESULT: 0x80131047) bây giờ nhưng tôi chưa có thời gian để tìm hiểu thêm (Im proberbly làm điều gì đó sai). Il có thêm thời gian vào ngày mai và il cho bạn biết – EKS

+0

Vì vậy, cuối cùng, đó là những gì bạn đang tìm kiếm? Tôi đoán, vì bạn dường như đã kiểm tra nó. :-) –

+0

@EKS bạn phải tải từ cùng thư mục hoặc một trong các thư mục con của thư mục. –

0

Nếu bạn đang sử dụng plugin, bạn có thể biết về proxy.

Trong khi tải lắp ráp của bạn thông qua một proxy, bạn có thể xác định mức độ chính sách bảo mật để lắp ráp đặc biệt này thông qua các phương pháp LoadAssembly() hoặc lâu hơn, nếu tôi nhớ không lầm. Nói cách khác, điều này được thực hiện thông qua sự phản chiếu.

Tôi biết câu trả lời của tôi là không có nhiều chi tiết, nhưng tôi hy vọng nó sẽ cung cấp cho bạn một ý tưởng về nơi để tìm kiếm giải pháp của bạn. Tôi sẽ theo dõi để tìm thêm chi tiết về chủ đề để tôi có thể giúp đỡ tốt hơn. =)

Hy vọng bạn sẽ chia sẻ những phát hiện của mình khi bạn đã thực hiện.

15

Đối net framework 4.0, hãy làm theo các mã sau từ this bài viết MSDN.

Ví dụ sau thực hiện quy trình trong phần trước. Trong ví dụ này, một dự án có tên Sandboxer trong một giải pháp Visual Studio cũng chứa một dự án có tên là UntrustedCode, thực hiện lớp UntrustedClass. Kịch bản này giả định rằng bạn đã tải xuống một thư viện lắp ráp có chứa một phương pháp được dự kiến ​​sẽ trả về true hoặc false để cho biết số bạn cung cấp là một số Fibonacci. Thay vào đó, phương pháp cố đọc một tệp từ máy tính của bạn. Ví dụ sau cho thấy mã không đáng tin cậy.

using System; 
using System.IO; 
namespace UntrustedCode 
{ 
    public class UntrustedClass 
    { 
     // Pretend to be a method checking if a number is a Fibonacci 
     // but which actually attempts to read a file. 
     public static bool IsFibonacci(int number) 
     { 
      File.ReadAllText("C:\\Temp\\file.txt"); 
      return false; 
     } 
    } 
} 

Ví dụ sau cho thấy mã ứng dụng Sandboxer thực thi mã không đáng tin cậy.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 
using System.Security; 
using System.Security.Policy; 
using System.Security.Permissions; 
using System.Reflection; 
using System.Runtime.Remoting; 

//The Sandboxer class needs to derive from MarshalByRefObject so that we can create it in another 
// AppDomain and refer to it from the default AppDomain. 
class Sandboxer : MarshalByRefObject 
{ 
    const string pathToUntrusted = @"..\..\..\UntrustedCode\bin\Debug"; 
    const string untrustedAssembly = "UntrustedCode"; 
    const string untrustedClass = "UntrustedCode.UntrustedClass"; 
    const string entryPoint = "IsFibonacci"; 
    private static Object[] parameters = { 45 }; 
    static void Main() 
    { 
     //Setting the AppDomainSetup. It is very important to set the ApplicationBase to a folder 
     //other than the one in which the sandboxer resides. 
     AppDomainSetup adSetup = new AppDomainSetup(); 
     adSetup.ApplicationBase = Path.GetFullPath(pathToUntrusted); 

     //Setting the permissions for the AppDomain. We give the permission to execute and to 
     //read/discover the location where the untrusted code is loaded. 
     PermissionSet permSet = new PermissionSet(PermissionState.None); 
     permSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution)); 

     //We want the sandboxer assembly's strong name, so that we can add it to the full trust list. 
     StrongName fullTrustAssembly = typeof(Sandboxer).Assembly.Evidence.GetHostEvidence<StrongName>(); 

     //Now we have everything we need to create the AppDomain, so let's create it. 
     AppDomain newDomain = AppDomain.CreateDomain("Sandbox", null, adSetup, permSet, fullTrustAssembly); 

     //Use CreateInstanceFrom to load an instance of the Sandboxer class into the 
     //new AppDomain. 
     ObjectHandle handle = Activator.CreateInstanceFrom(
      newDomain, typeof(Sandboxer).Assembly.ManifestModule.FullyQualifiedName, 
      typeof(Sandboxer).FullName 
      ); 
     //Unwrap the new domain instance into a reference in this domain and use it to execute the 
     //untrusted code. 
     Sandboxer newDomainInstance = (Sandboxer) handle.Unwrap(); 
     newDomainInstance.ExecuteUntrustedCode(untrustedAssembly, untrustedClass, entryPoint, parameters); 
    } 
    public void ExecuteUntrustedCode(string assemblyName, string typeName, string entryPoint, Object[] parameters) 
    { 
     //Load the MethodInfo for a method in the new Assembly. This might be a method you know, or 
     //you can use Assembly.EntryPoint to get to the main function in an executable. 
     MethodInfo target = Assembly.Load(assemblyName).GetType(typeName).GetMethod(entryPoint); 
     try 
     { 
      //Now invoke the method. 
      bool retVal = (bool)target.Invoke(null, parameters); 
     } 
     catch (Exception ex) 
     { 
      // When we print informations from a SecurityException extra information can be printed if we are 
      //calling it with a full-trust stack. 
      (new PermissionSet(PermissionState.Unrestricted)).Assert(); 
      Console.WriteLine("SecurityException caught:\n{0}", ex.ToString()); 
      CodeAccessPermission.RevertAssert(); 
      Console.ReadLine(); 
     } 
    } 
} 
Các vấn đề liên quan