syntax.asciidoc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[eql-syntax]]
  4. == EQL syntax reference
  5. ++++
  6. <titleabbrev>Syntax reference</titleabbrev>
  7. ++++
  8. beta::[]
  9. [discrete]
  10. [[eql-basic-syntax]]
  11. === Basic syntax
  12. EQL queries require an event category and a matching condition. The `where`
  13. keyword connects them.
  14. [source,eql]
  15. ----
  16. event_category where condition
  17. ----
  18. For example, the following EQL query matches `process` events with a
  19. `process.name` field value of `svchost.exe`:
  20. [source,eql]
  21. ----
  22. process where process.name == "svchost.exe"
  23. ----
  24. [discrete]
  25. [[eql-syntax-event-categories]]
  26. ==== Event categories
  27. An event category is a valid, indexed value of the
  28. <<eql-required-fields,event category field>>. You can set the event category
  29. field using the `event_category_field` parameter of the EQL search API.
  30. [discrete]
  31. [[eql-syntax-match-any-event-category]]
  32. ===== Match any event category
  33. To match events of any category, use the `any` keyword. You can also use the
  34. `any` keyword to search for documents without a event category field.
  35. For example, the following EQL query matches any documents with a
  36. `network.protocol` field value of `http`:
  37. [source,eql]
  38. ----
  39. any where network.protocol == "http"
  40. ----
  41. [discrete]
  42. [[eql-syntax-escape-an-event-category]]
  43. ===== Escape an event category
  44. Use enclosing double quotes (`"`) or three enclosing double quotes (`"""`) to
  45. escape event categories that:
  46. * Contain a special character, such as a hyphen (`-`) or dot (`.`)
  47. * Contain a space
  48. * Start with a numeral
  49. [source,eql]
  50. ----
  51. ".my.event.category"
  52. "my-event-category"
  53. "my event category"
  54. "6eventcategory"
  55. """.my.event.category"""
  56. """my-event-category"""
  57. """my event category"""
  58. """6eventcategory"""
  59. ----
  60. [discrete]
  61. [[eql-syntax-escape-a-field-name]]
  62. ===== Escape a field name
  63. Use enclosing enclosing backticks (+++`+++) to escape field names that:
  64. * Contain a hyphen (`-`)
  65. * Contain a space
  66. * Start with a numeral
  67. [source,eql]
  68. ----
  69. `my-field`
  70. `my field`
  71. `6myfield`
  72. ----
  73. Use double backticks (+++``+++) to escape any backticks (+++`+++) in the field
  74. name.
  75. [source,eql]
  76. ----
  77. my`field -> `my``field`
  78. ----
  79. [discrete]
  80. [[eql-syntax-conditions]]
  81. ==== Conditions
  82. A condition consists of one or more criteria an event must match.
  83. You can specify and combine these criteria using the following operators. Most
  84. EQL operators are case-sensitive by default.
  85. [discrete]
  86. [[eql-syntax-comparison-operators]]
  87. ===== Comparison operators
  88. [source,eql]
  89. ----
  90. < <= == : != >= >
  91. ----
  92. `<` (less than)::
  93. Returns `true` if the value to the left of the operator is less than the value
  94. to the right. Otherwise returns `false`.
  95. `<=` (less than or equal) ::
  96. Returns `true` if the value to the left of the operator is less than or equal to
  97. the value to the right. Otherwise returns `false`.
  98. `==` (equal, case-sensitive)::
  99. Returns `true` if the values to the left and right of the operator are equal.
  100. Otherwise returns `false`. For strings, matching is case-sensitive.
  101. `:` (equal, case-insensitive)::
  102. Returns `true` if strings to the left and right of the operator are equal.
  103. Otherwise returns `false`. Matching is case-insensitive and can only be used to
  104. compare strings.
  105. [IMPORTANT]
  106. ====
  107. Avoid using the `==` or `:` operators to perform exact matching on
  108. <<text,`text`>> field values.
  109. By default, {es} changes the values of `text` fields as part of <<analysis,
  110. analysis>>. This can make finding exact matches for `text` field values
  111. difficult.
  112. To search `text` fields, consider using a <<eql-search-filter-query-dsl,query
  113. DSL filter>> that contains a <<query-dsl-match-query,`match`>> query.
  114. ====
  115. `!=` (not equal, case-sensitive)::
  116. Returns `true` if the values to the left and right of the operator are not
  117. equal. Otherwise returns `false`. For strings, matching is case-sensitive.
  118. `>=` (greater than or equal) ::
  119. Returns `true` if the value to the left of the operator is greater than or equal
  120. to the value to the right. Otherwise returns `false`. When comparing strings,
  121. the operator uses a case-sensitive lexicographic order.
  122. `>` (greater than)::
  123. Returns `true` if the value to the left of the operator is greater than the
  124. value to the right. Otherwise returns `false`. When comparing strings,
  125. the operator uses a case-sensitive lexicographic order.
  126. NOTE: `=` is not supported as an equal operator. Use `==` or `:` instead.
  127. You cannot chain comparison operators. Instead, use a
  128. <<eql-syntax-logical-operators,logical operator>> between comparisons. For
  129. example, `foo < bar <= baz` is not supported. However, you can rewrite the
  130. expression as `foo < bar and bar <= baz`, which is supported.
  131. You also cannot use comparison operators to compare a field to another field.
  132. This applies even if the fields are changed using a <<eql-functions,function>>.
  133. *Example* +
  134. The following EQL query compares the `process.parent_name` field
  135. value to a static value, `foo`. This comparison is supported.
  136. However, the query also compares the `process.parent.name` field value to the
  137. `process.name` field. This comparison is not supported and will return an
  138. error for the entire query.
  139. [source,eql]
  140. ----
  141. process where process.parent.name == "foo" and process.parent.name == process.name
  142. ----
  143. Instead, you can rewrite the query to compare both the `process.parent.name`
  144. and `process.name` fields to static values.
  145. [source,eql]
  146. ----
  147. process where process.parent.name == "foo" and process.name == "foo"
  148. ----
  149. [discrete]
  150. [[eql-syntax-logical-operators]]
  151. ===== Logical operators
  152. [source,eql]
  153. ----
  154. and or not
  155. ----
  156. `and`::
  157. Returns `true` only if the condition to the left and right _both_ return `true`.
  158. Otherwise returns `false.
  159. `or`::
  160. Returns `true` if one of the conditions to the left or right `true`.
  161. Otherwise returns `false.
  162. `not`::
  163. Returns `true` if the condition to the right is `false`.
  164. [discrete]
  165. [[eql-syntax-lookup-operators]]
  166. ===== Lookup operators
  167. [source,eql]
  168. ----
  169. user.name in ("Administrator", "SYSTEM", "NETWORK SERVICE")
  170. user.name not in ("Administrator", "SYSTEM", "NETWORK SERVICE")
  171. ----
  172. `in` (case-sensitive)::
  173. Returns `true` if the value is contained in the provided list. For strings,
  174. matching is case-sensitive.
  175. `not in` (case-sensitive)::
  176. Returns `true` if the value is not contained in the provided list. For strings,
  177. matching is case-sensitive.
  178. [discrete]
  179. [[eql-syntax-math-operators]]
  180. ===== Math operators
  181. [source,eql]
  182. ----
  183. + - * / %
  184. ----
  185. `+` (add)::
  186. Adds the values to the left and right of the operator.
  187. `-` (subtract)::
  188. Subtracts the value to the right of the operator from the value to the left.
  189. `*` (multiply)::
  190. Multiplies the values to the left and right of the operator.
  191. `/` (divide)::
  192. Divides the value to the left of the operator by the value to the right.
  193. +
  194. [[eql-divide-operator-float-rounding]]
  195. [WARNING]
  196. ====
  197. If both the dividend and divisor are integers, the divide (`\`) operation
  198. _rounds down_ any returned floating point numbers to the nearest integer. To
  199. avoid rounding, convert either the dividend or divisor to a float.
  200. *Example* +
  201. The `process.args_count` field is a <<number,`long`>> integer field containing a
  202. count of process arguments.
  203. A user might expect the following EQL query to only match events with a
  204. `process.args_count` value of `4`.
  205. [source,eql]
  206. ----
  207. process where ( 4 / process.args_count ) == 1
  208. ----
  209. However, the EQL query matches events with a `process.args_count` value of `3`
  210. or `4`.
  211. For events with a `process.args_count` value of `3`, the divide operation
  212. returns a float of `1.333...`, which is rounded down to `1`.
  213. To match only events with a `process.args_count` value of `4`, convert
  214. either the dividend or divisor to a float.
  215. The following EQL query changes the integer `4` to the equivalent float `4.0`.
  216. [source,eql]
  217. ----
  218. process where ( 4.0 / process.args_count ) == 1
  219. ----
  220. ====
  221. `%` (modulo)::
  222. Divides the value to the left of the operator by the value to the right. Returns only the remainder.
  223. [discrete]
  224. [[eql-syntax-match-any-condition]]
  225. ==== Match any condition
  226. To match events solely on event category, use the `where true` condition.
  227. For example, the following EQL query matches any `file` events:
  228. [source,eql]
  229. ----
  230. file where true
  231. ----
  232. To match any event, you can combine the `any` keyword with the `where true`
  233. condition:
  234. [source,eql]
  235. ----
  236. any where true
  237. ----
  238. [discrete]
  239. [[eql-syntax-strings]]
  240. ==== Strings
  241. Strings are enclosed in double quotes (`"`).
  242. [source,eql]
  243. ----
  244. "hello world"
  245. ----
  246. Strings enclosed in single quotes (`'`) are not supported.
  247. [discrete]
  248. [[eql-syntax-escape-characters]]
  249. ===== Escape characters in a string
  250. When used within a string, special characters, such as a carriage return or
  251. double quote (`"`), must be escaped with a preceding backslash (`\`).
  252. [source,eql]
  253. ----
  254. "example \r of \" escaped \n characters"
  255. ----
  256. [options="header"]
  257. |====
  258. | Escape sequence | Literal character
  259. |`\n` | A newline (linefeed) character
  260. |`\r` | A carriage return character
  261. |`\t` | A tab character
  262. |`\\` | A backslash (`\`) character
  263. |`\"` | A double quote (`"`) character
  264. |====
  265. IMPORTANT: The single quote (`'`) character is reserved for future use. You
  266. cannot use an escaped single quote (`\'`) for literal strings. Use an escaped
  267. double quote (`\"`) instead.
  268. [discrete]
  269. [[eql-syntax-raw-strings]]
  270. ===== Raw strings
  271. Raw strings treat special characters, such as backslashes (`\`), as literal
  272. characters. Raw strings are enclosed in three double quotes (`"""`).
  273. [source,eql]
  274. ----
  275. """Raw string with a literal double quote " and blackslash \ included"""
  276. ----
  277. A raw string cannot contain three consecutive double quotes (`"""`). Instead,
  278. use a regular string with the `\"` escape sequence.
  279. [source,eql]
  280. ----
  281. "String containing \"\"\" three double quotes"
  282. ----
  283. [discrete]
  284. [[eql-sequences]]
  285. === Sequences
  286. You can use EQL sequences to describe and match an ordered series of events.
  287. Each item in a sequence is an event category and event condition,
  288. surrounded by square brackets (`[ ]`). Events are listed in ascending
  289. chronological order, with the most recent event listed last.
  290. [source,eql]
  291. ----
  292. sequence
  293. [ event_category_1 where condition_1 ]
  294. [ event_category_2 where condition_2 ]
  295. ...
  296. ----
  297. *Example* +
  298. The following EQL sequence query matches this series of ordered events:
  299. . Start with an event with:
  300. +
  301. --
  302. * An event category of `file`
  303. * A `file.extension` of `exe`
  304. --
  305. . Followed by an event with an event category of `process`
  306. [source,eql]
  307. ----
  308. sequence
  309. [ file where file.extension == "exe" ]
  310. [ process where true ]
  311. ----
  312. [discrete]
  313. [[eql-with-maxspan-keywords]]
  314. ==== `with maxspan` keywords
  315. You can use the `with maxspan` keywords to constrain a sequence to a specified
  316. timespan. All events in a matching sequence must occur within this duration,
  317. starting at the first event's timestamp.
  318. The `maxspan` keyword accepts <<time-units,time value>> arguments.
  319. [source,eql]
  320. ----
  321. sequence with maxspan=30s
  322. [ event_category_1 where condition_1 ] by field_baz
  323. [ event_category_2 where condition_2 ] by field_bar
  324. ...
  325. ----
  326. *Example* +
  327. The following sequence query uses a `maxspan` value of `15m` (15 minutes).
  328. Events in a matching sequence must occur within 15 minutes of the first event's
  329. timestamp.
  330. [source,eql]
  331. ----
  332. sequence with maxspan=15m
  333. [ file where file.extension == "exe" ]
  334. [ process where true ]
  335. ----
  336. [discrete]
  337. [[eql-by-keyword]]
  338. ==== `by` keyword
  339. You can use the `by` keyword with sequences to only match events that share the
  340. same field values. If a field value should be shared across all events, you
  341. can use `sequence by`.
  342. [source,eql]
  343. ----
  344. sequence by field_foo
  345. [ event_category_1 where condition_1 ] by field_baz
  346. [ event_category_2 where condition_2 ] by field_bar
  347. ...
  348. ----
  349. *Example* +
  350. The following sequence query uses the `by` keyword to constrain matching events
  351. to:
  352. * Events with the same `user.name` value
  353. * `file` events with a `file.path` value equal to the following `process`
  354. event's `process.path` value.
  355. [source,eql]
  356. ----
  357. sequence
  358. [ file where file.extension == "exe" ] by user.name, file.path
  359. [ process where true ] by user.name, process.path
  360. ----
  361. Because the `user.name` field is shared across all events in the sequence, it
  362. can be included using `sequence by`. The following sequence is equivalent to the
  363. prior one.
  364. [source,eql]
  365. ----
  366. sequence by user.name
  367. [ file where file.extension == "exe" ] by file.path
  368. [ process where true ] by process.path
  369. ----
  370. You can combine the `sequence by` and `with maxspan` keywords to constrain a
  371. sequence by both field values and a timespan.
  372. [source,eql]
  373. ----
  374. sequence by field_foo with maxspan=30s
  375. [ event_category_1 where condition_1 ] by field_baz
  376. [ event_category_2 where condition_2 ] by field_bar
  377. ...
  378. ----
  379. *Example* +
  380. The following sequence query uses the `sequence by` keyword and `with maxspan`
  381. keywords to match only a sequence of events that:
  382. * Share the same `user.name` field values
  383. * Occur within `15m` (15 minutes) of the first matching event
  384. [source,eql]
  385. ----
  386. sequence by user.name with maxspan=15m
  387. [ file where file.extension == "exe" ] by file.path
  388. [ process where true ] by process.path
  389. ----
  390. [discrete]
  391. [[eql-until-keyword]]
  392. ==== `until` keyword
  393. You can use the `until` keyword to specify an expiration event for a sequence.
  394. If this expiration event occurs _between_ matching events in a sequence, the
  395. sequence expires and is not considered a match. If the expiration event occurs
  396. _after_ matching events in a sequence, the sequence is still considered a
  397. match. The expiration event is not included in the results.
  398. [source,eql]
  399. ----
  400. sequence
  401. [ event_category_1 where condition_1 ]
  402. [ event_category_2 where condition_2 ]
  403. ...
  404. until [ event_category_3 where condition_3 ]
  405. ----
  406. *Example* +
  407. A dataset contains the following event sequences, grouped by shared IDs:
  408. [source,txt]
  409. ----
  410. A, B
  411. A, B, C
  412. A, C, B
  413. ----
  414. The following EQL query searches the dataset for sequences containing
  415. event `A` followed by event `B`. Event `C` is used as an expiration event.
  416. [source,eql]
  417. ----
  418. sequence by ID
  419. A
  420. B
  421. until C
  422. ----
  423. The query matches sequences `A, B` and `A, B, C` but not `A, C, B`.
  424. [TIP]
  425. ====
  426. The `until` keyword can be useful when searching for process sequences in
  427. Windows event logs.
  428. In Windows, a process ID (PID) is unique only while a process is running. After
  429. a process terminates, its PID can be reused.
  430. You can search for a sequence of events with the same PID value using the `by`
  431. and `sequence by` keywords.
  432. *Example* +
  433. The following EQL query uses the `sequence by` keyword to match a
  434. sequence of events that share the same `process.pid` value.
  435. [source,eql]
  436. ----
  437. sequence by process.pid
  438. [ process where event.type == "start" and process.name == "cmd.exe" ]
  439. [ process where file.extension == "exe" ]
  440. ----
  441. However, due to PID reuse, this can result in a matching sequence that
  442. contains events across unrelated processes. To prevent false positives, you can
  443. use the `until` keyword to end matching sequences before a process termination
  444. event.
  445. The following EQL query uses the `until` keyword to end sequences before
  446. `process` events with an `event.type` of `stop`. These events indicate a process
  447. has been terminated.
  448. [source,eql]
  449. ----
  450. sequence by process.pid
  451. [ process where event.type == "start" and process.name == "cmd.exe" ]
  452. [ process where file.extension == "exe" ]
  453. until [ process where event.type == "stop" ]
  454. ----
  455. ====
  456. [discrete]
  457. [[eql-functions]]
  458. === Functions
  459. You can use EQL functions to convert data types, perform math, manipulate
  460. strings, and more. Most functions are case-sensitive by default.
  461. For a list of supported functions, see <<eql-function-ref>>.
  462. [TIP]
  463. ====
  464. Using functions in EQL queries can result in slower search speeds. If you
  465. often use functions to transform indexed data, you can speed up search by making
  466. these changes during indexing instead. However, that often means slower index
  467. speeds.
  468. *Example* +
  469. An index contains the `file.path` field. `file.path` contains the full path to a
  470. file, including the file extension.
  471. When running EQL searches, users often use the `endsWith` function with the
  472. `file.path` field to match file extensions:
  473. [source,eql]
  474. ----
  475. file where endsWith(file.path,".exe") or endsWith(file.path,".dll")
  476. ----
  477. While this works, it can be repetitive to write and can slow search speeds. To
  478. speed up search, you can do the following instead:
  479. . <<indices-put-mapping,Add a new field>>, `file.extension`, to the index. The
  480. `file.extension` field will contain only the file extension from the
  481. `file.path` field.
  482. . Use an <<ingest,ingest pipeline>> containing the <<grok-processor,`grok`>>
  483. processor or another preprocessor tool to extract the file extension from the
  484. `file.path` field before indexing.
  485. . Index the extracted file extension to the `file.extension` field.
  486. These changes may slow indexing but allow for faster searches. Users
  487. can use the `file.extension` field instead of multiple `endsWith` function
  488. calls:
  489. [source,eql]
  490. ----
  491. file where file.extension in ("exe", "dll")
  492. ----
  493. We recommend testing and benchmarking any indexing changes before deploying them
  494. in production. See <<tune-for-indexing-speed>> and <<tune-for-search-speed>>.
  495. ====
  496. [discrete]
  497. [[eql-pipes]]
  498. === Pipes
  499. EQL pipes filter, aggregate, and post-process events returned by
  500. an EQL query. You can use pipes to narrow down EQL query results or make them
  501. more specific.
  502. Pipes are delimited using the pipe (`|`) character.
  503. [source,eql]
  504. ----
  505. event_category where condition | pipe
  506. ----
  507. *Example* +
  508. The following EQL query uses the `tail` pipe to return only the 10 most recent
  509. events matching the query.
  510. [source,eql]
  511. ----
  512. authentication where agent.id == 4624
  513. | tail 10
  514. ----
  515. You can pass the output of a pipe to another pipe. This lets you use multiple
  516. pipes with a single query.
  517. For a list of supported pipes, see <<eql-pipe-ref>>.
  518. [discrete]
  519. [[eql-syntax-limitations]]
  520. === Limitations
  521. EQL does not support the following features and syntax.
  522. [discrete]
  523. [[eql-compare-fields]]
  524. ==== Comparing fields
  525. You cannot use EQL comparison operators to compare a field to
  526. another field. This applies even if the fields are changed using a
  527. <<eql-functions,function>>.
  528. [discrete]
  529. [[eql-array-fields]]
  530. ==== Array field values are not supported
  531. EQL does not support <<array,array>> field values, also known as
  532. multi-value fields. EQL searches on array field values may return inconsistent
  533. results.
  534. [discrete]
  535. [[eql-nested-fields]]
  536. ==== EQL search on nested fields
  537. You cannot use EQL to search the values of a <<nested,`nested`>> field or the
  538. sub-fields of a `nested` field. However, data streams and indices containing
  539. `nested` field mappings are otherwise supported.
  540. [discrete]
  541. [[eql-unsupported-syntax]]
  542. ==== Differences from Endgame EQL syntax
  543. {es} EQL differs from the {eql-ref}/index.html[Elastic Endgame EQL syntax] as
  544. follows:
  545. * Most operators and functions in {es} EQL are case-sensitive. For
  546. case-insensitive equality comparisons, use the `:` operator.
  547. * Comparisons using the `==` and `!=` operators do not expand wildcard
  548. characters. For example, `process_name == "cmd*.exe"` interprets `*` as a
  549. literal asterisk, not a wildcard. For case-sensitive wildcard matching, use the
  550. <<eql-fn-wildcard,`wildcard`>> function.
  551. * `=` cannot be substituted for the `==` operator.
  552. * Strings enclosed in single quotes (`'`) are not supported. Enclose strings in
  553. double quotes (`"`) instead.
  554. * `?"` and `?'` do not indicate raw strings. Enclose raw strings in
  555. three double quotes (`"""`) instead.
  556. * {es} EQL does not support:
  557. ** Array functions:
  558. *** {eql-ref}/functions.html#arrayContains[`arrayContains`]
  559. *** {eql-ref}/functions.html#arrayCount[`arrayCount`]
  560. *** {eql-ref}/functions.html#arraySearch[`arraySearch`]
  561. ** The {eql-ref}//functions.html#match[`match`] function
  562. ** {eql-ref}/joins.html[Joins]
  563. ** {eql-ref}/basic-syntax.html#event-relationships[Lineage-related keywords]:
  564. *** `child of`
  565. *** `descendant of`
  566. *** `event of`
  567. ** The following {eql-ref}/pipes.html[pipes]:
  568. *** {eql-ref}/pipes.html#count[`count`]
  569. *** {eql-ref}/pipes.html#filter[`filter`]
  570. *** {eql-ref}/pipes.html#sort[`sort`]
  571. *** {eql-ref}/pipes.html#unique[`unique`]
  572. *** {eql-ref}/pipes.html#unique-count[`unique_count`]