C#装载沙盒大会大会

2023-09-03 10:29:57 作者:〒_〒

好了,所以,我有我要添加支持其他人写的模块,应用程序将加载的应用程序。

Okay so, I have an application which I want to add support for other people to write modules that the application will load.

该模块将是一个类扩展我的模块级,用.NET编写的。我需要知道如何在沙箱环境中加载这些dll,只允许他们读/写在某些目录。

The modules would be a class extending my Module class, written in .Net. I need to know how to load these DLLs in a sandboxed environment, only allowing them to read/write in certain directories.

这可能吗?

推荐答案

是的,这是可能的。使用$ C C访问安全性和.NET沙盒$。我建议你​​看一看的 CSScript 库(开源)。这是一个图书馆,提供C#编写脚本和上飞编译成动态程序集。我已经使用这一个项目,以使最终用户编写C#脚本,并执行它们让我的系统类的交互。所需要的项目,他们没有使用文件IO或消息框(UI)为用户脚本是在服务器上执行。 CSSCript使用的.NET框架的要素来限制大会有权访问,你会得到一个异常,如果任何违禁类型的调用。

Yes, this is possible. Using Code Access Security and the .NET Sandbox. I would advise you take a look at the CSScript library (open source). This is a library that provides C# scripting and on the fly compilation into dynamic assemblies. I have used this in a project to allow end-users to write C# scripts and execute them allowing interaction with classes in my system. The project required that they had no access to File IO or MessageBox (UI) as user scripts were to execute on a server. CSSCript used elements of the .NET framework to to limit what the assembly has access to and you will get an exception if any of these prohibited types are called.

那么,来看看这个。我将编辑我的答案,一旦我找到了一些细节上的如何的其可能的,只是为了让你知道它的是的可能!

So, take a look at that. I will edit my answer once I find out some more detail on how its possible, just to let you know it is possible!

确定找到了。这是一个讨论我在几年前已经与CSScript作者:

Ok found it. Here is a discussion I had with the author of CSScript a few years ago:

我:

我开发一个应用程序,并希望揭露的能力,为用户通过用户界面脚本的某些行动。 CSScript看起来非常好这一点。不过,我也希望让用户做到这一点,一个Web服务器上执行的脚本。现在,这是一个安全的噩梦,因为用户可以写Directory.Delete(@C:\,真),并擦拭硬盘驱动器。因此,才有可能限制组件,命名空间,甚至类,用户可以从他们的脚本访问,以排序在一个安全沙箱中运行的CSScript?

I am developing an application and wish to expose the ability for users to script certain actions through the UI. CSScript looks very good for this. However I also wish to allow users to do this and execute their scripts on a web server. Now this is a security nightmare as users could write "Directory.Delete(@"C:\", true)" and wipe the hard drive. So would it be possible to restrict the assemblies, namespaces or even classes that a user can access from their script, to sort of run the CSScript in a secure sandbox?

奥列格:

最直接的吸引力的解决方案是使用.NET沙盒。这也正是专为这种scenarios.The CLR标准沙箱可用于主机应用程序正在运行的脚本和CS-脚本。我们的想法是,你中科院初始化加载可疑脚本之前,剩下的就是一个CLR责任。如果你需要配置的目录/文件的权限,你与中科院工具做到这一点。这样的脚本是行为您的用户提供的程序。和CS-Script是一种方便的机制实现这种传输,但实际的安全问题是由.NET Sendoxing,其具有的COM prehensive设置的功能到几乎涵盖所有可能的安全方案解决。随着CS脚本下载内容您可以找到Sendboxing样品(\样本\沙箱),它演示了如何prevent脚本文件I ​​/ O操作(比如创建文件)

The immediate attractive solution is to use .NET Sandbox. It is designed exactly for this sort of scenarios.The CLR standard sandboxing is available for the host application running scripts with CS-Script. The idea is that you initialise CAS before loading the suspicious script and the rest is a CLR responsibility. And if you need to configure directories/files permissions you do it with CAS tools. This way scripting is a "transportation" for the routine provided by your user. And CS-Script is a convenient mechanism for implementing such transport but the actual security concerns are addressed by .NET Sendoxing, which has comprehensive set of functionality to cover practically all possible security scenarios. With CS-script downloadables you can find the Sendboxing sample (\Samples\Sandboxing) which demonstrates how to prevent script from file I/O operations (e.g. create file).

所以,从这个我相信你需要看看.NET沙盒并加载程序集成。我知道这个例子是特定于C#的脚本,但我相信这是适用于您的情况为CSScript例子上面会显示你的方式来实现沙盒​​和安全性。

So, from this I believe you need to look at .NET Sandbox and load the assemblies into that. I realise this example is specific to C# Scripting but I believe it is applicable to your scenario as CSScript examples above will show you a way to achieve sandboxing and security.

如何加载组件到沙盒中的一些具体的例子可以在这里找到:

Some specific examples of how to load assemblies into a sandbox can be found here:

Loading .NET程序集在沙箱中 code访问安全性在实践中 如何使用CAS来装配约束 Loading .NET Assemblies in a Sandbox Code Access Security in practice How to use CAS to constrain an assembly

虽然我们正在讨论模块加载,你听说过微软棱镜?这为模块加载和依赖注入一个良好的框架(通过统一或的 MEF )开发一个插件架构时,这可能是非常有用的。

While we're discussing module loading, have you heard of Microsoft Prism? This provides a good framework for module loading and dependency injection (via Unity or MEF) which could be very useful when developing a plugin architecture.

最好的问候,