// Why this matters
Sentinel detection rules for Entra ID app tampering matter because identity-management automation is a high-value target: whoever can add a credential to an app registration can become that app. If you have followed The App Registration Factory That Cannot Hold a Secret you have shrunk that risk; this is how you notice when someone tries anyway.
Detection without triage is noise. A rule that fires into an inbox nobody watches is a compliance artefact, not a control. The point of the chat loop is that a responder can see the incident, acknowledge it and close it with a reason, from where they already work.
Writing rules as code makes them reviewable, testable and reproducible across workspaces, which is the only way a rule set survives staff changes.
// The problem
Directory audit logs record every credential add, owner change and consent grant, but nothing alerts on them by default. The events that matter are a handful among millions, and the difference between "an engineer rotated a certificate" and "an attacker added a secret" is context, not the event type.
Chat-based triage creates its own attack surface. A button in a message posts back to an endpoint on the internet. If that endpoint trusts the request, anyone who can guess its URL can close incidents, which is exactly what an attacker wants after doing something noisy.
There is also a trust boundary inside the tooling: the Slack payload contains a response_url that your function will call. Calling an attacker-supplied URL from inside your network is a classic server-side request forgery.
// The solution
Rules are scheduled analytics rules defined in Terraform, each mapped to MITRE ATT&CK tactics and techniques and carrying an entity mapping so incidents show who and what. Queries run every five to sixty minutes with a lookback that overlaps slightly, so a delayed log never causes a gap.
The rules fall into groups: credential and owner changes on app registrations, dangerous permission grants (directory-wide application roles), privileged role assignment, redirect URI and federated credential changes, device-code and password-grant sign-ins, and cross-tenant abuse. A separate group watches the automation identity: any non-federated sign-in, any credential added to it, any owner change on apps it manages.
Because the automation identity has no secret by design, "this identity signed in with a client credential" has no benign explanation. That makes the rule near zero false positives, and it is why the invariant in the previous post is worth enforcing rather than merely documenting.
Routing is a Logic App triggered by the Sentinel incident automation. It formats a message, chooses a channel by severity and adds buttons. The button callback goes to a Function that verifies the request in layers and then updates the incident through the Sentinel API using a managed identity. I rejected a shared bot token in rules or code: all auth is managed identity or a Key Vault reference.
- Eventdirectory audit and sign-in logs land in a log workspace
- Detectionscheduled KQL rules mapped to ATT&CK techniques
- Incidentthe SIEM creates an incident with severity
- Routea Logic App posts to a critical or low-priority channel by severity
- Triageresponder clicks acknowledge or close (false positive, true positive, benign)
- Verify gatecallback checked: signed token, HMAC, function key, IP restriction, user authorisation, response-URL allow-list
- Confirmclose actions require confirmation
- Updatea Function updates the incident
// Before you start
- A Log Analytics workspace with Sentinel enabled and Entra ID audit and sign-in logs connected
- Terraform with the azurerm provider (and the azapi provider for newer rule types)
- A chat app with interactive messages and a signing secret stored in a vault
- Azure Functions (Python or Node) and a Logic App consumption or standard plan
- Reader plus Sentinel Responder on the workspace for the function managed identity
// Step-by-step implementation
01 Detect a credential added to an app registration
Start with the highest-value rule: a new secret or certificate added to an application. Query the audit log for the operation, extract who did it and which app, and surface both as entities.
Keep each rule small and explicit. A fifty-line query that tries to cover five attacks is impossible to tune; five short queries are easy to suppress individually.
02 Detect the automation identity signing in without federation
This rule is only valid because the identity has no secret. Any sign-in by that service principal whose authentication method is a client secret or certificate is by definition anomalous.
Pin the rule to the identity by its application ID, which you supply as a Terraform variable so the query is generated, not hand-edited.
03 Deploy rules from Terraform with ATT&CK mapping and entities
Define every rule with one
azurerm_sentinel_alert_rule_scheduledresource, generated from a list so adding a rule is adding a file. Load the query withfile()so the KQL stays in.kqlfiles that editors can highlight and tests can run.Map entities so the incident page shows the actor, the target app and the source address without anyone opening the query. Use
query_frequencyandquery_periodwith a deliberate overlap.04 Route incidents to chat with a Logic App
An automation rule on the workspace runs a playbook (the Logic App) for every incident. The playbook picks the channel from severity, formats the message with title, entities and a link, and adds Acknowledge and Close buttons whose values encode the incident ID and action.
Send high and critical severity to a monitored channel and the rest to a low-priority one. Routing by severity is a staffing decision, not a technical one; make it a variable.
05 Verify the button callback in depth
The function is internet-facing, so assume it is being probed. Check, in order: the timestamp is within five minutes (replay), the HMAC of
v0:timestamp:bodymatches the signature header using a constant-time compare, and the user is on the responder allow-list.Fail closed and tell the channel. Every failure path posts a message, because a silent rejection is indistinguishable from an outage and an unnoticed attack.
06 Make the response URL safe, then update the incident
The payload carries a
response_urlthat the function will POST to. Parse it, require HTTPS, require the hostname to be on an allow-list and refuse redirects. Without this, a forged payload (if everything else failed) turns your function into an SSRF proxy.Update the incident with the function managed identity. Closing needs a classification and a reason, and a close action first replies with a confirmation prompt, so a mis-click cannot close a true positive.
// How to verify it works
- Trigger each rule's test script in a non-production tenant and confirm an incident appears within the rule frequency.
- Run the factory sign-in rule query manually against a window containing only federated sign-ins: it must return zero rows.
- Replay a captured callback with a changed body byte: the function must return 401 and post a failure message.
- Send a callback with a timestamp ten minutes old: it must be rejected as stale.
- Send a payload whose response URL points to a different host: it must be rejected without any outbound request.
- Click Close on a test incident: it must ask for confirmation, then show Closed with the chosen classification in Sentinel.
// Design decisions and trade-offs
- 13 layered controls on the callback path, including SSRF-safe response URL handling.
- Shared managed identity; no stored credentials in rules.
- Every failure path notifies chat.
- The non-federated sign-in rule rests on an invariant: the automation identity has no secrets, so any such sign-in is anomalous.
- Test scripts and test queries ship with the stack.
// Pitfalls and lessons learned
- Audit log latency means a rule with query_period equal to query_frequency will miss events; overlap the window and deduplicate by correlation ID.
- Entity mapping columns must exist in the query output; a renamed column silently produces incidents with empty entities.
- Chat signatures are computed on the raw body. If middleware re-serialises it, every request fails verification.
- A shared function key alone is not authentication; it ends up in logs and screenshots. Layer it with signature checks.
- Following redirects on the response URL defeats the allow-list. Disable them.
- Rules that fire on legitimate platform activity train people to ignore the channel. Suppress by actor, not by rule, and review suppressions on a schedule.
// Frequently asked questions
How do I detect an app credential being added in Entra ID?
Query AuditLogs for the "Add service principal credentials" and application certificate and secret update operations, extract the initiator and the target, and alert on success.
Can Sentinel analytics rules be managed with Terraform?
Yes. azurerm_sentinel_alert_rule_scheduled covers scheduled rules, including tactics, techniques, entity mappings and incident settings.
How do I secure Slack interactive button callbacks?
Verify the signing secret HMAC and timestamp, restrict by user, restrict actions to an allow-list, and treat the response URL as untrusted input.
Why a rule for non-federated sign-ins?
If an identity is designed never to hold a secret, any secret-based sign-in is anomalous, which gives a high-signal, low-noise detection.
Should closing an incident require confirmation?
Yes. Closing is hard to notice if done by mistake, so a second click with a classification is cheap protection.
// The outcome
33 rule files: 12 high, 5 medium, 4 low and 1 informational severity in the dedicated set.
// Where to take it next
- Add a rule for any new federated credential subject on privileged apps, using The App Registration Factory That Cannot Hold a Secret as the baseline.
- Feed incident closures back into rule tuning on a monthly cadence.
- Add automated enrichment (sign-in history, group membership) to the chat message.
- Run the same rule set against a second workspace to prove the Terraform is reproducible.