Configuring Application Domain

通过给Application Domain赋予不同的权限,可以防止一些破坏行为。比如,如果把一个第三方的assembly放到一个单独的Application Domain里面的话,就算攻击行为发现了该第三方的Assembly的漏洞,也无法穿过它所在的Application Domain对其他的Application Domain里面的Aseembly进行攻击。这种赋予有限权限的行为是defense-in-depth的一个例子。Defense-in-depth is the security principle of providing multiple levels of protection so that you are still protected in the event of a vulnerability. Defense-in-depth在调用外部代码时非常有效,因为外部代码可能会含有暂时无法发现,甚至无法修复的安全隐患。

Some concepts refer to Evidence:

code groups: determine the assembly’s privileges.

Evidence: is the information that the runtime collects about an assembly to determine to which code groups the assembly belongs

通过分配evidence给一个assembly,你可以控制如何分配权限给assembly。

   1:  object[] hostEvidence = {new Zone(SecurityZone.Internet)};
   2:  Evidence internetEvidence = new Evidence(hostEvidence, null);
   3:  AppDomain myDomain = AppDomain.CreateDomain("MyDomain");
   4:  myDomain.ExecuteAssembly("SecondAssembly.exe", internetEvidence);

Evidence类的构造函数需要两个参数,第一个是 host evidence 数组,第二个是 assembly evidence 数组。这俩都可以是null。但是在生成数组时要用object。

最简单的方式是传递一个 Zone的对象,如上面代码所示。

也可以给Application Domain单独分配evidence,通过调用重载的AppDomain.CreateDomain方法来实现,这个比较类似前面给Assembly分配evidence的形式。

   1:  object [] hostEvidence = {new Zone(SecurityZone.Internet)};
   2:  Evidence appDomainEvidence = new Evidence(hostEvidence, null);
   3:  AppDomain d = AppDomain.CreateDomain("MyDomain", appDomainEvidence);
   4:  d.ExecuteAssembly("SecondAssembly.exe");

用AppDomainSetup类可以在创建新的application domain的时候,对其进行设置,ApplicationBase是最重要的一个属性。修改AppDomianSetup并不会对已存在的Application Domain有任何影响,这写改变只作用于新创建的application domain。

   1:  // Construct and initialize settings for a second AppDomain.
   2:  AppDomainSetup ads = new AppDomainSetup();
   3:  ads.ApplicationBase = "file://" + System.Environment.CurrentDirectory;
   4:  ads.DisallowBindingRedirects = false;
   5:  ads.DisallowCodeDownload = true;
   6:  ads.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
   7:  // Create the second AppDomain
   8:  AppDomain d = AppDomain.CreateDomain("New Domain", null, ads);

把AppDomianSetup的对象ads作为参数传递给AppDomain.CreateDomain(),以此来对新创建的Application Domain进行设置。
对于SecurityZone类的补充:使用SecurityZone.Internet时,就无法访问win.ini。而使用SecurityZone.MyComputer的时候,就可以访问。不同的zone规定了不同的访问权限。

posted @ 2009-08-20 15:11  我写的不是代码 是寂寞  阅读(359)  评论(2编辑  收藏  举报