Exemplo n.º 1
0
 /// <summary>
 /// Duplicate an instance from a process
 /// </summary>
 /// <param name="process">The process (with DupHandle access)</param>
 /// <param name="handle">The handle value to duplicate</param>
 /// <param name="access">The access rights to duplicate with</param>
 /// <param name="options">The options for duplication.</param>
 /// <param name="throw_on_error">True to throw an exception on error.</param>
 /// <returns>The NT status code and object result.</returns>
 public static NtResult <O> DuplicateFrom(NtProcess process, IntPtr handle,
                                          A access, DuplicateObjectOptions options, bool throw_on_error)
 {
     return(NtObject.DuplicateHandle(process, new SafeKernelObjectHandle(handle, false),
                                     NtProcess.Current, ToGenericAccess(access), AttributeFlags.None,
                                     options, throw_on_error).Map(h => FromHandle(h)));
 }
Exemplo n.º 2
0
        private DuplicateObjectOptions GetOptions()
        {
            DuplicateObjectOptions options = DuplicateObjectOptions.None;

            if (!DesiredAccess.HasValue && !DesiredAccessMask.HasValue)
            {
                options |= DuplicateObjectOptions.SameAccess;
            }

            if (!ObjectAttributes.HasValue)
            {
                options |= DuplicateObjectOptions.SameAttributes;
            }

            if (CloseSource)
            {
                options |= DuplicateObjectOptions.CloseSource;
            }

            if (NoRightsUpgrade)
            {
                options |= DuplicateObjectOptions.NoRightsUpgrade;
            }

            return(options);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Duplicate an instance from a process to an other process
 /// </summary>
 /// <param name="src_process">The source process (with DupHandle access)</param>
 /// <param name="handle">The handle value to duplicate</param>
 /// <param name="dst_process">The destination process (with DupHandle access)</param>
 /// <param name="access">The access rights to duplicate with</param>
 /// <param name="options">The options for duplication.</param>
 /// <param name="throw_on_error">True to throw an exception on error.</param>
 /// <returns>The NT status code and object result.</returns>
 public static NtResult <IntPtr> DuplicateTo(NtProcess src_process, IntPtr handle, NtProcess dst_process,
                                             A access, DuplicateObjectOptions options, bool throw_on_error)
 {
     return(NtObject.DuplicateHandle(src_process, handle,
                                     dst_process, ToGenericAccess(access), AttributeFlags.None,
                                     options, throw_on_error));
 }
Exemplo n.º 4
0
 /// <summary>
 /// Duplicate an instance from current process to an other process
 /// </summary>
 /// <param name="process">The destination process (with DupHandle access)</param>
 /// <param name="handle">The handle value to duplicate</param>
 /// <param name="access">The access rights to duplicate with</param>
 /// <param name="options">The options for duplication.</param>
 /// <param name="throw_on_error">True to throw an exception on error.</param>
 /// <returns>The NT status code and object result.</returns>
 public static NtResult <IntPtr> DuplicateTo(NtProcess process, SafeKernelObjectHandle handle,
                                             A access, DuplicateObjectOptions options, bool throw_on_error)
 {
     return(DuplicateHandle(NtProcess.Current, handle.DangerousGetHandle(),
                            process, ToGenericAccess(access), AttributeFlags.None,
                            options, throw_on_error));
 }
Exemplo n.º 5
0
 /// <summary>
 /// Duplicate the internal handle to a new handle.
 /// </summary>
 /// <param name="src_handle">The source handle to duplicate</param>
 /// <param name="dest_process">The desination process for the handle</param>
 /// <param name="options">Duplicate handle options</param>
 /// <param name="access_rights">The access rights for the new handle</param>
 /// <returns>The duplicated handle.</returns>
 internal static SafeKernelObjectHandle DuplicateHandle(
     SafeKernelObjectHandle src_handle,
     NtProcess dest_process, AccessMask access_rights,
     DuplicateObjectOptions options)
 {
     return(DuplicateHandle(NtProcess.Current, src_handle, dest_process,
                            access_rights, AttributeFlags.None, options, true).Result);
 }
 public static extern NtStatus NtDuplicateObject(
     SafeHandle SourceProcessHandle,
     IntPtr SourceHandle,
     IntPtr TargetProcessHandle,
     IntPtr TargetHandle,
     AccessMask DesiredAccess,
     AttributeFlags HandleAttributes,
     DuplicateObjectOptions Options
     );
Exemplo n.º 7
0
 public static extern NtStatus NtDuplicateObject(
     SafeHandle SourceProcessHandle,
     SafeHandle SourceHandle,
     SafeHandle TargetProcessHandle,
     out SafeKernelObjectHandle TargetHandle,
     GenericAccessRights DesiredAccess,
     AttributeFlags HandleAttributes,
     DuplicateObjectOptions Options
     );
Exemplo n.º 8
0
 /// <summary>
 /// Duplicate a handle to a new handle, potentially in a different process.
 /// </summary>
 /// <param name="flags">Attribute flags for new handle</param>
 /// <param name="src_handle">The source handle to duplicate</param>
 /// <param name="src_process">The source process to duplicate from</param>
 /// <param name="dest_process">The desination process for the handle</param>
 /// <param name="options">Duplicate handle options</param>
 /// <param name="access_rights">The access rights for the new handle</param>
 /// <returns>The NT status code and object result.</returns>
 public static IntPtr DuplicateHandle(
     NtProcess src_process, IntPtr src_handle,
     NtProcess dest_process, AccessMask access_rights,
     AttributeFlags flags, DuplicateObjectOptions options)
 {
     return(DuplicateHandle(src_process, src_handle,
                            dest_process, access_rights, flags,
                            options, true).Result);
 }
Exemplo n.º 9
0
 /// <summary>
 /// Duplicate the internal handle to a new handle.
 /// </summary>
 /// <param name="flags">Attribute flags for new handle</param>
 /// <param name="src_handle">The source handle to duplicate</param>
 /// <param name="src_process">The source process to duplicate from</param>
 /// <param name="dest_process">The desination process for the handle</param>
 /// <param name="options">Duplicate handle options</param>
 /// <param name="access_rights">The access rights for the new handle</param>
 /// <param name="throw_on_error">True to throw an exception on error.</param>
 /// <returns>The NT status code and object result.</returns>
 internal static NtResult <SafeKernelObjectHandle> DuplicateHandle(
     NtProcess src_process, SafeKernelObjectHandle src_handle,
     NtProcess dest_process, AccessMask access_rights,
     AttributeFlags flags, DuplicateObjectOptions options,
     bool throw_on_error)
 {
     return(DuplicateHandle(src_process, src_handle.DangerousGetHandle(),
                            dest_process, access_rights, flags, options,
                            throw_on_error).Map(h => new SafeKernelObjectHandle(h, true)));
 }
Exemplo n.º 10
0
 /// <summary>
 /// Duplicate a handle to a new handle, potentially in a different process.
 /// </summary>
 /// <param name="flags">Attribute flags for new handle</param>
 /// <param name="src_handle">The source handle to duplicate</param>
 /// <param name="src_process">The source process to duplicate from</param>
 /// <param name="dest_process">The desination process for the handle</param>
 /// <param name="options">Duplicate handle options</param>
 /// <param name="access_rights">The access rights for the new handle</param>
 /// <param name="throw_on_error">True to throw an exception on error.</param>
 /// <returns>The NT status code and object result.</returns>
 public static NtResult <IntPtr> DuplicateHandle(
     NtProcess src_process, IntPtr src_handle,
     NtProcess dest_process, AccessMask access_rights,
     AttributeFlags flags, DuplicateObjectOptions options,
     bool throw_on_error)
 {
     return(NtSystemCalls.NtDuplicateObject(src_process.Handle, src_handle,
                                            dest_process.Handle, out IntPtr external_handle, access_rights, flags,
                                            options).CreateResult(throw_on_error, () => external_handle));
 }
        /// <summary>
        /// Duplicate the internal handle to a new handle.
        /// </summary>
        /// <param name="flags">Attribute flags for new handle</param>
        /// <param name="src_handle">The source handle to duplicate</param>
        /// <param name="src_process">The source process to duplicate from</param>
        /// <param name="dest_process">The desination process for the handle</param>
        /// <param name="options">Duplicate handle options</param>
        /// <param name="access_rights">The access rights for the new handle</param>
        /// <param name="throw_on_error">True to throw an exception on error.</param>
        /// <returns>The NT status code and object result.</returns>
        internal static NtResult <SafeKernelObjectHandle> DuplicateHandle(
            NtProcess src_process, SafeKernelObjectHandle src_handle,
            NtProcess dest_process, AccessMask access_rights,
            AttributeFlags flags, DuplicateObjectOptions options,
            bool throw_on_error)
        {
            SafeKernelObjectHandle new_handle;

            return(NtSystemCalls.NtDuplicateObject(src_process.Handle, src_handle,
                                                   dest_process.Handle, out new_handle, access_rights, flags,
                                                   options).CreateResult(throw_on_error, () => new_handle));
        }
Exemplo n.º 12
0
        /// <summary>
        /// Duplicate an instance from current process to an other process
        /// </summary>
        /// <param name="pid">The destination process ID</param>
        /// <param name="handle">The handle value to duplicate</param>
        /// <param name="access">The access rights to duplicate with</param>
        /// <param name="options">The options for duplication.</param>
        /// <param name="throw_on_error">True to throw an exception on error.</param>
        /// <returns>The NT status code and object result.</returns>
        public static NtResult <IntPtr> DuplicateTo(int pid, SafeKernelObjectHandle handle,
                                                    A access, DuplicateObjectOptions options, bool throw_on_error)
        {
            using (var process = NtProcess.Open(pid, ProcessAccessRights.DupHandle, throw_on_error))
            {
                if (!process.IsSuccess)
                {
                    return(new NtResult <IntPtr>(process.Status, IntPtr.Zero));
                }

                return(DuplicateTo(process.Result, handle, access, options, throw_on_error));
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// Duplicate an instance from a process
        /// </summary>
        /// <param name="pid">The process ID</param>
        /// <param name="handle">The handle value to duplicate</param>
        /// <param name="access">The access rights to duplicate with</param>
        /// <param name="options">The options for duplication.</param>
        /// <param name="throw_on_error">True to throw an exception on error.</param>
        /// <returns>The NT status code and object result.</returns>
        public static NtResult <O> DuplicateFrom(int pid, IntPtr handle,
                                                 A access, DuplicateObjectOptions options, bool throw_on_error)
        {
            using (var process = NtProcess.Open(pid, ProcessAccessRights.DupHandle, throw_on_error))
            {
                if (!process.IsSuccess)
                {
                    return(new NtResult <O>(process.Status, default(O)));
                }

                return(DuplicateFrom(process.Result, handle, access, options, throw_on_error));
            }
        }
Exemplo n.º 14
0
        /// <summary>
        /// Duplicate an instance from a process to an other process
        /// </summary>
        /// <param name="src_pid">The source process ID</param>
        /// <param name="handle">The handle value to duplicate</param>
        /// <param name="dst_pid">The destination process ID</param>
        /// <param name="access">The access rights to duplicate with</param>
        /// <param name="options">The options for duplication.</param>
        /// <param name="throw_on_error">True to throw an exception on error.</param>
        /// <returns>The NT status code and object result.</returns>
        public static NtResult <IntPtr> DuplicateTo(int src_pid, IntPtr handle, int dst_pid,
                                                    A access, DuplicateObjectOptions options, bool throw_on_error)
        {
            using (var src_process = NtProcess.Open(src_pid, ProcessAccessRights.DupHandle, throw_on_error))
            {
                if (!src_process.IsSuccess)
                {
                    return(new NtResult <IntPtr>(src_process.Status, IntPtr.Zero));
                }

                using (var dst_process = NtProcess.Open(dst_pid, ProcessAccessRights.DupHandle, throw_on_error))
                {
                    if (!dst_process.IsSuccess)
                    {
                        return(new NtResult <IntPtr>(dst_process.Status, IntPtr.Zero));
                    }

                    return(DuplicateTo(src_process.Result, handle, dst_process.Result, access, options, throw_on_error));
                }
            }
        }
Exemplo n.º 15
0
 /// <summary>
 /// Duplicate an instance from a process
 /// </summary>
 /// <param name="process">The process (with DupHandle access)</param>
 /// <param name="handle">The handle value to duplicate</param>
 /// <param name="access">The access rights to duplicate with</param>
 /// <param name="options">The options for duplication.</param>
 /// <param name="attributes">The attribute flags for the new object.</param>
 /// <returns>The NT status code and object result.</returns>
 public static O DuplicateFrom(NtProcess process, IntPtr handle,
                               A access, AttributeFlags attributes, DuplicateObjectOptions options)
 {
     return(DuplicateFrom(process, handle, access, attributes, options, true).Result);
 }
Exemplo n.º 16
0
 /// <summary>
 /// Duplicate object.
 /// </summary>
 /// <param name="access_rights">Access rights to duplicate with.</param>
 /// <param name="flags">Attribute flags.</param>
 /// <param name="options">Duplicate options</param>
 /// <param name="throw_on_error">True to throw an exception on error.</param>
 /// <returns>The duplicated object.</returns>
 public sealed override NtResult <NtObject> DuplicateObject(AccessMask access_rights, AttributeFlags flags, DuplicateObjectOptions options, bool throw_on_error)
 {
     return(Duplicate(access_rights.ToSpecificAccess <A>(), flags, options, throw_on_error).Cast <NtObject>());
 }
Exemplo n.º 17
0
 /// <summary>
 /// Duplicate object.
 /// </summary>
 /// <param name="access_rights">Access rights to duplicate with.</param>
 /// <param name="flags">Attribute flags.</param>
 /// <param name="options">Duplicate options</param>
 /// <param name="throw_on_error">True to throw an exception on error.</param>
 /// <returns>The duplicated object.</returns>
 public abstract NtResult <NtObject> DuplicateObject(AccessMask access_rights, AttributeFlags flags, DuplicateObjectOptions options, bool throw_on_error);
Exemplo n.º 18
0
 /// <summary>
 /// Duplicate an instance from current process to an other process
 /// </summary>
 /// <param name="process">The destination process (with DupHandle access)</param>
 /// <param name="access">The access rights to duplicate with</param>
 /// <param name="options">The options for duplication.</param>
 /// <param name="throw_on_error">True to throw an exception on error.</param>
 /// <returns>The NT status code and object result.</returns>
 public NtResult <IntPtr> DuplicateTo(NtProcess process,
                                      A access, DuplicateObjectOptions options, bool throw_on_error)
 {
     return(DuplicateTo(process, Handle,
                        access, options, throw_on_error));
 }
 static extern bool DuplicateHandle(IntPtr hSourceProcessHandle, IntPtr hSourceHandle, IntPtr hTargetProcessHandle, out IntPtr lpTargetHandle,
                                    uint dwDesiredAccess,
                                    [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle,
                                    DuplicateObjectOptions dwOptions);
Exemplo n.º 20
0
        /// <summary>
        /// Duplicate the internal handle to a new handle.
        /// </summary>
        /// <param name="source_process">The source process for the handle</param>
        /// <param name="dest_process">The desination process for the handle</param>
        /// <param name="handle">The handle in the source process to duplicate</param>
        /// <param name="options">Duplicate handle options</param>
        /// <param name="access_rights">The access rights for the new handle</param>
        /// <returns>The new duplicated handle.</returns>
        public static SafeKernelObjectHandle DuplicateHandle(NtProcess source_process, SafeHandle handle, NtProcess dest_process, GenericAccessRights access_rights, DuplicateObjectOptions options)
        {
            SafeKernelObjectHandle new_handle;

            NtSystemCalls.NtDuplicateObject(source_process.Handle, handle,
                                            dest_process.Handle, out new_handle,
                                            GenericAccessRights.None, AttributeFlags.None,
                                            DuplicateObjectOptions.SameAccess).ToNtException();

            return(new_handle);
        }
Exemplo n.º 21
0
        /// <summary>
        /// Duplicate the internal handle to a new handle.
        /// </summary>
        /// <param name="dest_process">The desination process for the handle</param>
        /// <param name="options">Duplicate handle options</param>
        /// <param name="access_rights">The access rights for the new handle</param>
        /// <returns>The new duplicated handle.</returns>
        public SafeKernelObjectHandle DuplicateHandle(NtProcess dest_process, uint access_rights, DuplicateObjectOptions options)
        {
            SafeKernelObjectHandle new_handle;

            NtSystemCalls.NtDuplicateObject(NtProcess.Current.Handle, Handle,
                                            dest_process.Handle, out new_handle, (GenericAccessRights)access_rights, AttributeFlags.None,
                                            options).ToNtException();

            return(new_handle);
        }
Exemplo n.º 22
0
 /// <summary>
 /// Duplicate object.
 /// </summary>
 /// <param name="access_rights">Access rights to duplicate with.</param>
 /// <param name="flags">Attribute flags.</param>
 /// <param name="options">Duplicate options</param>
 /// <param name="throw_on_error">True to throw an exception on error.</param>
 /// <returns>The duplicated object.</returns>
 public NtResult <O> Duplicate(A access_rights, AttributeFlags flags, DuplicateObjectOptions options, bool throw_on_error)
 {
     return(DuplicateHandle(NtProcess.Current, Handle, NtProcess.Current, ToGenericAccess(access_rights), flags, options, throw_on_error).Map(h => ShallowClone(h, true)));
 }
Exemplo n.º 23
0
 /// <summary>
 /// Duplicate object.
 /// </summary>
 /// <param name="access_rights">Access rights to duplicate with.</param>
 /// <param name="flags">Attribute flags.</param>
 /// <param name="options">Duplicate options</param>
 /// <returns>The duplicated object.</returns>
 public O Duplicate(A access_rights, AttributeFlags flags, DuplicateObjectOptions options)
 {
     return(Duplicate(access_rights, flags, options, true).Result);
 }
Exemplo n.º 24
0
 /// <summary>
 /// Duplicate object.
 /// </summary>
 /// <param name="access_rights">Access rights to duplicate with.</param>
 /// <param name="flags">Attribute flags.</param>
 /// <param name="options">Duplicate options</param>
 /// <returns>The duplicated object.</returns>
 public NtObject DuplicateObject(AccessMask access_rights, AttributeFlags flags, DuplicateObjectOptions options)
 {
     return(DuplicateObject(access_rights, flags, options, true).Result);
 }
 static extern bool DuplicateHandle(IntPtr hSourceProcessHandle, IntPtr hSourceHandle, IntPtr hTargetProcessHandle, out IntPtr lpTargetHandle,
     uint dwDesiredAccess,
     [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle,
     DuplicateObjectOptions dwOptions);