简体   繁体   English

NamedPipeClientStream 抛出 UnauthorizedAccessException:对路径的访问被拒绝

[英]NamedPipeClientStream throws UnauthorizedAccessException: Access to the path is denied

I have reviewed lots of similar questions but none seems to cover this case.我已经审查了很多类似的问题,但似乎没有一个涵盖这个案例。 So please check the details before assuming it is a duplicate.因此,请在假设它是重复的之前检查详细信息。

Scenario: I have a client and server application written in c# targeting .NET Framework 4.7.2 that communicates using NamedPipeClientStream and NamedPipeServerStream respectively.场景:我有一个用 c# 编写的客户端和服务器应用程序,目标是 .NET Framework 4.7.2,分别使用 NamedPipeClientStream 和 NamedPipeServerStream 进行通信。

The server is running as the local administrator account and creates the NamedPipeServerStream like this:服务器以本地管理员帐户运行,并像这样创建 NamedPipeServerStream:

PipeSecurity ps = new PipeSecurity();
ps.AddAccessRule(new PipeAccessRule(WindowsIdentity.GetCurrent().Owner, PipeAccessRights.FullControl, AccessControlType.Allow));
ps.AddAccessRule(new PipeAccessRule(new SecurityIdentifier("S-1-5-32-544"), PipeAccessRights.ReadWrite, AccessControlType.Allow));
NamedPipeServerStream mypipe = new NamedPipeServerStream("mypipe", PipeDirection.Out, 1, 0, PipeOptions.WriteThrough, 0, 0, ps);
mypipe.WaitForConnection();

On the server I have created a local account called "testuser" that is a member of the local administrators group.在服务器上,我创建了一个名为“testuser”的本地帐户,它是本地管理员组的成员。 This account is used for authentication on the client when connecting to the Named Pipes over the network.当通过网络连接到命名管道时,此帐户用于在客户端上进行身份验证。

The client program is running as a local user on a different machine connected via network and creates the connection like this:客户端程序以本地用户身份在通过网络连接的另一台计算机上运行,并创建如下连接:

NamedPipeClientStream mypipe = new NamedPipeClientStream("<ip of server>", "mypipe", PipeDirection.In, PipeOptions.None);
mypipe.Connect();

When running the server software on Windows Server 2012 this works fine.在 Windows Server 2012 上运行服务器软件时,它工作正常。 But on Windows Server 2016 and Windows Server 2019 I get the UnauthorizedAccessException.但是在 Windows Server 2016 和 Windows Server 2019 上,我得到了 UnauthorizedAccessException。 If instead of my local user "testuser" I authenticate with the built-in Administrator account over the network, it works fine on all three version of windows server.如果不是我的本地用户“testuser”,而是通过网络使用内置管理员帐户进行身份验证,它可以在所有三个版本的 windows 服务器上正常工作。

If I change the pipe security to use a SID of for example S-1-5-11 to allow "authenticated users" it also works on all three versions but the requirement is to only allow members of the local administrators group from connecting to the server over the network.如果我更改 pipe 安全性以使用例如 S-1-5-11 的 SID 以允许“经过身份验证的用户”,它也适用于所有三个版本,但要求是只允许本地管理员组的成员连接到服务器通过网络。 Ideally it should not matter if the account is a local or domain account, but I have only tested with local accounts.理想情况下,该帐户是本地帐户还是域帐户都没有关系,但我只使用本地帐户进行了测试。

I have done some debugging using Event Viewer on the server and I can see Event 5145 (Detailed File Share) with Audit success for my testuser when accessing the pipe, but the client still reports UnauthorizedAccessException.我已经使用服务器上的事件查看器进行了一些调试,并且在访问 pipe 时,我的测试用户可以看到事件 5145(详细文件共享)审核成功,但客户端仍然报告 UnauthorizedAccessException。

Is there any way to restrict access for only members of the local administrators group on windows server 2016 and 2019?有什么方法可以限制仅 windows 服务器 2016 和 2019 上的本地管理员组成员的访问权限?

If anyone else runs into this problem I found the cause.如果其他人遇到这个问题,我找到了原因。 The problem was "UAC remote restrictions".问题是“UAC 远程限制”。 More information can be found in Microsoft's KB 951016更多信息可以在 Microsoft 的 KB 951016 中找到

If a local account is a member of the Builtin\Administrators group and authenticates over the network for instance to an SMB share such as \\remotecomputer\C$ or a named pipe, the account will not authenticate as a "full" administrator and thus not have access to administrative resources.如果本地帐户是Builtin\Administrators组的成员并通过网络进行身份验证,例如 SMB 共享,例如\\remotecomputer\C$或命名的 pipe,则该帐户将不会作为“完整”管理员进行身份验证,因此无权访问管理资源。 Such accounts will have to be elevated from a interactive logon such as RDP to gain full administrative privileges.必须从 RDP 等交互式登录中提升此类帐户才能获得完全的管理权限。 As such, my scenario with builtin accounts does not work because of this security feature.因此,由于此安全功能,我的内置帐户方案不起作用。

There seems to be three ways to handle this problem.似乎有三种方法可以处理这个问题。

  1. Disable this security measure in the registry (probably not recommended).在注册表中禁用此安全措施(可能不推荐)。
  2. Use a domain account and add that as a local administrator.使用域帐户并将其添加为本地管理员。 This security restriction appears to only affect SAM (local) accounts.此安全限制似乎只影响SAM (本地)帐户。
  3. Only use the builtin Administrator account as this is also not affected by the security restriction.仅使用内置管理员帐户,因为这也不受安全限制的影响。

Note that if the registry is edited incorrectly there might be serious consequences.请注意,如果注册表编辑不正确,可能会产生严重后果。 So backup the registry before performing any changes.因此,在执行任何更改之前备份注册表。 The following instructions are based on Microsoft's article on the subject:以下说明基于 Microsoft 关于该主题的文章:

To disable the restriction in the registry:要禁用注册表中的限制:

  1. Open the registry using regedit and select the following key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System使用regedit和 select 打开注册表: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System
  2. Create the following key (type DWORD) if it does not exist, otherwise just edit it to have a value of 1: LocalAccountTokenFilterPolicy .如果它不存在,则创建以下键(类型 DWORD),否则只需将其编辑为值为 1: LocalAccountTokenFilterPolicy

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

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