- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个类库,它在注册表 (HKLM\Software\XXX) 中保存系统范围的配置数据。该库用于各种 Windows 版本(XP、2003、7、2008 R2)上的各种应用程序(服务、Windows 窗体、Web 应用程序、控制台应用程序)。正因为如此,应用程序的身份并不一致,甚至可能不是机器管理员组的成员。所以我创建了一个 AD 域管理员用户并进行模拟以获得对注册表的写入权限。这在 XP/2003 中完美运行,但在支持 UAC 的系统 (7/2008R2) 中运行不佳。据我了解,只有交互式登录才会拆分 token ,这意味着非交互式登录(服务身份、应用程序池身份等)不会。我找不到任何东西来证实这一点,但根据这个假设,我正在做的模拟应该有效。
我编写了一个包装类来使用 native LogonUser(网络登录类型,默认提供程序)和 DuplicateTokenEx(模拟,主 token )然后使用 WindowsIdentity.Impersonate() 进行模拟。我得到了对我的根 key 的引用:
using (ECR.Impersonator imp = new ECR.Impersonator("XXX", "XXX", "XXX"))
{
_root = Registry.LocalMachine.CreateSubKey("SOFTWARE\\XXX", RegistryKeyPermissionCheck.ReadWriteSubTree);
}
根据 MSDN ,通过使用 ReadWriteSubTree,这应该是完成安全检查的唯一时间。我可以将值写入该键,创建子键(也使用 ReadWriteSubTree)并将值写入这些子键,而无需再次进行安全检查。所以我认为我只需要进行一次代价高昂的模拟 - 获取对我的根 key 的引用。
我可以很好地将值写入我的根键:
_root.SetValue("cachedDate", value.ToBinary(), RegistryValueKind.QWord); }
但是当我使用 ReadWriteSubTree 创建/打开子键时:
RegistryKey key = _root.CreateSubKey("XXX", RegistryKeyPermissionCheck.ReadWriteSubTree);
它用 Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\XXX\XXX' is denied
轰炸。
虽然我很好奇为什么在 MSDN 说不应该进行安全检查时进行安全检查,但我的问题是如何通过模拟可能未在交互式登录下运行的应用程序来提升权限?
最佳答案
有人提出 LogonUser() 只返回受限 token 。搜索确认,我得到的印象是 LogonUser() 只为交互式 session 返回受限 token 。我创建了几个测试来找出答案。
第一个是控制台应用程序:
using (ECR.Impersonator imp = new ECR.Impersonator("XXX", "XXX", "XXX"))
{
WindowsIdentity ident = WindowsIdentity.GetCurrent();
WindowsPrincipal princ = new WindowsPrincipal(ident);
Console.WriteLine("{0}, {1}", ident.Name, princ.IsInRole(WindowsBuiltInRole.Administrator));
RegistryKey root = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Connection Strings", RegistryKeyPermissionCheck.ReadWriteSubTree);
RegistryKey key = root.CreateSubKey("AbacBill", RegistryKeyPermissionCheck.ReadWriteSubTree);
}
在提升的控制台中运行时,IsInRole() 返回 true 并且打开子项时没有错误。在未提升的控制台中运行时,IsInRole() 返回 true 并在打开子项时出错:
Unhandled Exception: System.IO.IOException: Unknown error "1346".
at Microsoft.Win32.RegistryKey.Win32Error(Int32 errorCode, String str)
at Microsoft.Win32.RegistryKey.CreateSubKey(String subkey, RegistryKeyPermissionCheck permissionCheck, RegistrySecurity registrySecurity)
at test.Program.test14()
at test.Program.Main(String[] args)
因此,在非提升的交互式 session 中,LogonUser() 确实返回了受限 token 。有趣的是,执行 IsInRole() 的正常测试意外返回 true。
第二个测试是一个网站。我将相同的代码放入(替换为 literal1.Text = string.Format 的 Console.Write):IsInRole() 返回 true,打开子项没有错误,IIS7.5:匿名身份验证,应用程序池:经典管道,ApplicationPoolIdentity,2.0 框架, web.config: authentication mode = none, 没有模拟。
因此,这似乎证实了我的印象,即 LogonUser() 仅为交互式 session 返回受限 token ,但非交互式 session 会获得完整 token 。
做这些测试帮助我回答了我自己的问题。我的类库主要用于 web 应用程序,并且在应用配置更新时它们不断地轰炸(拒绝访问打开子项)。所以我更改了我的测试以更准确地反射(reflect)我在做什么(模拟只是为了获得对我的根 key 的引用):
protected void Page_Load(object sender, EventArgs e)
{
RegistryKey root = null;
using (ECR.Impersonator imp = new ECR.Impersonator("XXX", "XXX", "XXX"))
{
WindowsIdentity ident = WindowsIdentity.GetCurrent();
WindowsPrincipal princ = new WindowsPrincipal(ident);
lit.Text = string.Format("{0}, {1}", ident.Name, princ.IsInRole(WindowsBuiltInRole.Administrator));
root = Registry.LocalMachine.CreateSubKey("SOFTWARE\\XXX", RegistryKeyPermissionCheck.ReadWriteSubTree);
}
root.SetValue("test", "test");
RegistryKey key = root.CreateSubKey("XXX", RegistryKeyPermissionCheck.ReadWriteSubTree);
}
它出错了:
[UnauthorizedAccessException: Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\XXX\XXX' is denied.]
Microsoft.Win32.RegistryKey.Win32Error(Int32 errorCode, String str) +3803431
Microsoft.Win32.RegistryKey.CreateSubKey(String subkey, RegistryKeyPermissionCheck permissionCheck, RegistrySecurity registrySecurity) +743
webtest.impTest.Page_Load(Object sender, EventArgs e) in D:\VS 2008 Projects\test\webtest\impTest.aspx.cs:28
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +25
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +42
System.Web.UI.Control.OnLoad(EventArgs e) +132
System.Web.UI.Control.LoadRecursive() +66
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2428
同样,将值写入我的根 key 没有问题,只需打开子 key 即可。因此,看起来使用 RegistryKeyPermissionCheck.ReadWriteSubTree 在写入该 key 时确实不会进行任何进一步的安全检查,但在打开子 key 时会进行另一次安全检查,即使使用 RegistryKeyPermissionCheck.ReadWriteSubTree(尽管文档说它没有)。
我的问题的答案是,它确实通过在非交互式登录下的模拟适本地提供了提升的权限。我的问题是我曾假设即使在模拟结束后,RegistryKeyPermissionCheck.ReadWriteSubTree 也不会对该引用进行任何进一步的安全检查(如文档所述)。
我想我每次需要写入注册表时都必须进行模拟。 :(
关于c# - 如何在非交互式登录下通过模拟获得提升的权限 (UAC)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5098121/
我正在开发一个使用 JTable 的简单 Java 应用程序。我正在尝试实现我所看到的here 。给定链接上的表格的行为与我在表格中想要的行为完全相同。我已完全按照引用链接上显示的方式进行操作。 但是
我需要将 *.ml 文件加载到 Ocaml 顶层(交互式解释器,当您在 shell 中键入“ocaml”时),然后从 Matlab 进程发送指令,获取指令结果,发送返回另一条指令,... 我编写了这个
我正在尝试实现交互式 UILocalNotifications。 以下是我的代码。我无法获得接收通知的 3 个操作按钮。 UIMutableUserNotificationAction *nActio
我不是一个干净的 CSS 编码器,所以这可能是我问题的症结所在……但是……我从本教程中拼凑了一张交互式 map :http://www.noobcube.com/tutorials/html-css/
这个问题在这里已经有了答案: 关闭 13 年前。 Possible Duplicate: How to save a Python interactive session? 我可以在“头脑 Stor
我试图在不扩展 ListActivity 类的情况下创建交互式 ListView。布局应该是左侧的 RatingBar 和标签向右。这是我目前的代码,没有任何编译错误,但在应用程序启动时崩溃: pub
我正在尝试使用 antlr 编写一种简单的交互式(使用 System.in 作为源)语言,但我遇到了一些问题。我在网上找到的例子都是使用每行循环,例如: while(readline) resul
我想创建一个交互式 JTable。为此,我想在表格的单元格中添加 JPanel。一旦 JPanel 位于单元格中,我就可以将我的各种组件添加到 JPanel 中,从而使表格具有交互性。每个 JPane
我有两个具有完全相同文件的分支(如果您想知道它是一个 .sql 文件),我想以交互方式 merge 它。 我非常想像在发生冲突(或命令行)时那样打开一个 diff 程序,然后准确选择行到哪里。 有什么
我想用交互式 rebase 编辑一个提交。当我用 edit 替换 pick 时,此提交的更改仍应用于 repo(以便我可以编辑提交)但我想从头开始重写它。我该怎么做? 最佳答案 在交互式 rebase
来自 Python relative imports for the billionth time : 要使 from .. import 起作用,模块名称中的点数必须至少与 import 语句中的点
如何使条形与一整天一样宽?现在,条形图更像是划分一天的开始的线。我是否从条形标记切换到矩形标记?我会玩带秤吗?或者我是否在不修改数据的情况下强制分箱? 现在条形图太细了。因为图形是交互式的,所以我无法
是否有任何命令行技巧可以让 SVN 添加来自 svn stat 的所有丢失的文件?互动? 例如,类似于: svn add --interactive $ new file: file1.tmp
我想知道这是否可以(可能不是)使用 R 中的并行处理后端之一来完成。 .我尝试了一些谷歌搜索,但一无所获。 我目前遇到的一般问题: 我有一些大物体需要大约半小时才能到达 load 我想在数据上生成一系
我一直在关注问题 here 的建议暂时将各种提示/信息放在我在 Altair 中的地 block 上。但是,如果 Altair 绘图设置为 interactive(),则此建议不起作用 - 在我看来,
简短的问题是,如果 shell 位于不拥有 tty 的孤立进程组中,它应该做什么?但我建议阅读长问题,因为它很有趣。 这是一种有趣且令人兴奋的方法,可以使用您最喜欢的 shell 将您的笔记本电脑变成
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
有没有办法实现交互式 cli,允许您从多个选项中进行选择? 我想要实现如下目标: 显示的示例是使用 JavaScript 中的 Quirer.js 库实现的,但我似乎找不到在 Java 中实现类似功能
我正在尝试使用运行一些命令的输入脚本来运行交互式 R (Windows XP),然后让我进入 R 命令行提示符。但是,当我运行它时,它会退出。 例如,这是输入文件: test.r: x = 1 x 以
我是一名优秀的程序员,十分优秀!