JACOB PARK
SECURITY / KQL

Failed Windows logons: start with a question, then write the KQL

Investigate failed Windows logons with KQL, a downloadable synthetic dataset, expected results, and an offline Python check.

Start with a question

A failed sign-in is an event. It becomes useful when it helps answer a question. Which source is generating repeated failures? Are the attempts aimed at one account or many? Is this a new pattern, or something the environment does every morning?

A dashboard can make a count look important before any of those questions have been answered. A better starting point is a small, explicit hypothesis: one source is producing enough failed Windows logons in a short period to deserve a closer look.

That is an investigation starting point. It does not establish intent. The example below uses synthetic counts, so the reasoning is easy to follow without implying that a real incident occurred.

Understand the event you are counting

Windows Security Event ID 4625 records a failed logon. Its fields can help explain the attempt, including the account, logon type, failure information, and network details when those details are available. Microsoft's 4625 reference describes the event and its fields.

The query here assumes the relevant events are collected in the SecurityEvent table. Confirm that assumption in your own workspace. Collection settings and connectors matter: an empty result can mean there were no matching events, or that the expected telemetry never arrived.

Define the unit before choosing a threshold: failed logon events, grouped by source address, during the last fifteen minutes. Changing any part of that definition changes what the number means.

Write a small query with an explicit scope

SecurityEvent
| where TimeGenerated between (ago(15m) .. now())
| where EventID == 4625
| where isnotempty(IpAddress) and IpAddress !in ("-", "127.0.0.1", "::1")
| summarize
    FailedAttempts = count(),
    Accounts = make_set(Account, 10),
    Hosts = make_set(Computer, 10)
  by IpAddress
| where FailedAttempts >= 5
| order by FailedAttempts desc

The early filters keep the time range and event type visible. summarize produces a count for each source address. The two sets retain a small sample of associated accounts and computers, so the output is more useful than an isolated number.

Those sets are capped at ten distinct values each. They are context, not a complete inventory. If a source is interesting, inspect its underlying events before drawing conclusions. Keep missing-address events available for a separate check; filtering them out of this source-based query does not make them irrelevant.

Test what the threshold actually changes

Consider the following synthetic fifteen-minute sample. These documentation addresses represent three example sources.

Synthetic failed-logon counts
Source addressFailed attempts
198.51.100.2411
203.0.113.84
192.0.2.411

At a threshold of five, one source is returned. Lower it to three and two sources are returned. Neither setting changes the underlying events. It changes which sources enter the review queue.

That distinction matters when tuning a detection. A smaller threshold may create more investigation work. A larger one may exclude activity worth reviewing. Five is an illustrative value here; choosing a useful value requires knowledge of the environment and a clear idea of what the investigation is trying to find.

Follow the context after the count

For a source that passes the threshold, open the matching events and look at their timing, target accounts, destination hosts, and failure details. A misconfigured service and a deliberate password-guessing attempt can both produce repeated failures. A shared address can also represent more than one device.

Groupings are choices. Counting by address answers a different question from counting by account or by source-and-host pair. Keep the original question in view, and change the grouping when the question changes.

A useful investigation note records the query, time range, what was observed, and what still needs checking. Keep observation separate from interpretation: “eleven matching failures from this address” is an observation; “this was an attack” needs additional evidence.

The Sentinel SOC Lab overview connects this approach to Windows-log investigation. For a different kind of reliability problem, see building a live feed that keeps working when its API does not.

Reproduce the investigation

The downloadable example contains 22 fictional events, a self-contained KQL query, and a Python check of the same grouping. It includes the sixteen failures shown above, two events without a usable source address, a loopback event, an old event, a future event, and a successful logon. These extra rows let you check what the filter leaves out.

Try it locally · Python 3.10+

No packages, accounts, or network requests. Extract the ZIP before running the script.

Download the investigation example ↗Inspect the synthetic CSV ↗Read the complete KQL fixture ↗
python failed_logons.py
python failed_logons.py --threshold 3

The fixture fixes the end of its window at 2026-10-25T12:15:00Z. Running it next month gives the same result. The first command returns eleven failures from 198.51.100.24. The second also returns the four failures from 203.0.113.8.

Expected output at a threshold of five
{
  "window_end": "2026-10-25T12:15:00+00:00",
  "threshold": 5,
  "missing_address_events": 2,
  "loopback_events": 1,
  "findings": [
    {
      "IpAddress": "198.51.100.24",
      "FailedAttempts": 11,
      "Accounts": [
        "LAB\\analyst",
        "LAB\\service"
      ],
      "Hosts": [
        "LAB-PC"
      ]
    }
  ]
}
Investigation flow: 22 synthetic events, a fifteen-minute event filter, then one source above the threshold of five.
The sample keeps excluded events visible as separate checks. Open the diagram for a larger view.

Check the exclusions before interpreting the result

The Python output reports two missing-address events and one loopback event separately. They are outside this source-address investigation, not evidence that nothing happened. The old event, future event, and successful logon also stay outside the count. In a live investigation, missing fields and unexpected timestamps deserve their own check.

To try the KQL version, open investigation.kql in a Kusto-compatible query editor. It declares the sample with datatable, so it needs no event ingestion. Compare the returned source, count, accounts, and host with the expected output; set ordering can differ. The Python check is executable offline, but it is not a Kusto engine.

For collected Windows events, use the SecurityEvent query above and verify what Account and TargetAccount mean in your connector's records. A repeated failure from a service account might come from an expired password. A shared address might represent several machines. The query identifies something to inspect; surrounding evidence determines the explanation.

The Sentinel lab's committed KQL explores a different grouping: source, computer, and five-minute buckets. Keep that distinction in view when comparing results.

References

BACK TO THE NOTEBOOK
IN THIS NOTE
  1. Start with a question
  2. Understand the event you are counting
  3. Write a small query with an explicit scope
  4. Test what the threshold actually changes
  5. Follow the context after the count
  6. Reproduce the investigation
  7. Check the exclusions before interpreting the result
  8. References