internal static OptionalLength GetOptionalLength(this SafeBuffer buffer)
 {
     if (buffer == null || buffer.IsInvalid)
     {
         return(null);
     }
     return(new OptionalLength(buffer.GetLength()));
 }
 /// <summary>
 /// Method to query information for a message.
 /// </summary>
 /// <param name="info_class">The information class.</param>
 /// <param name="port">The port which has processed the message.</param>
 /// <param name="buffer">The buffer to return data in.</param>
 /// <param name="return_length">Return length from the query.</param>
 /// <returns>The NT status code for the query.</returns>
 public NtStatus QueryInformation(NtAlpc port, AlpcMessageInformationClass info_class,
                                  SafeBuffer buffer, out int return_length)
 {
     if (_port == null)
     {
         throw new ArgumentNullException("Message must be associated with a port");
     }
     return(NtSystemCalls.NtAlpcQueryInformationMessage(port.Handle, Header,
                                                        info_class, buffer, buffer.GetLength(), out return_length));
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Find a byte array in a buffer. Returns all instances of the compare array.
 /// </summary>
 /// <param name="buffer">The buffer to find the data in.</param>
 /// <param name="start_offset">Start offset in the buffer.</param>
 /// <param name="compare">The comparison byte array.</param>
 /// <returns>A list of offsets into the buffer where the compare was found.</returns>
 public static IEnumerable <int> FindBuffer(this SafeBuffer buffer, int start_offset, byte[] compare)
 {
     using (var compare_buffer = compare.ToBuffer()) {
         int max_length = buffer.GetLength() - compare.Length - start_offset;
         for (int i = 0; i < max_length; ++i)
         {
             if (buffer.EqualBuffer(start_offset + i, compare_buffer, 0, compare.Length))
             {
                 yield return(i + start_offset);
             }
         }
     }
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Issue a trace control request.
 /// </summary>
 /// <param name="function">The trace control function code.</param>
 /// <param name="input_buffer">The optional input buffer.</param>
 /// <param name="output_buffer">The optional output buffer.</param>
 /// <param name="throw_on_error">True to throw on error.</param>
 /// <returns>The output length.</returns>
 public static NtResult <int> Control(TraceControlFunctionCode function, SafeBuffer input_buffer,
                                      SafeBuffer output_buffer, bool throw_on_error)
 {
     if (input_buffer == null)
     {
         input_buffer = SafeHGlobalBuffer.Null;
     }
     if (output_buffer == null)
     {
         output_buffer = SafeHGlobalBuffer.Null;
     }
     return(NtSystemCalls.NtTraceControl(function, input_buffer, input_buffer.GetLength(),
                                         output_buffer, output_buffer.GetLength(), out int return_length)
            .CreateResult(throw_on_error, () => return_length));
 }
        /// <summary>
        /// Compare two buffers for equality.
        /// </summary>
        /// <param name="left">The left buffer.</param>
        /// <param name="left_offset">The offset into the left buffer.</param>
        /// <param name="right">The right buffer.</param>
        /// <param name="right_offset">The offset into the right buffer.</param>
        /// <param name="length">The length to compare.</param>
        /// <returns>True if the buffers are equal.</returns>
        public static bool EqualBuffer(this SafeBuffer left, int left_offset, SafeBuffer right, int right_offset, int length)
        {
            if (length == 0)
            {
                return(true);
            }

            long left_length  = left.GetLength();
            long right_length = right.GetLength();

            if (left_length < (left_offset + length) ||
                right_length < (right_offset + length))
            {
                return(false);
            }

            IntPtr compare_length = new IntPtr(length);

            return(NtRtl.RtlCompareMemory(left.DangerousGetHandle() + left_offset, right.DangerousGetHandle() + right_offset, compare_length) == compare_length);
        }
Ejemplo n.º 6
0
        private static NtResult <NtToken> LsaLogonUser(SecurityLogonType type, string auth_package, string origin_name,
                                                       SafeBuffer buffer, IEnumerable <UserGroup> local_groups, bool throw_on_error)
        {
            using (var list = new DisposableList())
            {
                var hlsa = list.AddResource(SafeLsaLogonHandle.Connect(throw_on_error));
                if (!hlsa.IsSuccess)
                {
                    return(hlsa.Cast <NtToken>());
                }
                NtStatus status = SecurityNativeMethods.LsaLookupAuthenticationPackage(
                    hlsa.Result, new LsaString(auth_package), out uint auth_pkg);
                if (!status.IsSuccess())
                {
                    return(status.CreateResultFromError <NtToken>(throw_on_error));
                }

                var groups = local_groups == null ? SafeTokenGroupsBuffer.Null
                    : list.AddResource(SafeTokenGroupsBuffer.Create(local_groups));

                TOKEN_SOURCE tokenSource = new TOKEN_SOURCE("NT.NET");
                SecurityNativeMethods.AllocateLocallyUniqueId(out tokenSource.SourceIdentifier);
                QUOTA_LIMITS quota_limits = new QUOTA_LIMITS();
                return(SecurityNativeMethods.LsaLogonUser(hlsa.Result, new LsaString(origin_name),
                                                          type, auth_pkg, buffer, buffer.GetLength(), groups,
                                                          tokenSource, out SafeLsaReturnBufferHandle profile,
                                                          out int cbProfile, out Luid logon_id, out SafeKernelObjectHandle token_handle,
                                                          quota_limits, out NtStatus subStatus).CreateResult(throw_on_error, () =>
                {
                    using (profile)
                    {
                        return NtToken.FromHandle(token_handle);
                    }
                }));
            }
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Method to set information for this object type.
 /// </summary>
 /// <param name="info_class">The information class.</param>
 /// <param name="buffer">The buffer to set data from.</param>
 /// <returns>The NT status code for the set.</returns>
 public override NtStatus SetInformation(SymbolicLinkInformationClass info_class, SafeBuffer buffer)
 {
     return(NtSystemCalls.NtSetInformationSymbolicLink(Handle, info_class, buffer, buffer.GetLength()));
 }
 /// <summary>
 /// Method to set information for this object type.
 /// </summary>
 /// <param name="info_class">The information class.</param>
 /// <param name="buffer">The buffer to set data from.</param>
 /// <returns>The NT status code for the set.</returns>
 public override NtStatus SetInformation(EnlistmentInformationClass info_class, SafeBuffer buffer)
 {
     return(NtSystemCalls.NtSetInformationEnlistment(Handle, info_class, buffer, buffer.GetLength()));
 }
 /// <summary>
 /// Method to set information for this object type.
 /// </summary>
 /// <param name="info_class">The information class.</param>
 /// <param name="buffer">The buffer to set data from.</param>
 /// <returns>The NT status code for the set.</returns>
 public override NtStatus SetInformation(TransactionInformationClass info_class, SafeBuffer buffer)
 {
     return(NtSystemCalls.NtSetInformationTransaction(Handle, info_class, buffer, buffer.GetLength()));
 }
 /// <summary>
 /// Extracts strings from a safe buffer.
 /// </summary>
 /// <param name="buffer">Safe buffer to extract the value from.</param>
 /// <param name="minimum_length">The minimum string length.</param>
 /// <param name="type">The type of strings to search for.</param>
 /// <returns>The list of extracted strings.</returns>
 public static IEnumerable <ExtractedString> Extract(SafeBuffer buffer, int minimum_length, ExtractedStringType type)
 {
     return(Extract(buffer, 0, buffer.GetLength(), minimum_length, type));
 }
Ejemplo n.º 11
0
 public NtResult <LsaCallPackageResponse> CallPackage(uint auth_package, SafeBuffer buffer, bool throw_on_error)
 {
     return(SecurityNativeMethods.LsaCallAuthenticationPackage(this, auth_package, buffer, buffer.GetLength(),
                                                               out SafeLsaReturnBufferHandle ret, out int ret_length, out NtStatus status).CreateResult(throw_on_error, () => CreateResponse(status, ret, ret_length)));
 }
 /// <summary>
 /// Method to query information for this object type.
 /// </summary>
 /// <param name="info_class">The information class.</param>
 /// <param name="buffer">The buffer to return data in.</param>
 /// <param name="return_length">Return length from the query.</param>
 /// <returns>The NT status code for the query.</returns>
 public override NtStatus QueryInformation(TransactionManagerInformationClass info_class, SafeBuffer buffer, out int return_length)
 {
     return(NtSystemCalls.NtQueryInformationTransactionManager(Handle, info_class, buffer, buffer.GetLength(), out return_length));
 }
Ejemplo n.º 13
0
 public override NtStatus SetInformation(SystemInformationClass info_class, SafeBuffer buffer)
 {
     return(NtSystemCalls.NtSetSystemInformation(info_class, buffer, buffer.GetLength()));
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Send a message to the filter.
 /// </summary>
 /// <param name="input">The input buffer.</param>
 /// <param name="output">The output buffer.</param>
 /// <param name="throw_on_error">True to throw on error.</param>
 /// <returns>The bytes in the output buffer.</returns>
 public NtResult <int> SendMessage(SafeBuffer input, SafeBuffer output, bool throw_on_error)
 {
     return(FilterManagerNativeMethods.FilterSendMessage(Handle, input ?? SafeHGlobalBuffer.Null,
                                                         input?.GetLength() ?? 0, output ?? SafeHGlobalBuffer.Null, output?.GetLength() ?? 0,
                                                         out int bytes_returned).CreateResult(throw_on_error, () => bytes_returned));
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Split an allocated address into a list of pages. This can be used to pass to
 /// ReadScatter or WriteGather file APIs.
 /// </summary>
 /// <param name="buffer">The allocated buffer to split. The address should be page aligned.</param>
 /// <remarks>The buffer will be split up based on its length. Note that the length will be rounded up.</remarks>
 /// <returns>The list of pages.</returns>
 public static IEnumerable <long> SplitAddressToPages(SafeBuffer buffer)
 {
     return(SplitAddressToPages(buffer.DangerousGetHandle().ToInt64(), buffer.GetLength()));
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Zero an entire buffer.
 /// </summary>
 /// <param name="buffer">The buffer to zero.</param>
 public static void ZeroBuffer(SafeBuffer buffer)
 {
     NtRtl.RtlZeroMemory(buffer.DangerousGetHandle(), new IntPtr(buffer.GetLength()));
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Method to set information for this object type.
 /// </summary>
 /// <param name="info_class">The information class.</param>
 /// <param name="buffer">The buffer to set data from.</param>
 /// <returns>The NT status code for the set.</returns>
 public override NtStatus SetInformation(ResourceManagerInformationClass info_class, SafeBuffer buffer)
 {
     return(NtSystemCalls.NtSetInformationResourceManager(Handle, info_class, buffer, buffer.GetLength()));
 }
Ejemplo n.º 18
0
 /// <summary>
 /// Fill an entire buffer with a specific byte value.
 /// </summary>
 /// <param name="buffer">The buffer to full.</param>
 /// <param name="fill">The fill value.</param>
 public static void FillBuffer(SafeBuffer buffer, byte fill)
 {
     NtRtl.RtlFillMemory(buffer.DangerousGetHandle(), new IntPtr(buffer.GetLength()), fill);
 }
 /// <summary>
 /// Method to query information for this object type.
 /// </summary>
 /// <param name="info_class">The information class.</param>
 /// <param name="buffer">The buffer to return data in.</param>
 /// <param name="return_length">Return length from the query.</param>
 /// <returns>The NT status code for the query.</returns>
 public override NtStatus QueryInformation(EventInformationClass info_class, SafeBuffer buffer, out int return_length)
 {
     return(NtSystemCalls.NtQueryEvent(Handle, info_class, buffer, buffer.GetLength(), out return_length));
 }
        /// <summary>
        /// Logon a user.
        /// </summary>
        /// <param name="type">The type of logon.</param>
        /// <param name="auth_package">The authentication package to use.</param>
        /// <param name="origin_name">The name of the origin.</param>
        /// <param name="source_context">The token source context.</param>
        /// <param name="buffer">The authentication credentials buffer.</param>
        /// <param name="local_groups">Additional local groups.</param>
        /// <param name="throw_on_error">True to throw on error.</param>
        /// <returns>The LSA logon result.</returns>
        public NtResult <LsaLogonResult> LsaLogonUser(SecurityLogonType type, string auth_package, string origin_name,
                                                      TokenSource source_context, SafeBuffer buffer, IEnumerable <UserGroup> local_groups, bool throw_on_error)
        {
            using (var list = new DisposableList())
            {
                var auth_pkg = _handle.LookupAuthPackage(auth_package, throw_on_error);
                if (!auth_pkg.IsSuccess)
                {
                    return(auth_pkg.Cast <LsaLogonResult>());
                }

                var groups = local_groups == null ? SafeTokenGroupsBuffer.Null
                    : list.AddResource(SafeTokenGroupsBuffer.Create(local_groups));

                QUOTA_LIMITS quota_limits = new QUOTA_LIMITS();
                return(SecurityNativeMethods.LsaLogonUser(_handle, new LsaString(origin_name),
                                                          type, auth_pkg.Result, buffer, buffer.GetLength(), groups,
                                                          source_context, out SafeLsaReturnBufferHandle profile,
                                                          out int cbProfile, out Luid logon_id, out SafeKernelObjectHandle token_handle,
                                                          quota_limits, out NtStatus subStatus).CreateResult(throw_on_error, () =>
                {
                    profile.InitializeLength(cbProfile);
                    return new LsaLogonResult(NtToken.FromHandle(token_handle), profile, logon_id, quota_limits);
                }));
            }
        }