Beta

Create a tenant

A new tenant starts with its own users, OAuth settings, audit history and logs. You are its first Tenant Admin.

Identity telemetry and detection queries

After the incident, Quinn's team at Cedar Inc. wrote its detection rules down as queries. This lesson reads four of them, for password spraying, push fatigue, impossible travel and session replay, over the fields an identity provider records, and then looks at how a team tunes and tests queries so that silence means nothing happened rather than a broken rule.

Questions asked of records

After the incident, Quinn's team at Cedar Inc. took stock of its detection rules. Detecting identity attacks described what those rules look for: many accounts failing from one network, a new authenticator minutes after an unusual sign-in, a session used from somewhere it did not start. Each of those is a question, and each question has to be written down precisely enough for a computer to answer it every few minutes over millions of records. That written question is a detection query.

The records it runs over are the identity provider's telemetry: the sign-in events, second steps, method changes and token requests that Recording identity activity said every identity system should keep. Most organizations send them, together with records from mail, endpoints and applications, to a security information and event management system, or SIEM, where analysts write queries in whatever language that product uses. The languages differ. The logic does not, so this lesson writes it in plain SQL, the standard language for asking questions of tables of records, which reads close enough to all of them.

Before any query works, the records need consistent fields. Every identity provider names them differently, so teams map each source onto one shape first. Cedar's looks like this:

The fields Cedar's detection queries rely on
FieldMeaningExample
event_timeWhen the identity provider handled the request, in UTC2026-03-10T09:14:06Z
event_typeWhat was attemptedsign_in, second_step, method_added, token_refresh
outcome, reasonWhether it succeeded, and why notfailure, invalid_credentials
account_idThe account the event concerns, as a stable IDusr_4182
methodHow the person proved themselves at this steppassword, otp, push, passkey
source_network, network_typeThe address the request came from, and what kind of network that address belongs to203.0.113.24, hosting
country, latitude, longitudeThe approximate location, looked up from the addressNL, 52.37, 4.90
device_idThe device or browser that made the requestdev_91c2
session_id, request_idThe session the request belonged to, and the request itselfs-7a91, req_7f3a2c90

Two details decide whether queries can be trusted. Times must come from synchronized clocks and be stored in one zone, or a sequence of events will appear in the wrong order. And a failed sign-in records the account someone typed, which is a claim, not an authenticated person. None of the fields holds a password, a code or a token, so the queries below can answer their questions without ever touching a secret.

Spraying: many accounts, one source

Cedar's Day 0 wave tried one or two passwords on each of hundreds of accounts. Per account, it looked like people mistyping. The question that exposes it is asked per source instead: how many different accounts has each network failed against in the last hour, and how many attempts did each account receive?

SELECT source_network,
       COUNT(DISTINCT account_id)                 AS accounts,
       COUNT(*)                                   AS failures,
       COUNT(*) * 1.0 / COUNT(DISTINCT account_id) AS per_account
FROM   sign_in_events
WHERE  event_type = 'sign_in'
  AND  outcome    = 'failure'
  AND  reason     = 'invalid_credentials'
  AND  event_time >= NOW() - INTERVAL '1 hour'
GROUP  BY source_network
HAVING COUNT(DISTINCT account_id) >= 20
   AND COUNT(*) * 1.0 / COUNT(DISTINCT account_id) <= 3;

Read it from the middle out. FROM names the table of records, WHERE keeps only refused sign-ins with a wrong password in the last hour, and GROUP BY gathers the rows that remain into one group per network. SELECT then reports, for each group, how many different accounts it touched and how many failures it made, and HAVING keeps only the groups that pass its conditions. AS gives each reported value a column name, NOW() - INTERVAL '1 hour' means one hour before the query runs, and multiplying by 1.0 makes the division keep its decimals instead of rounding to a whole number. The two conditions in HAVING describe the shape: many accounts, few attempts each. Brute force against one account fails the first condition, and a person who keeps mistyping their own password fails it too. On Day 0, three networks each returned around seventy accounts at about one attempt per account.

A spray matters most where it worked, so the alert does not stop at failures. A second query looks for a successful sign-in from any network the first query returned, in the same window. At Cedar it found none. At the photo application, during the credential stuffing burst described in the earlier lesson, the same follow-up found 187 accounts that signed in at the first attempt, and those accounts, not the thousands of failures, were what needed a response. Attackers who spread a spray across many networks defeat the per-network count, which is why teams also watch the service-wide failure rate and failures on usernames that do not exist.

Cedar's Day 0 spray

Networks
3
Accounts per network
Around seventy
Per-network query
Returns all 3 networks
Service-wide failure rate
About 210 accounts an hour

The spray is found

The same spray spread out

Networks
Hundreds
Accounts per network
One or two
Per-network query
Returns nothing
Service-wide failure rate
About 210 accounts an hour

Missed by the per-network count

Attackers who spread a spray across many networks defeat the per-network count.

The per-network spraying query on Cedar's Day 0 spray and on the same spray spread over hundreds of networks. Only the service-wide failure rate sees both.

Teams usually label each query with the technique it watches for in MITRE ATT&CK, a public catalog that gives each known attack technique a name and an ID. Password spraying is T1110.003, Brute Force: Password Spraying.

Push fatigue: denials, then an approval

An attacker with someone's password, facing an approval prompt as the second step, can simply ask again and again until the tired or confused owner approves one, the attack Getting around MFA called wearing down approvals. ATT&CK names it T1621, Multi-Factor Authentication Request Generation. A single denied prompt means little, and an approval is ordinary. The pattern is an approval that follows several denials for the same account:

SELECT a.account_id, a.event_time AS approved_at, a.source_network,
       COUNT(d.event_time)          AS denials_before
FROM   sign_in_events a
JOIN   sign_in_events d
  ON   d.account_id = a.account_id
 AND   d.event_type = 'second_step' AND d.method = 'push'
 AND   d.outcome    = 'failure'     AND d.reason = 'push_denied'
 AND   d.event_time BETWEEN a.event_time - INTERVAL '15 minutes' AND a.event_time
WHERE  a.event_type = 'second_step' AND a.method = 'push' AND a.outcome = 'success'
GROUP  BY a.account_id, a.event_time, a.source_network
HAVING COUNT(d.event_time) >= 3;

The table appears twice, as a for the approvals and d for the denials, and the JOIN pairs each successful approval with the denials for the same account in the quarter of an hour before it. BETWEEN keeps the times from the first value to the second, both included. Three denials and then an approval is rare for someone signing in themselves, who usually approves at the first prompt, and typical of someone being worn down. The query also returns the network the approved sign-in came from, because a fatigue attack's approved session usually starts somewhere the owner has never been. Number matching makes this attack much harder, and the query still earns its place: denials without any approval tell the owner that someone else knows their password.

Impossible travel: distance over time

The impossible travel rule compares each successful sign-in with the same account's previous one, and asks whether one person could have covered the distance in the time between them. A window function does the comparison: LAG reads a value from the previous row of the same account, ordered by time, so each sign-in sits next to the one before it:

WITH ordered AS (
  SELECT account_id, event_time, country, latitude, longitude, device_id, network_type,
         LAG(event_time) OVER (PARTITION BY account_id ORDER BY event_time) AS prev_time,
         LAG(latitude)   OVER (PARTITION BY account_id ORDER BY event_time) AS prev_lat,
         LAG(longitude)  OVER (PARTITION BY account_id ORDER BY event_time) AS prev_lon,
         LAG(device_id)  OVER (PARTITION BY account_id ORDER BY event_time) AS prev_device
  FROM   sign_in_events
  WHERE  event_type = 'sign_in' AND outcome = 'success'
)
SELECT account_id, prev_time, event_time, country,
       distance_km(prev_lat, prev_lon, latitude, longitude) AS km
FROM   ordered
WHERE  prev_time IS NOT NULL
  AND  network_type NOT IN ('corporate_vpn', 'mobile_carrier')
  AND  device_id IS DISTINCT FROM prev_device
  AND  distance_km(prev_lat, prev_lon, latitude, longitude)
       / GREATEST(EXTRACT(EPOCH FROM event_time - prev_time) / 3600, 0.1) > 900;

WITH ordered AS (...) names an intermediate result, which the main query then reads like a table. OVER (PARTITION BY account_id ORDER BY event_time) tells LAG to look only at the same account's rows, in time order. IS DISTINCT FROM is a not-equal that also counts a missing value as different. EXTRACT(EPOCH FROM event_time - prev_time) turns the gap between the two sign-ins into seconds, dividing by 3600 makes it hours, and GREATEST(..., 0.1) keeps it at least a tenth of an hour so the division never uses zero. distance_km stands for a function that measures the distance between two points on the globe, and 900 km/h is roughly the speed of an airliner. Most of the query is about what not to report. The locations come from network addresses, so sign-ins through Cedar's VPN exits or a mobile carrier are left out, because their addresses do not say where the person is. Two sign-ins from the same device cannot be two people, however far apart the addresses look. Without those exclusions, the rule fires on every employee who connects to the VPN, which is how impossible travel earned its reputation for noise.

A sign-in from an unexpected country is the use of valid credentials by someone else, T1078 Valid Accounts in ATT&CK, and impossible travel is one of the few ways to see it when the password and second step were correct.

Token and session replay

A replayed session or token, as How sessions and tokens are stolen described, carries the result of a real sign-in, so nothing at the sign-in page looks wrong. What gives it away is continuity. A session is created in one place, and every later request that uses it should come from the same device and, usually, the same kind of network:

SELECT u.session_id, u.account_id,
       c.source_network AS created_from, u.source_network AS used_from,
       c.event_time     AS created_at,   u.event_time     AS used_at,  u.event_type
FROM   sign_in_events c
JOIN   sign_in_events u
  ON   u.session_id     = c.session_id
 AND   u.event_time     > c.event_time
 AND   u.source_network <> c.source_network
WHERE  c.event_type = 'sign_in' AND c.outcome = 'success'
  AND  u.event_time <= c.event_time + INTERVAL '1 hour';

<> means not equal, so the join keeps later uses of the same session from a different address. Run over Cedar's Day 1 records, this returns session s-7a91: created from 203.0.113.24 at 09:14, used from 203.0.113.57 at 09:20 to add an authenticator. Laptops move between networks during a day, so a match is a signal to combine with others, such as a hosting network or a sensitive action, not a verdict. The corresponding techniques are T1539, Steal Web Session Cookie, for the theft and T1550.004, Use Alternate Authentication Material: Web Session Cookie, for the replay.

Refresh tokens leave an even clearer trace. An authorization server that rotates them can recognize a used refresh token coming back, as Limiting what stolen access can do explained, and records it with a reason of its own. A query for that reason needs no correlation at all. Almost every such event means two parties held the same token. The exception is a genuine app retrying after a lost response, which that lesson showed looks the same to the server.

Tuning and testing queries

Every threshold in these queries is a guess until it meets real data. Twenty accounts an hour might be too many for a small company and too few for a consumer service. Cedar runs each new query over the last month of records before it raises alerts, and reads what it would have found. Findings that turn out to be harmless become exclusions, and each exclusion gets an owner and a review date, because the VPN range excluded today may belong to someone else next year.

A query that never fires is not proof that nothing happened. It may be broken. A renamed field, a source that stopped sending records, or a changed reason code silences a rule without any error. Cedar therefore tests detection: within an agreed scope, it plants each pattern in a test account on a schedule, a few wrong passwords across test accounts or three denied prompts and an approval, and confirms that the query finds it and the alert arrives within minutes. Testing identity defenses goes further into tests like these.

Mapping each query to an ATT&CK technique shows what the set of rules covers and what it does not. Laid beside the steps of the Cedar incident, Quinn's queries left two steps unwatched: the 09:40 forwarding rule and the 09:31 Invoice Sync consent. Those became the next queries the team wrote. Investigating an account compromise builds that map step by step.

Check your understanding

Try these questions before moving on. If an answer isn't right, use the feedback and try again.

A query groups yesterday's failed sign-ins by source network. Network A failed on 2 accounts 140 times, network B on 180 accounts 190 times, and network C on 15 accounts 16 times. Which looks most like password spraying? 1 of 4

Choose one answer.

An impossible travel query flags two successful sign-ins an hour apart, from Lisbon and Singapore, made by the same device. What is the most likely explanation? 2 of 4

Choose one answer.

Cedar's push fatigue query, which looks for three denied prompts followed by an approval, has not fired once in four months. What should the team conclude? 3 of 4

Choose one answer.

Cedar's replay query returns session s-7a91, created from 203.0.113.24 at 09:14 and used from 203.0.113.57 at 09:20 to add an authenticator. What does this match establish on its own? 4 of 4

Choose one answer.

Try it in the Lab

We value your privacy

We use cookies and similar technologies to enhance your browsing experience, and analytics to understand our traffic. By clicking "Allow all", you consent to optional analytics. Cookie Policy

Learn identity