繁体   English   中英

线程可以作为其他用户执行吗? (.NET 2.0 / 3.5)

[英]Can a Thread be executed as another user? (.NET 2.0/3.5)

我有一个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
    }
  }
}

然后加载动态程序集,并通过反射执行Evaluate方法。 这很好。

问题在于恶意代码注入的可能性很大,因此我想在“沙盒”线程中运行Evaluate方法(不进行任何非托管API调用)。 为了进行测试,我使用了内置的Windows匿名用户,并提供了以下代码:

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

这给了我匿名用户的身份和主体。 如何将此方法应用于线程tSandbox,以便该线程上的代码在指定的安全上下文中运行,而无需使用非托管API调用?

谢谢!

AFAIK模仿其他用户的唯一方法是通过不受管理的win32 api(例如LogonUser())...您可以通过互操作调用它们。

有这样一个代码示例在这里

如果您只需要匿名帐户,则您可能已经很接近了-您只需将原理分配给线程(您可以通过设置System.Threading.CurrentPrinciple,在要更改的线程中运行的代码来完成此工作)

但是,如果您需要验证Windows用户并将该身份分配给线程,我认为您将不得不对LogonUser()进行非托管调用。 我没有办法解决。

这是一些与匿名用户一起使用的代码(假定使用ASP.Net Web ApplicationProject,在该表单上有一个名为“ identityLabel”的标记,并且已分配onInit =“ IdentityLabelInit”)(请注意,代码的运行方式取决于无论您是在IIS还是Cassini下运行它-在IIS下,初始身份都是匿名用户。在Cassini下,初始身份是我。


        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;
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM