【问题标题】:C# Sandbox EnvironmentC# 沙盒环境
【发布时间】:2017-03-13 18:14:35
【问题描述】:

我正在 Visual Studio 中处理一个小型沙盒项目。这是我的代码:

namespace Andromeda.PCTools
{
    public partial class Sandbox : MetroForm
    {
        private AppDomain sandbox;

        public Sandbox()
        {
            InitializeComponent();
        }

        private void Sandbox_Load(object sender, EventArgs e)
        {

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Applications|*.exe", ValidateNames = true, Multiselect = false })
            {
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    listBoxItems.Items.Add(ofd.FileName);

                }
            }
        }

        private void removeSelectedToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listBoxItems.SelectedItems.Count != 0)
            {
                while (listBoxItems.SelectedIndex != -1)
                {
                    listBoxItems.Items.RemoveAt(listBoxItems.SelectedIndex);
                }
            }
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            PermissionSet ps = new PermissionSet(PermissionState.None);
            ps.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));

            AppDomainSetup setup = new AppDomainSetup();

            Evidence ev = new Evidence();
            //ev.AddHostEvidence(new Zone(SecurityZone.Internet));
            PermissionSet internetPS = SecurityManager.GetStandardSandbox(ev);

            setup.ApplicationBase = Path.GetFullPath(Application.StartupPath);



            //StrongName fullTrustAssembly = typeof(Sandboxer).Assembly.Evidence.GetHostEvidence<StrongName>();

            sandbox = AppDomain.CreateDomain(listBoxItems.SelectedItem.ToString(), ev, setup, ps);

            try
            {
                sandbox.ExecuteAssembly(listBoxItems.SelectedItem.ToString());
                btnLoad.Enabled = false;
                btnUnload.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("The following error occurred!\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void btnUnload_Click(object sender, EventArgs e)
        {
            try
            {
                AppDomain.Unload(sandbox);
                btnLoad.Enabled = true;
                btnUnload.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("The following error occurred!\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

    }
}

但我收到以下错误:

抛出异常:仙女座中的“System.Security.SecurityException” 4.0.exe ("请求类型'System.Security.Permissions.FileIOPermission, mscorlib, 版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089' 失败。”)

【问题讨论】:

  • 得到错误在哪里?上面有很多不相关的代码,见MCVE
  • 你尝试执行的程序集,是否涉及文件I/O?如果是这样,那真的不会发生吗?
  • 我基本上只是想让 EXE 在无法更改(损害)设备的环境中运行
  • 有点像沙盒

标签: c# sandbox environment


【解决方案1】:

您正在授予沙盒程序集执行权限,但您还需要将FileIOPermission 添加到权限集,从而授予它访问文件系统的权限。请尝试以下操作:

ps.AddPermission(new FileIOPermission(PermissionState.Unrestricted));

选择您希望如何适当地配置您的 IO 权限。在我的示例中,它不受限制,但这是一个沙箱。您可以选择将其锁定一点 :) 根据需要选择适当的构造函数。

【讨论】:

  • 啊,你明白了吗?那太棒了! :)
  • 您可以使用您找到的解决方案来回答您的问题,然后在一段时间后接受您的回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 2013-05-03
  • 2021-07-31
  • 2021-09-04
  • 1970-01-01
相关资源
最近更新 更多