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 sprayingAn attack that tries a few common passwords against many accounts, one or two attempts each, so that no single account sees enough failures to stand out., push fatigue, impossible travel and sessionThe ongoing connection between later requests and an earlier sign-in, so you are not asked for your password on every click. It is often tracked with a cookie holding an unpredictable session identifier. replay, over the fields an identity providerA service that authenticates users and tells other applications about the sign-in, so those applications never handle the users' passwords. In OpenID Connect it is called an OpenID 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 authenticatorSomething a person controls and uses to prove control of an account, such as a memorized password, an app that generates one-time codes, or a security key. It counts only after it has been registered to that account. 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 tokenA piece of data issued by a trusted service that a client presents as a credential, for example to call an API or to report a sign-in. Some tokens carry readable claims; others are random strings that only the issuer can interpret. 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:
| Field | Meaning | Example |
|---|---|---|
event_time | When the identity provider handled the request, in UTC | 2026-03-10T09:14:06Z |
event_type | What was attempted | sign_in, second_step, method_added, token_refresh |
outcome, reason | Whether it succeeded, and why not | failure, invalid_credentials |
account_id | The account the event concerns, as a stable ID | usr_4182 |
method | How the person proved themselves at this step | password, otp, push, passkey |
source_network, network_type | The address the request came from, and what kind of network that address belongs to | 203.0.113.24, hosting |
country, latitude, longitude | The approximate location, looked up from the address | NL, 52.37, 4.90 |
device_id | The device or browser that made the request | dev_91c2 |
session_id, request_id | The session the request belonged to, and the request itself | s-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 stuffingAn attack that takes email and password pairs leaked from one service and tries them at other services, relying on people reusing passwords. 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.
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 AuthenticationSigning in with more than one type of factor, such as a password plus a code from an enrolled authenticator app. Two passwords are still one factor type, however many screens they appear on. 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 CookieA small value that a website asks the browser to store and send back with later matching requests. Cookies commonly carry a session identifier, so the site can recognize an ongoing sign-in without your password., for the theft and T1550.004, Use Alternate AuthenticationChecking evidence that someone, or some application, controls an account, such as a password or a passkey. It establishes which account is being used, not what that account may do. Material: Web Session Cookie, for the replay.
Refresh tokens leave an even clearer trace. An authorization serverIn OAuth, the service that handles a person's sign-in and approval and decides which tokens to issue to a client. It may belong to the same company as the API, but its job is separate. that rotates them can recognize a used refresh tokenA longer-lived credential that a client sends to the authorization server, never to an API, to get new access tokens without asking the person again. 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.
Try it in the Lab
- Record a test spray and a push fatigue pattern, then find them with queriesFail one sign-in on each of three accounts, deny three approval prompts before approving one, export the day's protocol records and find both patterns with jq queries.