AdjustTokenPrivileges Enable permissions

Source: Internet
Author: User

Original link Address: http://blog.csdn.net/xbgprogrammer/article/details/7276760

We have a lot of operations that need to use the OpenProcess function, and in order for the program to have permission to use this function, we often take advantage of adjusttokenprivileges to elevate the privileges (not to mention the elevation, but to enable the disabled permissions in the access token)

BOOL Setprivilege (HANDLE htoken,//access token handleLPCTSTR Lpszprivilege,//name of privilege to Enable/disableBOOL Benableprivilege//To enable or disable privilege) {token_privileges tp;    LUID LUID; if( !Lookupprivilegevalue (NULL,//Lookup privilege on Local systemLpszprivilege,//privilege to Lookup&LUID))//receives LUID of privilege{printf ("lookupprivilegevalue Error:%u\n", GetLastError ()); returnFALSE; } TP. Privilegecount=1; Tp. privileges[0]. Luid =Luid; if(benableprivilege) TP. privileges[0]. Attributes =se_privilege_enabled; ElseTP. privileges[0]. Attributes =0; //Enable the privilege or disable all privileges.    if( !adjusttokenprivileges (Htoken, FALSE,&TP,sizeof(Token_privileges), (ptoken_privileges) null, (Pdword) null)) {printf ("adjusttokenprivileges Error:%u\n", GetLastError ()); returnFALSE; }     if(GetLastError () = =error_not_all_assigned) {printf ("The token does not has the specified privilege. \ n"); returnFALSE; }     returnTRUE;}voidMain () {HANDLE htoken; BOOL BRet= OpenProcessToken (getcurrentprocess (),token_all_access,&htoken); Setprivilege (htoken,se_debug_name,true);}

This code is no problem on XP, but if the program starts with a standard user on Windows 7 or Vista, AdjustTokenPrivileges will fail to start with the admin province.

This is because on Windows 7, there are few standard user permissions, no debug permissions, and, more than the ability to enable debug permissions, users can enable the console in both administrator and standard user mode, enter the command whoami/all

To see the different permissions under both permissions

PS: Even if you elevate debug permissions, it does not mean that calling OpenProcess to other processes will succeed (for example, system and AUDIODG processes under Win7 systems)

About the token_privileges structure

struct _token_privileges {  DWORD privilegecount;   *ptoken_privileges;

AdjustTokenPrivileges Enable permissions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.