rex
Extract new fields from text with regex named groups, or rewrite text with a sed-style substitution.
Use rex to extract fields from text with named regular-expression groups. You can also rewrite text with a sed-style substitution; extract is an alias.
Syntax
... | rex [field=<field>] "<regex with (?<name>...) groups>"
... | rex [field=<field>] mode=sed "s/<regex>/<replacement>/[g]"| Argument | Default | Description |
|---|---|---|
field | message | The field to read: an OCSF path or a field computed earlier. |
| regex | A regular expression with one or more named groups, (?<name>...). | |
mode=sed | Substitute instead of extract. Any delimiter works (`s |
max_match= is accepted for compatibility with other SIEM query languages and ignored: rex always takes the first match.
How it works
- Each named group creates a field with that name. If the regex doesn't match, the fields are empty.
- In extraction mode, the source field is unchanged.
- Matching is case-sensitive, unlike search filters. Start the pattern with
(?i)to ignore case. - You can extract named groups from
raw_data. Named groups retain their positions even when the pattern also contains unnamed capture groups. Use non-capturing groups(?:...)for grouping that does not need an output. mode=sedrewrites the source field in place. Later commands can use the rewritten field. To keep the source value and create another field, useevalwithregex_replace_all().
Examples
Pull the payload out of encoded PowerShell command lines:
class_uid=1007 process.name=powershell.exe process.cmd_line="*-enc*" | rex field=process.cmd_line "(?i)-enc(?:odedcommand)?\s+(?<payload>[A-Za-z0-9+/=]{20,})" | table time, device.hostname, payloadSplit a URL into host and path:
class_uid=4002 | rex field=http_request.url.url_string "https?://(?<host>[^/:]+)(?::(?<port>\d+))?(?<path>/[^?]*)?" | stats count by host, pathBrowser family and version from the user agent:
class_uid=4002 | rex field=http_request.user_agent "(?<browser>Firefox|Edg|Chrome|Safari)/(?<version>[0-9.]+)" | stats count by browser, versionMask long Base64 runs in command lines (rewrites process.cmd_line):
class_uid=1007 | rex field=process.cmd_line mode=sed "s/[A-Za-z0-9+\/=]{40,}/<b64>/g"The same rewrite as a field you can group on:
class_uid=1007 | eval cmd=regex_replace_all(process.cmd_line, "[A-Za-z0-9+/=]{40,}", "<b64>") | stats count by cmd