> 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/enumeration/mg-module.md).

# MG Module

{% hint style="warning" %}
Need credentials or some kind or token from here on out.
{% endhint %}

## Mg Module

To be able to use this PowerShell module, we must connect to MS Graph first. Below command opens a credential prompt:

```powershell
Connect-MgGraph
```

We can also login using an access token (obtained using Az Powershell module or any other method)

```powershell
$Token = eyJ0…
Connect-MgGraph –AccessToken ($Token | ConvertTo-SecureString -AsPlainText -Force)
```

{% stepper %}
{% step %}

#### Get the current session state

```powershell
Get-MgContext
```

{% endstep %}

{% step %}

#### Get details of the current tenant (Available in Beta version)

```powershell
Get-MgOrganization | fl *
```

{% endstep %}

{% step %}

#### Enumerate all users

```powershell
Get-MgUser -All
```

{% endstep %}
{% endstepper %}

***

### Mg Module — Users

{% stepper %}
{% step %}

#### Enumerate all users

```powershell
Get-MgUser -All
```

{% endstep %}

{% step %}

#### Enumerate a specific user

```powershell
Get-MgUser -UserId test@defcorphq.onmicrosoft.com
```

{% endstep %}

{% step %}

#### Search for a user based on string in first characters of DisplayName or userPrincipalName (wildcard not supported)

```powershell
Get-MgUser -Filter "startsWith(DisplayName, 'a')" -ConsistencyLevel eventual
```

{% endstep %}

{% step %}

#### Search for users who contain the word "admin" in their Display name:

```powershell
Get-MgUser -All |?{$_.Displayname -match "admin"}
Get-MgUser -Search '"DisplayName:admin"' -ConsistencyLevel eventual
```

{% endstep %}

{% step %}

#### List all the attributes for a user

```powershell
Get-MgUser -UserId test@defcorphq.onmicrosoft.com | fl *
Get-MgUser -UserId test@defcorphq.onmicrosoft.com | %{$_.PSObject.Properties.Name}
```

{% endstep %}

{% step %}

#### Search attributes for all users that contain the string "password"

```powershell
Get-MgUser -All |%{$Properties = $_;$Properties.PSObject.Properties.Name | % {if ($Properties.$_ -match 'password') {"$($Properties.UserPrincipalName) - $_ - $($Properties.$_)"}}}
```

{% endstep %}

{% step %}

#### All users who are synced from on-prem

```powershell
Get-MgUser -All | ?{$_.OnPremisesSecurityIdentifier -ne $null}
```

{% endstep %}

{% step %}

#### All users who are from Entra ID

```powershell
Get-MgUser -All | ?{$_.OnPremisesSecurityIdentifier -eq $null}
```

{% endstep %}

{% step %}

#### Objects created by any user (use <mark style="color:orange;">-ObjectId</mark> for a specific user)

```powershell
Get-MgUserCreatedObject -UserId test@defcorphq.onmicrosoft.com | fl *
```

{% endstep %}

{% step %}

### Objects owned by a specific user

```powershell
Get-MgUserOwnedObject -UserId test@defcorphq.onmicrosoft.com | fl *
```

{% endstep %}
{% endstepper %}

***

### Mg Module — Groups

{% stepper %}
{% step %}

#### List all Groups

```powershell
Get-MgGroup -All
```

{% endstep %}

{% step %}

#### Enumerate a specific group

```powershell
Get-MgGroup -GroupId 783a312d-0de2-4490-92e4-539b0e4ee03e
```

{% endstep %}

{% step %}

#### Search for a group based on string in first characters of DisplayName (wildcard not supported)

```powershell
Get-MgGroup -ConsistencyLevel eventual -Search '"DisplayName:A"'
```

{% endstep %}

{% step %}

#### To search for groups which contain the word "admin" in their name:

```powershell
Get-MgGroup -ConsistencyLevel eventual -Search '"DisplayName:Admin"'
```

{% endstep %}

{% step %}

#### Get Groups that allow Dynamic membership

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

{% endstep %}

{% step %}

#### All groups that are synced from on-prem (note that security groups are not synced)

```powershell
Get-MgGroup -All| ?{$_.OnPremisesSecurityIdentifier -ne $null}
```

{% endstep %}

{% step %}

#### Get members of a group

```powershell
Get-MgGroupMember -GroupId 783a312d-0de2-4490-92e4-539b0e4ee03e
```

{% endstep %}

{% step %}

#### Get groups and roles where the specified user is a member ( "()" are not a mistake)

```powershell
(Get-MgUserMemberOf -UserId test@defcorphq.onmicrosoft.com ).AdditionalProperties
```

{% endstep %}
{% endstepper %}

***

### Mg Module — Roles

{% stepper %}
{% step %}

#### Get all available role templates

```powershell
Get-MgDirectoryRoleTemplate
```

{% endstep %}

{% step %}

#### Get all enabled roles (a built-in role must be enabled before usage)

```powershell
Get-MgDirectoryRole
```

{% endstep %}

{% step %}

#### Enumerate users to whom roles are assigned

```powershell
$RoleId = (Get-MgDirectoryRole -Filter "DisplayName eq 'Global Administrator'").Id
```

```powershell
(Get-MgDirectoryRoleMember -DirectoryRoleId $RoleId).AdditionalProperties
```

{% endstep %}
{% endstepper %}

***

### Mg Module — Devices

{% stepper %}
{% step %}

#### Get all Azure joined and registered devices

```powershell
Get-MgDevice –All | fl *
```

{% endstep %}

{% step %}

#### List all the active devices (and not the stale devices)

```powershell
Get-MgDevice –All | ?{$_.ApproximateLastSignInDateTime -ne $null}
```

{% endstep %}

{% step %}
List registered owners of all the devices

```powershell
$Ids = (Get-MgDevice –All).Id; foreach($i in $Ids){ (Get-MgDeviceRegisteredOwner -DeviceId $i).AdditionalProperties}
```

```powershell
$Ids = (Get-MgDevice –All).Id; foreach($i in $Ids){ (Get-MgDeviceRegisteredUser -DeviceId $i).AdditionalProperties.userPrincipalName}
```

{% endstep %}

{% step %}

#### List registered users of all the devices

```powershell
$Ids = (Get-MgDevice –All).Id; foreach($i in $Ids){ (Get-MgDeviceRegisteredUser -DeviceId $i).AdditionalProperties}
```

```powershell
$Ids = (Get-MgDevice –All).Id; foreach($i in $Ids){ (Get-MgDeviceRegisteredUser -DeviceId $i).AdditionalProperties.userPrincipalName}
```

{% endstep %}

{% step %}

#### List devices owned by a user

```powershell
(Get-MgUserOwnedDevice -userId michaelmbarron@defcorphq.onmicrosoft.com).AdditionalProperties
```

{% endstep %}

{% step %}

#### List devices registered by a user

```powershell
(Get-MgUserRegisteredDevice -userId michaelmbarron@defcorphq.onmicrosoft.com).AdditionalProperties
```

{% endstep %}

{% step %}

### List devices managed using Intune

```powershell
Get-MgDevice -All| ?{$_.IsCompliant -eq "True"} | fl *
```

{% endstep %}
{% endstepper %}

***

### Mg Module — Apps

{% stepper %}
{% step %}

#### Get all the application objects registered with the current tenant (visible in App Registrations in Azure portal). An application object is the global representation of an app.

```powershell
Get-MgApplication -All
```

{% endstep %}

{% step %}

#### Get all details about an application

```powershell
Get-MgApplicationByAppId -AppId f072c4a6-b440-40de-983f-a7f3bd317d8f | fl *
```

{% endstep %}

{% step %}

#### Get an application based on the display name

```powershell
Get-MgApplication -All | ?{$_.DisplayName -match "app"}
```

{% endstep %}

{% step %}

#### The Get-MgApplication will show all the applications details including password but password value is not shown. List all the apps with an application password

```powershell
Get-MgApplication -All| ?{$_.PasswordCredentials -ne $null}
```

{% endstep %}

{% step %}

#### Get owner of an application

```powershell
(Get-MgApplicationOwner -ApplicationId 35589758-714e-43a9-be9e-94d22fdd34f6).AdditionalProperties.userPrincipalName
```

{% endstep %}

{% step %}

#### Get Apps where a User has a role (exact role is not shown)

```powershell
Get-MgUserAppRoleAssignment -UserId roygcain@defcorphq.onmicrosoft.com | fl *
```

{% endstep %}

{% step %}

#### Get Apps where a Group has a role (exact role is not shown)

```powershell
Get-MgGroupAppRoleAssignment -GroupId 57ada729-a581-4d6f-9f16-3fe0961ada82 | fl *
```

{% endstep %}
{% endstepper %}

***

### Mg Module — Service Principals

Service principal is local representation for an app in a specific tenant and it is the security object that has privileges. This is the 'service account'! Service Principals can be assigned Azure roles.

{% stepper %}
{% step %}

#### Get all service principals

```powershell
Get-MgServicePrincipal -All
```

{% endstep %}

{% step %}

#### Get all details about a service principal

```powershell
Get-MgServicePrincipal -ServicePrincipalId fd518680-b290-4db2-b92a-5dbd025c6791 | fl *
```

{% endstep %}

{% step %}

#### Get a service principal based on the display name

```powershell
Get-MgServicePrincipal –All | ?{$_.DisplayName -match "app"}
```

{% endstep %}

{% step %}

#### List all the service principals with an application password

```powershell
Get-MgServicePrincipal –All | ?{$_.KeyCredentials -ne $null}
```

{% endstep %}

{% step %}

#### Get owner of a service principal

```powershell
(Get-MgServicePrincipalOwner -ServicePrincipalId fd518680-b290-4db2-b92a-5dbd025c6791).AdditionalProperties.userPrincipalName
```

{% endstep %}

{% step %}

#### Get objects owned by a service principal

```powershell
Get-MgServicePrincipalOwnedObject -ServicePrincipalId fd518680-b290-4db2-b92a-5dbd025c6791
```

{% endstep %}

{% step %}

#### Get objects created by a service principal

```powershell
Get-MgServicePrincipalCreatedObject -ServicePrincipalId fd518680-b290-4db2-b92a-5dbd025c6791
```

{% endstep %}

{% step %}

#### Get group and role memberships of a service principal

```powershell
Get-MgServicePrincipalMemberOf -ServicePrincipalId fd518680-b290-4db2-b92a-5dbd025c6791 | fl *
```

{% endstep %}
{% endstepper %}
