Advertisement
captmicro

Untitled

Oct 27th, 2012
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.95 KB | None | 0 0
  1. void SetDebugPrivileges()
  2. {
  3.         TOKEN_PRIVILEGES Debug_Privileges;
  4.  
  5.         //STEP 1
  6.         if (!LookupPrivilegeValue (NULL, // Privieleges for the local system
  7.                 SE_DEBUG_NAME, // define the name of the privilege
  8.                 &Debug_Privileges.Privileges[0].Luid)) // will get the LUID value into this variable
  9.         {       //if function failed, cannot proceed to the next step
  10.                 return GetLastError(); //terminate the outer function
  11.         }
  12.  
  13.         //STEP 2
  14.         DWORD err = 0; // define error holder, used to store the error code in case of failure
  15.         HANDLE hToken = 0; //  instantiate a token handle
  16.         if (!OpenProcessToken (GetCurrentProcess (), // current process ID handle
  17.                 TOKEN_ADJUST_PRIVILEGES, //set the desired access
  18.                 &hToken)) // handle to the token will be held here
  19.         {       // if function failed, cannot proceed to the next step
  20.                 err = GetLastError();  
  21.                 if (hToken) // if handle is still valid
  22.                         CloseHandle (hToken); // destroy it
  23.                 return err; //terminate the outer function
  24.         }
  25.  
  26.         //STEP3
  27.         Debug_Privileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; // set to enable privilege
  28.         Debug_Privileges.PrivilegeCount = 1; // working with only one privilege
  29.  
  30.         if (!AdjustTokenPrivileges (hToken, // access token handle
  31.                 FALSE, // do not disable privileges
  32.                 &Debug_Privileges, // pointer to the token structure
  33.                 0,  // no need for a buffer
  34.                 NULL, // previous state not set
  35.                 NULL)) //  no need for a buffer
  36.         {
  37.                 err = GetLastError();
  38.                 if (hToken) // if handle is still valid
  39.                         CloseHandle (hToken); // destroy it
  40.                 return err; //terminate the outer function
  41.         }
  42.  
  43.         return err;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement