/// <summary>
 /// Free a buffer from the heap.
 /// </summary>
 /// <param name="flags">Heap flags.</param>
 /// <param name="address">Address of the allocation.</param>
 public void Free(HeapAllocFlags flags, long address)
 {
     if (!NtRtl.RtlFreeHeap(_heap_handle, flags, new IntPtr(address)))
     {
         throw new NtException(NtObjectUtils.MapDosErrorToStatus());
     }
 }
Пример #2
0
 /// <summary>
 /// Set the Window Station for the Process.
 /// </summary>
 /// <param name="throw_on_error">True to throw on error.</param>
 /// <returns>The NT status.</returns>
 public NtStatus SetProcess(bool throw_on_error = true)
 {
     if (!NtSystemCalls.NtUserSetProcessWindowStation(Handle))
     {
         return(NtObjectUtils.MapDosErrorToStatus().ToNtException(throw_on_error));
     }
     return(NtStatus.STATUS_SUCCESS);
 }
 /// <summary>
 /// Close the Window Stations. This is different from normal Close as it destroys the Window Station.
 /// </summary>
 /// <param name="throw_on_error">True to throw on error.</param>
 /// <returns>The NT status.</returns>
 public NtStatus CloseWindowStation(bool throw_on_error = true)
 {
     if (!NtSystemCalls.NtUserCloseWindowStation(Handle))
     {
         return(NtObjectUtils.MapDosErrorToStatus());
     }
     return(NtStatus.STATUS_SUCCESS);
 }
 /// <summary>
 /// Free a buffer from the heap.
 /// </summary>
 /// <param name="flags">Heap flags.</param>
 /// <param name="address">Address of the allocation.</param>
 /// <param name="throw_on_error">True to throw on error.</param>
 public NtStatus Free(HeapAllocFlags flags, long address, bool throw_on_error)
 {
     if (!NtRtl.RtlFreeHeap(_heap_handle, flags, new IntPtr(address)))
     {
         return(NtObjectUtils.MapDosErrorToStatus().ToNtException(throw_on_error));
     }
     return(NtStatus.STATUS_SUCCESS);
 }
Пример #5
0
 /// <summary>
 /// Close the Desktop. This is different from normal Close as it destroys the Desktop.
 /// </summary>
 /// <param name="throw_on_error">True to throw on error.</param>
 /// <returns>The NT status.</returns>
 public NtStatus CloseDesktop(bool throw_on_error = true)
 {
     if (!NtSystemCalls.NtUserCloseDesktop(Handle))
     {
         return(NtObjectUtils.MapDosErrorToStatus().ToNtException(throw_on_error));
     }
     Handle.SetHandleAsInvalid();
     return(NtStatus.STATUS_SUCCESS);
 }
        /// <summary>
        /// Allocate a buffer from the heap.
        /// </summary>
        /// <param name="flags">Heap flags.</param>
        /// <param name="size">Size of the allocation.</param>
        /// <returns>The allocated memory address.</returns>
        public long Allocate(HeapAllocFlags flags, long size)
        {
            long address = NtRtl.RtlAllocateHeap(_heap_handle, flags, new IntPtr(size)).ToInt64();

            if (address == 0)
            {
                throw new NtException(NtObjectUtils.MapDosErrorToStatus());
            }
            return(address);
        }
        /// <summary>
        /// Allocate a buffer from the heap.
        /// </summary>
        /// <param name="flags">Heap flags.</param>
        /// <param name="size">Size of the allocation.</param>
        /// <param name="throw_on_error">True to throw on error.</param>
        /// <returns>The allocated memory address.</returns>
        public NtResult <long> Allocate(HeapAllocFlags flags, long size, bool throw_on_error)
        {
            long address = NtRtl.RtlAllocateHeap(_heap_handle, flags, new IntPtr(size)).ToInt64();

            if (address == 0)
            {
                return(NtObjectUtils.MapDosErrorToStatus().CreateResultFromError <long>(throw_on_error));
            }
            return(address.CreateResult());
        }
Пример #8
0
 /// <summary>
 /// Convert an SDDL SID string to a Sid
 /// </summary>
 /// <param name="sddl">The SDDL SID string</param>
 /// <param name="throw_on_error">True to throw on error.</param>
 /// <returns>The converted Sid</returns>
 /// <exception cref="NtException">Thrown if cannot convert from a SDDL string.</exception>
 public static NtResult <Sid> SidFromSddl(string sddl, bool throw_on_error)
 {
     if (!Win32NativeMethods.ConvertStringSidToSid(sddl, out SafeLocalAllocHandle handle))
     {
         return(NtObjectUtils.MapDosErrorToStatus().CreateResultFromError <Sid>(throw_on_error));
     }
     using (handle)
     {
         return(new Sid(handle.DangerousGetHandle()).CreateResult());
     }
 }
Пример #9
0
 /// <summary>
 /// Convert an SDDL SID string to a Sid
 /// </summary>
 /// <param name="sddl">The SDDL SID string</param>
 /// <returns>The converted Sid</returns>
 /// <exception cref="NtException">Thrown if cannot convert from a SDDL string.</exception>
 public static Sid SidFromSddl(string sddl)
 {
     if (!Win32NativeMethods.ConvertStringSidToSid(sddl, out SafeLocalAllocHandle handle))
     {
         throw new NtException(NtObjectUtils.MapDosErrorToStatus());
     }
     using (handle)
     {
         return(new Sid(handle.DangerousGetHandle()));
     }
 }
Пример #10
0
        /// <summary>
        /// Convert a security descriptor to SDDL string
        /// </summary>
        /// <param name="sd">The security descriptor</param>
        /// <param name="security_information">Indicates what parts of the security descriptor to include</param>
        /// <returns>The SDDL string</returns>
        /// <exception cref="NtException">Thrown if cannot convert to a SDDL string.</exception>
        public static string SecurityDescriptorToSddl(byte[] sd, SecurityInformation security_information)
        {
            if (!Win32NativeMethods.ConvertSecurityDescriptorToStringSecurityDescriptor(sd,
                                                                                        1, security_information, out SafeLocalAllocHandle handle, out int return_length))
            {
                throw new NtException(NtObjectUtils.MapDosErrorToStatus());
            }

            using (handle)
            {
                return(Marshal.PtrToStringUni(handle.DangerousGetHandle()));
            }
        }
Пример #11
0
        /// <summary>
        /// Convert an SDDL string to a binary security descriptor
        /// </summary>
        /// <param name="sddl">The SDDL string</param>
        /// <param name="throw_on_error">True to throw on error.</param>
        /// <returns>The binary security descriptor</returns>
        /// <exception cref="NtException">Thrown if cannot convert from a SDDL string.</exception>
        public static NtResult <byte[]> SddlToSecurityDescriptor(string sddl, bool throw_on_error)
        {
            if (!Win32NativeMethods.ConvertStringSecurityDescriptorToSecurityDescriptor(sddl, 1,
                                                                                        out SafeLocalAllocHandle handle, out int return_length))
            {
                return(NtObjectUtils.MapDosErrorToStatus().CreateResultFromError <byte[]>(throw_on_error));
            }

            using (handle)
            {
                byte[] ret = new byte[return_length];
                Marshal.Copy(handle.DangerousGetHandle(), ret, 0, return_length);
                return(ret.CreateResult());
            }
        }
Пример #12
0
        /// <summary>
        /// Convert an SDDL string to a binary security descriptor
        /// </summary>
        /// <param name="sddl">The SDDL string</param>
        /// <returns>The binary security descriptor</returns>
        /// <exception cref="NtException">Thrown if cannot convert from a SDDL string.</exception>
        public static byte[] SddlToSecurityDescriptor(string sddl)
        {
            if (!Win32NativeMethods.ConvertStringSecurityDescriptorToSecurityDescriptor(sddl, 1,
                                                                                        out SafeLocalAllocHandle handle, out int return_length))
            {
                throw new NtException(NtObjectUtils.MapDosErrorToStatus());
            }

            using (handle)
            {
                byte[] ret = new byte[return_length];
                Marshal.Copy(handle.DangerousGetHandle(), ret, 0, return_length);
                return(ret);
            }
        }