Parsing with DISSECT and GROK
14 min · 1 ejercicio(s)
Command lines, URLs and log messages often contain the information you need, but not in a field of its own. ES|QL can extract it at query time.
DISSECT: fixed delimiters
DISSECT splits a string with a pattern of literal text and %{name} keys:
FROM lab-web
| WHERE url.query IS NOT NULL
| DISSECT url.query "%{param}=%{value}"
| STATS requests = COUNT(*) BY paramGROK: regular expressions
GROK uses named regular-expression patterns, written as %{PATTERN:name}. Common patterns are WORD, NUMBER, IP, DATA (anything, lazy) and GREEDYDATA (anything, greedy):
FROM lab-endpoint
| WHERE process.name == "PsExec64.exe"
| GROK process.command_line """\\\\%{DATA:target} %{GREEDYDATA:rest}"""
| KEEP @timestamp, host.name, target, restThe command line contains \\srv-file-02. In a regular expression a literal backslash is written \\, so two backslashes become \\\\. Triple quotes """…""" keep the pattern readable, because inside them backslashes and double quotes need no extra string escaping.
Persistence via scheduled tasks
schtasks.exe /create … /tn "Name" /tr "Program" creates a scheduled task, a very common persistence technique (MITRE ATT&CK T1053.005). The name after /tn is what you would look for on the host.
🔎 Profundiza: funciones, errores habituales y práctica
DISSECT or GROK?
| DISSECT | GROK | |
|---|---|---|
| Pattern | literal delimiters | regular expressions |
| Speed | faster | slower |
| Use when | the format is fixed (CSV-like logs, key=value) | the format varies, optional parts |
| Types | everything is a string (convert with TO_*) |
%{NUMBER:n:int} converts directly |
DISSECT features
%{+name}appends to an existing key,%{?skip}parses but drops a part.APPEND_SEPARATOR=" "controls how appended parts are joined.
Common GROK patterns
| Pattern | Matches |
|---|---|
%{WORD} |
letters, digits, underscore |
%{NUMBER} / %{INT} |
numbers |
%{IP} |
IPv4 or IPv6 |
%{DATA} |
anything, as little as possible |
%{GREEDYDATA} |
anything, as much as possible |
%{QUOTEDSTRING} |
text in quotes |
Query time vs. ingest time
Parsing at query time is flexible but costs CPU on every query. If you need a field regularly (dashboards, rules), parse it once at ingest with the dissect/grok processor in an ingest pipeline, or define a runtime field. ES|QL parsing is perfect for exploration and one-off hunts.