using System;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
try
{
EnablePrivilege("SeSecurityPrivilege");
EnablePrivilege("SeTakeOwnershipPrivilege");
Console.WriteLine("All privileges enabled successfully.");
}
catch (Exception ex)
{
Console.WriteLine("Failed to enable privileges: " + ex.ToString());
}
}
private static void EnablePrivilege(string privilegeName)
{
using (WindowsIdentity identity = WindowsIdentity.GetCurrent())
{
IntPtr token;
if (!OpenProcessToken(Process.GetCurrentProcess().Handle, TokenAccessLevels.AdjustPrivileges | TokenAccessLevels.Query, out token))
{
throw new Exception("Failed to obtain process token.");
}
try
{
LUID luid;
if (!LookupPrivilegeValue(null, privilegeName, out luid))
{
throw new Exception("Failed to obtain privilege ID for " + privilegeName + ".");
}
TOKEN_PRIVILEGES tokenPrivs = new TOKEN_PRIVILEGES();
tokenPrivs.PrivilegeCount = 1;
tokenPrivs.Privileges = new LUID_AND_ATTRIBUTES[1];
tokenPrivs.Privileges[0].Luid = luid;
tokenPrivs.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (!AdjustTokenPrivileges(token, false, ref tokenPrivs, 0, IntPtr.Zero, IntPtr.Zero))
{
throw new Exception("Failed to enable privilege: " + privilegeName);
}
}
finally {
CloseHandle(token);
}
}
}
[StructLayout(LayoutKind.Sequential)]
public struct LUID {
public uint LowPart;
public int HighPart;
}
[StructLayout(LayoutKind.Sequential)]
public struct LUID_AND_ATTRIBUTES {
public LUID Luid;
public uint Attributes;
}
[StructLayout(LayoutKind.Sequential)]
public struct TOKEN_PRIVILEGES {
public uint PrivilegeCount;
public LUID_AND_ATTRIBUTES[] Privileges;
}
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool CloseHandle(IntPtr handle);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool OpenProcessToken(IntPtr processHandle, TokenAccessLevels desiredAccess, out IntPtr tokenHandle);
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern bool LookupPrivilegeValue(string systemName, string name, out LUID luid);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool AdjustTokenPrivileges(IntPtr tokenHandle, bool disableAllPrivileges, ref TOKEN_PRIVILEGES newState, uint bufferLength, IntPtr previousState, IntPtr returnLength);
private const uint SE_PRIVILEGE_ENABLED = 0x00000002;
[Flags]
private enum TokenAccessLevels : uint
{
AssignPrimary = 0x00000001,
Duplicate = 0x00000002,
Impersonate = 0x00000004,
Query = 0x00000008,
QuerySource = 0x00000010,
AdjustPrivileges = 0x00000020,
AdjustGroups = 0x00000040,
AdjustDefault = 0x00000080,
AdjustSessionId = 0x00000100,
AllAccess = (AssignPrimary |
Duplicate |
Impersonate |
Query |
QuerySource |
AdjustPrivileges |
AdjustGroups |
AdjustDefault |
AdjustSessionId),
Read = (Query | QuerySource),
Write = (AdjustPrivileges |
AdjustGroups |
AdjustDefault),
}
}