Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@shelld0n
Created February 1, 2020 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shelld0n/be0ad4fc091b1fc0326d1cd424174172 to your computer and use it in GitHub Desktop.
Save shelld0n/be0ad4fc091b1fc0326d1cd424174172 to your computer and use it in GitHub Desktop.
adjusttokenpriv_block
// Enable SeDebugPrivilege Routine
string Privilege = "SeDebugPrivilege";
myAPI.LUID luid = new myAPI.LUID();
IntPtr hProcess = myAPI.GetCurrentProcess();
IntPtr hToken;
if (!myAPI.OpenProcessToken(hProcess, myAPI.TOKEN_QUERY | myAPI.TOKEN_ADJUST_PRIVILEGES, out hToken)) { Console.WriteLine("No tokens for current process"); Environment.Exit(2); };
if (!myAPI.LookupPrivilegeValue(null, Privilege, out luid)) { Console.WriteLine("No handle for privilege"); Environment.Exit(2); };
myAPI.LUID_AND_ATTRIBUTES luAttr = new myAPI.LUID_AND_ATTRIBUTES { Luid = luid, Attributes = myAPI.LUID_AND_ATTRIBUTES.SE_PRIVILEGE_ENABLED };
myAPI.TOKEN_PRIVILEGES tp = new myAPI.TOKEN_PRIVILEGES { PrivilegeCount = 1, Privileges = new myAPI.LUID_AND_ATTRIBUTES[1] };
tp.Privileges[0] = luAttr;
myAPI.TOKEN_PRIVILEGES oldState = new myAPI.TOKEN_PRIVILEGES(); // Our old state.
UInt32 trash;
if (!myAPI.AdjustTokenPrivileges(hToken, false, ref tp, (UInt32)Marshal.SizeOf(tp), ref oldState, out trash)) { Console.WriteLine("Can't Adjust access Token"); Environment.Exit(2); };
// End of SeDebugPrivileges Routine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment