简体   繁体   English

流程开始和模拟

[英]Process start and Impersonation

I have problem with starting processes in impersonated context in ASP.NET 2.0. 我在ASP.NET 2.0中模拟上下文中启动进程时遇到问题。

I am starting new Process in my web service code. 我在我的Web服务代码中启动了新的Process。 IIS 5.1, .NET 2.0 IIS 5.1,.NET 2.0

[WebMethod]
public string HelloWorld()
{
    string path = @"C:\KB\GetWindowUser.exe";
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.WorkingDirectory = Path.GetDirectoryName(path);
    startInfo.FileName = path;
    startInfo.UseShellExecute = false;
    startInfo.CreateNoWindow = true;
    startInfo.ErrorDialog = false;
    startInfo.RedirectStandardOutput = true;
    startInfo.RedirectStandardError = true;
    Process docCreateProcess = Process.Start(startInfo);

    string errors = docCreateProcess.StandardError.ReadToEnd();
    string output = docCreateProcess.StandardOutput.ReadToEnd();
}

The "C:\\KB\\GetWindowUser.exe" is console application containing following code: “C:\\ KB \\ GetWindowUser.exe”是包含以下代码的控制台应用程序:

static void Main(string[] args)
{
    Console.WriteLine("Windows: " + WindowsIdentity.GetCurrent().Name);
}

When I invoke web service without impersonation, everything works fine. 当我在没有模仿的情况下调用Web服务时,一切正常。

When I turn on impersonation, following error is written in "errors" variable in web service code: 当我打开模拟时,跟随错误写在Web服务代码中的“errors”变量中:

Unhandled Exception: System.Security.SecurityException: Access is denied.\\r\\n\\r\\n at System.Security.Principal.WindowsIdentity.GetCurrentInternal(TokenAccessLevels desiredAccess, Boolean threadOnly)\\r\\n at System.Security.Principal.WindowsIdentity.GetCurrent()\\r\\n at ObfuscatedMdc.Program.Main(String[] args)\\r\\nThe Zone of the assembly that failed was:\\r\\nMyComputer 未处理的异常:System.Security.SecurityException:访问被拒绝。\\ r \\ n \\ r \\ n在System.Security.Principal.WindowsIdentity的System.Security.Principal.WindowsIdentity.GetCurrentInternal(TokenAccessLevels desiredAccess,Boolean threadOnly)\\ r \\ n中.GetCurrent()\\ r \\ n在ObfuscatedMdc.Program.Main(String [] args)\\ r \\ n失败的程序集区域是:\\ r \\ nMyComputer

Impersonated user is local administrator and has access to C:\\KB\\GetWindowUser.exe executable. 模拟用户是本地管理员,可以访问C:\\ KB \\ GetWindowUser.exe可执行文件。

When I specify window user explicitly in ProcesStartInfo properties Domain, User and Password, I got following message: http://img201.imageshack.us/img201/5870/pstartah8.jpg 当我在ProcesStartInfo属性Domain,User和Password中明确指定窗口用户时,我收到以下消息: http//img201.imageshack.us/img201/5870/pstartah8.jpg

Is it possible to start process with different credentials than ASPNET from asp.net (IIS 5.1) ? 是否可以从asp.net(IIS 5.1)开始使用不同于ASPNET的凭据进程?

You have to put privileged code into the GAC (or run in Full trust). 您必须将特权代码放入GAC(或以完全信任方式运行)。

The code in the GAC must assert the XXXPermission, where XXX is what ever permission you are requesting, be it impersonation, access to the harddrive or what have you. GAC中的代码必须声明XXXPermission,其中XXX是您要求的权限,无论是模仿,访问硬盘还是您有什么。

You should revert the assert immediately afterwords. 您应该在词语后立即恢复断言。

You should make sure that the API on your DLL that you put in the GAC has no opportunities for abuse. 您应该确保放在GAC中的DLL上的API没有滥用的机会。 For example, if you were writing a website for letting users backup the server via a command line application, your API should old expose a method like "BackUp()" and not "LaunchAribitraryProcess(string path)" 例如,如果您正在编写一个允许用户通过命令行应用程序备份服务器的网站,那么您的API应该暴露出像“BackUp()”这样的方法,而不是“LaunchAribitraryProcess(string path)”

The web.config file must have impersonation set up as well, or you will run into NTFS permission problems as well as CAS. web.config文件也必须设置模拟,否则您将遇到NTFS权限问题以及CAS。

Here is the complete explanation . 这是完整的解释

You might also try wrapping your code inside 您也可以尝试将代码包装在里面

using (Impersonator person = new Impersonator("domainName", "userName",
"password")
{
    // do something requiring special permissions
}

as mentioned in http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework.windowsforms/topic62740.aspx http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework.windowsforms/topic62740.aspx中所述

What exactly are you trying to do? 你究竟想做什么? I can't quite see what the point of your code is in creating a different executable. 我无法确定您的代码在创建不同的可执行文件时的意义。 It looks rather odd. 看起来很奇怪。 Perhaps it would be more helpful to state the busines problem you are trying to solve first. 也许说出你想要首先解决的业务问题会更有帮助。

It looks like you're trying to have the IIS service impersonate a user with higher privileges than the service itself (in this case, an administrator). 看起来您正在尝试让IIS服务模拟具有比服务本身更高权限的用户(在本例中为管理员)。 Windows blocks this as a security hole, since at that point you're basically begging someone to take over your system. Windows将此视为安全漏洞,因为此时您基本上是在乞求某人接管您的系统。 There may be a way to work around this limitation, but don't do that--it's for your own good. 可能有办法解决这个限制,但不要这样做 - 这是为了你自己的利益。

Instead, have IIS impersonate a user with limited permissions, who has exactly the rights that you need it to have. 相反,让IIS模拟具有有限权限的用户,该用户具有您需要的权限。 Eg create a user account that owns only the folders that you want your web service to write to, or whatever other combination of rights is appropriate. 例如,创建一个用户帐户,该帐户只拥有您希望Web服务写入的文件夹,或者其他任何权限组合。 If impersonating a limited user, you won't see this error code, but should still be able to call the benign executable you have here. 如果冒充有限的用户,您将看不到此错误代码,但仍应能够调用此处的良性可执行文件。

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

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