Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropping privileges in C++ on Windows

Is it possible for a C++ application running on Windows to drop privileges at runtime?

For instance, if a user starts my application as Administrator, but there's no reason to run my application as administrator, can I in some way give up the Administrator-privileges?

In short, I would like to write code in the main() function which drops privileges I don't need (for instance, Write access on the Windows directory).

like image 516
Nitramk Avatar asked Oct 07 '09 17:10

Nitramk


People also ask

What is drop privilege?

The first of two functions, spc_drop_privileges( ) drops any extra group or user privileges either permanently or temporarily, depending on the value of its only argument. If a nonzero value is passed, privileges will be dropped permanently; otherwise, the privilege drop is temporary.

What are Windows privileges?

A privilege is the right of an account, such as a user or group account, to perform various system-related operations on the local computer, such as shutting down the system, loading device drivers, or changing the system time.

How do I elevate my privileges in Windows 10?

How Do I Get Full Administrator Privileges On Windows 10? Search settings, then open the Settings App. Then, click Accounts -> Family & other users. Finally, click your user name and click Change account type – then, on the Account type drop-down, select Administrators and click OK.


1 Answers

Yes, you can use AdjustTokenPrivileges to remove unneeded and dangerous privileges from your token. You can either disable if not immediately needed (the privilege can be enabled later) or remove a privilege from your token altogether.

You can also create a restricted token via CreateRestrictedToken and relaunch your application running with that restricted token. CreateRestrictedToken can be used to disable privileges and remove groups (like Administrators Group) from a token.

You may be able to use AdjustTokenGroups to remove the administrator group from the token of your running process, but I've never tried this on an already running process.

Note that write-access to the Windows directory is not covered by a privilege. Resources in the system have ACL's which govern who has access. System and administrators have write-access to the Windows directory.

like image 142
Michael Avatar answered Sep 21 '22 03:09

Michael