在ASP.NET应用程序中使用身份模拟(Impersonation)
摘要
缺省情况下,ASP.NET应用程序以本机的ASPNET帐号运行,该帐号属于普通用户组,权限受到一定的限制,以保障ASP.NET应用程序运行的安全。但是有时需要某个ASP.NET应用程序或者程序中的某段代码执行需要特定权限的操作,比如某个文件的存取,这时就需要给该程序或相应的某段代码赋予某个帐号的权限以执行该操作,这种方法称之为身份模拟(Impersonation)。本文介绍了在ASP.NET应用程序中使用身份模拟的几种方法,并比较了它们各自适用的范围。
在阅读本文之前,建议您先阅读文章:《ASP .NET 中的身份验证:.NET 安全性指导》 以便对ASP.NET的安全控制有一个总体的了解。
目录
-
ASP.NET中的身份模拟
-
模拟IIS认证帐号
-
在某个ASP.NET应用程序中模拟指定的用户帐号
-
在代码中模拟IIS认证帐号
-
在代码中模拟指定的用户帐号
-
更多信息
ASP.NET中的身份模拟
ASP.NET 通过使用身份验证提供程序来实现身份验证,一般情况下,ASP.NET的身份验证提供程序包括表单身份验证、Windows身份验证和Passport身份验证3种。当通过身份验证后,ASP.NET会检查是否启用身份模拟。如果启用,ASP .NET 应用程序使用客户端标识以客户端的身份有选择地执行。否则,ASP.NET应用程序使用本机身份标识运行(一般使用本机的ASPNET帐号),具体流程如下图所示:
在ASP.NET应用程序中使用身份模拟一般用于资源访问控制,主要有如下几种方法:
-
模拟IIS认证帐号
-
在某个ASP.NET应用程序中模拟指定的用户帐号
-
在代码中模拟IIS认证帐号
-
在代码中模拟指定的用户帐号
模拟IIS认证帐号
这是最简单的一种方法,使用经过IIS认证的帐号执行应用程序。您需要在Web.config文件中添加<identity>标记,并将impersonate属性设置为true:
<identity impersonate="true" />
在这种情况下,用户身份的认证交给IIS来进行。当允许匿名登录时,IIS将一个匿名登录使用的标识(缺省情况下是IUSR_MACHINENAME)交给ASP.NET应用程序。当不允许匿名登录时,IIS将认证过的身份标识传递给ASP.NET应用程序。ASP.NET的具体访问权限由该账号的权限决定。
模拟指定的用户帐号
当ASP.NET应用程序需要以某个特定的用户帐号执行,可以在Web.config文件的<identity>标记中指定具体的用户帐号:
<identity impersonate="true" userName="accountname" password="password" />
这时该ASP.NET应用程序的所有页面的所有请求都将以指定的用户帐号权限执行。
在代码中模拟IIS认证帐号
在代码中使用身份模拟更加灵活,可以在指定的代码段中使用身份模拟,在该代码段之外恢复使用ASPNET本机帐号。该方法要求必须使用Windows的认证身份标识。下面的例子在代码中模拟IIS认证帐号:
Visual Basic .NET
Dim impersonationContext As System.Security.Principal.WindowsImpersonationContextDim currentWindowsIdentity As System.Security.Principal.WindowsIdentitycurrentWindowsIdentity = CType(User.Identity, System.Security.Principal.WindowsIdentity)impersonationContext = currentWindowsIdentity.Impersonate()'Insert your code that runs under the security context of the authenticating user here.impersonationContext.Undo()
Visual C# .NET
System.Security.Principal.WindowsImpersonationContext impersonationContext;impersonationContext = ((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();//Insert your code that runs under the security context of the authenticating user here.impersonationContext.Undo();
在代码中模拟指定的用户帐号
下面的例子在代码中模拟指定的用户帐号:
Visual Basic .NET
<%@ Page Language="VB" %><%@ Import Namespace = "System.Web" %><%@ Import Namespace = "System.Web.Security" %><%@ Import Namespace = "System.Security.Principal" %><%@ Import Namespace = "System.Runtime.InteropServices" %><script runat=server>Dim LOGON32_LOGON_INTERACTIVE As Integer = 2Dim LOGON32_PROVIDER_DEFAULT As Integer = 0Dim impersonationContext As WindowsImpersonationContextDeclare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As String, _ByVal lpszDomain As String, _ByVal lpszPassword As String, _ByVal dwLogonType As Integer, _ByVal dwLogonProvider As Integer, _ByRef phToken As IntPtr) As IntegerDeclare Auto Function DuplicateToken Lib "advapi32.dll"(ByVal ExistingTokenHandle As IntPtr, _ImpersonationLevel As Integer, _ByRef DuplicateTokenHandle As IntPtr) As IntegerPublic Sub Page_Load(s As Object, e As EventArgs)If impersonateValidUser("username", "domain", "password") Then'Insert your code that runs under the security context of a specific user here.undoImpersonation()Else'Your impersonation failed. Therefore, include a fail-safe mechanism here.End IfEnd SubPrivate Function impersonateValidUser(userName As String, _domain As String, password As String) As BooleanDim tempWindowsIdentity As WindowsIdentityDim token As IntPtrDim tokenDuplicate As IntPtrIf LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE, _LOGON32_PROVIDER_DEFAULT, token) <> 0 ThenIf DuplicateToken(token, 2, tokenDuplicate) <> 0 ThentempWindowsIdentity = new WindowsIdentity(tokenDuplicate)impersonationContext = tempWindowsIdentity.Impersonate()If impersonationContext Is Nothing ThenimpersonateValidUser = FalseElseimpersonateValidUser = TrueEnd IfElseimpersonateValidUser = FalseEnd IfElseimpersonateValidUser = FalseEnd IfEnd FunctionPrivate Sub undoImpersonation()impersonationContext.Undo()End Sub</script>
Visual C# .NET
<%@ Page Language="C#"%><%@ Import Namespace = "System.Web" %><%@ Import Namespace = "System.Web.Security" %><%@ Import Namespace = "System.Security.Principal" %><%@ Import Namespace = "System.Runtime.InteropServices" %><script runat=server>public const int LOGON32_LOGON_INTERACTIVE = 2;public const int LOGON32_PROVIDER_DEFAULT = 0;WindowsImpersonationContext impersonationContext;[DllImport("advapi32.dll", CharSet=CharSet.Auto)]public static extern int LogonUser(String lpszUserName,String lpszDomain,String lpszPassword,int dwLogonType,int dwLogonProvider,ref IntPtr phToken);[DllImport("advapi32.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto, SetLastError=true)]public extern static int DuplicateToken(IntPtr hToken,int impersonationLevel,ref IntPtr hNewToken);public void Page_Load(Object s, EventArgs e){if(impersonateValidUser("username", "domain", "password")){//Insert your code that runs under the security context of a specific user here.undoImpersonation();}else{//Your impersonation failed. Therefore, include a fail-safe mechanism here.}}private bool impersonateValidUser(String userName, String domain, String password){WindowsIdentity tempWindowsIdentity;IntPtr token = IntPtr.Zero;IntPtr tokenDuplicate = IntPtr.Zero;if(LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE,LOGON32_PROVIDER_DEFAULT, ref token) != 0){if(DuplicateToken(token, 2, ref tokenDuplicate) != 0){tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);impersonationContext = tempWindowsIdentity.Impersonate();if (impersonationContext != null)return true;elsereturn false;}elsereturn false;}elsereturn false;}private void undoImpersonation(){impersonationContext.Undo();}</script>
下面介绍ASP.NET应用程序中使用身份模拟的一个简单应用。例如有一个ASP.NET应用程序要检查服务器端某个文件是否存在,相应的程序代码为:
bool a = File.Exists("D:\\Share\\test.txt");
缺省情况下该ASP.NET应用程序以ASPNET帐号运行。为了安全起见,ASPNET这个帐号并没有服务器端D:\Share\这个目录的访问权限。在不使用身份模拟的情况下,由于ASP.NET应用程序不具有访问该目录的权限,无论文件是否存在,File.Exists的返回值将永远是false。为了解决这个问题,可以另建一个用户帐号:FileExist,并赋予该帐号D:\Share\目录的访问权限。然后在该应用程序的Web.config文件的<identity>标记中指定具体的用户帐号:
<identity impersonate="true" userName="FileExist" password="password" />
来执行该程序。
分享到:
相关推荐
在本文中,我将向您展示如何使用JWT身份验证和授权来实现ASP.NET Core Web API应用程序。 在本文中,我们将使用Angular构建一个简单的应用程序。 我们将实现AuthService类,以处理登录,注销和刷新令牌过程以及...
或是当ASP.NET应 用程序以另一个Windows用户而非ASPNET运行时,使用ASP.NET impersonation方法。 为了使ASPNET帐户可以访问ASP.NET应用程序的数据库,需要完成以下步骤: 1)启动SQL Server Management Studio,指定...
身份模拟的配置上,IIS7 和 IIS6有很大不同,网上IIS6的身份模拟的文章比较多,但介绍IIS7的比较少,我把的一些折腾的经验在这篇博客中写下来,以供参考。 IIS 7 有两种 ASP.NET Application Mode。 一种是 集成模式...
Laravel开发-impersonation 正在模拟Laravel应用程序的用户。
每个这样的身份验证都记录在数据库中,并在管理界面中列出,每个人都可以访问ImpostorLog界面。 但是,无法通过管理界面删除日志条目,从而使覆盖曲目更加困难。 Impostor已在Django 1.11及更高版本上进行了测试。...
在模拟的上下文中执行具有提供的参数的任何应用程序。 通过使用管道进行父子进程通信,可以在非交互式上下文(例如,反向外壳)中使用。用法: General options: --help Display help menu.Impersonation Options: ...
提供0xsp猫鼬红色版本可满足您在网络安全模拟过程中的需求,通过使用此版本,您将能够针对目标Windows操作系统进行系统漏洞,错误配置和特权升级攻击的审核,并在其中复制高级对手的策略和技术。一个网络。 借助...
使用Spring Security进行用户模拟
129 0×00000081 %1 这个应用程序无法在 Win32 模式下执行。 130 0×00000082 Attempt to use a file handle to an open disk partition for an operation other than raw disk I/O. 131 0×00000083 尝试将档案指针...
PYTMIPE (用于令牌操作的PYthon库和用于特权升级的模拟)是一个Python 3库,用于处理Windows令牌和管理模拟,以便在Windows上获得更多特权。 TMIPE是使用pytmipe库的python 3客户端。 内容 一个python客户端: ...
23. Impersonation: ExplorerAsParent 24. Impersonation: DDE 25. Impersonation: BITS 26. Hijacking: WinlogonNotify 27. Hijacking: Userinit 28. Hijacking: UIHost 29. Hijacking: SupersedeServiceDll ...
You can use the pxf.service.user.impersonation property to configure user impersonation for a new JDBC server configuration. Note: If you have previously configured a PXF JDBC server to access ...
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation NULL, // Authentication info EOAC_NONE, // Additional capabilities NULL // Reserved ); //第三步获取WMI初始定位器-------------------------...
15/06/22 15:05:25 WARN conf.HiveConf: HiveConf of name hive.server2.enable.impersonation does not exist Logging initialized using configuration in file:/etc/hive/conf/hive-log4j.properties SLF4J: ...
execute an impersonation/privacy breach attack. We discuss the feasibility of the attack. Finally, we recommend an approach to session management which separates the security from the functionality –...
Spring 配置可以在 src/main/resources/com/microsoft/exchange 中找到。 笔记 此项目包含一个 Java 密钥库,其中包含用于签署 Office 365 的 Exchange 服务的证书。 文件在src/main/resources/ews.truststore,...
在Windows服务的安装类ProjectInstaller中加入以下方法: 代码如下:protected override void OnCommitted(System.Collections.IDictionary savedState){base.OnCommitted(savedState);//将服务更改为允许桌面交互...
Definition of Bypass Items covered Network controls--...File Impersonation Browsers – Loading Files Sandbox Endpoint Proxy/VPN Bypassing Endpoints Hooking Issues Security Logging Tools Take Away
To facilitate saving thumbnails to a remote drive, the LogonUser method has been added for admin account impersonation. A bug in the DrawImage method has been fixed which prevented CMYK images to be...