在Windows上使用C ++删除权限 [英] Dropping privileges in C++ on Windows

查看:212
本文介绍了在Windows上使用C ++删除权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows上运行的C ++应用程序可能在运行时删除权限吗?

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?

简而言之,我想在main()函数中编写代码,这会丢掉我不需要的特权(例如,Windows目录上的写访问权限)。

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).

推荐答案

是的,您可以使用AdjustTokenPrivileges 从您的令牌中删除不必要的和危险的权限。

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.

您也可以通过创建一个受限制的令牌,如果不是立即需要(可以稍后启用该权限) href =http://msdn.microsoft.com/en-us/library/aa446583%28VS.85%29.aspx> CreateRestrictedToken ,然后重新启动使用该受限令牌运行的应用程序。 CreateRestrictedToken可用于禁用权限并从令牌中删除组(如Administrators组)。

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.

您可以使用 AdjustTokenGroups 从正在运行的进程的令牌中删除管理员组,但我从来没有在已经正在运行的进程。

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.

请注意,对Windows目录的写访问不受特权覆盖。系统中的资源具有 ACL ,用于管理具有访问权限的用户。系统和管理员对Windows目录具有写入权限。

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.

这篇关于在Windows上使用C ++删除权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆