gpt4 book ai didi

c# - 如何确保我的 Windows 模拟级别有效?

转载 作者:行者123 更新时间:2023-11-30 15:26:49 25 4
gpt4 key购买 nike

我正在处理一个令人讨厌的问题,我已经准备好撕掉我的头发了。我有一个 C# 控制台应用程序,它使用 Microsoft 的 HttpListener 类来监听 Web 请求。这个想法是控制台应用程序作为 UserAccountA(低权限)在后台运行。 UserAccountB(管理员等)出现,通过监听器访问网页,并冒充他或她的身份。 IIS 或 WCF 做的事情是一样的。我相信我在 Windows 7 上有这个工作,但现在我在 Windows 8.1 上,它又失败了。也许我一开始就没有它,或者这是一个新的转折点。

如果我通过 Visual Studio 运行该程序,我可以使用 Internet Explorer 11 访问它。出于某种原因,它要求我输入我的本地凭据。我假设这与 IE11 的开箱即用行为有关。一旦我输入它,它就会接受它。我的代码是这样开始的:

    protected virtual void Listen(object o)
{
HttpListener h = (HttpListener)o;

while (h.IsListening && (this.Disposed == false))
{
IAsyncResult Result = null;

Result = h.BeginGetContext(new AsyncCallback(this.ListenerCallback), h);

Result.AsyncWaitHandle.WaitOne();
}
}

然后像这样拾取(缩写):

    protected virtual void ListenerCallback(IAsyncResult Result)
{
HttpListener h = (HttpListener)Result.AsyncState;
HttpListenerContext context = null;
System.Security.Principal.WindowsIdentity identity = null;

context = h.EndGetContext(Result);

identity = (System.Security.Principal.WindowsIdentity)context.User.Identity;

using (System.Security.Principal.WindowsImpersonationContext wic = identity.Impersonate())
{
//method call to process request, under impersonation
}
}

我在使用两个不同的帐户(一个用于托管,一个用于访问)时逐步完成了这段代码。我已经使用此语句进行了验证:

   system.security.principal.windowsidentity.getcurrent().name

当前身份确实发生了转变。监听器配置为使用 NTLM AuthenticationScheme 启动。

我想复杂的部分是在模拟过程中调用的方法。我们正在做一些抽象,但让我指出失败的部分。在本例中,我们使用 SSPI 连接到 SQL Server——所以整个模拟的目的是获取客户端现有的登录信息,并优雅地将它们导入 SQL Server。同样,现在,这都是一台计算机的本地操作。它甚至不在事件目录域中。一切都是本地的,有本地帐户。它是这样开始的:

        System.Data.Common.DbProviderFactory fact = null;

if (UseSql())
{
fact = System.Data.Common.DbProviderFactories.GetFactory("System.Data.SqlClient");
}

没什么大不了的。我们在另一个项目中不断地使用提供者工厂,没有任何问题(但这只是一个普通的 WPF 应用程序,不是这样的)。在此之后,我们将使用 SSPI 属性设置 SQL 登录字符串。但我们甚至没有走到那一步。这正是它失败的地方,并显示以下消息:

A first chance exception of type 'System.IO.FileLoadException' occurred in mscorlib.dll

Additional information: Could not load file or assembly 'System.Data.OracleClient, Version=4.0.0.0, Culture=neutral, > PublicKeyToken=b77a5c561934e089' or one of its dependencies. Either a required impersonation level was not provided, or the provided impersonation level is invalid. (Exception from HRESULT: 0x80070542)

http://i854.photobucket.com/albums/ab103/srVincentVega/error7_zpsmb1xqrp0.png http://i854.photobucket.com/albums/ab103/srVincentVega/error7_zpsmb1xqrp0.png

我们根本没有使用 Oracle,我假设这只是它尝试打开 DbProviderFactory 时吐出的第一件事。对我来说,关键是模仿级别的事情。在此特定实验中,两个帐户(A 和 B)都是本地管理员。我已经通过本地安全策略确保他们可以在登录后模拟帐户。如果我查看我的事件日志,模拟似乎确实有效...

Special privileges assigned to new logon.

Subject: Security ID:

pc1\userA

Account Name: userA

Account Domain: pc1 Logon ID: 0xC4D0F3F

Privileges:

SeSecurityPrivilege

SeTakeOwnershipPrivilege

SeLoadDriverPrivilege

SeBackupPrivilege

SeRestorePrivilege

SeDebugPrivilege

SeSystemEnvironmentPrivilege

SeImpersonatePrivilege

我在这里尝试了太多变体,以至于开始妨碍我自己的分析。我基本上回到原点,不知道如何处理这个问题。有没有人有任何建议或提示?这看起来应该是一件非常简单的事情。我不确定为什么我遇到这么多麻烦。也许我只需要放弃使用 DbProviderFactory 并使用 OleDb 或其他任何东西。但是我还没有确认我可以在模拟后的运行时加载任何库。也许这是要做的事情。但是,如果有任何帮助,我将不胜感激。

谢谢,

约翰

最后一件事,这里是复制步骤:

在您的计算机上创建二级用户。登录以创建配置文件。注销并像往常一样重新登录。以该二级用户身份运行 Visual Studio(有几种方法可以执行此操作,按住 Shift 键并右键单击 VS 图标),然后在其中使用此框架创建一个 C# 控制台应用程序:

    namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
using (HttpListener h = new HttpListener())
{
h.Prefixes.Add("http://+:8090/");

h.AuthenticationSchemes = AuthenticationSchemes.Ntlm;

h.Start();

Console.WriteLine("Running");

HttpListenerContext context = h.GetContext();

System.Security.Principal.WindowsIdentity identity = null;
identity = (System.Security.Principal.WindowsIdentity)context.User.Identity;

using (System.Security.Principal.WindowsImpersonationContext wic = identity.Impersonate())
{
System.Data.Common.DbProviderFactory fact = null;
fact = System.Data.Common.DbProviderFactories.GetFactory("System.Data.SqlClient");
}

Console.ReadLine();

h.Stop();
}
}
}
}

调试/运行控制台应用程序。只需调用 Internet Explorer(vanilla,使用您的主帐户)并访问该 URL ( http://machinename:8090 )。异常发生

enter image description here http://i854.photobucket.com/albums/ab103/srVincentVega/error8_zpsilkay0bl.png

最佳答案

好吧,我读到一半就停在了你遇到的异常处:

A first chance exception of type 'System.IO.FileLoadException' occurred in mscorlib.dll 

这里的问题是,您用来运行该服务的帐户在执行模拟之前对加载 .Net 框架核心库的访问权限有限。通常,用于托管服务的此类帐户在托管服务的节点上具有管理员权限。

此外,为了允许模拟,您需要使用本地安全策略为服务帐户授予这样做的权限 - 本地策略 - 用户权限分配:

作为操作系统的一部分

此用户权限允许进程在未经身份验证的情况下模拟任何用户。因此,该进程可以访问与该用户相同的本地资源。

身份验证后模拟客户端

将此权限分配给用户允许代表该用户运行的程序模拟客户端。

另请注意,如果您使用 kerberos 而不是 NTLM,则需要在 AD (Active Directory) 中设置 SPN 和委派目标。

我不知道这是否对您有帮助,但这些是我们在进行模拟/委派时遇到的问题。

哦,是的,如果您执行不止一跳来执行模拟,则模拟级别必须设置为委派。

关于c# - 如何确保我的 Windows 模拟级别有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28597551/

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