Iniciar sesión

and, or, not

10 min · 2 ejercicio(s)

Real questions have more than one condition. KQL combines them with the boolean operators and, or and not. The operators are case insensitive: and and AND are the same.

event.outcome: failure and service.name: ssh
user.name: root or user.name: admin
not host.name: srv-web-01

Precedence

and binds tighter than or. This query

service.name: ssh or service.name: rdp and event.outcome: failure

is read as ssh logons (any outcome) or failed rdp logons. If you mean "failed logons via ssh or rdp", use parentheses:

(service.name: ssh or service.name: rdp) and event.outcome: failure

Analyst habit: When you mix and and or, always add parentheses. Your future self (and your team) will thank you.

Negation

not excludes events. It is how you remove known noise:

event.outcome: failure and not source.ip: 185.220.101.47

🔎 Profundiza: funciones, errores habituales y práctica

How Kibana translates boolean logic

KQL Query DSL (simplified)
a and b bool.filter: [a, b]
a or b bool.should: [a, b], minimum_should_match: 1
not a bool.must_not: [a]

You can see the same translation in the lab: Querynaut compiles your KQL exactly like this before sending it to Elasticsearch. Because and becomes a filter, KQL results have no relevance score. That's why Kibana sorts by time instead.

Negation and missing fields

not service.name: ssh also returns documents that don't have service.name at all. If you only want documents that have the field with a different value, combine it with an existence check:

service.name: * and not service.name: ssh

Readable queries

  • One condition per line is allowed. KQL ignores line breaks, so you can format long queries in a saved search.
  • Prefer and not for exclusions of known noise and document why the exclusion exists (e.g. "vulnerability scanner 10.0.5.9").
  • Exclusions are a security risk too: an attacker who knows your filters can hide behind them. Exclude as narrowly as possible (source.ip and user.name, not just one of them).

Ejercicios prácticos de esta lección

Resuelve estos ejercicios con una consulta en el laboratorio Elasticsearch real. Inicia sesión gratis para acceder al laboratorio, las pistas y la corrección automática.

  1. 1. Failed SSH logons

    How many failed SSH logons hit the server srv-web-01?

  2. 2. Remove the loudest attacker

    One IP is responsible for most failed SSH logons on srv-web-01. Exclude 185.220.101.47 from the previous query. How many failed SSH logons are left (the usual internet background noise)?