ThreatVIS PPL basics
ThreatVIS PPL is the query language of ThreatVIS Unified Security Operations Platform. You use it in Search, in the Case queue, and wherever the console asks for a query, such as dashboard widgets, reports and query-based Detection Rules. A query is a pipeline: a source, then stages separated by the pipe character (|). Each stage takes the rows the previous stage produced and returns fewer, different or summarised rows.
This page explains how a query is built. For queries to copy, see ThreatVIS PPL examples. For a list of every command and function, see the Reference section.
Role needed: Search: Read to run a query in Search.
The shape of a query
source=log_* | where event.outcome = 'failure' | stats count() as failures by user.name | sort - failures | head 10
| Part | Meaning |
|---|---|
source=log_* | Where the rows come from. Every query starts with a source or a saved Filter Query. |
where event.outcome = 'failure' | Keep only the rows that match the condition. |
stats count() as failures by user.name | Group the rows by user and count each group as failures. |
sort - failures | Order the groups, largest first. A - before the field means descending. |
head 10 | Keep the first ten rows. |
Stages run from left to right. Put the stages that remove the most rows first, because each stage then has less to do.
Sources
| Source | Rows |
|---|---|
log_* | Events from your data sources. |
alerts | Alerts raised by Detection Rules. |
cases | Cases. The Case queue uses this source; a query over it needs Cases: Read. |
lookup_ followed by a file name | The rows of a lookup file. See Use lookups in a search. |
You can name more than one source, separated by commas, for example source=log_*,alerts.
An account that is not an Admin or Super Admin must start every query with source= and the source names, in lower case, with nothing before the first pipe. It can combine events and alerts, but not other kinds of source in one query. Queries that read another source with join, append or a subsearch, or that use lookup on anything other than a lookup file, are refused with a reason. source=cases needs Cases: Read, and the ueba_* sources need UEBA: Read.
Shortcuts in Search
Search completes a query for you so that short input works.
| You type | Search runs |
|---|---|
| Nothing | source=log_*,alerts, which lists events and alerts together. |
A stage that starts with a command, such as where user.name = 'jdoe' | The same stage after source=log_*,alerts. |
A plain word or phrase, such as password | A search of every field for that text, over log_*,alerts. |
A query that starts with source= | Exactly what you typed. |
Write source= yourself when you want a different source, or events without alerts (source=log_*).
The time range
You never write the time condition. The time range picker adds it for you, right after the source, using the time column you chose. See Choose a time range and time zone. A lookup file has no time column, so the range is not applied to it.
Filter rows with where
where keeps the rows for which a condition is true.
| Need | Write |
|---|---|
| Equals, not equals | host.hostname = 'ws-01', host.hostname != 'ws-01' |
| Greater, less | destination.port > 1023 |
| Text contains | like(message, '%password%'). The % matches any text. |
| Field has a value, or has none | isnotnull(user.name), isnull(user.name) |
| Combine conditions | and, or, not, with brackets to group: (a = 1 or b = 2) and c = 3 |
| Match a phrase in a field | match_phrase(message, 'failed logon') |
| Search every field | query_string(["*"], 'password') |
Write text values in single quotes. To put a single quote inside a value, write it twice: 'O''Brien'. Field names that contain characters other than letters, digits, underscore and dot, such as @timestamp, go in backticks: `@timestamp`. Search puts these in for you when you filter from a value on screen.
You can use $currentUser() in a query. It stands for your own user, and ThreatVIS USOP replaces it each time the query runs.
Choose and rename fields
| Command | What it does | Example |
|---|---|---|
fields | Keeps only the named fields. | fields host.hostname, user.name |
rename | Gives a field another name. | rename user.name as account |
eval | Adds a field computed from others. | eval user_lower = lower(user.name) |
dedup | Keeps one row for each value of a field. | dedup host.hostname |
A fields stage removes every field you do not name, including the time column. In Search, name the fields you need in the stage and the event list shows them.
Sort and limit
sort orders rows. Write sort field for ascending order and sort - field for descending order. head keeps the first rows: head 50. To skip rows, write head 50 from 100.
A query that ends with head is run exactly as written and returns at most that many rows, for example | sort host.hostname | head 20.
Summarise with aggregations
An aggregation turns many events into a few rows. A query that contains stats, timechart or top is an aggregating query. Search shows its result in a table and offers charts for it. See Visualise results.
| Command | What it does | Example |
|---|---|---|
stats | Computes values, optionally for each group. | stats count() by host.hostname |
top | The most common values of a field. | top 10 source.ip |
timechart | Values over time, in buckets of a span you choose. | timechart span=1h count() |
Functions you can use in stats include count(), sum(field), avg(field), min(field), max(field) and dc(field), the count of distinct values. Name a result with as: stats count() as events by host.hostname. A name makes later stages and charts easier to read.
Spans for timechart are written as a number and a unit, such as 5m, 1h or 1d.
Combine with other data
lookup adds columns from a lookup file to each event. See Use lookups in a search.
Use a saved Filter Query
A Filter Query is a PPL filter that someone saved from Search. Type $filter( at the start of a query to pick one from the list, or write its name: $filter(Failed_logons) or, when the name has spaces, $filter("Failed logons"). It must be the first stage. You can add stages after it with a pipe when the Filter Query lists events: $filter("Failed logons") | stats count() by user.name. To save a query, see Save, share and export a search.
Get help while you type
Suggestions appear as you type, and Ctrl Space shows them on request. They offer sources, commands, fields from your data, field values in where conditions, saved Filter Queries after $filter(, and lookup files after source= and lookup. PPL help, in the footer of the editor, shows a short reference. Prettify query lays the query out one stage per line.
Next steps
- ThreatVIS PPL examples: queries for common investigations.
- Search events: run and read a query.
- Visualise results: chart an aggregation.