【问题标题】:Starting remote Windows services with ServiceController and impersonation使用 ServiceController 和模拟启动远程 Windows 服务
【发布时间】:2012-01-24 19:10:56
【问题描述】:

我有一个需要能够打开和关闭远程服务的 .NET MVC3 应用程序。为了做到这一点,我通过 WindowsIdentity.Impersonate() 模拟了一个特定的用户帐户。要测试用户的权限,我可以以用户身份登录并从命令提示符处执行sc.exe \\[server] start [service]。我还知道 impersonate 命令按预期工作,因为应用程序匿名运行,因此无法在没有模拟的情况下控制我的本地计算机 (.) 上的服务,但 可以 通过模拟控制本地服务。但是,当我将它放在一起并尝试启动远程服务而不是本地服务时,我总是收到错误“无法在计算机上打开 [service] 服务'[server]'”

有没有人遇到过类似的问题?在我意识到 sc.exe 可以正常工作之前,我一直以为它是服务器配置而不是 .NET 问题。这是我正在使用的类的缩写版本:

public class Service
{
    public string Name;
    public bool Running;
    private ServiceController serviceController;

    public Service(string name, string host)
    {
        Name = name;

        serviceController = new ServiceController(Name, host);
        Running = serviceController.Status == ServiceControllerStatus.Running;
    }

    public bool StartService()
    {
        ServiceControllerPermission scp = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, serviceController.MachineName, Name);
        scp.Assert();

        serviceController.Start();
        serviceController.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 5));
        serviceController.Refresh();

        Running = serviceController.Status == ServiceControllerStatus.Running;

        return Running;
    }
}

附加说明:如果我将应用程序而不是服务器指向域中的另一台 Windows 7 PC 并将模拟凭据更改为该 PC 所有者的凭据,我实际上可以毫无问题地远程控制他们的服务。

根据要求,我将在此处添加模拟代码。它有点长,所以请耐心等待:

public class Impersonate
{
    public const int LOGON32_LOGON_INTERACTIVE = 2;
    public const int LOGON32_PROVIDER_DEFAULT = 0;

    WindowsImpersonationContext impersonationContext;

    [DllImport("advapi32.dll")]
    public static extern int LogonUserA(String lpszUserName,
        String lpszDomain,
        String lpszPassword,
        int dwLogonType,
        int dwLogonProvider,
        ref IntPtr phToken);
    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern int DuplicateToken(IntPtr hToken,
        int impersonationLevel,
        ref IntPtr hNewToken);

    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool RevertToSelf();

    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    public static extern bool CloseHandle(IntPtr handle);

    public bool impersonateValidUser(String userName, String domain, String password)
    {
        WindowsIdentity tempWindowsIdentity;
        IntPtr token = IntPtr.Zero;
        IntPtr tokenDuplicate = IntPtr.Zero;

        if (RevertToSelf())
        {
            if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
                LOGON32_PROVIDER_DEFAULT, ref token) != 0)
            {
                if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
                {
                    tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                    impersonationContext = tempWindowsIdentity.Impersonate();
                    if (impersonationContext != null)
                    {
                        CloseHandle(token);
                        CloseHandle(tokenDuplicate);
                        return true;
                    }
                }
            }
        }
        if (token != IntPtr.Zero)
            CloseHandle(token);
        if (tokenDuplicate != IntPtr.Zero)
            CloseHandle(tokenDuplicate);
        return false;
    }

    public void undoImpersonation()
    {
        impersonationContext.Undo();
    }
}

我在尝试启动或停止服务之前调用此代码:

Service s = new Service(ServiceName, MachineName);

if (Impersonation.impersonateValidUser(Username, Domain, Password))
{
    if (s.Running)
        s.StopService();
    else
        s.StartService();

    Impersonation.undoImpersonation();
}

值得注意的是,我可以列出服务并获取单个服务的状态(就像我在这里所做的那样) - 只有当我开始或停止服务时才会遇到麻烦。

【问题讨论】:

  • 你能显示你正在执行模拟的代码吗?我怀疑这可能是问题所在。
  • @competent_tech 感谢您的帮助。我已在问题中添加了与模仿相关的代码。
  • 您是使用 AD 域作为域还是运行服务的本地计算机?

标签: .net windows-services impersonation servicecontroller


【解决方案1】:

也许我找到了答案:

在模拟方法中LogonUserA

使用 LOGON32_LOGON_SERVICE (= 5) 代替 LOGON32_LOGON_INTERACTIVE。

确保您模拟的用户与运行服务的用户相同。

在我的例子中,用户包含在本地策略中 --> 将会话作为服务启动。我没有对不包含在该本地策略中的用户进行测试。

希望对你有帮助!

【讨论】:

  • 好吧,如果您要模拟的用户是机器管理员,那是正确的。
【解决方案2】:

好吧,如果您要模拟的用户是机器管理员,那是正确的。如果不是,那么您必须授予用户启动服务的特定权限。

由于我不是网络管理员,所以我不知道如何制作,但我找到了解释如何使用 subinacl 工具完成此操作的链接:

下载

最后,命令行是:

  • subinacl /service SERVICENAME /grant=DOMAINNAME\USERNAME Users=TO

Users=TO 授予用户启动/停止权限

在这种情况下,您必须使用 INTERACTIVE 模式而不是 SERVICE 模式模拟用户

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多