Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
function Get-MtServicePrincipalRiskDetection {
<#
.SYNOPSIS
Returns the Entra ID Protection workload identity risk detections that are still open.

.DESCRIPTION
Reads identityProtection/servicePrincipalRiskDetections (Microsoft Graph v1.0) and returns the
detections whose risk state is atRisk or confirmedCompromised: the detections nobody has remediated
or dismissed. Each state is read with its own single-property filter, because Graph rejects some
compound filters on the Identity Protection endpoints, and each read goes through the Graph cache so
the checks that call this in one run share the same two requests.

Graph keeps risk detections for 90 days, so older detections are not returned. The API needs a
Microsoft Entra Workload ID Premium licence and the IdentityRiskEvent.Read.All permission.

.EXAMPLE
Get-MtServicePrincipalRiskDetection | Group-Object riskEventType

Counts the open workload identity risk detections by detection type.

.LINK
https://learn.microsoft.com/graph/api/identityprotectionroot-list-serviceprincipalriskdetections?view=graph-rest-1.0
#>
[CmdletBinding()]
[OutputType([object[]])]
param()

$select = @(
'id', 'riskEventType', 'riskLevel', 'riskState', 'riskDetail', 'activity', 'detectionTimingType',
'ipAddress', 'location', 'activityDateTime', 'detectedDateTime', 'lastUpdatedDateTime',
'servicePrincipalId', 'servicePrincipalDisplayName', 'appId', 'keyIds'
)

foreach ($state in @('atRisk', 'confirmedCompromised')) {
Invoke-MtGraphRequest -ApiVersion 'v1.0' `
-RelativeUri 'identityProtection/servicePrincipalRiskDetections' `
-Filter "riskState eq '$state'" `
-Select $select
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"TenantId": null,
"ConfigFile": "powershell/tests/fixtures/golden/config-layouts/root-custom-tenant/maester-config.json",
"ConfigSource": "maester-config.json",
"CustomOverlay": "powershell/tests/fixtures/golden/config-layouts/root-custom-tenant/Custom/maester-config.json",
"CustomOverlay": "powershell/tests/fixtures/golden/config-layouts/root-custom-tenant/custom/maester-config.json",
"UsedShippedFile": false,
"GlobalSettings": {
"EmergencyAccessAccounts": [
Expand All @@ -37,7 +37,7 @@
"TenantId": "11111111-2222-3333-4444-555555555555",
"ConfigFile": "powershell/tests/fixtures/golden/config-layouts/root-custom-tenant/maester-config.11111111-2222-3333-4444-555555555555.json",
"ConfigSource": "maester-config.11111111-2222-3333-4444-555555555555.json",
"CustomOverlay": "powershell/tests/fixtures/golden/config-layouts/root-custom-tenant/Custom/maester-config.json",
"CustomOverlay": "powershell/tests/fixtures/golden/config-layouts/root-custom-tenant/custom/maester-config.json",
"UsedShippedFile": false,
"GlobalSettings": {
"EmergencyAccessAccounts": [
Expand Down
9 changes: 6 additions & 3 deletions powershell/tests/fixtures/golden/selection.json
Original file line number Diff line number Diff line change
Expand Up @@ -1921,7 +1921,7 @@
"AD"
]
},
"SelectedCount": 590,
"SelectedCount": 591,
"SelectedFamilies": [
"MT.1024",
"MT.1059",
Expand Down Expand Up @@ -2451,6 +2451,7 @@
"MT.1259",
"MT.1260",
"MT.1265",
"MT.1274",
"ORCA.100",
"ORCA.101",
"ORCA.102",
Expand Down Expand Up @@ -2532,7 +2533,7 @@
"AD"
]
},
"SelectedCount": 620,
"SelectedCount": 621,
"SelectedFamilies": [
"MT.1024",
"MT.1033",
Expand Down Expand Up @@ -3094,6 +3095,7 @@
"MT.1259",
"MT.1260",
"MT.1265",
"MT.1274",
"ORCA.100",
"ORCA.101",
"ORCA.102",
Expand Down Expand Up @@ -3928,7 +3930,7 @@
"AD"
]
},
"SelectedCount": 576,
"SelectedCount": 577,
"SelectedFamilies": [
"MT.1024",
"MT.1033",
Expand Down Expand Up @@ -4446,6 +4448,7 @@
"MT.1259",
"MT.1260",
"MT.1265",
"MT.1274",
"ORCA.100",
"ORCA.101",
"ORCA.102",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
Describe 'Entra ID Protection open workload identity risk detections (MT.1274)' {
BeforeAll {
Import-Module $PSScriptRoot/../../Maester.psd1 -Force
. "$PSScriptRoot/../helpers/Use-MtModuleFunction.ps1"
# The native-test functions are not exported; reach them the same way the other check tests do.
Use-MtModuleFunction -Name Test-MtEntraServicePrincipalRiskDetection, Get-MtServicePrincipalRiskDetectionInstance

function script:New-TestDetection {
param(
[string] $Type = 'leakedCredentials',
[string] $Level = 'high',
[string] $State = 'atRisk',
[string] $Upn = 'alice@contoso.com',
[string] $Detected = '2026-10-01T00:00:00Z'
)
[pscustomobject]@{
id = [guid]::NewGuid().ToString()
riskEventType = $Type
riskLevel = $Level
riskState = $State
activity = 'signin'
ipAddress = '203.0.113.10'
location = [pscustomobject]@{ city = 'Seattle'; countryOrRegion = 'US' }
detectedDateTime = $Detected
servicePrincipalId = 'sp-1'
servicePrincipalDisplayName = 'Alice'
appId = $Upn
}
}

# The documented instances, read once so the per-test mocks of Get-MtServicePrincipalRiskDetection stay in charge.
$script:DocumentedInstances = InModuleScope Maester {
# Capture the script block first: Set-Item updates the FunctionInfo object in place.
$realBody = (Get-Command Get-MtServicePrincipalRiskDetection).ScriptBlock
Set-Item -Path function:Get-MtServicePrincipalRiskDetection -Value { @() }
try { Get-MtServicePrincipalRiskDetectionInstance } finally { Set-Item -Path function:Get-MtServicePrincipalRiskDetection -Value $realBody }
}

function script:Get-TestInstance {
param([string] $Id)
$script:DocumentedInstances | Where-Object { $_.Id -eq $Id }
}
}

BeforeEach {
$script:TestResult = $null
$script:SkippedBecause = $null
$script:Severity = $null

Mock -ModuleName Maester Add-MtTestResultDetail {
param($Result, $SkippedBecause, $SkippedError, $Severity)
$script:TestResult = $Result
$script:SkippedBecause = $SkippedBecause
$script:Severity = $Severity
}
}

AfterEach {
# Result text must not contain backtick-wrapped code spans (repo result-text lint).
if ($null -ne $script:TestResult) {
$script:TestResult | Should -Not -Match '`[^`]+`'
}
}

Context 'Instance source' {
It 'returns one instance per documented detection type' {
$instances = InModuleScope Maester {
Mock Get-MtServicePrincipalRiskDetection { return @() }
Get-MtServicePrincipalRiskDetectionInstance
}
$instances.Count | Should -Be 8
($instances.Id | Sort-Object -Unique).Count | Should -Be 8
$instances.Id | Should -Contain 'maliciousApplication'
$instances.Id | Should -Contain 'anomalousServicePrincipalActivity'
foreach ($instance in $instances) {
$instance.Id | Should -Match '^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$'
$instance.Severity | Should -BeIn @('High', 'Medium', 'Low')
$instance.Data.About | Should -Not -BeNullOrEmpty
$instance.Data.Tips | Should -Match '^https://learn\.microsoft\.com/'
}
}

It 'adds a row for an undocumented detection type that has open detections' {
$instances = InModuleScope Maester {
Mock Get-MtServicePrincipalRiskDetection {
return @(
[pscustomobject]@{ riskEventType = 'leakedCredentials' }
[pscustomobject]@{ riskEventType = 'brandNewDetection' }
)
}
Get-MtServicePrincipalRiskDetectionInstance
}
$instances.Count | Should -Be 9
$instances.Id | Should -Contain 'brandNewDetection'
}

It 'returns the documented types when the detections cannot be read' {
$instances = InModuleScope Maester {
Mock Get-MtServicePrincipalRiskDetection { throw '403 Forbidden' }
Get-MtServicePrincipalRiskDetectionInstance
}
$instances.Count | Should -Be 8
}
}

Context 'Check' {
It 'passes when no detection of the type is open' {
Mock -ModuleName Maester Get-MtServicePrincipalRiskDetection { return @(New-TestDetection -Type 'suspiciousSignins') }

Test-MtEntraServicePrincipalRiskDetection -Instance (Get-TestInstance 'leakedCredentials') | Should -BeTrue
$script:TestResult | Should -Match 'Well done'
$script:Severity | Should -Be 'High'
}

It 'fails and lists the open detections of the type, high risk first' {
Mock -ModuleName Maester Get-MtServicePrincipalRiskDetection {
return @(
New-TestDetection -Level 'low' -Upn 'low@contoso.com'
New-TestDetection -Level 'high' -Upn 'high@contoso.com'
New-TestDetection -Level 'medium' -Upn 'medium@contoso.com' -State 'confirmedCompromised'
New-TestDetection -Type 'suspiciousSignins' -Upn 'other@contoso.com'
)
}

Test-MtEntraServicePrincipalRiskDetection -Instance (Get-TestInstance 'leakedCredentials') | Should -BeFalse
$script:TestResult | Should -Match 'Found 3 open'
$script:TestResult | Should -Match '1 high, 1 medium, 1 low'
$script:TestResult | Should -Not -Match 'other@contoso\.com'
$script:TestResult.IndexOf('high@contoso.com') | Should -BeLessThan $script:TestResult.IndexOf('medium@contoso.com')
$script:TestResult.IndexOf('medium@contoso.com') | Should -BeLessThan $script:TestResult.IndexOf('low@contoso.com')
}

It 'skips as not authorized when Graph returns 403' {
Mock -ModuleName Maester Get-MtServicePrincipalRiskDetection { throw 'Response status code does not indicate success: 403 (Forbidden).' }

Test-MtEntraServicePrincipalRiskDetection -Instance (Get-TestInstance 'leakedCredentials') | Should -BeNullOrEmpty
$script:SkippedBecause | Should -Be 'NotAuthorized'
}

It 'limits the table and says how many more there are' {
Mock -ModuleName Maester Get-MtServicePrincipalRiskDetection {
return @(1..105 | ForEach-Object { New-TestDetection -Type 'suspiciousAPITraffic' -Level 'low' -Upn "user$_@contoso.com" })
}

Test-MtEntraServicePrincipalRiskDetection -Instance (Get-TestInstance 'suspiciousAPITraffic') | Should -BeFalse
$script:TestResult | Should -Match 'Showing the first 100 of 105'
}

It 'escapes a tenant-controlled display name' {
Mock -ModuleName Maester Get-MtServicePrincipalRiskDetection {
$d = New-TestDetection
$d.servicePrincipalDisplayName = '[Sign in again](https://phish.example)'
return @($d)
}

Test-MtEntraServicePrincipalRiskDetection -Instance (Get-TestInstance 'leakedCredentials') | Should -BeFalse
$script:TestResult | Should -Not -Match '(?<!\\)\[Sign in again\]\('
}
}

Context 'Data access' {
It 'reads the two open risk states from the v1.0 endpoint' {
InModuleScope Maester {
Mock Invoke-MtGraphRequest { return @() }
Get-MtServicePrincipalRiskDetection | Should -BeNullOrEmpty
Should -Invoke Invoke-MtGraphRequest -Exactly -Times 2
Should -Invoke Invoke-MtGraphRequest -Exactly -Times 1 -ParameterFilter {
$ApiVersion -eq 'v1.0' -and $RelativeUri -eq 'identityProtection/servicePrincipalRiskDetections' -and $Filter -eq "riskState eq 'atRisk'"
}
Should -Invoke Invoke-MtGraphRequest -Exactly -Times 1 -ParameterFilter {
$Filter -eq "riskState eq 'confirmedCompromised'"
}
}
}
}
}
31 changes: 31 additions & 0 deletions tests/maester/entra/Test.MT.1274.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Open Entra ID Protection workload identity risk detections should be reviewed.

Entra ID Protection raises risk detections for workload identities (service principals) as well as users: a client secret found in public code, sign-ins from an unusual place or with an unusual credential, unusual Microsoft Graph traffic, threat intelligence matches, and apps that Microsoft has found or disabled as malicious. Each detection stays open (**atRisk**) until someone remediates or dismisses it. A service principal cannot answer an MFA prompt, so a stolen credential works until it is removed, and an open detection nobody reviews leaves that door open. Graph keeps detections for 90 days.

Maester creates one result per detection type (MT.1274.&lt;riskEventType&gt;), so the report shows which kinds of signal are waiting. A result fails when at least one detection of its type has the risk state **atRisk** or **confirmedCompromised**, and lists those detections with the high-risk ones first. Detections that have been remediated or dismissed pass. Each result starts with a short explanation of its detection type. The severity of each result reflects how strong a sign of compromise that detection type is. A detection type that Microsoft adds later, and that Maester does not know yet, gets a result of its own when it has open detections.

This check reads the detections behind the risk. MT.1262 and MT.1263 report the service principals that are high or medium risk; the checks complement each other.

The check needs a Microsoft Entra Workload ID Premium licence, which the servicePrincipalRiskDetections API requires; tenants without it skip the check as not licensed. It reads identityProtection/servicePrincipalRiskDetections with the Microsoft Graph permission IdentityRiskEvent.Read.All, which Maester requests by default. It never changes a service principal's risk.

#### Remediation action

The security operations team should review every open detection and close it one way or the other.

1. Open the [Microsoft Entra admin center](https://entra.microsoft.com) and go to **ID Protection** > **Risk detections** > **Workload identity detections**. Start with high-risk detections and with service principals that hold privileged app roles or directory roles.
2. Investigate each service principal: its sign-ins, recent changes to its credentials, owners and app roles, and its configuration changes in the audit log.
3. If the activity is legitimate, go to **ID Protection** > **Risky workload identities** and select **Dismiss risk**.
4. If the service principal is compromised, select **Confirm compromised**, then:
- inventory the credentials of the service principal and its application object, add a new credential (preferably a certificate) and remove all the old ones;
- rotate any Azure Key Vault secrets the service principal can read;
- remove app roles, owners or permissions an attacker added, or disable the service principal until it is clean.
5. To block risky workload identities automatically, use a Conditional Access policy for workload identities with the service principal risk condition.

#### Related links

* [Securing workload identities with Microsoft Entra ID Protection](https://learn.microsoft.com/entra/id-protection/concept-workload-identity-risk)
* [Conditional Access for workload identities](https://learn.microsoft.com/entra/identity/conditional-access/workload-identity)
* [Microsoft Entra security operations guide for applications](https://learn.microsoft.com/entra/architecture/security-operations-applications)

<!--- Results --->
%TestResult%
Loading
Loading