首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >更改命名管道访问权限

更改命名管道访问权限
EN

Stack Overflow用户
提问于 2018-07-26 19:36:06
回答 1查看 4.3K关注 0票数 4

我已经使用System.IO.Pipes创建了一个命名管道。它的工作很好,直到我不得不运行程序在管理模式。当提升时,客户端无法再连接(客户端没有运行提升)。如果我以管理员身份运行客户端,则它连接良好,因此它看起来像是权限问题。我一直在研究如何解决这个问题,但没有成功(我发现处理Windows安全问题令人难以置信)。我的目标是允许任何客户端--无论是否提升--能够连接到管道。

我更改的第一件事是打开具有访问权限的管道:

代码语言:javascript
运行
复制
pipeServer = new NamedPipeServerStream(pipeName,
                                       PipeDirection.InOut,
                                       1,
                                       PipeTransmissionMode.Message,
                                       PipeOptions.Asynchronous,
                                       0x4000,
                                       0x400,
                                       null,
                                       HandleInheritability.Inheritable,
                                       PipeAccessRights.ChangePermissions | PipeAccessRights.AccessSystemSecurity);

然后我把这段代码拼凑在一起。所有操作都会运行,直到SetEntriesInAcl调用失败为止:

错误: 0x534 “帐户名称和安全ID之间没有进行映射。”

代码语言:javascript
运行
复制
IntPtr ownerSid = IntPtr.Zero;
IntPtr groupSid = IntPtr.Zero;
IntPtr dacl = IntPtr.Zero, newDacl = IntPtr.Zero;
IntPtr sacl = IntPtr.Zero;
IntPtr securityDescriptor = IntPtr.Zero;
if (SUCCEEDED(GetSecurityInfo(pipeServer.SafePipeHandle.handle.DangerousGetHandle(),                                                  
                              SE_OBJECT_TYPE.SE_KERNEL_OBJECT,
                              SECURITY_INFORMATION.DACL_SECURITY_INFORMATION,
                              out ownerSid,
                              out groupSid,
                              out dacl,
                              out sacl,
                              out securityDescriptor))) {
    EXPLICIT_ACCESS ea = new EXPLICIT_ACCESS();
    BuildExplicitAccessWithName(ref ea, "Everyone", GENERIC_ALL, ACCESS_MODE.GRANT_ACCESS, NO_INHERITANCE);
    // Next line fails
    if (SUCCEEDED(SetEntriesInAcl(1, ref ea, dacl, out newDacl))) {
        uint retval = SetSecurityInfo(handle,
                             SE_OBJECT_TYPE.SE_KERNEL_OBJECT,
                             SECURITY_INFORMATION.DACL_SECURITY_INFORMATION,
                             IntPtr.Zero,
                             IntPtr.Zero,
                             newDacl,
                             IntPtr.Zero);
                        // Haven't reached this point yet
    }
}

BuildExplicitAccessWithName函数不返回值,但似乎成功了。这就是电话之后的样子:

我希望能在这里提供任何帮助。

(所有Win32函数和数据类型都在pinvoke.net上找到。另外,我使用的是Windows 10。)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-27 13:41:36

我最终不用使用任何本地电话。PipeSecurity类起作用了。诀窍是我必须将它传递给构造函数:

代码语言:javascript
运行
复制
// Creates a PipeSecurity that allows users read/write access
PipeSecurity CreateSystemIOPipeSecurity()
{
    PipeSecurity pipeSecurity = new PipeSecurity();

    var id = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null);

    // Allow Everyone read and write access to the pipe. 
    pipeSecurity.SetAccessRule(new PipeAccessRule(id, PipeAccessRights.ReadWrite, AccessControlType.Allow));

    return pipeSecurity;
} 

在创建管道时使用该函数:

代码语言:javascript
运行
复制
PipeSecurity pipeSecurity = CreateSystemIOPipeSecurity();
pipeServer = new NamedPipeServerStream(pipeName,
                                       PipeDirection.InOut,
                                       1,
                                       PipeTransmissionMode.Message,
                                       PipeOptions.Asynchronous,
                                       0x4000,
                                       0x400,
                                       pipeSecurity,
                                       HandleInheritability.Inheritable);
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51546328

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档