Ejemplo n.º 1
0
 public static extern uint RpcMgmtEpEltInqNext(
     IntPtr InquiryContext,
     ref RPC_IF_ID IfId,
     out IntPtr Binding,
     int ObjectUuid, // going to be 0/NULL, so we don't care about "ref RPC_IF_ID IfId"
     out IntPtr Annotation
     );
Ejemplo n.º 2
0
        public override IEnumerable <CommandDTOBase?> Execute(string[] args)
        {
            // Ref - https://stackoverflow.com/questions/21805038/how-do-i-pinvoke-rpcmgmtepeltinqnext

            uint retCode; // RPC_S_OK
            uint status;  // RPC_S_OK

            var    bindingHandle  = IntPtr.Zero;
            var    inquiryContext = IntPtr.Zero;
            var    ifId           = new RPC_IF_ID();
            string host           = null;

            if (args.Length >= 1)
            {
                // if we're specifying a remote host via arguments
                host = args[0];
            }

            try
            {
                // built the RPC binding string we're going to use
                retCode = RpcStringBindingCompose(null, "ncacn_ip_tcp", host, null, null, out var stringBinding);
                if (retCode != 0)
                {
                    WriteError($"Bad return value from RpcStringBindingCompose : {retCode}");
                    yield break;
                }

                // create the actual RPC binding (from the binding string)
                retCode = RpcBindingFromStringBinding(stringBinding, out bindingHandle);
                if (retCode != 0)
                {
                    WriteError($"Bad return value from RpcBindingFromStringBinding : {retCode}");
                    yield break;
                }

                // create an inquiry context for viewing the elements in an endpoint map
                retCode = RpcMgmtEpEltInqBegin(bindingHandle, 0, 0, 0, 0, out inquiryContext);
                if (retCode != 0)
                {
                    WriteError($"Bad return value from RpcMgmtEpEltInqBegin : {retCode}");
                    yield break;
                }

                var prev   = new Guid();
                var result = new RPCMappedEndpointsDTO();
                result.Elements = new List <string>();

                do
                {
                    // iterate through all of the elements in the RPC endpoint map
                    status = RpcMgmtEpEltInqNext(inquiryContext, ref ifId, out var elementBindingHandle, 0, out var elementAnnotation);

                    if (status == 0)
                    {
                        if (ifId.Uuid != prev)
                        {
                            if (prev != new Guid())
                            {
                                var result2 = new RPCMappedEndpointsDTO();
                                result2         = result;
                                result          = new RPCMappedEndpointsDTO();
                                result.Elements = new List <string>();

                                yield return(result2);
                            }

                            var annotation = Marshal.PtrToStringAuto(elementAnnotation);
                            result.UUID       = ifId.Uuid;
                            result.Annotation = annotation;

                            if (!String.IsNullOrEmpty(annotation))
                            {
                                RpcStringFree(ref elementAnnotation);
                            }

                            prev = ifId.Uuid;
                        }
                        if (elementBindingHandle != IntPtr.Zero)
                        {
                            var stringBinding2 = IntPtr.Zero;
                            status = RpcBindingToStringBinding(elementBindingHandle, out stringBinding2);

                            if (status == 0)
                            {
                                var stringBindingStr = Marshal.PtrToStringAuto(stringBinding2);
                                result.Elements.Add(stringBindingStr);

                                RpcStringFree(ref stringBinding2);
                                RpcBindingFree(ref elementBindingHandle);
                            }
                            else
                            {
                                // throw new Exception("[X] RpcBindingToStringBinding: " + retCode);
                            }
                        }
                    }
                }while (status == 0);

                yield return(result);
            }
            finally
            {
                retCode = RpcMgmtEpEltInqDone(ref inquiryContext);
                if (retCode != 0)
                {
                    WriteError($"Bad return value from RpcMgmtEpEltInqDone : {retCode}");
                }

                retCode = RpcBindingFree(ref bindingHandle);
                if (retCode != 0)
                {
                    WriteError($"Bad return value from RpcBindingFree : {retCode}");
                }
            }
        }
Ejemplo n.º 3
0
        public override IEnumerable <CommandDTOBase?> Execute(string[] args)
        {
            // Ref - https://stackoverflow.com/questions/21805038/how-do-i-pinvoke-rpcmgmtepeltinqnext

            uint retCode; // RPC_S_OK
            uint status;  // RPC_S_OK


            var    ifId = new RPC_IF_ID();
            string host = null;

            if (args.Length >= 1)
            {
                // if we're specifying a remote host via arguments
                host = args[0];
            }

            // built the RPC binding string we're going to use
            retCode = RpcStringBindingCompose(null, "ncacn_ip_tcp", host, null, null, out var stringBinding);
            if (retCode != 0)
            {
                WriteError($"Bad return value from RpcStringBindingCompose : {retCode}");
                yield break;
            }

            // create the actual RPC binding (from the binding string)
            retCode = RpcBindingFromStringBinding(stringBinding, out var bindingHandle);
            if (retCode != 0)
            {
                WriteError($"Bad return value from RpcBindingFromStringBinding : {retCode}");
                yield break;
            }

            // create an inquiry context for viewing the elements in an endpoint map
            retCode = RpcMgmtEpEltInqBegin(bindingHandle, 0, 0, 0, 0, out var inquiryContext);
            if (retCode != 0)
            {
                WriteError($"Bad return value from RpcMgmtEpEltInqBegin : {retCode}");
                yield break;
            }

            do
            {
                // iterate through all of the elements in the RPC endpoint map
                status = RpcMgmtEpEltInqNext(inquiryContext, ref ifId, out var elementBinding, 0, out var elementAnnotation);

                if (status == RPC_X_NO_MORE_ENTRIES)
                {
                    break;
                }
                else if (status != 0)
                {
                    Console.WriteLine($"RpcMgmtEpEltInqNext failed. Error code: {status}");
                    break;
                }

                string annotation = elementAnnotation.ToString();
                string binding    = elementBinding.ToString();

                yield return(new RPCMappedEndpointsDTO(
                                 ifId.Uuid,
                                 annotation,
                                 binding,
                                 new Version(ifId.VersMajor, ifId.VersMinor)
                                 ));
            }while (status == 0);
        }
Ejemplo n.º 4
0
        private string InquiryAdldsDynamicPort(String servicePrincipalName, Guid?clientGuid, String protocolSequence, String serverComputerName, String endPoint, String networkOptions)
        {
            //Get the ldap port of ADLDS service:
            int index = servicePrincipalName.IndexOf(':');

            if (index < 0)
            {
                throw new InvalidOperationException("The LDAP port should be provided when SUT is ADLDS.");
            }
            string ldapPort = servicePrincipalName.Substring(index + 1);

            IntPtr bindingString = IntPtr.Zero;
            IntPtr bindingHandle = IntPtr.Zero;
            uint   status        = RpcNativeMethods.RpcStringBindingCompose(
                (clientGuid == null) ?
                null : clientGuid.Value.ToString(),
                protocolSequence,
                serverComputerName,
                endPoint,
                networkOptions,
                out bindingString);

            if (status != 0)
            {
                throw new InvalidOperationException("Failed to RpcStringBindingCompose. The returned status is "
                                                    + status);
            }

            status = RpcNativeMethods.RpcBindingFromStringBinding(
                bindingString,
                out bindingHandle);
            if (status != 0)
            {
                RpcNativeMethods.RpcBindingFree(ref bindingString);
                throw new InvalidOperationException("Failed to RpcBindingFromStringBinding. The returned status is "
                                                    + status);
            }

            RpcNativeMethods.RpcBindingFree(ref bindingString);

            IntPtr inquiryContext = IntPtr.Zero;
            Guid   uuid           = (clientGuid == null) ?
                                    new Guid() : clientGuid.Value;
            RPC_IF_ID rpcInterfaceInfo = new RPC_IF_ID();

            rpcInterfaceInfo.Uuid      = DrsrUtility.DRSUAPI_RPC_INTERFACE_UUID;
            rpcInterfaceInfo.VersMajor = (short)RPC_C_VERS.MAJOR_ONLY;
            status = RpcNativeMethods.RpcMgmtEpEltInqBegin(
                bindingHandle,
                RPC_C_EP.MATCH_BY_IF,
                ref rpcInterfaceInfo,
                RPC_C_VERS.COMPATIBLE,
                ref uuid,
                out inquiryContext);
            if (status != 0)
            {
                RpcNativeMethods.RpcBindingFree(ref bindingString);
                throw new InvalidOperationException("Failed to RpcMgmtEpEltInqBegin. The returned status is "
                                                    + status);
            }

            //the loop will break when the dynamic port bound to the ADLDS is found. Otherwise, thrown exception:
            while (true)
            {
                Guid   queriedUuid          = new Guid();
                IntPtr queriedBindingHandle = IntPtr.Zero;
                IntPtr queriedAnnotation    = IntPtr.Zero;
                status = RpcNativeMethods.RpcMgmtEpEltInqNext(
                    inquiryContext,
                    ref rpcInterfaceInfo,
                    out queriedBindingHandle,
                    out queriedUuid,
                    out queriedAnnotation);
                if (status != 0)
                {
                    RpcNativeMethods.RpcMgmtEpEltInqDone(ref inquiryContext);
                    RpcNativeMethods.RpcBindingFree(ref bindingString);
                    throw new InvalidOperationException("Failed to RpcMgmtEpEltInqNext. The returned status is "
                                                        + status);
                }

                //the LDAP port is found:
                if (Marshal.PtrToStringAnsi(queriedAnnotation).Equals(ldapPort))
                {
                    RpcNativeMethods.RpcStringFree(ref queriedAnnotation);
                    RpcNativeMethods.RpcMgmtEpEltInqDone(ref inquiryContext);

                    IntPtr queriedBindingString = IntPtr.Zero;
                    status = RpcNativeMethods.RpcBindingToStringBinding(
                        queriedBindingHandle, out queriedBindingString);
                    RpcNativeMethods.RpcBindingFree(ref queriedBindingHandle);
                    if (status != 0)
                    {
                        RpcNativeMethods.RpcBindingFree(ref bindingHandle);
                        throw new InvalidOperationException("Failed to RpcBindingToStringBinding. The status is "
                                                            + status);
                    }

                    //get the port from the stringBinding.
                    string dynamicPort = Marshal.PtrToStringAnsi(queriedBindingString);
                    dynamicPort = dynamicPort.Substring(dynamicPort.IndexOf('[') + 1);
                    dynamicPort = dynamicPort.Remove(dynamicPort.Length - 1, 1);

                    RpcNativeMethods.RpcStringFree(ref queriedBindingString);
                    RpcNativeMethods.RpcBindingFree(ref bindingHandle);
                    return(dynamicPort);
                }
                else
                {
                    RpcNativeMethods.RpcStringFree(ref queriedAnnotation);
                    RpcNativeMethods.RpcBindingFree(ref queriedBindingHandle);
                }
            }
        }