我有一个C#应用程序进行计算包含到动态组件源文件的一些运行时编译。显然,这提出了一个严重的安全问题。

这,将产生下面的代码下面“式”,并创建了动态组件:

公式:

Int32 _index = value.LastIndexOf('.');
String _retVal = value.Substring(_index + 1);
return _retVal;

代码生成:

using System;
namespace Dynamics
{
  public class Evaluator
  {
    public Object Evaluate(String value)
    {
      // Begin external code
      Int32 _index = value.LastIndexOf('.');
      String _retVal = value.Substring(_index + 1);
      return _retVal;
      // End external code
    }
  }
}

在动态组件然后被装载和通过反射所执行的评估方法。这个伟大的工程。

问题是,恶意代码注入的潜力是巨大的,所以我想运行在“沙盒”线的评估方法(没有任何非托管API调用)。 为了测试我使用的是内置的匿名Windows用户,并都拿出了下面的代码:

Thread tSandbox = new Thread(
    new ParameterizedThreadStart(this.DoSandboxedEvaluation));
WindowsIdentity tIdentity = WindowsIdentity.GetAnonymous();
WindowsPrincipal tPrincipal = new WindowsPrincipal(i);

这使我有匿名用户的身份和校长。这怎么可能适用于螺纹tSandbox等线程的代码在指定的安全上下文中运行,而无需使用非托管API调用?

谢谢!

有帮助吗?

解决方案

如果您只需要匿名帐户,你可能会很接近 - 你只需要为原则分配给线程(你的代码,你想通过设置System.Threading.CurrentPrinciple改变线内运行做到这一点)

不过,如果您需要进行身份验证的Windows用户并分配身份的线索,我认为你将不得不作出的LogonUser非托管()调用。我没有看到周围的一种方式。

下面是一些代码,与匿名用户的工作(它假定一个ASP.Net的Web ApplicationProject,在这里你有一个名为“identityLabel”窗体上的标签,并已分配的OnInit =“IdentityLabelInit”) (注意代码运行不同取决于你是否IIS或卡西尼下运行它。 - IIS下,最初的身份是匿名用户在卡西尼号,最初的身份是我


        protected void IdentityLabelInit(object sender, EventArgs e)
        {
            System.Threading.Thread testThread = new Thread(ReportIdentity);
            testThread.Start();
            testThread.Join();
        }

        private void ReportIdentity()
        {
            identityLabel.InnerText = "Initialized: " + System.Threading.Thread.CurrentPrincipal.Identity.Name;
            System.Security.Principal.IPrincipal currentPrincipal = System.Threading.Thread.CurrentPrincipal;
            SetAnonymousIdentity();
            System.Security.Principal.IPrincipal newPrincipal = System.Threading.Thread.CurrentPrincipal;
            if (newPrincipal.Equals(currentPrincipal))
                identityLabel.InnerText += ("no change");
            else
                identityLabel.InnerText += " | new name: " + System.Threading.Thread.CurrentPrincipal.Identity.Name;
        }

        private void SetAnonymousIdentity()
        {
            WindowsIdentity tIdentity = WindowsIdentity.GetAnonymous();
            WindowsPrincipal tPrincipal = new WindowsPrincipal(tIdentity);
            System.Threading.Thread.CurrentPrincipal = tPrincipal;
        }

其他提示

AFAIK模拟其他用户的唯一方法是经由非托管的Win32 API(例如,LogonUser的())...可以调用它们通过互操作。

有此此处

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top