V
V
Vasily Lizogub2016-04-22 15:26:05
.NET
Vasily Lizogub, 2016-04-22 15:26:05

How to connect to a named pipe without admin rights?

Hello. There are two C# applications - a service (windows service) and a client application that exchange information via named pipes (Named Pipes). The problem is that the client application can only connect to the named pipe service with administrative privileges. I found a solution on the net, which suggests assigning a PipeSecurity to a server named pipe containing one or more PipeAccessRule. After applying this approach, the server application crashes with a System.UnauthorizedAccessException. Tell me how to connect to the service of a client application that is running without admin rights via a named pipe? I do this:
Windows Service

protected override void OnStart(string[] args)
        {
            new Thread(() =>
            {
                while (true)
                {
                    using (var pipe = new NamedPipeServerStream("test", PipeDirection.InOut, 1, PipeTransmissionMode.Byte))
                    {
                        var ps = new PipeSecurity();
                        ps.AddAccessRule(new PipeAccessRule(WindowsIdentity.GetCurrent().Name, PipeAccessRights.FullControl, AccessControlType.Allow));
                        pipe.SetAccessControl(ps);

                        pipe.WaitForConnection();
                        int x = pipe.ReadByte();
                        pipe.WriteByte((byte)(x + 1));
                    }
                }
            })
            { IsBackground = true }.Start();
        }

Customer:
using (var pipe = new NamedPipeClientStream(".", "test", PipeDirection.InOut))
            {
                pipe.Connect();
                pipe.WriteByte(100);
                int x = pipe.ReadByte();
                Console.WriteLine(x);
            }

I also tried to change the code of the WindowsIdentity.GetCurrent().Name service to "Everyone", but in this case the exception is System.Security.Principal.IdentityNotMappedException.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2016-04-22
@Sing303

The server needs these settings

var pipeSecurity = new PipeSecurity();
pipeSecurity.SetAccessRule(new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null), PipeAccessRights.ReadWrite, AccessControlType.Allow));

WindowsIdentity.GetCurrent().Name is the user name of the service, because in a service from the system is running

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question