【问题标题】:Killing a Windows process from a C# console application: how do I set permissions?从 C# 控制台应用程序中终止 Windows 进程:如何设置权限?
【发布时间】:2010-12-18 22:03:24
【问题描述】:

使用 ASP.NET Web 应用程序中的 Process.Kill(),我收到带有文本“访问被拒绝”的 Win32Exception。

在网上搜索多次告诉我设置权限。但是,我对 Windows XP 用户系统的了解还不够深入,不知道如何开始。

在抛出异常时,Thread.CurrentPrincipal.Identity 具有以下可见属性:

  1. AuthenticationType = ""
  2. IsAuthenticated = "假"
  3. 名称 = ""

WindowsIdentity.GetCurrent() 显示我以“NAME\ASPNET”的身份登录,但我认为这无关紧要。

我需要做什么?我能以某种方式让线程以某个 Windows 用户身份登录吗?

【问题讨论】:

  • 那么...这是 C# 控制台应用程序还是 ASP.NET 应用程序?它确实对答案产生了影响。
  • 这是一个 ASP.NET 应用程序。但是我也可能是从控制台应用程序中执行此操作的...因此,如果您愿意在两种可能性的情况下帮助我,我将非常感激

标签: c# asp.net process kill permissions


【解决方案1】:

您需要以具有足够权限的用户身份运行您的 C# 应用程序。

如果您不能信任具有此类权限的 ASP AppPool(您不应该),您需要创建一个单独的服务,该服务在具有足够权限的帐户下运行,并在低权限应用程序和更高权限的服务之间建立协议以与之通信杀死进程的目的。

不要在 AppPool 中冒充高权限用户。你必须出示它的密码,这样你就有效地将低权限帐户提升到高权限帐户,通过所有有效的手段,在 AppPool 被破坏的情况下,就像你在高权限下运行 AppPool 并且没有完成任何隔离一样.

【讨论】:

  • 但我如何“以具有足够权限的用户身份运行 C# 应用程序”?
  • 您将所需的权限授予运行应用程序的帐户,即。从本地安全策略应用程序(在管理工具中)到 NAME\ASPNET,并将 NAME\ASPNET 添加到“调试程序”权限。见support.microsoft.com/kb/155075
【解决方案2】:

我认为您的方法是正确的,“访问被拒绝”的问题是由于 ASP.NET 进程与 ASPNET 用户一起运行,该用户具有有限的权限,这就是您遇到的错误。您可以做的是为您的 Web 应用程序设置 imersnation。您可以通过更改 web.config 或代码来实现。更多关于冒充的信息你可以阅读here

web.comfig 非常简单,您需要在 web.config 的 system.web 部分添加以下行

<identity impersonate="true" userName="domain\user" password="password" />

用户需要在服务器上拥有管理员权限

如果您想在下面的代码中执行模拟是您如何执行此操作的示例:

...
WindowsImpersonationContext context = ImpersonateUser("domain", "user", "password");
// kill your process
context.Undo();
...

[DllImport("advapi32.dll")]
private static extern bool LogonUser(
    String lpszUsername, String lpszDomain, String lpszPassword,
    int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

[DllImport("advapi32.dll")]
private static extern bool DuplicateToken(
    IntPtr ExistingTokenHandle, int ImpersonationLevel,
    ref IntPtr DuplicateTokenHandle);

[DllImport("kernel32.dll")]
private static extern bool CloseHandle(IntPtr hObject);


private enum SecurityImpersonationLevel
{
    SecurityAnonymous,
    SecurityIdentification,
    SecurityImpersonation,
    SecurityDelegation
}

private enum LogonTypes
{
    LOGON32_PROVIDER_DEFAULT=0,
    LOGON32_LOGON_INTERACTIVE=2,
    LOGON32_LOGON_NETWORK=3,
    LOGON32_LOGON_BATCH=4,
    LOGON32_LOGON_SERVICE=5,
    LOGON32_LOGON_UNLOCK=7,
    LOGON32_LOGON_NETWORK_CLEARTEXT=8,
    LOGON32_LOGON_NEW_CREDENTIALS=9
}

public static WindowsImpersonationContext ImpersonateUser(string domain, string username, string password)
{
    WindowsImpersonationContext result = null;
    IntPtr existingTokenHandle = IntPtr.Zero;
    IntPtr duplicateTokenHandle = IntPtr.Zero;

    try
    {
        if (LogonUser(username, domain, password,
            (int)LogonTypes.LOGON32_LOGON_NETWORK_CLEARTEXT, (int)LogonTypes.LOGON32_PROVIDER_DEFAULT,
            ref existingTokenHandle))
        {
            if (DuplicateToken(existingTokenHandle,
                (int)SecurityImpersonationLevel.SecurityImpersonation,
                ref duplicateTokenHandle))
            {
                WindowsIdentity newId = new WindowsIdentity(duplicateTokenHandle);
                result = newId.Impersonate();
            }
        }
    }
    finally
    {
        if (existingTokenHandle != IntPtr.Zero)
            CloseHandle(existingTokenHandle);
        if (duplicateTokenHandle != IntPtr.Zero)
            CloseHandle(duplicateTokenHandle);
    }
    return result;
}

希望这会有所帮助,问候

【讨论】:

    【解决方案3】:

    以 Serge Gubenko 的回答为基础,以下是模拟和终止进程的代码(他在回答中写道“//kill your process”):

    using (WindowsImpersonationContext context = ImpersonateUser("domain", "user", "password"))
        {
            Process[] proc = Process.GetProcessesByName("process name");
            if (proc.Length > 0)
                proc[0].Kill();
    
            context.Undo();
        }
    

    您仍然需要在他的回答中使用其余代码。 请注意,如果您不是域的一部分,只需输入一个空字符串

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-23
      • 1970-01-01
      • 2018-03-13
      • 1970-01-01
      • 2013-03-14
      相关资源
      最近更新 更多