示例#1
0
 private static void bindingSetAuthInfo(RPC_C_AUTHN_LEVEL level, RPC_C_AUTHN[] authTypes,
                                        RpcHandle handle, string serverPrincipalName, NetworkCredential credentails)
 {
     if (credentails == null)
     {
         foreach (RPC_C_AUTHN atype in authTypes)
         {
             RPC_STATUS result = NativeMethods.RpcBindingSetAuthInfo2(handle.Handle, serverPrincipalName, level, atype, IntPtr.Zero, 0);
             if (result != RPC_STATUS.RPC_S_OK)
             {
                 RpcTrace.Warning("Unable to register {0}, result = {1}", atype, new RpcException(result).Message);
             }
         }
     }
     else
     {
         SEC_WINNT_AUTH_IDENTITY pSecInfo = new SEC_WINNT_AUTH_IDENTITY(credentails);
         foreach (RPC_C_AUTHN atype in authTypes)
         {
             RPC_STATUS result = NativeMethods.RpcBindingSetAuthInfo(handle.Handle, serverPrincipalName, level, atype, ref pSecInfo, 0);
             if (result != RPC_STATUS.RPC_S_OK)
             {
                 RpcTrace.Warning("Unable to register {0}, result = {1}", atype, new RpcException(result).Message);
             }
         }
     }
 }
示例#2
0
        private static void bindingFromStringBinding(RpcHandle handle, String bindingString)
        {
            RPC_STATUS result = NativeMethods.RpcBindingFromStringBinding(bindingString, out handle.Handle);

            Guard.Assert(result);
            RpcTrace.Verbose("RpcClient.bindingFromStringBinding({0} = {1})", handle.Handle, bindingString);
        }
示例#3
0
 public static void Assert(RPC_STATUS errorsCode)
 {
     if (errorsCode != RPC_STATUS.RPC_S_OK)
     {
         RpcException ex = new RpcException(errorsCode);
         RpcTrace.Error("RPC_STATUS.{0} - {1}", errorsCode, ex.Message);
         throw ex;
     }
 }
示例#4
0
        private RPC_STATUS CheckRPCStatus()
        {
            RPC_STATUS status = ReceiveRPCStatus();

            if (IsFatalStatus(status))
            {
                StatusMessages.TryGetValue(status, out string value);
                throw new Exception($"librpc: {value}");
            }

            return(status);
        }
示例#5
0
        private static bool serverRegisterAuthInfo(RPC_C_AUTHN auth, string serverPrincName)
        {
            RpcTrace.Verbose("serverRegisterAuthInfo({0})", auth);
            RPC_STATUS response = NativeMethods.RpcServerRegisterAuthInfo(serverPrincName, (uint)auth, IntPtr.Zero, IntPtr.Zero);

            if (response != RPC_STATUS.RPC_S_OK)
            {
                RpcTrace.Warning("serverRegisterAuthInfo - unable to register authentication type {0}", auth);
                return(false);
            }
            return(true);
        }
示例#6
0
        private static void serverListen(int maxCalls)
        {
            RpcTrace.Verbose("Begin Server Listening");
            // starts listening all server in process on registered protocols
            //TODO: make server isolated of other process services, make it not static as possible
            RPC_STATUS result = NativeMethods.RpcServerListen(1, maxCalls, 1);

            if (result == RPC_STATUS.RPC_S_ALREADY_LISTENING)
            {
                result = RPC_STATUS.RPC_S_OK;
            }
            Guard.Assert(result);
            RpcTrace.Verbose("Server Ready");
        }
示例#7
0
        private static bool serverUseProtseqEp(RpcProtseq protocol, int maxCalls, String endpoint)
        {
            RpcTrace.Verbose("serverUseProtseqEp({0})", protocol);
            // all RPC servers within the process will be available on that protocol
            // once invoked this can not be undone
            //TODO: make server isolated of other process services, make it not static as possible
            RPC_STATUS err = NativeMethods.RpcServerUseProtseqEp(protocol.ToString(), maxCalls, endpoint, IntPtr.Zero);

            if (err != RPC_STATUS.RPC_S_DUPLICATE_ENDPOINT)
            {
                Guard.Assert(err);
            }
            return(err == RPC_STATUS.RPC_S_OK);
        }
示例#8
0
        // Creates a string binding handle.
        // This function is nothing more than a printf.
        // Connection is not done here.
        private static String stringBindingCompose(EndpointBindingInfo endpointBindingInfo,
                                                   String Options)
        {
            IntPtr     lpBindingString;
            RPC_STATUS result = NativeMethods.RpcStringBindingCompose(null, endpointBindingInfo.Protseq.ToString(), endpointBindingInfo.NetworkAddr, endpointBindingInfo.EndPoint, Options,
                                                                      out lpBindingString);

            Guard.Assert(result);

            try
            {
                return(Marshal.PtrToStringUni(lpBindingString));
            }
            finally
            {
                Guard.Assert(NativeMethods.RpcStringFree(ref lpBindingString));
            }
        }
示例#9
0
        private static void serverStopListening()
        {
            RpcTrace.Verbose("Stop Server Listening");
            // stops listening on all registered protocols.
            //TODO: make server isolated of other process services, make it not static as possible
            RPC_STATUS result = NativeMethods.RpcMgmtStopServerListening(IntPtr.Zero);

            if (result != RPC_STATUS.RPC_S_OK)
            {
                RpcTrace.Warning("RpcMgmtStopServerListening result = {0}", result);
            }
            //TODO: make server isolated of other process services, make it not static as possible
            result = NativeMethods.RpcMgmtWaitServerListen();
            if (result != RPC_STATUS.RPC_S_OK)
            {
                RpcTrace.Warning("RpcMgmtWaitServerListen result = {0}", result);
            }
        }
示例#10
0
        /// <summary>
        /// Get process information (memory map)
        /// </summary>
        /// <param name="pid">Process ID</param>
        /// <returns></returns>
        public ProcessInfo GetProcessInfo(int pid)
        {
            if (!IsConnected)
            {
                throw new Exception(NotConnectedErrorMessage);
            }

            SendCMDPacket(RPC_CMDS.RPC_PROC_INFO, RPC_PROC_INFO1_SIZE);
            SendPacketData(RPC_PROC_INFO1_SIZE, pid);

            RPC_STATUS status = CheckRPCStatus();

            if (status == RPC_STATUS.RPC_INFO_NO_MAP)
            {
                return(new ProcessInfo(pid, null));
            }

            // recv count
            byte[] bnumber = new byte[4];
            sock.Receive(bnumber, 4, SocketFlags.None);
            int number = BitConverter.ToInt32(bnumber, 0);

            // recv data
            byte[] data = ReceiveData(number * RPC_PROC_INFO2_SIZE);

            // parse data
            MemoryEntry[] entries = new MemoryEntry[number];
            for (int i = 0; i < number; i++)
            {
                int offset = i * RPC_PROC_INFO2_SIZE;
                entries[i] = new MemoryEntry
                {
                    name   = GetNullTermString(data, offset),
                    start  = BitConverter.ToUInt64(data, offset + 32),
                    end    = BitConverter.ToUInt64(data, offset + 40),
                    offset = BitConverter.ToUInt64(data, offset + 48),
                    prot   = BitConverter.ToUInt32(data, offset + 56)
                };
            }

            return(new ProcessInfo(pid, entries));
        }
示例#11
0
 private static bool IsFatalStatus(RPC_STATUS status)
 {
     // if status first nibble starts with F
     return((uint)status >> 28 == 15);
 }
示例#12
0
 /// <summary>
 /// Exception class: RpcException : System.ComponentModel.Win32Exception
 /// Unspecified rpc error
 /// </summary>
 public RpcException(RPC_STATUS errorsCode)
     : base(unchecked((int)errorsCode))
 {
 }
示例#13
0
 /// <summary>
 /// Exception class: RpcException : System.ComponentModel.Win32Exception
 /// Unspecified rpc error
 /// </summary>
 public RpcException(RPC_STATUS errorsCode)
     : base(unchecked ((int)errorsCode))
 {
 }
示例#14
0
        private RPC_CALL_ATTRIBUTES_V2 GetCallInfo()
        {
            if (_callAttrs.Version != 0)
            {
                return(_callAttrs);
            }
            var attrs = new RPC_CALL_ATTRIBUTES_V2
            {
                Version = 2,
                Flags   = RPC_CALL_ATTRIBUTES_FLAGS.RPC_QUERY_NO_AUTH_REQUIRED
            };
            RPC_STATUS err = NativeMethods.RpcServerInqCallAttributes(_clientHandle, ref attrs);

            if (err == RPC_STATUS.RPC_S_INVALID_ARG) //may not support v2 on early edditions of XP/SP3
            {
                attrs.Version = 1;
                err           = NativeMethods.RpcServerInqCallAttributes(_clientHandle, ref attrs);
            }

            if (err == RPC_STATUS.RPC_S_OK)
            {
                _callAttrs       = attrs;
                _isAuthenticated = false;
                attrs.Flags      = RPC_CALL_ATTRIBUTES_FLAGS.RPC_QUERY_IS_CLIENT_LOCAL |
                                   RPC_CALL_ATTRIBUTES_FLAGS.RPC_QUERY_NO_AUTH_REQUIRED;
                if ((err = NativeMethods.RpcServerInqCallAttributes(_clientHandle, ref attrs)) == RPC_STATUS.RPC_S_OK)
                {
                    _callAttrs.IsClientLocal = attrs.IsClientLocal;

                    if (_callAttrs.ProtocolSequence == RpcProtoseqType.LRPC)
                    {
                        attrs.Flags = RPC_CALL_ATTRIBUTES_FLAGS.RPC_QUERY_CLIENT_PID;
                        if ((err = NativeMethods.RpcServerInqCallAttributes(_clientHandle, ref attrs)) == RPC_STATUS.RPC_S_OK)
                        {
                            _callAttrs.ClientPID = attrs.ClientPID;
                        }
                    }
                }

                if (_callAttrs.ProtocolSequence != RpcProtoseqType.LRPC)
                {
                    using (Ptr <byte[]> callerAddress = new Ptr <byte[]>(new byte[1024]))
                    {
                        var localAddress = new RPC_CALL_LOCAL_ADDRESS_V1();
                        localAddress.Version       = 1;
                        localAddress.Buffer        = callerAddress.Handle;
                        localAddress.BufferSize    = 1024;
                        localAddress.AddressFormat = RpcLocalAddressFormat.Invalid;
                        _callAttrs = attrs;

                        using (var callerAddressv1 = new Ptr <RPC_CALL_LOCAL_ADDRESS_V1>(localAddress))
                        {
                            attrs.CallLocalAddress = callerAddressv1.Handle;
                            attrs.Flags            = RPC_CALL_ATTRIBUTES_FLAGS.RPC_QUERY_CALL_LOCAL_ADDRESS |
                                                     RPC_CALL_ATTRIBUTES_FLAGS.RPC_QUERY_NO_AUTH_REQUIRED;
                            if ((err = NativeMethods.RpcServerInqCallAttributes(_clientHandle, ref attrs)) == RPC_STATUS.RPC_S_OK)
                            {
                                _clientAddress = new byte[callerAddressv1.Data.BufferSize];
                                Array.Copy(callerAddress.Data, _clientAddress, _clientAddress.Length);
                            }
                        }
                    }
                }

                using (Ptr <byte[]> clientPrincipal = new Ptr <byte[]>(new byte[1024]))
                {
                    attrs.ClientPrincipalName             = clientPrincipal.Handle;
                    attrs.ClientPrincipalNameBufferLength = 1024;
                    attrs.Flags = RPC_CALL_ATTRIBUTES_FLAGS.RPC_QUERY_CLIENT_PRINCIPAL_NAME;
                    if ((err = NativeMethods.RpcServerInqCallAttributes(_clientHandle, ref attrs)) == RPC_STATUS.RPC_S_OK)
                    {
                        _clientPrincipalName = Marshal.PtrToStringUni(clientPrincipal.Handle);

                        if (!String.IsNullOrEmpty(_clientPrincipalName))
                        {
                            _isAuthenticated = true;
                            //On Windows XP this only returns a value on LRPC so we know they are local
                            if (attrs.Version == 1)
                            {
                                _callAttrs.IsClientLocal = RpcCallClientLocality.Local;
                            }
                        }
                    }
                }
            }
            else
            {
                RpcTrace.Warning("RpcServerInqCallAttributes error {0} = {1}", err, new RpcException(err).Message);
            }


            return(_callAttrs);
        }