syntax.asciidoc 18 KB

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