gpt4 book ai didi

c# - 拒绝模拟管理员访问注册表项 "..."

转载 作者:行者123 更新时间:2023-11-30 22:39:42 24 4
gpt4 key购买 nike

场景如下:
1. 应用已经以本地管理员身份运行
2. 它模拟域帐户,同时也是本地框的管理员
3. 模拟时,该应用程序尝试在对“此 key 和子 key ”的管理员组具有完全控制权的 key 下创建注册 key 。

此步骤失败并出现 UnauthorizedException“访问注册表项 ..”被拒绝。现在,如果我为域用户显式 ACL 注册 key ,则注册 key 的创建将通过。但是这个解决方案违背了加入管理员组的目的。

有什么想法可能会出错吗?

编辑:我在 Windows Server 2008 R2 上运行。我认为这个问题是由于启用了 UAC。 LogonUser 方法返回一个受限 token ,该 token 没有对注册表的提升访问权限。关于如何使用 LogonUser 方法获得提升的访问权限的任何想法?

我是这样调用它的:
IntPtr token = IntPtr.Zero;<br/>
LogonUser(username, domain, password, LOGON32_LOGON_BATCH, LOGON32_PROVIDER_DEFAULT, out token)

最佳答案

我会建议检查几件事:

  • 您应该将您的类(执行模拟)归因于完全信任模式请求,您可以使用

    [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
  • 此外,您还应该导入“advapi32.dll”和 LogonUser 以备后用。

  • 最后,您应该获得一个安全 token 句柄(继承自 SafeHandleZeroOrMinusOneIsInvalid,它为 Win32 安全句柄实现提供了一个基类,其中 0 或 -1 的值表示无效句柄)。
  • 在此您应该这样调用它(*使用 LOGON32_LOGON_INTERACTIVE,因为 BATCH 将不起作用*)

    LogonUser(userName, domainName, password, 2, 0, out safeTokenHandle);

获得句柄后,您应该使用它来执行任何操作:

    WindowsIdentity impid = new WindowsIdentity(safeTokenHandle.DangerousGetHandle());

获取后封装自己的action:

    using (WindowsImpersonationContext imp = impid.Impersonate())
{
// myActions
}

这应该能让您正确地执行并检测它是如何进行的。

我现在尝试在 ASP.NET 应用程序中执行此操作并成功了。这是 MVC 应用程序 Controller 的工作代码:

using System;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Security.Principal;
using System.Web.Mvc;
using Microsoft.Win32;
using Microsoft.Win32.SafeHandles;

namespace StackOimpersonationExample.Controllers
{
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public class HomeController : Controller
{
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, out TokenHandle phToken);

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle);

public ActionResult Index()
{
ViewBag.Message = "This line contains status info.";


#region ImpersonateTestUserAndWriteToRegistry

try
{
const string domainName = "W8CP";
const string userName = "testadmin";
const string password = "sxt";

TokenHandle tokenHandle;
bool returnValue = LogonUser(userName, domainName, password, 2, 0, out tokenHandle);

if (returnValue == false)
{
int retVal = Marshal.GetLastWin32Error();
ViewBag.Message = String.Format("Failed logon: {0}", retVal);
throw new System.ComponentModel.Win32Exception(retVal);
}
using (tokenHandle)
{
ViewBag.Message = "Logon successful!";
var newId = new WindowsIdentity(tokenHandle.DangerousGetHandle());
using (newId.Impersonate())
{
RegistryKey parentKey = Registry.LocalMachine;
RegistryKey softwareKey = parentKey.OpenSubKey("SOFTWARE", true);
if (softwareKey != null)
{
RegistryKey subKey = softwareKey.CreateSubKey("StackAnswer");

subKey.SetValue("CreatedAs", WindowsIdentity.GetCurrent().Name, RegistryValueKind.String);
subKey.SetValue("Website", "http://codecentral.org", RegistryValueKind.String);
subKey.SetValue("Email", "tonci.jukic@gmail.com", RegistryValueKind.String);

}
}
}
}
catch (Exception ex)
{
ViewBag.Message += String.Format(" Exception: " + ex.Message);
}
#endregion

return View();
}
}

public sealed class TokenHandle : SafeHandleZeroOrMinusOneIsInvalid
{
private TokenHandle(): base(true){}

[DllImport("kernel32.dll")]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CloseHandle(IntPtr handle);

protected override bool ReleaseHandle()
{
return CloseHandle(handle);
}
}
}

关于c# - 拒绝模拟管理员访问注册表项 "...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5575081/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com