syntax.asciidoc 19 KB

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