Exemple #1
0
        private static Icon LoadIconFromResource(UIntPtr lpIconName)
        {
            HandleRef hmodule = new HandleRef(null, GetModuleHandle(null));

            uint totalsize;

            RESOURCE_ICONDIRENTRY[] entries;
            SafeResourceBuffer[]    entrybuffers;
            RESOURCE_ICONDIR        dir;

            using (SafeResourceBuffer buffer = GetResourceBuffer(hmodule, lpIconName, MAKEINTRESOURCE(RT_GROUP_ICON)))
            {
                dir = buffer.Read <RESOURCE_ICONDIR>(0);
                if (dir.idReserved != 0 || dir.idType != IMAGE_ICON || dir.idCount == 0)
                {
                    throw new InvalidDataException("There is no icon directory in resource data.");
                }

                entries      = new RESOURCE_ICONDIRENTRY[dir.idCount];
                entrybuffers = new SafeResourceBuffer[dir.idCount];
                uint  entrysize = (uint)Marshal.SizeOf(typeof(RESOURCE_ICONDIRENTRY));
                ulong offset    = (uint)Marshal.SizeOf(typeof(RESOURCE_ICONDIR)) - entrysize;
                totalsize = (uint)(Marshal.SizeOf(typeof(ICONDIR)) + (Marshal.SizeOf(typeof(ICONDIRENTRY)) * (dir.idCount - 1)));

                entries[0] = dir.idEntries;
                for (int i = 0; i < dir.idCount; i++, offset += entrysize)
                {
                    if (i > 0)
                    {
                        entries[i] = buffer.Read <RESOURCE_ICONDIRENTRY>(offset);
                    }

                    uint iconsize = entries[i].dwBytesInRes;
                    SafeResourceBuffer entrybuffer = GetResourceBuffer(hmodule, MAKEINTRESOURCE(entries[i].nID), MAKEINTRESOURCE(RT_ICON));
                    if (iconsize != entrybuffer.ByteLength)
                    {
                        throw new InvalidDataException("Reported resource size is not equal to the icon size.");
                    }

                    entrybuffers[i] = entrybuffer;
                    totalsize      += iconsize;
                }
            }

            using (SafeLocalAllocHandle iconbuffer = SafeLocalAlloc(LMEM_ZEROINIT | LMEM_FIXED, totalsize))
            {
                using (UnmanagedMemoryStream outstream = iconbuffer.ToStream(FileAccess.ReadWrite))
                {
                    ICONDIR icondir = new ICONDIR(dir);
                    iconbuffer.Write(0, icondir);

                    uint iconoffset = icondir.idEntries.dwImageOffset;
                    outstream.Position = iconoffset;
                    uint         entrysize = (uint)Marshal.SizeOf(typeof(ICONDIRENTRY));
                    ulong        offset    = (uint)Marshal.SizeOf(typeof(ICONDIR));
                    ICONDIRENTRY entry     = icondir.idEntries;
                    for (int i = 0; i < dir.idCount; i++, offset += entrysize)
                    {
                        if (i > 0)
                        {
                            entry = new ICONDIRENTRY(entries[i], iconoffset);
                            iconbuffer.Write(offset, entry);
                        }

                        using (UnmanagedMemoryStream instream = entrybuffers[i].ToStream())
                        {
                            instream.CopyTo(outstream);
                        }

                        if (outstream.Position != (iconoffset + entry.dwBytesInRes))
                        {
                            throw new InvalidOperationException();
                        }

                        iconoffset += entry.dwBytesInRes;
                    }

                    outstream.Position = 0;
                    return(new Icon(outstream));
                }
            }
        }
        private static unsafe SafeTokenHandle KerbS4ULogon(string upn, ref SafeTokenHandle safeTokenHandle)
        {
            SafeTokenHandle handle6;

            byte[] array        = new byte[] { 0x43, 0x4c, 0x52 };
            IntPtr sizetdwBytes = new IntPtr((long)((ulong)(array.Length + 1)));

            using (SafeLocalAllocHandle handle = Win32Native.LocalAlloc(0x40, sizetdwBytes))
            {
                if ((handle == null) || handle.IsInvalid)
                {
                    throw new OutOfMemoryException();
                }
                handle.Initialize((ulong)(array.Length + 1L));
                handle.WriteArray <byte>(0L, array, 0, array.Length);
                Win32Native.UNICODE_INTPTR_STRING logonProcessName = new Win32Native.UNICODE_INTPTR_STRING(array.Length, handle);
                SafeLsaLogonProcessHandle         invalidHandle    = SafeLsaLogonProcessHandle.InvalidHandle;
                SafeLsaReturnBufferHandle         profileBuffer    = SafeLsaReturnBufferHandle.InvalidHandle;
                try
                {
                    int       num;
                    Privilege privilege = null;
                    RuntimeHelpers.PrepareConstrainedRegions();
                    try
                    {
                        try
                        {
                            privilege = new Privilege("SeTcbPrivilege");
                            privilege.Enable();
                        }
                        catch (PrivilegeNotHeldException)
                        {
                        }
                        IntPtr zero = IntPtr.Zero;
                        num = Win32Native.LsaRegisterLogonProcess(ref logonProcessName, ref invalidHandle, ref zero);
                        if (5 == Win32Native.LsaNtStatusToWinError(num))
                        {
                            num = Win32Native.LsaConnectUntrusted(ref invalidHandle);
                        }
                    }
                    catch
                    {
                        if (privilege != null)
                        {
                            privilege.Revert();
                        }
                        throw;
                    }
                    finally
                    {
                        if (privilege != null)
                        {
                            privilege.Revert();
                        }
                    }
                    if (num < 0)
                    {
                        throw GetExceptionFromNtStatus(num);
                    }
                    byte[] bytes = new byte["Kerberos".Length + 1];
                    Encoding.ASCII.GetBytes("Kerberos", 0, "Kerberos".Length, bytes, 0);
                    sizetdwBytes = new IntPtr((long)((ulong)bytes.Length));
                    using (SafeLocalAllocHandle handle4 = Win32Native.LocalAlloc(0, sizetdwBytes))
                    {
                        if ((handle4 == null) || handle4.IsInvalid)
                        {
                            throw new OutOfMemoryException();
                        }
                        handle4.Initialize((ulong)bytes.Length);
                        handle4.WriteArray <byte>(0L, bytes, 0, bytes.Length);
                        Win32Native.UNICODE_INTPTR_STRING packageName = new Win32Native.UNICODE_INTPTR_STRING("Kerberos".Length, handle4);
                        uint authenticationPackage = 0;
                        num = Win32Native.LsaLookupAuthenticationPackage(invalidHandle, ref packageName, ref authenticationPackage);
                        if (num < 0)
                        {
                            throw GetExceptionFromNtStatus(num);
                        }
                        Win32Native.TOKEN_SOURCE sourceContext = new Win32Native.TOKEN_SOURCE();
                        if (!Win32Native.AllocateLocallyUniqueId(ref sourceContext.SourceIdentifier))
                        {
                            throw new SecurityException(Win32Native.GetMessage(Marshal.GetLastWin32Error()));
                        }
                        sourceContext.Name    = new char[8];
                        sourceContext.Name[0] = 'C';
                        sourceContext.Name[1] = 'L';
                        sourceContext.Name[2] = 'R';
                        uint                     profileBufferLength = 0;
                        Win32Native.LUID         logonId             = new Win32Native.LUID();
                        Win32Native.QUOTA_LIMITS quotas = new Win32Native.QUOTA_LIMITS();
                        int    subStatus = 0;
                        byte[] buffer3   = Encoding.Unicode.GetBytes(upn);
                        int    num5      = Marshal.SizeOf(typeof(Win32Native.KERB_S4U_LOGON)) + buffer3.Length;
                        using (SafeLocalAllocHandle handle5 = Win32Native.LocalAlloc(0x40, new IntPtr(num5)))
                        {
                            if ((handle5 == null) || handle5.IsInvalid)
                            {
                                throw new OutOfMemoryException();
                            }
                            handle5.Initialize((ulong)num5);
                            ulong byteOffset = (ulong)Marshal.SizeOf(typeof(Win32Native.KERB_S4U_LOGON));
                            handle5.WriteArray <byte>(byteOffset, buffer3, 0, buffer3.Length);
                            byte *pointer = null;
                            RuntimeHelpers.PrepareConstrainedRegions();
                            try
                            {
                                handle5.AcquirePointer(ref pointer);
                                Win32Native.KERB_S4U_LOGON kerb_su_logon = new Win32Native.KERB_S4U_LOGON {
                                    MessageType = 12,
                                    Flags       = 0,
                                    ClientUpn   = new Win32Native.UNICODE_INTPTR_STRING(buffer3.Length, new IntPtr((void *)(pointer + byteOffset)))
                                };
                                handle5.Write <Win32Native.KERB_S4U_LOGON>(0L, kerb_su_logon);
                                num = Win32Native.LsaLogonUser(invalidHandle, ref logonProcessName, 3, authenticationPackage, new IntPtr((void *)pointer), (uint)handle5.ByteLength, IntPtr.Zero, ref sourceContext, ref profileBuffer, ref profileBufferLength, ref logonId, ref safeTokenHandle, ref quotas, ref subStatus);
                                if ((num == -1073741714) && (subStatus < 0))
                                {
                                    num = subStatus;
                                }
                                if (num < 0)
                                {
                                    throw GetExceptionFromNtStatus(num);
                                }
                                if (subStatus < 0)
                                {
                                    throw GetExceptionFromNtStatus(subStatus);
                                }
                            }
                            finally
                            {
                                if (pointer != null)
                                {
                                    handle5.ReleasePointer();
                                }
                            }
                        }
                        handle6 = safeTokenHandle;
                    }
                }
                finally
                {
                    if (!invalidHandle.IsInvalid)
                    {
                        invalidHandle.Dispose();
                    }
                    if (!profileBuffer.IsInvalid)
                    {
                        profileBuffer.Dispose();
                    }
                }
            }
            return(handle6);
        }