Annotation of cleanflash/CleanFlashCommon/WinAPI.cs, revision 1.1

1.1     ! root        1: using System;
        !             2: using System.ComponentModel;
        !             3: using System.Runtime.InteropServices;
        !             4: using System.Security.Principal;
        !             5: 
        !             6: namespace CleanFlashCommon {
        !             7:     public class WinAPI {
        !             8: 
        !             9:         public static void AllowModifications() {
        !            10:             ModifyPrivilege(PrivilegeName.SeRestorePrivilege, true);
        !            11:             ModifyPrivilege(PrivilegeName.SeTakeOwnershipPrivilege, true);
        !            12:         } 
        !            13: 
        !            14:         public static bool ModifyPrivilege(PrivilegeName privilege, bool enable) {
        !            15:             LUID luid;
        !            16: 
        !            17:             if (!LookupPrivilegeValue(null, privilege.ToString(), out luid)) {
        !            18:                 throw new Win32Exception();
        !            19:             }
        !            20: 
        !            21:             using (var identity = WindowsIdentity.GetCurrent(TokenAccessLevels.AdjustPrivileges | TokenAccessLevels.Query)) {
        !            22:                 var newPriv = new TOKEN_PRIVILEGES();
        !            23:                 newPriv.Privileges = new LUID_AND_ATTRIBUTES[1];
        !            24:                 newPriv.PrivilegeCount = 1;
        !            25:                 newPriv.Privileges[0].Luid = luid;
        !            26:                 newPriv.Privileges[0].Attributes = enable ? SE_PRIVILEGE_ENABLED : 0;
        !            27: 
        !            28:                 var prevPriv = new TOKEN_PRIVILEGES();
        !            29:                 prevPriv.Privileges = new LUID_AND_ATTRIBUTES[1];
        !            30:                 prevPriv.PrivilegeCount = 1;
        !            31:                 uint returnedBytes;
        !            32: 
        !            33:                 if (!AdjustTokenPrivileges(identity.Token, false, ref newPriv, (uint)Marshal.SizeOf(prevPriv), ref prevPriv, out returnedBytes)) {
        !            34:                     throw new Win32Exception();
        !            35:                 }
        !            36: 
        !            37:                 return prevPriv.PrivilegeCount == 0 ? enable /* didn't make a change */ : ((prevPriv.Privileges[0].Attributes & SE_PRIVILEGE_ENABLED) != 0);
        !            38:             }
        !            39:         }
        !            40: 
        !            41:         const uint SE_PRIVILEGE_ENABLED = 2;
        !            42: 
        !            43:         [DllImport("advapi32.dll", SetLastError = true)]
        !            44:         [return: MarshalAs(UnmanagedType.Bool)]
        !            45:         static extern bool AdjustTokenPrivileges(IntPtr TokenHandle, [MarshalAs(UnmanagedType.Bool)] bool DisableAllPrivileges, ref TOKEN_PRIVILEGES NewState,
        !            46:            UInt32 BufferLengthInBytes, ref TOKEN_PRIVILEGES PreviousState, out UInt32 ReturnLengthInBytes);
        !            47: 
        !            48:         [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        !            49:         [return: MarshalAs(UnmanagedType.Bool)]
        !            50:         static extern bool LookupPrivilegeValue(string lpSystemName, string lpName, out LUID lpLuid);
        !            51: 
        !            52:         struct TOKEN_PRIVILEGES {
        !            53:             public UInt32 PrivilegeCount;
        !            54:             [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1 /*ANYSIZE_ARRAY*/)]
        !            55:             public LUID_AND_ATTRIBUTES[] Privileges;
        !            56:         }
        !            57: 
        !            58:         [StructLayout(LayoutKind.Sequential)]
        !            59:         struct LUID_AND_ATTRIBUTES {
        !            60:             public LUID Luid;
        !            61:             public UInt32 Attributes;
        !            62:         }
        !            63: 
        !            64:         [StructLayout(LayoutKind.Sequential)]
        !            65:         struct LUID {
        !            66:             public uint LowPart;
        !            67:             public int HighPart;
        !            68:         }
        !            69:     }
        !            70: 
        !            71:     public enum PrivilegeName {
        !            72:         SeAssignPrimaryTokenPrivilege,
        !            73:         SeAuditPrivilege,
        !            74:         SeBackupPrivilege,
        !            75:         SeChangeNotifyPrivilege,
        !            76:         SeCreateGlobalPrivilege,
        !            77:         SeCreatePagefilePrivilege,
        !            78:         SeCreatePermanentPrivilege,
        !            79:         SeCreateSymbolicLinkPrivilege,
        !            80:         SeCreateTokenPrivilege,
        !            81:         SeDebugPrivilege,
        !            82:         SeEnableDelegationPrivilege,
        !            83:         SeImpersonatePrivilege,
        !            84:         SeIncreaseBasePriorityPrivilege,
        !            85:         SeIncreaseQuotaPrivilege,
        !            86:         SeIncreaseWorkingSetPrivilege,
        !            87:         SeLoadDriverPrivilege,
        !            88:         SeLockMemoryPrivilege,
        !            89:         SeMachineAccountPrivilege,
        !            90:         SeManageVolumePrivilege,
        !            91:         SeProfileSingleProcessPrivilege,
        !            92:         SeRelabelPrivilege,
        !            93:         SeRemoteShutdownPrivilege,
        !            94:         SeRestorePrivilege,
        !            95:         SeSecurityPrivilege,
        !            96:         SeShutdownPrivilege,
        !            97:         SeSyncAgentPrivilege,
        !            98:         SeSystemEnvironmentPrivilege,
        !            99:         SeSystemProfilePrivilege,
        !           100:         SeSystemtimePrivilege,
        !           101:         SeTakeOwnershipPrivilege,
        !           102:         SeTcbPrivilege,
        !           103:         SeTimeZonePrivilege,
        !           104:         SeTrustedCredManAccessPrivilege,
        !           105:         SeUndockPrivilege,
        !           106:         SeUnsolicitedInputPrivilege,
        !           107:     }
        !           108: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.