InitSandboxedInstance() 공개 메소드

public InitSandboxedInstance ( byte AssemblyData ) : void
AssemblyData byte
리턴 void
예제 #1
0
        public void InitializeSandbox(string TempPath, string AssemblyPath)
        {
            lock (this)
            {
                //deinit if not null
                if (newDomain != null)
                {
                    DeInitializeSandbox();
                }

                //Setting the AppDomainSetup.
                //It is very important to set the ApplicationBase to a folder other than the one in which the sandboxer resides.
                var adSetup = new AppDomainSetup();
                adSetup.ApplicationBase = Path.GetFullPath(TempPath);

                //Setting the permissions for the AppDomain.
                //We give the permission to execute and to read/discover the location where the untrusted code is loaded.
                var 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.
                //var fullTrustAssembly = typeof(Sandboxer).Assembly.Evidence.GetHostEvidence<StrongName>();

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

                //Use CreateInstanceFrom to load an instance of the Sandboxer class into the new AppDomain.
                var 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.
                newDomainInstance = (Sandboxer)handle.Unwrap();
                newDomainInstance.InitSandboxedInstance(File.ReadAllBytes(AssemblyPath));
            }
        }
예제 #2
0
파일: Sandboxer.cs 프로젝트: yodiwo/plegma
        public void InitializeSandbox(string TempPath, string AssemblyPath)
        {
            lock (this)
            {
                //deinit if not null
                if (newDomain != null)
                    DeInitializeSandbox();

                //Setting the AppDomainSetup. 
                //It is very important to set the ApplicationBase to a folder other than the one in which the sandboxer resides.
                var adSetup = new AppDomainSetup();
                adSetup.ApplicationBase = Path.GetFullPath(TempPath);

                //Setting the permissions for the AppDomain.
                //We give the permission to execute and to read/discover the location where the untrusted code is loaded.
                var 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.
                //var fullTrustAssembly = typeof(Sandboxer).Assembly.Evidence.GetHostEvidence<StrongName>();

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

                //Use CreateInstanceFrom to load an instance of the Sandboxer class into the new AppDomain. 
                var 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.
                newDomainInstance = (Sandboxer)handle.Unwrap();
                newDomainInstance.InitSandboxedInstance(File.ReadAllBytes(AssemblyPath));
            }
        }