如何在 .Net 中操纵令牌权限?
我想使用 C# 来确定分配给我的进程/线程令牌的权限,并根据需要调整它们。例如,为了让我的程序重新启动计算机,它必须首先启用 SeShutdownPrivilege
权限。
如何通过托管代码安全地完成此操作?
I'd like to use C# to determine which privileges are assigned to my process/thread token, and adjust them as necessary. For example, in order for my program to restart the computer, it must first enable the SeShutdownPrivilege
privilege.
How can that be done safely from managed code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实证明这并不简单,因为没有内置的机制。不仅需要 P/Invoke,而且您必须仔细编码,以确保不会通过启用它们然后不尽快禁用它们来“泄漏”权限(尽管如果您重新启动计算机,则这不是问题)。
有关包含说明的完整代码示例,请阅读 2005 年 3 月的 MSDN 杂志文章“操纵权限可靠、安全、高效的托管代码”,作者:Mark Novak。
以下是 P/Invoke 声明:
This turns out to be non-trivial because there's no built-in mechanism for it. Not only is P/Invoke required, but you must code carefully to make sure that you don't "leak" privileges by enabling them and then not disabling them soon enough (though not an issue if you're restarting the computer).
For a complete code sample with description, read the MSDN magazine article from March 2005 "Manipulate Privileges in Managed Code Reliably, Securely, and Efficiently" by Mark Novak.
Here's the P/Invoke declarations:
这是我用的。它基于 Mark Novak 的文章,但减少了对不受信任的堆栈框架、CER 或重入的偏执(因为我假设您不是在编写 Internet Explorer 或 SQL Server 插件)。
Privilege.cs:
NativeMethods.cs:
在另一个用户的会话中创建进程的示例用法:
Here's what I use. It is based off of the Mark Novak article, but with less paranoia for untrusted stack frames, CER's, or reentrance (since I assume you are not writing internet explorer or a SQL Server Add-in).
Privilege.cs:
NativeMethods.cs:
Example Usage to create a process in another user's session: