Exemple #1
0
 public static extern int ZwAcceptConnectPort(ref System.IntPtr PortHandle, System.IntPtr PortContext, ref PORT_MESSAGE ConnectionRequest, byte AcceptConnection, ref PORT_VIEW ServerView, ref REMOTE_PORT_VIEW ClientView);
Exemple #2
0
    public static unsafe void ServerThread1()
    {
//        SECURITY_DESCRIPTOR sd = new SECURITY_DESCRIPTOR();
        IntPtr sd = Marshal.AllocHGlobal(sizeof(SECURITY_DESCRIPTOR));

        OBJECT_ATTRIBUTES ObjAttr  = new OBJECT_ATTRIBUTES();             // Object attributes for the name
        UNICODE_STRING    PortName = new UNICODE_STRING();
        int    Status;
        IntPtr LpcPortHandle;
        IntPtr RequestBuffer = Marshal.AllocHGlobal(sizeof(G.PORT_MESSAGE) + Constants.MAX_LPC_DATA);
        bool   WeHaveToStop  = false;
        int    nError;
        TRANSFERRED_MESSAGE tm = TRANSFERRED_MESSAGE.Create();

        //
        // Initialize security descriptor that will be set to
        // "Everyone has the full access"
        //


        if (!InitializeSecurityDescriptor_Ptr(out sd, SECURITY_DESCRIPTOR_REVISION))
        {
            nError = Marshal.GetLastWin32Error();
            if (nError != Constants.S_OK)
            {
                throw new Win32Exception(nError);
            }
        }

        //
        // Set the empty DACL to the security descriptor
        //

        if (!SetSecurityDescriptorDacl_Ptr(ref sd, true, IntPtr.Zero, false))
        {
            nError = Marshal.GetLastWin32Error();
            if (nError != Constants.S_OK)
            {
                throw new Win32Exception(nError);
            }
        }

        uint securityDescriptorSize = 0;

        ConvertStringSecurityDescriptorToSecurityDescriptor("S:(ML;;NW;;;LW)", 1, ref sd, ref securityDescriptorSize);

        //
        // Initialize attributes for the port and create it
        //

        //RtlInitUnicodeString(ref PortName, LpcPortName);
        PortName.Length        = (ushort)(LpcPortName.Length * 2);
        PortName.MaximumLength = (ushort)(PortName.Length + 2);
        PortName.buffer        = Marshal.StringToHGlobalUni(LpcPortName);

        NRegFreeCom.AssemblySystem a = new AssemblySystem();
        //var qwe = LoadLibrary(@"I:\src\NLocalIpc\build\Debug\rpcrtex.dll");
        // var aaaaa = NRegFreeCom.Interop.NativeMethods.GetProcAddress(qwe, "secddd");
        var aa  = a.LoadFrom(@"I:\src\NLocalIpc\build\Debug\rpcrtex.dll");
        var aaa = aa.GetDelegate <secddd>();

        sd = aaa();

        OBJECT_ATTRIBUTES.InitializeObjectAttributes_Ptr(ref ObjAttr, ref PortName, 0, IntPtr.Zero, sd);

        Console.WriteLine("Server: Creating LPC port \"{0}\" (NtCreatePort) ...\n", LpcPortName);
        var msg = (uint)(sizeof(PORT_MESSAGE) + G.Constants.MAX_LPC_DATA);

        Status = G.NativeMethods.NtCreatePort(out LpcPortHandle,
                                              ref ObjAttr,
                                              100,
                                              msg,
                                              0);
        Console.WriteLine("Server: NtCreatePort result {0:x8}", Status);


        AlpcErrorHandler.Check(Status);

        //
        // Process all incoming LPC messages
        //
        uint qwe = 0;

        while (WeHaveToStop == false)
        {
            //PTRANSFERRED_MESSAGE
            IntPtr LpcMessage   = IntPtr.Zero;
            IntPtr ServerHandle = IntPtr.Zero;

            //
            // Create the data buffer for the request
            //

            LpcMessage = RequestBuffer;
            Console.WriteLine("Server: ------------- Begin loop ----------------------\n {0}", Status);

            //
            // Listen to the port. This call is blocking, and cannot be interrupted,
            // even if the handle is closed. The only way how to stop the block is to send
            // an LPC request which will be recognized by server thread as "Stop" command
            //
            AlpcErrorHandler.Check(Status);

            Console.WriteLine("Server: Listening to LPC port (NtListenPort) ... {0} \n", LpcPortName);


            Status = G.NativeMethods.NtListenPort(LpcPortHandle,
                                                  ref tm.Header);
            Console.WriteLine("Server: NtListenPort result {0:x8}\n", Status);

            //
            // Accept the port connection
            //

            AlpcErrorHandler.Check(Status);

            Console.WriteLine("Server: Accepting LPC connection (NtAcceptConnectPort) ...{0:}\n", LpcPortName);
            IntPtr             NULL = IntPtr.Zero;
            G.PORT_VIEW        pv   = new G.PORT_VIEW();
            G.REMOTE_PORT_VIEW cv   = new G.REMOTE_PORT_VIEW();

            AlpcPortHandle sh;
            Status = G.NativeMethods.NtAcceptConnectPort_Ptr(out ServerHandle,
                                                             IntPtr.Zero,
                                                             ref tm.Header,
                                                             1,
                                                             NULL,
                                                             NULL);
            Console.WriteLine("Server: NtAcceptConnectPort result {0:x8}\n", Status);



            //
            // Complete the connection
            //

            AlpcErrorHandler.Check(Status);

            Console.WriteLine("Server: Completing LPC connection (NtCompleteConnectPort) ...{0:}\n", LpcPortName);

            Status = G.NativeMethods.NtCompleteConnectPort(ServerHandle);
            Console.WriteLine("Server: NtCompleteConnectPort result {0:x8}\n", Status);



            //
            // Now accept the data request coming from the port.
            //

            AlpcErrorHandler.Check(Status);
            // lpcMsg = (TRANSFERRED_MESSAGE)Marshal.PtrToStructure(LpcMessage, typeof(TRANSFERRED_MESSAGE));
            //  mh = lpcMsg.Header;
            Console.WriteLine("Server: Receiving LPC data (NtReplyWaitReceivePort) ...{0:}\n", LpcPortName);


            IntPtr asd = Marshal.AllocHGlobal(sizeof(TRANSFERRED_MESSAGE));
            Marshal.StructureToPtr(tm, asd, false);

            Status = G.NativeMethods.NtReplyWaitReceivePort_NoMarshal(ServerHandle,
                                                                      ref NULL,
                                                                      IntPtr.Zero,
                                                                      asd);
            Console.WriteLine("Server: NtReplyWaitReceivePort result {0:x8}\n", Status);
            var wqe = Marshal.PtrToStructure(asd, typeof(TRANSFERRED_MESSAGE));
            //  var qwe = Marshal.PtrToStringUni(tm.MessageText);
            //
            // Get the data sent by the client
            //

            AlpcErrorHandler.Check(Status);



            // If a request has been received, answer to it.
            switch (tm.Command)
            {
            case LPC_COMMAND_REQUEST_NOREPLY:

                Console.WriteLine("Server: Received request {0}\n", tm.MessageText);

                break;         // Nothing more to do

            case LPC_COMMAND_REQUEST_REPLY:
                //_tprintf(_T("Server: Received request \"%s\"\n"), LpcMessage->MessageText);
                //_tprintf(_T("Server: Sending reply (NtReplyPort) ...\n"), LpcPortName);
                Status = G.NativeMethods.NtReplyPort(LpcPortHandle, ref tm.Header);
                //_tprintf(_T("Server: NtReplyPort result 0x%08lX\n"), Status);
                break;

            case LPC_COMMAND_STOP:         // Stop the work
                //_tprintf(_T("Server: Stopping ...\n"));
                WeHaveToStop = false;
                break;
            }


            //
            // Close the server connection handle
            //

            if (ServerHandle != NULL)
            {
                // _tprintf(_T("Server: Closing the request handle (NtClose) ...\n"), LpcPortName);
                Status = NAlpc.NativeMethods.NtClose(ServerHandle);
                //_tprintf(_T("Server: NtClose result 0x%08lX\n"), Status);
            }

            //_tprintf(_T("Server: ------------- End loop ----------------------\n"), Status);
        }



        return;
    }
Exemple #3
0
 public static extern int ZwConnectPort(ref System.IntPtr PortHandle, ref NAlpc.UNICODE_STRING PortName, ref SECURITY_QUALITY_OF_SERVICE SecurityQos, ref PORT_VIEW ClientView, ref REMOTE_PORT_VIEW ServerView, ref uint MaxMessageLength, System.IntPtr ConnectionInformation, ref uint ConnectionInformationLength);