2009-08-31 11 views
8

के माध्यम से फ़ाइल सिस्टम और नेटवर्क पर प्लगइन पहुंच प्रतिबंधित करें मैंने कुछ समय पहले प्लगइन्स एक्सेस को प्रतिबंधित करने के लिए कहा था (मैं उन्हें डिस्क या नेटवर्क पर लिखने से रोकना चाहता हूं) और मुझे AppDomain का उपयोग करने के लिए कहा गया था। मैंने इस काम को कैसे प्राप्त किया है इस पर खोज की है और कोशिश की है और विफल रही है।एपडोमेन

कोई भी कुछ जानकारी प्रदान कर सकता है ताकि मैं शुरू कर सकूं, बस एक ऐपडॉमेन डालें जो फ़ाइल या नेटवर्क पर लिखने की अनुमति नहीं देता है।

उत्तर

5

मुझे लगता है कि अगर आपको सही मायने में सही लगता है तो मुझे यही चाहिए।

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"); 

क्या यह आपके सटीक रूप से क्या है?

ध्यान दें कि आप उन पथों को containg स्ट्रिंग प्रदान कर सकते हैं जहां आप प्लगइन को एक्सेस नहीं करना चाहते हैं। यदि आप FileIOPermission क्लास के नए उदाहरण को प्रारंभ करते हैं तो आप प्रदान कर सकते हैं।

अगर यह मदद करता है तो मुझे बताएं। :-)

+0

ऐसा लगता है कि मैं क्या कर रहा हूं, मुझे दिया गया असेंबली नाम या कोडबेस अमान्य था। (HRESULT से अपवाद: 0x80131047) अब मेरे पास अभी तक और अधिक देखने के लिए समय नहीं है (Im proberbly कुछ गलत कर रहा है)। Il कल अधिक समय प्राप्त करें और आईएल आपको – EKS

+0

बताएं तो आखिरकार, क्या आप यही खोज रहे थे? मुझे लगता है, क्योंकि आपने इसे जांच लिया है। :-) –

+0

@EKS आपको उसी फ़ोल्डर या फ़ोल्डर के बच्चों में से एक लोड करना होगा। –

0

यदि आप प्लगइन का उपयोग कर रहे हैं, तो शायद आपको प्रॉक्सी के बारे में पता हो सकता है।

प्रॉक्सी के माध्यम से अपनी असेंबली लोड करते समय, आप LoadAssembly() विधि के माध्यम से इस विशेष असेंबली के लिए सुरक्षा नीति स्तर निर्दिष्ट कर सकते हैं या यदि मुझे सही याद है। दूसरे शब्दों में, यह प्रतिबिंब के माध्यम से किया जाता है।

मुझे पता है कि मेरा जवाब इतना विस्तृत नहीं है, लेकिन मुझे उम्मीद है कि यह आपको एक समाधान देगा कि आपके समाधान को कहां देखना है। मैं इस विषय पर और जानकारी पाने के लिए नजर रखूंगा ताकि मैं बेहतर मदद कर सकूं। =)

आशा है कि जब आप इसे कर लेंगे तो आप अपने निष्कर्ष साझा करेंगे।

15

.नेट फ्रेमवर्क 4.0 के लिए, कृपया this एमएसडीएन आलेख से निम्नलिखित कोड का पालन करें।

निम्नलिखित उदाहरण पिछले खंड में प्रक्रिया लागू करता है। उदाहरण में, विजुअल स्टूडियो समाधान में सैंडबॉक्सर नाम की एक परियोजना में अविश्वसनीय कोड नामक एक प्रोजेक्ट भी शामिल है, जो कक्षा अविश्वसनीय क्लास लागू करता है। यह परिदृश्य मानता है कि आपने एक लाइब्रेरी असेंबली डाउनलोड की है जिसमें एक विधि है जो वास्तविक या गलत लौटने की उम्मीद है, यह इंगित करने के लिए कि क्या आपके द्वारा प्रदान की गई संख्या एक फिबोनाची संख्या है। इसके बजाए, विधि आपके कंप्यूटर से एक फ़ाइल को पढ़ने का प्रयास करती है। निम्नलिखित उदाहरण अविश्वसनीय कोड दिखाता है।

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; 
     } 
    } 
} 

निम्न उदाहरण सैंडबॉक्सर एप्लिकेशन कोड दिखाता है जो अविश्वसनीय कोड निष्पादित करता है।

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(); 
     } 
    } 
} 
संबंधित मुद्दे