You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
3.9 KiB
C#
109 lines
3.9 KiB
C#
using System;
|
|
using System.ComponentModel;
|
|
using System.Runtime.InteropServices;
|
|
using System.Security.Principal;
|
|
|
|
namespace CleanFlashCommon {
|
|
public class WinAPI {
|
|
|
|
public static void AllowModifications() {
|
|
ModifyPrivilege(PrivilegeName.SeRestorePrivilege, true);
|
|
ModifyPrivilege(PrivilegeName.SeTakeOwnershipPrivilege, true);
|
|
}
|
|
|
|
public static bool ModifyPrivilege(PrivilegeName privilege, bool enable) {
|
|
LUID luid;
|
|
|
|
if (!LookupPrivilegeValue(null, privilege.ToString(), out luid)) {
|
|
throw new Win32Exception();
|
|
}
|
|
|
|
using (var identity = WindowsIdentity.GetCurrent(TokenAccessLevels.AdjustPrivileges | TokenAccessLevels.Query)) {
|
|
var newPriv = new TOKEN_PRIVILEGES();
|
|
newPriv.Privileges = new LUID_AND_ATTRIBUTES[1];
|
|
newPriv.PrivilegeCount = 1;
|
|
newPriv.Privileges[0].Luid = luid;
|
|
newPriv.Privileges[0].Attributes = enable ? SE_PRIVILEGE_ENABLED : 0;
|
|
|
|
var prevPriv = new TOKEN_PRIVILEGES();
|
|
prevPriv.Privileges = new LUID_AND_ATTRIBUTES[1];
|
|
prevPriv.PrivilegeCount = 1;
|
|
uint returnedBytes;
|
|
|
|
if (!AdjustTokenPrivileges(identity.Token, false, ref newPriv, (uint)Marshal.SizeOf(prevPriv), ref prevPriv, out returnedBytes)) {
|
|
throw new Win32Exception();
|
|
}
|
|
|
|
return prevPriv.PrivilegeCount == 0 ? enable /* didn't make a change */ : ((prevPriv.Privileges[0].Attributes & SE_PRIVILEGE_ENABLED) != 0);
|
|
}
|
|
}
|
|
|
|
const uint SE_PRIVILEGE_ENABLED = 2;
|
|
|
|
[DllImport("advapi32.dll", SetLastError = true)]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
static extern bool AdjustTokenPrivileges(IntPtr TokenHandle, [MarshalAs(UnmanagedType.Bool)] bool DisableAllPrivileges, ref TOKEN_PRIVILEGES NewState,
|
|
UInt32 BufferLengthInBytes, ref TOKEN_PRIVILEGES PreviousState, out UInt32 ReturnLengthInBytes);
|
|
|
|
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
static extern bool LookupPrivilegeValue(string lpSystemName, string lpName, out LUID lpLuid);
|
|
|
|
struct TOKEN_PRIVILEGES {
|
|
public UInt32 PrivilegeCount;
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1 /*ANYSIZE_ARRAY*/)]
|
|
public LUID_AND_ATTRIBUTES[] Privileges;
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
struct LUID_AND_ATTRIBUTES {
|
|
public LUID Luid;
|
|
public UInt32 Attributes;
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
struct LUID {
|
|
public uint LowPart;
|
|
public int HighPart;
|
|
}
|
|
}
|
|
|
|
public enum PrivilegeName {
|
|
SeAssignPrimaryTokenPrivilege,
|
|
SeAuditPrivilege,
|
|
SeBackupPrivilege,
|
|
SeChangeNotifyPrivilege,
|
|
SeCreateGlobalPrivilege,
|
|
SeCreatePagefilePrivilege,
|
|
SeCreatePermanentPrivilege,
|
|
SeCreateSymbolicLinkPrivilege,
|
|
SeCreateTokenPrivilege,
|
|
SeDebugPrivilege,
|
|
SeEnableDelegationPrivilege,
|
|
SeImpersonatePrivilege,
|
|
SeIncreaseBasePriorityPrivilege,
|
|
SeIncreaseQuotaPrivilege,
|
|
SeIncreaseWorkingSetPrivilege,
|
|
SeLoadDriverPrivilege,
|
|
SeLockMemoryPrivilege,
|
|
SeMachineAccountPrivilege,
|
|
SeManageVolumePrivilege,
|
|
SeProfileSingleProcessPrivilege,
|
|
SeRelabelPrivilege,
|
|
SeRemoteShutdownPrivilege,
|
|
SeRestorePrivilege,
|
|
SeSecurityPrivilege,
|
|
SeShutdownPrivilege,
|
|
SeSyncAgentPrivilege,
|
|
SeSystemEnvironmentPrivilege,
|
|
SeSystemProfilePrivilege,
|
|
SeSystemtimePrivilege,
|
|
SeTakeOwnershipPrivilege,
|
|
SeTcbPrivilege,
|
|
SeTimeZonePrivilege,
|
|
SeTrustedCredManAccessPrivilege,
|
|
SeUndockPrivilege,
|
|
SeUnsolicitedInputPrivilege,
|
|
}
|
|
}
|