syntax.asciidoc 18 KB

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