Tampering with Conditional Access Policies Using Azure AD Graph API
The legacy Azure AD Graph API allowed any tenant user to read Conditional Access policies without the required administrator role, and allowed administrators to modify policy metadata and settings without generating the expected audit log events. An attacker with administrator rights could tamper with policies and obscure their changes, while any user could view policies to identify security gaps.
Published on secureworks.com
Azure Active Directory (Azure AD) is Microsoft's cloud-based identity and access management service, and it supports multiple authentication methods. The premium version of Azure AD also supports Conditional Access policies (CAPs) that grant or block access based on defined criteria, such as device compliance or user…
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
Azure Active Directory (Azure AD) is Microsoft's cloud-based identity and access management service, and it supports multiple authentication methods. The premium version of Azure AD also supports Conditional Access policies (CAPs) that grant or block access based on defined criteria, such as device compliance or user location. Azure AD stores the settings for the authentication methods and CAPs. CAPs can be modified via the Azure AD portal, PowerShell, and API calls.
CAPs allow organisations to grant or block access to services protected by Azure AD. They can also be used for session monitoring and limiting a session lifetime. CAPs are enforced during the Azure AD authentication process. Azure AD uses the following common signals to make a policy decision:
- User or group membership
- IP location information
- Device
- Application
Only users with specific roles can access CAPs in the Azure AD portal.
| Access type | Azure AD role |
|---|---|
| Read | Global Administrator, Global Reader, Security Administrator, Conditional Access Administrator, Security Reader |
| Modify | Global Administrator, Security Administrator, Conditional Access Administrator |
Table 1. Azure AD roles required to access CAPs.
Figure 1 in the report shows an example CAP that requires all users to perform multi-factor authentication (MFA). The policy is not enabled in this example; it is set to Report-only mode, which allows organisations to assess the impact of the CAP before enforcing it.
The Azure AD portal displays the name, state, and creation and modification timestamps of each policy, and reflects changes whenever the CAP is modified.
Azure AD audit logs capture CAP creation and modification events. Both the 'Add conditional access policy' and 'Update conditional access policy' events include details of the modified properties. This feature provides a full audit trail and includes modified settings.
Setup
In May 2022, Secureworks Counter Threat Unit (CTU) researchers investigated which APIs allow editing of CAP settings. They identified three: the legacy Azure AD Graph (also known as AADGraph), Microsoft Graph, and an undocumented Azure IAM API. The researchers used the AADInternals toolkit to tamper with CAPs.
Finding it
AADGraph was the only API that allowed modification of all CAP settings, including the metadata. This capability lets administrators tamper with all CAP settings, including the creation and modification timestamps. Modifications made using AADGraph are not properly logged, endangering integrity and non-repudiation of Azure AD policies.
The Azure AD portal is a graphical user interface (GUI) that allows administrators to create and maintain CAPs via a browser. GUIs can perform ad-hoc tasks but not automation and programmatic access. To address those needs, Microsoft provides three APIs that can interact with CAPs:
- Azure AD IAM
- MS Graph
- Azure AD Graph (AADGraph)
Azure AD IAM API
The Azure AD portal uses an undocumented Azure AD IAM API to create, view, and edit CAPs. The API is available at https://main.iam.ad.ext.azure.com/api/Policies/Policies. Because the Azure AD portal uses Azure AD IAM APIs, access requires the permissions listed in Table 1. The API returns a list of CAPs as a JSON object.
When the API opens a CAP for editing, it returns the CAP details as a JSON object. This returned JSON object has many fields, which correspond to the CAP settings available in the Azure AD portal. The response also includes creation and modification timestamps.
Modifying a CAP sends a JSON object to https://main.iam.ad.ext.azure.com/api/Policies/ConvertPolicyMsGraph as an HTTP POST request. In the researchers' example the CAP state was changed from Off to Report-only. Only the modified data and not the metadata is sent to Azure AD.
MS Graph API
MS Graph API support for conditional access is well-documented, and Microsoft also published examples for creating and editing CAPs. The following permissions are required to access CAPs via MS Graph API.
| Access type | Permissions |
|---|---|
| Modify (all three required) | Policy.Read.All, Policy.ReadWrite.ConditionalAccess, Application.Read.All |
| Read | Policy.Read.All |
Table 2. MS Graph API permissions required for CAPs.
Users or applications with these permissions can list CAPs by calling the API at https://graph.microsoft.com/v1.0/identity/conditionalAccess/policies. The API returns all CAPs and details as a JSON object.
Creating or modifying a CAP uses the same API endpoint:
- Creating a CAP makes an HTTP POST with a JSON object.
- Modifying a CAP makes an HTTP PATCH request with a JSON object.
Only the modified data is sent to Azure AD. The metadata is not included.
Azure AD Graph API (AADGraph)
Microsoft has attempted to deprecate the AADGraph API for years. As of the publication, its retirement is scheduled to occur sometime after June 30, 2023. Microsoft has removed public AADGraph API documentation to discourage its use.
CAPs can be accessed using the AADGraph API at https://graph.windows.net/<tenant>/policies?api-version=<api version>, where <tenant> is the Azure AD tenant and <api version> is the desired AADGraph API version. Using 1.6 as the API version returns some Azure AD policies that the user can access if they have appropriate permissions, but CAPs are not listed. However, using 1.61-internal as the version returns all Azure AD policies, including CAPs, regardless of the user's permissions. As a result, any user of the tenant can list CAPs and bypass the role requirements.
The API returns all policies as JSON objects. A CAP policy is indicated by the policyType of 18.
The CAP settings and metadata are stored in the policyDetail attribute as a JSON object. Administrators with permissions to modify CAPs can edit this attribute, enabling them to tamper with the CAP conditions and metadata.
Updating an existing CAP with the AADGraph API involves an HTTP PATCH request to https://graph.windows.net/<tenant>/policies/<objectid>?api-version=1.61-internal, where <objectid> is the object ID of the CAP to be modified. The content of the request is a JSON object that only includes the policyDetail attribute.
Exploitation
CTU researchers used the AADInternals toolkit to tamper with CAPs. Administrators or threat actors can leverage the AADGraph API to make changes that are not properly logged.
Step 1: Retrieve the current policyDetail value
- Acquired an access token for an administrator with permissions to modify CAPs.
- Saved the example CAP to a variable.
- Extracted the policyDetail value and copied the data to the clipboard.
Step 2: Modify the policyDetail JSON
The policyDetail value was pasted into a text editor and the JSON reformatted for readability. The researchers then emptied the ModifiedDateTime attribute (line 4) and changed the State attribute from Reporting to Disabled. The modified JSON was flattened and copied to the clipboard.
Step 3: Update the CAP with the modified policyDetail
The modified policyDetail from the clipboard was used to update the CAP. The Azure AD portal updated the modifications within a minute.
When CAPs are updated via the AADGraph API, the 'Update conditional access policy' event is not generated in the audit logs. As a result, there is an incomplete audit trail on what modifications were made.
Threat actors with administrator permissions can leverage this omission to obscure CAPs. For instance, the following PowerShell script removes the timestamps and display names of all CAPs.
After running the script, CAPs are fully functional. However, the Azure AD portal cannot open or edit them.
Administrators can still delete CAPs and make duplicates to view existing CAP settings. If organisations keep audit logs for a longer period of time, they may be able to restore CAP names and timestamps based on historical audit log data.
Root cause
AADGraph exposes the full policyDetail attribute, including metadata such as creation and modification timestamps, and accepts updates to it. The other two APIs (Azure AD IAM and MS Graph) only accept the modified policy data and not the metadata. Because AADGraph updates do not generate the 'Update conditional access policy' event, and the corresponding Core Directory service audit log item does not contain the details of the changed properties and values, the audit trail is incomplete. The role requirements enforced by the portal and the other APIs are also bypassed for reads: with api-version=1.61-internal, any user of the tenant can list CAPs regardless of their permissions.
Impact
Administrators can use the AADGraph API to change CAPs. The API does not properly log changes, and the lack of an audit trail breaks integrity and non-repudiation of CAPs. As a result, organisations cannot trust CAP information shown in the Azure AD portal or in directory audit logs. In addition, any tenant user can view CAPs without administrator permissions. This ability allows low-privileged threat actors to identify gaps in CAPs or target them for future modification. Third-party tools such as ROADTools and TSxAzureADExport exploit this ability.
Fix
On May 11, 2023, the MSRC informed the CTU research team of planned changes to address these issues:
- Improve audit logs to reflect the type of policy being updated when CA policies are updated through AAD Graph.
- Prevent admins from using AAD Graph to make updates to CA policies.
In addition to these improvements, AAD Graph is set to be retired.
CTU researchers recommend that organisations store Azure AD audit logs in the Log Analytics workspace or in other storage solutions such as Secureworks Taegis XDR. Organisations can detect CAP modifications via the AADGraph API by monitoring audit logs for an 'Update policy' event that does not have a corresponding 'Update conditional access policy' event within two seconds.
The following script can restore the names and modification dates of CAPs that have been created or modified using the Azure AD portal or the MS Graph API:
# Read legit CAP events from the audit log
$CAPEvents=Get-AADIntAzureAuditLog -Export `
| Where-Object activityDisplayName -in `
"Add conditional access policy", `
"Update conditional access policy" `
| Select-Object "activityDateTime" -ExpandProperty "targetResources" `
| Select-Object "id","displayName","activityDateTime"
# Loop through the events to get the first (latest) update
$CAPInfos=@{}
foreach($CAPEvent in $CAPEvents)
{
if(!$CAPInfos.ContainsKey($CAPEvent.id))
{
$CAPInfos[$CAPEvent.id] = [pscustomobject]@{
"displayName" = $CAPEvent.displayName
"modifiedDateTime" = $CAPEvent.activityDateTime
}
}
}
# Read current CAPs
$CAPs = Get-AADIntConditionalAccessPolicies
# Loop through CAPs
foreach($CAP in $CAPs)
{
# Create the return value
$retVal = [pscustomobject][ordered]@{
"id" = $CAP.objectId
"isEmpty" = [string]::IsNullOrWhiteSpace($CAP.displayName)
"success" = $null
"name" = $CAP.displayName
}
# Check whether the displayName is empty
if($retVal.isEmpty)
{
# Check whether we found the old information
if($CAPInfo = $CAPInfos[$retVal.id])
{
# Get policyDetails and fix Modified Date
$policyDetail = $CAP.policyDetail[0] | ConvertFrom-Json
try
{
$policyDetail.ModifiedDateTime = $CAPInfo.modifiedDateTime
}
catch{}
$newPolicyDetail = $policyDetail | ConvertTo-Json -Depth 10 -Compress
# Replace name with the old displayName
$retVal.name = $CAPInfo.displayName
try
{
Set-AADIntAzureADPolicyDetails -ObjectId $retVal.id `
-PolicyDetail $newPolicyDetail `
-DisplayName $retVal.name `
| Out-Null
$retVal.success = $true
}
catch
{
# Failed
$retVal.success = $false
$retVal.name = $CAP.displayName
}
}
}
# Return
$retVal
}
The following KQL query can be used to identify 'Update policy' events that do not have a corresponding 'Update conditional access policy' event within two seconds:
AuditLogs
| where OperationName == "Update policy"
| mv-expand TargetResources
| where TargetResources.displayName != "Default Policy"
| mv-expand InitiatedBy
| project PolicyName = TargetResources.displayName, PolicyId = tostring(TargetResources.id), UserPrincipalName = InitiatedBy.user.userPrincipalName, UserId = tostring(InitiatedBy.user.id), OperationName, Time = bin(TimeGenerated, 2s), TimeGenerated, CorrelationId
|join kind=leftanti (AuditLogs
| where OperationName == "Update conditional access policy"
| mv-expand TargetResources
| mv-expand InitiatedBy
| project PolicyId = tostring(TargetResources.id), UserId = tostring(InitiatedBy.user.id), Time = bin(TimeGenerated, 2s)) on PolicyId,UserId,Time
| order by TimeGenerated
- Query the AADGraph policies endpoint with
api-version=1.61-internalas a low-privileged tenant user. A positive result is a JSON list that includes policies with policyType 18 (CAPs), which the user should not be able to read. - Compare the audit log after a CAP update made through AADGraph against one made through the portal or MS Graph. A positive result is an 'Update policy' event with no corresponding 'Update conditional access policy' event within two seconds.
- Inspect the policyDetail attribute of a CAP returned by AADGraph. A positive result is that it contains creation and modification timestamps that can be edited through a PATCH to the policy object.
- Run the KQL detection query against a tenant's audit logs. A positive result is any row returned, indicating a policy update that bypassed the conditional access audit event.
- Check whether the portal can still open and edit a CAP after its display name and timestamps have been emptied. A positive result is a policy that remains functional but cannot be opened or edited in the portal.
What happened, in plain words
Microsoft runs a service called Azure Active Directory (Azure AD). It is the part of Microsoft's cloud that decides who is allowed to log in to what. Many organisations use it to manage the accounts of their staff and to control which apps those staff can open.
A premium feature of Azure AD is called Conditional Access policies, or CAPs for short. A CAP is a rule such as "everyone must prove who they are with a second step (a code on their phone) before they can read email". Organisations rely on these rules to keep attackers out.
In May 2022, researchers at Secureworks looked at the different ways a CAP can be changed by software rather than by a person clicking in a web page. They found three ways. One of them, an older interface called Azure AD Graph (AADGraph), turned out to be too permissive in two ways:
- It let an administrator change not only the rule itself but also the small pieces of information that describe the rule, such as when it was created and when it was last changed. Those pieces are called metadata.
- Changes made through it did not produce the usual entry in the log that records who changed which policy. The log is the record organisations use to investigate incidents afterwards.
On top of that, the researchers found that any user in the organisation, even one with no special rights, could read the list of CAPs through the same old interface by asking for a particular version of it.
The researchers told Microsoft. Microsoft first said the behaviour was expected. Later, in May 2023, Microsoft said it would improve the logs and stop administrators from changing CAPs through AADGraph, and that AADGraph itself is being retired.
This article walks through the whole story slowly, explaining each technical idea as it appears.
Background: what Azure AD and CAPs are
Azure AD is Microsoft's cloud-based identity and access management service. In everyday terms, it is the doorman of a building. It checks who you are (authentication) and what you are allowed to do (authorisation). It supports several authentication methods, which are simply the different ways you can prove who you are: a password, a code on your phone, a fingerprint, and so on.
The premium version of Azure AD also supports Conditional Access policies. A CAP is a rule that grants or blocks access based on defined criteria, such as whether the device is compliant (for example, patched and managed by the company) or where the user is located. Think of a CAP as a rule pinned to the doorman's desk: "let this person in only if they are on the company laptop and inside the country".
CAPs can also be used for session monitoring and for limiting how long a session lasts, so that someone who logged in in the morning is asked to log in again in the afternoon.
CAPs are enforced during the Azure AD authentication process. That means the rule is checked at the moment someone tries to log in, before any access is granted.
Azure AD uses the following common signals to make a policy decision:
- User or group membership: who the person is, or which team they belong to.
- IP location information: roughly where the connection comes from.
- Device: what kind of computer or phone is being used, and whether it is known and healthy.
- Application: which app the person is trying to open.
Only users with specific roles can access CAPs in the Azure AD portal. A role is a named bundle of permissions. The portal is the website administrators use to manage Azure AD.
| Access type | Azure AD role |
|---|---|
| Read | Global Administrator, Global Reader, Security Administrator, Conditional Access Administrator, Security Reader |
| Modify | Global Administrator, Security Administrator, Conditional Access Administrator |
Table 1. Azure AD roles required to access CAPs.
In plain words: to look at CAPs you need one of five roles; to change them you need one of three. A Global Administrator is the most powerful role in the tenant. A tenant is one organisation's own separate instance of Azure AD.
The report shows an example CAP that requires all users to perform multi-factor authentication (MFA). MFA means logging in with more than one kind of proof, for example a password plus a code. That example policy is not enabled; it is set to Report-only mode. Report-only mode lets the organisation see what would have happened if the rule were switched on, without actually blocking anyone, so they can assess the impact before enforcing it.
The Azure AD portal displays the name, state, and creation and modification timestamps of each policy. A timestamp is simply the date and time something happened. The portal reflects changes whenever the CAP is modified, so what you see on screen should match reality.
Azure AD audit logs capture CAP creation and modification events. An audit log is a diary of actions: who did what, and when. Both the 'Add conditional access policy' and 'Update conditional access policy' events include details of the modified properties. That means the diary does not just say "a policy changed"; it says which settings inside the policy changed. This feature provides a full audit trail, which is the complete story of changes that an investigator can follow afterwards.
Setup: how the researchers approached it
In May 2022, Secureworks Counter Threat Unit (CTU) researchers investigated which APIs allow editing of CAP settings. An API, short for Application Programming Interface, is a way for one piece of software to talk to another. If the portal is a restaurant menu you click on, the API is the waiter who carries your order to the kitchen. Software uses APIs to automate tasks that a person would otherwise do by clicking.
They identified three APIs: the legacy Azure AD Graph (also known as AADGraph), Microsoft Graph, and an undocumented Azure IAM API. "Legacy" means old but still running. "Undocumented" means Microsoft does not publish instructions for it; the researchers found it by watching what the portal itself does.
The researchers used the AADInternals toolkit to tamper with CAPs. A toolkit here is a collection of ready-made scripts that save the researcher from writing everything from scratch. An access token is a short-lived string of characters that proves to the service that you have already logged in. Think of it as a wristband you get at the entrance of an event: you show it once, and afterwards you can walk in and out without queueing again. It is a secret, so it must never be shared.
Finding it: the three APIs compared
AADGraph was the only API that allowed modification of all CAP settings, including the metadata. This capability lets administrators tamper with all CAP settings, including the creation and modification timestamps. Modifications made using AADGraph are not properly logged, endangering integrity and non-repudiation of Azure AD policies.
Two big words there. Integrity means the data is what it claims to be and has not been quietly altered. Non-repudiation means nobody can later deny having done something, because there is a trustworthy record. If a change leaves no proper trace, both properties are broken.
The Azure AD portal is a graphical user interface (GUI): a web page with buttons and forms that administrators use to create and maintain CAPs via a browser. GUIs are good for ad-hoc tasks, one-off jobs done by hand, but they cannot automate work or give programmatic access, which is access driven by code. To address those needs, Microsoft provides three APIs that can interact with CAPs:
- Azure AD IAM
- MS Graph
- Azure AD Graph (AADGraph)
Azure AD IAM API
The Azure AD portal uses an undocumented Azure AD IAM API to create, view, and edit CAPs. In other words, when you click around in the portal, the portal is quietly calling this API behind the scenes. The API is available at:
https://main.iam.ad.ext.azure.com/api/Policies/Policies
Reading that address from left to right: https:// is the secure web protocol; main.iam.ad.ext.azure.com is the server; /api/Policies/Policies is the path that names the collection of policies. Because the Azure AD portal uses Azure AD IAM APIs, access requires the permissions listed in Table 1. The API returns a list of CAPs as a JSON object. JSON is a text format for structured data, using curly braces, names and values; it is how software passes information around.
When the API opens a CAP for editing, it returns the CAP details as a JSON object. This returned JSON object has many fields, which correspond to the CAP settings available in the Azure AD portal. The response also includes creation and modification timestamps.
Modifying a CAP sends a JSON object to the following address as an HTTP POST request:
https://main.iam.ad.ext.azure.com/api/Policies/ConvertPolicyMsGraph
HTTP is the language browsers and servers use to exchange requests and responses. A POST request is one that carries data to the server, like handing in a filled form. In the researchers' example the CAP state was changed from Off to Report-only. Only the modified data and not the metadata is sent to Azure AD. That last point matters: this API cannot touch the timestamps, so it cannot be used to rewrite history.
MS Graph API
MS Graph API support for conditional access is well-documented, and Microsoft also published examples for creating and editing CAPs. The following permissions are required to access CAPs via MS Graph API.
| Access type | Permissions |
|---|---|
| Modify (all three required) | Policy.Read.All, Policy.ReadWrite.ConditionalAccess, Application.Read.All |
| Read | Policy.Read.All |
Table 2. MS Graph API permissions required for CAPs.
A permission name like Policy.Read.All is self-describing: it allows reading all policies. Policy.ReadWrite.ConditionalAccess allows reading and writing conditional access policies. Application.Read.All allows reading all application registrations. To modify, all three are required.
Users or applications with these permissions can list CAPs by calling the API at:
https://graph.microsoft.com/v1.0/identity/conditionalAccess/policies
Here graph.microsoft.com is Microsoft's main Graph server, v1.0 is the version of the API, and the rest of the path points at the conditional access policies. The API returns all CAPs and details as a JSON object.
Creating or modifying a CAP uses the same API endpoint:
- Creating a CAP makes an HTTP POST with a JSON object.
- Modifying a CAP makes an HTTP PATCH request with a JSON object.
PATCH is the HTTP method that means "change part of this existing thing", as opposed to POST, which usually means "create something new". Only the modified data is sent to Azure AD. The metadata is not included. Again, this API cannot rewrite the timestamps.
Azure AD Graph API (AADGraph)
Microsoft has attempted to deprecate the AADGraph API for years. To deprecate means to mark something as old and discourage its use, usually before switching it off. As of the publication, its retirement is scheduled to occur sometime after June 30, 2023. Microsoft has removed public AADGraph API documentation to discourage its use.
CAPs can be accessed using the AADGraph API at:
https://graph.windows.net/<tenant>/policies?api-version=<api version>
Here <tenant> is the Azure AD tenant, the organisation's own instance, and <api version> is the desired AADGraph API version. The part after the question mark, api-version=..., is a parameter: extra information sent with the request. It is like telling a shop assistant which catalogue edition you want to look at.
Using 1.6 as the API version returns some Azure AD policies that the user can access if they have appropriate permissions, but CAPs are not listed. However, using 1.61-internal as the version returns all Azure AD policies, including CAPs, regardless of the user's permissions. As a result, any user of the tenant can list CAPs and bypass the role requirements. Bypass means getting around a control without being entitled to.
The API returns all policies as JSON objects. A CAP policy is indicated by the policyType of 18. In other words, each policy carries a number that says what kind it is, and 18 means conditional access.
The CAP settings and metadata are stored in the policyDetail attribute as a JSON object. An attribute is simply a named field inside a record. Administrators with permissions to modify CAPs can edit this attribute, enabling them to tamper with the CAP conditions and metadata. This is the heart of the finding: the whole rule, including its history, sits in one editable field.
Updating an existing CAP with the AADGraph API involves an HTTP PATCH request to:
https://graph.windows.net/<tenant>/policies/<objectid>?api-version=1.61-internal
where <objectid> is the object ID of the CAP to be modified. An object ID is a unique identifier, like a serial number, so the server knows exactly which policy you mean. The content of the request is a JSON object that only includes the policyDetail attribute.
Exploitation: how the tampering was done
CTU researchers used the AADInternals toolkit to tamper with CAPs. Administrators or threat actors can leverage the AADGraph API to make changes that are not properly logged. A threat actor is anyone trying to do harm; here the point is that the same trick is available both to a careless administrator and to an attacker who has stolen administrator rights.
Step 1: Retrieve the current policyDetail value
- Acquired an access token for an administrator with permissions to modify CAPs. You need the wristband before you can ask for anything.
- Saved the example CAP to a variable. A variable is a named box in a script that holds a value so you can use it later.
- Extracted the policyDetail value and copied the data to the clipboard. The clipboard is the temporary storage behind copy and paste.
The reason for this step is simple: to change a record safely, you first need to see exactly what it currently contains, so you can alter only what you intend to alter.
Step 2: Modify the policyDetail JSON
The policyDetail value was pasted into a text editor and the JSON reformatted for readability. JSON is often delivered as one long line; reformatting adds line breaks and indentation so a human can read it. The researchers then emptied the ModifiedDateTime attribute (line 4) and changed the State attribute from Reporting to Disabled. The modified JSON was flattened and copied to the clipboard. Flattening is the reverse of reformatting: putting it back into the compact form the API expects.
Why do this? Emptying the modification date removes the visible sign that the policy was touched, and switching the state from Reporting to Disabled changes what the policy does. Together they show that both the rule and its history can be rewritten.
Step 3: Update the CAP with the modified policyDetail
The modified policyDetail from the clipboard was used to update the CAP. The Azure AD portal updated the modifications within a minute.
When CAPs are updated via the AADGraph API, the 'Update conditional access policy' event is not generated in the audit logs. As a result, there is an incomplete audit trail on what modifications were made. The diary has a missing page.
Threat actors with administrator permissions can leverage this omission to obscure CAPs. To obscure here means to make something hard to find or understand. For instance, the following PowerShell script removes the timestamps and display names of all CAPs. PowerShell is Microsoft's scripting language for automating tasks on Windows and in Microsoft cloud services.
After running the script, CAPs are fully functional. However, the Azure AD portal cannot open or edit them. Picture a set of rules that still work but whose labels have been peeled off, so the portal no longer knows how to display them.
Administrators can still delete CAPs and make duplicates to view existing CAP settings. If organisations keep audit logs for a longer period of time, they may be able to restore CAP names and timestamps based on historical audit log data. Keeping logs longer is therefore a practical recovery measure.
Root cause: why it worked
AADGraph exposes the full policyDetail attribute, including metadata such as creation and modification timestamps, and accepts updates to it. The other two APIs (Azure AD IAM and MS Graph) only accept the modified policy data and not the metadata. Because AADGraph updates do not generate the 'Update conditional access policy' event, and the corresponding Core Directory service audit log item does not contain the details of the changed properties and values, the audit trail is incomplete. The role requirements enforced by the portal and the other APIs are also bypassed for reads: with api-version=1.61-internal, any user of the tenant can list CAPs regardless of their permissions.
In short: one old door lets you rewrite the label on the box as well as the contents, and it does not ring the bell when opened.
Impact: what it means for an organisation
Administrators can use the AADGraph API to change CAPs. The API does not properly log changes, and the lack of an audit trail breaks integrity and non-repudiation of CAPs. As a result, organisations cannot trust CAP information shown in the Azure AD portal or in directory audit logs. If the record can be edited silently, the record stops being evidence.
In addition, any tenant user can view CAPs without administrator permissions. This ability allows low-privileged threat actors to identify gaps in CAPs or target them for future modification. A low-privileged actor is someone with an ordinary account, not an administrator. Seeing the rules tells them where the weak spots are.
Third-party tools such as ROADTools and TSxAzureADExport exploit this ability. To exploit means to take advantage of a weakness.
Fix: what Microsoft said and what to do
On May 11, 2023, the MSRC informed the CTU research team of planned changes to address these issues. MSRC stands for Microsoft Security Response Center, the team that handles security reports about Microsoft products.
- Improve audit logs to reflect the type of policy being updated when CA policies are updated through AAD Graph.
- Prevent admins from using AAD Graph to make updates to CA policies.
In addition to these improvements, AAD Graph is set to be retired.
CTU researchers recommend that organisations store Azure AD audit logs in the Log Analytics workspace or in other storage solutions such as Secureworks Taegis XDR. Log Analytics is Microsoft's service for storing and querying log data; keeping logs in a durable place means they survive even if someone tampers with the live system.
Organisations can detect CAP modifications via the AADGraph API by monitoring audit logs for an 'Update policy' event that does not have a corresponding 'Update conditional access policy' event within two seconds. The logic: a normal change produces both entries, close together in time. A change that produces only the first is suspicious.
The following script can restore the names and modification dates of CAPs that have been created or modified using the Azure AD portal or the MS Graph API:
# Read legit CAP events from the audit log
$CAPEvents=Get-AADIntAzureAuditLog -Export `
| Where-Object activityDisplayName -in `
"Add conditional access policy", `
"Update conditional access policy" `
| Select-Object "activityDateTime" -ExpandProperty "targetResources" `
| Select-Object "id","displayName","activityDateTime"
# Loop through the events to get the first (latest) update
$CAPInfos=@{}
foreach($CAPEvent in $CAPEvents)
{
if(!$CAPInfos.ContainsKey($CAPEvent.id))
{
$CAPInfos[$CAPEvent.id] = [pscustomobject]@{
"displayName" = $CAPEvent.displayName
"modifiedDateTime" = $CAPEvent.activityDateTime
}
}
}
# Read current CAPs
$CAPs = Get-AADIntConditionalAccessPolicies
# Loop through CAPs
foreach($CAP in $CAPs)
{
# Create the return value
$retVal = [pscustomobject][ordered]@{
"id" = $CAP.objectId
"isEmpty" = [string]::IsNullOrWhiteSpace($CAP.displayName)
"success" = $null
"name" = $CAP.displayName
}
# Check whether the displayName is empty
if($retVal.isEmpty)
{
# Check whether we found the old information
if($CAPInfo = $CAPInfos[$retVal.id])
{
# Get policyDetails and fix Modified Date
$policyDetail = $CAP.policyDetail[0] | ConvertFrom-Json
try
{
$policyDetail.ModifiedDateTime = $CAPInfo.modifiedDateTime
}
catch{}
$newPolicyDetail = $policyDetail | ConvertTo-Json -Depth 10 -Compress
# Replace name with the old displayName
$retVal.name = $CAPInfo.displayName
try
{
Set-AADIntAzureADPolicyDetails -ObjectId $retVal.id `
-PolicyDetail $newPolicyDetail `
-DisplayName $retVal.name `
| Out-Null
$retVal.success = $true
}
catch
{
# Failed
$retVal.success = $false
$retVal.name = $CAP.displayName
}
}
}
# Return
$retVal
}
Line by line, in plain words:
$CAPEvents=Get-AADIntAzureAuditLog -Exportasks the AADInternals toolkit to download the audit log and store it in a variable called$CAPEvents.- The lines starting with
|pass that data through filters.Where-Object activityDisplayName -in ...keeps only the entries whose activity name is either 'Add conditional access policy' or 'Update conditional access policy'. These are the legitimate changes made through the portal or MS Graph. Select-Object "activityDateTime" -ExpandProperty "targetResources"pulls out the time of each event and opens up the list of objects the event affected.- The next
Select-Objectkeeps only the policy id, its display name and the time. $CAPInfos=@{}creates an empty dictionary, a lookup table that will map each policy id to its known name and date.- The
foreachloop goes through the events one by one.if(!$CAPInfos.ContainsKey($CAPEvent.id))means "if we have not already recorded this policy". Because the events arrive newest first, the first one seen is the latest, so that is the one kept. - Inside, it stores a small object with the display name and the modification date taken from the event.
$CAPs = Get-AADIntConditionalAccessPoliciesfetches the current list of policies.- The second
foreachloop goes through each current policy. It builds a result object with the policy id, a flag saying whether the name is empty, a success field set to null, and the current name. [string]::IsNullOrWhiteSpace($CAP.displayName)is true when the name is missing or only spaces, which is the sign of tampering.- If the name is empty, it looks up the saved information for that id. If found, it reads the policyDetail, converts it from JSON into an object, and sets its
ModifiedDateTimeback to the saved date. Thetry/catchmeans "attempt this, and if it fails, carry on". ConvertTo-Json -Depth 10 -Compressturns the object back into compact JSON text.- It then calls
Set-AADIntAzureADPolicyDetailswith the policy id, the new policyDetail and the old display name, and| Out-Nulldiscards the output because the script does not need it. - If that works,
successis set to true; if it throws an error,successis set to false and the name is left as it was. - Finally
$retValis printed, so the operator sees one line per policy and whether it was repaired.
The following KQL query can be used to identify 'Update policy' events that do not have a corresponding 'Update conditional access policy' event within two seconds. KQL is Kusto Query Language, the query language used by Microsoft's log tools.
AuditLogs
| where OperationName == "Update policy"
| mv-expand TargetResources
| where TargetResources.displayName != "Default Policy"
| mv-expand InitiatedBy
| project PolicyName = TargetResources.displayName, PolicyId = tostring(TargetResources.id), UserPrincipalName = InitiatedBy.user.userPrincipalName, UserId = tostring(InitiatedBy.user.id), OperationName, Time = bin(TimeGenerated, 2s), TimeGenerated, CorrelationId
|join kind=leftanti (AuditLogs
| where OperationName == "Update conditional access policy"
| mv-expand TargetResources
| mv-expand InitiatedBy
| project PolicyId = tostring(TargetResources.id), UserId = tostring(InitiatedBy.user.id), Time = bin(TimeGenerated, 2s)) on PolicyId,UserId,Time
| order by TimeGenerated
What each part does:
-
AuditLogsnames the table of log entries. -
| where OperationName == "Update policy"keeps only the generic policy update events. -
| mv-expand TargetResourcesand| mv-expand InitiatedBysplit list-valued fields into one row per item, so each policy and each actor gets its own row. -
| where TargetResources.displayName != "Default Policy"drops the built-in default policy, which is not interesting here. -
| project ...chooses the columns to keep and renames some.bin(TimeGenerated, 2s)rounds each timestamp down to a two-second bucket, so events a second apart can be matched. -
|join kind=leftanti (...)keeps only rows from the first set that have no match in the second set. The second set is the same log filtered to 'Update conditional access policy' events, reduced to policy id, user id and time bucket. So the result is generic updates with no matching conditional access update at the same moment. -
| order by TimeGeneratedsorts the output by time so the newest or oldest are easy to find. -
Query the AADGraph policies endpoint with
api-version=1.61-internalas a low-privileged tenant user. A positive result is a JSON list that includes policies with policyType 18 (CAPs), which the user should not be able to read. -
Compare the audit log after a CAP update made through AADGraph against one made through the portal or MS Graph. A positive result is an 'Update policy' event with no corresponding 'Update conditional access policy' event within two seconds.
-
Inspect the policyDetail attribute of a CAP returned by AADGraph. A positive result is that it contains creation and modification timestamps that can be edited through a PATCH to the policy object.
-
Run the KQL detection query against a tenant's audit logs. A positive result is any row returned, indicating a policy update that bypassed the conditional access audit event.
-
Check whether the portal can still open and edit a CAP after its display name and timestamps have been emptied. A positive result is a policy that remains functional but cannot be opened or edited in the portal.
Glossary
- AADGraph (Azure AD Graph): an older Microsoft API for reading and changing Azure AD data, being retired.
- AADInternals: a toolkit of PowerShell scripts for working with Azure AD, used here to read and change policies.
- Access token: a short-lived secret string that proves you have already logged in, sent with requests instead of a password.
- API (Application Programming Interface): a way for software to talk to software, like a waiter carrying orders to a kitchen.
- Attribute: a named field inside a record, such as
policyDetailordisplayName. - Audit log: a diary of actions recording who did what and when.
- Audit trail: the complete, followable story of changes made to a system.
- Authentication: proving who you are, for example with a password or a code.
- Authorisation: deciding what a proven identity is allowed to do.
- Azure AD (Azure Active Directory): Microsoft's cloud service for managing identities and access.
- Azure AD IAM API: the undocumented API the Azure AD portal itself uses to manage policies.
- CAP (Conditional Access policy): a rule that grants or blocks access based on conditions such as device, location, user or app.
- Clipboard: the temporary storage used by copy and paste.
- Conditional Access Administrator: an Azure AD role that can read and modify CAPs.
- Core Directory service audit log: the general Azure AD log entry for directory changes, as opposed to the conditional-access-specific entry.
- Deprecate: to mark something as old and discourage its use before switching it off.
- Display name: the human-readable label of a policy.
- Elevation of privilege: gaining more access than you are entitled to.
- Exploit: to take advantage of a weakness.
- Flatten: to turn formatted JSON back into a compact single-line form.
- Global Administrator: the most powerful role in an Azure AD tenant.
- Global Reader: a role that can read most settings without changing them.
- GUI (graphical user interface): a screen with buttons and forms, used by people rather than code.
- HTTP: the language browsers and servers use to exchange requests and responses.
- Integrity: the property that data is what it claims to be and has not been quietly altered.
- JSON: a text format for structured data using braces, names and values.
- KQL (Kusto Query Language): the query language used by Microsoft's log tools.
- Log Analytics: Microsoft's service for storing and querying log data.
- Metadata: descriptive information about a record, such as when it was created or last changed.
- MFA (multi-factor authentication): logging in with more than one kind of proof, such as a password plus a code.
- MS Graph (Microsoft Graph): Microsoft's main, documented API for Microsoft cloud data.
- MSRC (Microsoft Security Response Center): the team that handles security reports about Microsoft products.
- Non-repudiation: the property that nobody can credibly deny having done something, because a trustworthy record exists.
- Object ID: a unique identifier, like a serial number, for a record.
- PATCH: an HTTP method meaning "change part of an existing thing".
- Permission: a named allowance to perform an action, such as
Policy.Read.All. - policyDetail: the AADGraph attribute that holds a CAP's settings and metadata as JSON.
- policyType: a number in a policy record saying what kind of policy it is; 18 means conditional access.
- POST: an HTTP method that carries data to the server, usually to create something.
- PowerShell: Microsoft's scripting language for automating tasks.
- Report-only mode: a CAP setting that records what would have happened without enforcing the rule.
- Role: a named bundle of permissions, such as Global Administrator.
- ROADTools: a third-party toolkit that can read Azure AD data, including CAPs.
- Security Administrator: an Azure AD role that can read and modify security-related settings including CAPs.
- Security Reader: an Azure AD role that can read security-related settings without changing them.
- Session: one continuous period of being logged in.
- Tampering: changing data in an unauthorised or misleading way.
- Tenant: one organisation's own separate instance of Azure AD.
- Threat actor: anyone trying to do harm, from an outsider to a malicious insider.
- Timestamp: the date and time something happened.
- TSxAzureADExport: a third-party tool that can export CAPs as an ordinary user.
- Variable: a named box in a script that holds a value for later use.