public static bool ApiAdjustTokenPrivileges(ref IntPtr pProcessTokenHandle, string sPrivilizeValue) { Logger.Log(string.Format("SecurityDescriptorWrapper.ApiAdjustTokenPrivileges()"), Logger.SecurityDescriptorLogLevel); bool bIsSuccess = false; uint returnLength = 0, IreturnLength = 0; try { SecurityDescriptorApi.TOKEN_PRIVILEGES pPreviousTpStruct = new SecurityDescriptorApi.TOKEN_PRIVILEGES(); IntPtr pPreviousToken = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SecurityDescriptorApi.TOKEN_PRIVILEGES))); //Call the GetTokenInformation at the first time to get the length of the TOKEN_PRIVILEGES structure. bIsSuccess = SecurityDescriptorApi.GetTokenInformation( pProcessTokenHandle, SecurityDescriptorApi.TOKEN_INFORMATION_CLASS.TokenPrivileges, pPreviousTpStruct, 0, out returnLength); Logger.Log("Error at SecurityDescriptorApi.GetTokenInformation: " + Marshal.GetLastWin32Error(), Logger.SecurityDescriptorLogLevel); bIsSuccess = SecurityDescriptorApi.GetTokenInformation( pProcessTokenHandle, SecurityDescriptorApi.TOKEN_INFORMATION_CLASS.TokenPrivileges, pPreviousTpStruct, returnLength, out IreturnLength); Logger.Log("Error at SecurityDescriptorApi.GetTokenInformation: " + Marshal.GetLastWin32Error(), Logger.SecurityDescriptorLogLevel); IntPtr pLuid = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SecurityDescriptorApi.LwLUID))); SecurityDescriptorApi.LookupPrivilegeValueW("", sPrivilizeValue, out pLuid); uint privilige = SecurityDescriptorApi.SE_PRIVILEGE_ENABLED ;//| SecurityDescriptorApi.SE_SECURITY_NAME; pPreviousTpStruct.Attributes = (int)privilige; pPreviousTpStruct.PrivilegeCount = 1; pPreviousTpStruct.Luid = pLuid; bIsSuccess = SecurityDescriptorApi.AdjustTokenPrivileges( pProcessTokenHandle, false, pPreviousTpStruct, 0, IntPtr.Zero, 0); Logger.Log("Error at SecurityDescriptorApi.AdjustTokenPrivileges: " + Marshal.GetLastWin32Error(), Logger.SecurityDescriptorLogLevel); if (!bIsSuccess) { Logger.Log(string.Format("SecurityDescriptorWrapper.ApiAdjustTokenPrivileges:bIsSuccess()" + bIsSuccess), Logger.SecurityDescriptorLogLevel); Logger.Log("Error code: " + Marshal.GetLastWin32Error()); } } catch(Exception ex) { Logger.LogException(string.Format("SecurityDescriptorWrapper.ApiAdjustTokenPrivileges()"), ex); } finally { //if (pProcessTokenHandle != IntPtr.Zero) // SecurityDescriptorApi.CloseHandle(pProcessTokenHandle); } return bIsSuccess; }
public static bool ApiAdjustTokenPrivileges(ref IntPtr pProcessTokenHandle, string sPrivilizeValue) { Logger.Log(string.Format("SecurityDescriptorWrapper.ApiAdjustTokenPrivileges()"), Logger.SecurityDescriptorLogLevel); bool bIsSuccess = false; uint returnLength = 0, IreturnLength = 0; try { SecurityDescriptorApi.TOKEN_PRIVILEGES pPreviousTpStruct = new SecurityDescriptorApi.TOKEN_PRIVILEGES(); IntPtr pPreviousToken = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SecurityDescriptorApi.TOKEN_PRIVILEGES))); //Call the GetTokenInformation at the first time to get the length of the TOKEN_PRIVILEGES structure. bIsSuccess = SecurityDescriptorApi.GetTokenInformation( pProcessTokenHandle, SecurityDescriptorApi.TOKEN_INFORMATION_CLASS.TokenPrivileges, pPreviousTpStruct, 0, out returnLength); Logger.Log("Error at SecurityDescriptorApi.GetTokenInformation: " + Marshal.GetLastWin32Error(), Logger.SecurityDescriptorLogLevel); bIsSuccess = SecurityDescriptorApi.GetTokenInformation( pProcessTokenHandle, SecurityDescriptorApi.TOKEN_INFORMATION_CLASS.TokenPrivileges, pPreviousTpStruct, returnLength, out IreturnLength); Logger.Log("Error at SecurityDescriptorApi.GetTokenInformation: " + Marshal.GetLastWin32Error(), Logger.SecurityDescriptorLogLevel); IntPtr pLuid = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SecurityDescriptorApi.LwLUID))); SecurityDescriptorApi.LookupPrivilegeValueW("", sPrivilizeValue, out pLuid); uint privilige = SecurityDescriptorApi.SE_PRIVILEGE_ENABLED; //| SecurityDescriptorApi.SE_SECURITY_NAME; pPreviousTpStruct.Attributes = (int)privilige; pPreviousTpStruct.PrivilegeCount = 1; pPreviousTpStruct.Luid = pLuid; bIsSuccess = SecurityDescriptorApi.AdjustTokenPrivileges( pProcessTokenHandle, false, pPreviousTpStruct, 0, IntPtr.Zero, 0); Logger.Log("Error at SecurityDescriptorApi.AdjustTokenPrivileges: " + Marshal.GetLastWin32Error(), Logger.SecurityDescriptorLogLevel); if (!bIsSuccess) { Logger.Log(string.Format("SecurityDescriptorWrapper.ApiAdjustTokenPrivileges:bIsSuccess()" + bIsSuccess), Logger.SecurityDescriptorLogLevel); Logger.Log("Error code: " + Marshal.GetLastWin32Error()); } } catch (Exception ex) { Logger.LogException(string.Format("SecurityDescriptorWrapper.ApiAdjustTokenPrivileges()"), ex); } finally { //if (pProcessTokenHandle != IntPtr.Zero) // SecurityDescriptorApi.CloseHandle(pProcessTokenHandle); } return(bIsSuccess); }