syntax.asciidoc 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[eql-syntax]]
  4. == EQL syntax reference
  5. ++++
  6. <titleabbrev>Syntax reference</titleabbrev>
  7. ++++
  8. [discrete]
  9. [[eql-basic-syntax]]
  10. === Basic syntax
  11. EQL queries require an event category and a matching condition. The `where`
  12. keyword connects them.
  13. [source,eql]
  14. ----
  15. event_category where condition
  16. ----
  17. An event category is an indexed value of the <<eql-required-fields,event
  18. category field>>. By default, the <<eql-search-api,EQL search API>> uses the
  19. `event.category` field from the {ecs-ref}[Elastic Common Schema (ECS)]. You can
  20. specify another event category field using the API's
  21. <<specify-a-timestamp-or-event-category-field,`event_category_field`>>
  22. parameter.
  23. For example, the following EQL query matches events with an event category of
  24. `process` and a `process.name` of `svchost.exe`:
  25. [source,eql]
  26. ----
  27. process where process.name == "svchost.exe"
  28. ----
  29. [discrete]
  30. [[eql-syntax-match-any-event-category]]
  31. ==== Match any event category
  32. To match events of any category, use the `any` keyword. You can also use the
  33. `any` keyword to search for documents without a event category field.
  34. For example, the following EQL query matches any documents with a
  35. `network.protocol` field value of `http`:
  36. [source,eql]
  37. ----
  38. any where network.protocol == "http"
  39. ----
  40. [discrete]
  41. [[eql-syntax-escape-an-event-category]]
  42. ==== Escape an event category
  43. Use enclosing double quotes (`"`) or three enclosing double quotes (`"""`) to
  44. escape event categories that:
  45. * Contain a special character, such as a hyphen (`-`) or dot (`.`)
  46. * Contain a space
  47. * Start with a numeral
  48. [source,eql]
  49. ----
  50. ".my.event.category"
  51. "my-event-category"
  52. "my event category"
  53. "6eventcategory"
  54. """.my.event.category"""
  55. """my-event-category"""
  56. """my event category"""
  57. """6eventcategory"""
  58. ----
  59. [discrete]
  60. [[eql-syntax-escape-a-field-name]]
  61. ==== Escape a field name
  62. Use enclosing backticks (+++`+++) to escape field names that:
  63. * Contain a hyphen (`-`)
  64. * Contain a space
  65. * Start with a numeral
  66. [source,eql]
  67. ----
  68. `my-field`
  69. `my field`
  70. `6myfield`
  71. ----
  72. Use double backticks (+++``+++) to escape any backticks (+++`+++) in the field
  73. name.
  74. [source,eql]
  75. ----
  76. my`field -> `my``field`
  77. ----
  78. [discrete]
  79. [[eql-syntax-conditions]]
  80. === Conditions
  81. A condition consists of one or more criteria an event must match.
  82. You can specify and combine these criteria using the following operators. Most
  83. EQL operators are case-sensitive by default.
  84. [discrete]
  85. [[eql-syntax-comparison-operators]]
  86. ==== Comparison operators
  87. [source,eql]
  88. ----
  89. < <= == : != >= >
  90. ----
  91. `<` (less than)::
  92. Returns `true` if the value to the left of the operator is less than the value
  93. to the right. Otherwise returns `false`.
  94. `<=` (less than or equal) ::
  95. Returns `true` if the value to the left of the operator is less than or equal to
  96. the value to the right. Otherwise returns `false`.
  97. `==` (equal, case-sensitive)::
  98. Returns `true` if the values to the left and right of the operator are equal.
  99. Otherwise returns `false`. Wildcards are not supported.
  100. `:` (equal, case-insensitive)::
  101. Returns `true` if strings to the left and right of the operator are equal.
  102. Otherwise returns `false`. Can only be used to compare strings. Supports
  103. <<eql-syntax-wildcards,wildcards>> and <<eql-syntax-lookup-operators,list
  104. lookups>>.
  105. `!=` (not equal, case-sensitive)::
  106. Returns `true` if the values to the left and right of the operator are not
  107. equal. Otherwise returns `false`. Wildcards are not supported.
  108. `>=` (greater than or equal) ::
  109. Returns `true` if the value to the left of the operator is greater than or equal
  110. to the value to the right. Otherwise returns `false`. When comparing strings,
  111. the operator uses a case-sensitive lexicographic order.
  112. `>` (greater than)::
  113. Returns `true` if the value to the left of the operator is greater than the
  114. value to the right. Otherwise returns `false`. When comparing strings,
  115. the operator uses a case-sensitive lexicographic order.
  116. NOTE: `=` is not supported as an equal operator. Use `==` or `:` instead.
  117. [discrete]
  118. [[eql-syntax-pattern-comparison-keywords]]
  119. ==== Pattern comparison keywords
  120. [source,eql]
  121. ----
  122. my_field like "VALUE*" // case-sensitive wildcard matching
  123. my_field like~ "value*" // case-insensitive wildcard matching
  124. my_field regex "VALUE[^Z].?" // case-sensitive regex matching
  125. my_field regex~ "value[^z].?" // case-insensitive regex matching
  126. ----
  127. `like` (case-sensitive)::
  128. Returns `true` if the string to the left of the keyword matches a
  129. <<eql-syntax-wildcards,wildcard pattern>> to the right. Supports
  130. <<eql-syntax-lookup-operators,list lookups>>. Can only be used to compare
  131. strings. For case-insensitive matching, use `like~`.
  132. `regex` (case-sensitive)::
  133. Returns `true` if the string to the left of the keyword matches a regular
  134. expression to the right. For supported regular expression syntax, see
  135. <<regexp-syntax>>. Supports <<eql-syntax-lookup-operators,list lookups>>. Can
  136. only be used to compare strings. For case-insensitive matching, use `regex~`.
  137. [discrete]
  138. [[limitations-for-comparisons]]
  139. ===== Limitations for comparisons
  140. You cannot chain comparisons. Instead, use a
  141. <<eql-syntax-logical-operators,logical operator>> between comparisons. For
  142. example, `foo < bar <= baz` is not supported. However, you can rewrite the
  143. expression as `foo < bar and bar <= baz`, which is supported.
  144. You also cannot compare a field to another field, even if the fields are changed
  145. using a <<eql-functions,function>>.
  146. *Example* +
  147. The following EQL query compares the `process.parent_name` field
  148. value to a static value, `foo`. This comparison is supported.
  149. However, the query also compares the `process.parent.name` field value to the
  150. `process.name` field. This comparison is not supported and will return an
  151. error for the entire query.
  152. [source,eql]
  153. ----
  154. process where process.parent.name == "foo" and process.parent.name == process.name
  155. ----
  156. Instead, you can rewrite the query to compare both the `process.parent.name`
  157. and `process.name` fields to static values.
  158. [source,eql]
  159. ----
  160. process where process.parent.name == "foo" and process.name == "foo"
  161. ----
  162. [discrete]
  163. [[eql-syntax-logical-operators]]
  164. ==== Logical operators
  165. [source,eql]
  166. ----
  167. and or not
  168. ----
  169. `and`::
  170. Returns `true` only if the condition to the left and right _both_ return `true`.
  171. Otherwise returns `false`.
  172. `or`::
  173. Returns `true` if one of the conditions to the left or right `true`.
  174. Otherwise returns `false`.
  175. `not`::
  176. Returns `true` if the condition to the right is `false`.
  177. [discrete]
  178. [[eql-syntax-lookup-operators]]
  179. ==== Lookup operators
  180. [source,eql]
  181. ----
  182. my_field in ("Value-1", "VALUE2", "VAL3") // case-sensitive
  183. my_field in~ ("value-1", "value2", "val3") // case-insensitive
  184. my_field not in ("Value-1", "VALUE2", "VAL3") // case-sensitive
  185. my_field not in~ ("value-1", "value2", "val3") // case-insensitive
  186. my_field : ("value-1", "value2", "val3") // case-insensitive
  187. my_field like ("Value-*", "VALUE2", "VAL?") // case-sensitive
  188. my_field like~ ("value-*", "value2", "val?") // case-insensitive
  189. my_field regex ("[vV]alue-[0-9]", "VALUE[^2].?", "VAL3") // case-sensitive
  190. my_field regex~ ("value-[0-9]", "value[^2].?", "val3") // case-sensitive
  191. ----
  192. `in` (case-sensitive)::
  193. Returns `true` if the value is contained in the provided list. For
  194. case-insensitive matching, use `in~`.
  195. `not in` (case-sensitive)::
  196. Returns `true` if the value is not contained in the provided list. For
  197. case-insensitive matching, use `not in~`.
  198. `:` (case-insensitive)::
  199. Returns `true` if the string is contained in the provided list. Can only be used
  200. to compare strings.
  201. `like` (case-sensitive)::
  202. Returns `true` if the string matches a <<eql-syntax-wildcards,wildcard pattern>>
  203. in the provided list. Can only be used to compare strings. For case-insensitive
  204. matching, use `like~`.
  205. `regex` (case-sensitive)::
  206. Returns `true` if the string matches a regular expression pattern in the
  207. provided list. For supported regular expression syntax, see <<regexp-syntax>>.
  208. Can only be used to compare strings. For case-insensitive matching, use
  209. `regex~`.
  210. [discrete]
  211. [[eql-syntax-math-operators]]
  212. ==== Math operators
  213. [source,eql]
  214. ----
  215. + - * / %
  216. ----
  217. `+` (add)::
  218. Adds the values to the left and right of the operator.
  219. `-` (subtract)::
  220. Subtracts the value to the right of the operator from the value to the left.
  221. `*` (multiply)::
  222. Multiplies the values to the left and right of the operator.
  223. `/` (divide)::
  224. Divides the value to the left of the operator by the value to the right.
  225. +
  226. [[eql-divide-operator-float-rounding]]
  227. [WARNING]
  228. ====
  229. If both the dividend and divisor are integers, the divide (`\`) operation
  230. _rounds down_ any returned floating point numbers to the nearest integer. To
  231. avoid rounding, convert either the dividend or divisor to a float.
  232. *Example* +
  233. The `process.args_count` field is a <<number,`long`>> integer field containing a
  234. count of process arguments.
  235. A user might expect the following EQL query to only match events with a
  236. `process.args_count` value of `4`.
  237. [source,eql]
  238. ----
  239. process where ( 4 / process.args_count ) == 1
  240. ----
  241. However, the EQL query matches events with a `process.args_count` value of `3`
  242. or `4`.
  243. For events with a `process.args_count` value of `3`, the divide operation
  244. returns a float of `1.333...`, which is rounded down to `1`.
  245. To match only events with a `process.args_count` value of `4`, convert
  246. either the dividend or divisor to a float.
  247. The following EQL query changes the integer `4` to the equivalent float `4.0`.
  248. [source,eql]
  249. ----
  250. process where ( 4.0 / process.args_count ) == 1
  251. ----
  252. ====
  253. `%` (modulo)::
  254. Divides the value to the left of the operator by the value to the right. Returns only the remainder.
  255. [discrete]
  256. [[eql-syntax-match-any-condition]]
  257. ==== Match any condition
  258. To match events solely on event category, use the `where true` condition.
  259. For example, the following EQL query matches any `file` events:
  260. [source,eql]
  261. ----
  262. file where true
  263. ----
  264. To match any event, you can combine the `any` keyword with the `where true`
  265. condition:
  266. [source,eql]
  267. ----
  268. any where true
  269. ----
  270. [discrete]
  271. [[eql-syntax-check-field-exists]]
  272. ==== Check if a field exists
  273. To match events containing any value for a field, compare the field to `null`
  274. using the `!=` operator:
  275. [source,eql]
  276. ----
  277. my_field != null
  278. ----
  279. To match events that do not contain a field value, compare the field to `null`
  280. using the `==` operator:
  281. [source,eql]
  282. ----
  283. my_field == null
  284. ----
  285. IMPORTANT: To avoid errors, the field must contain a non-`null` value in at
  286. least one document or be <<explicit-mapping,explicitly mapped>>.
  287. [discrete]
  288. [[eql-syntax-strings]]
  289. === Strings
  290. Strings are enclosed in double quotes (`"`).
  291. [source,eql]
  292. ----
  293. "hello world"
  294. ----
  295. Strings enclosed in single quotes (`'`) are not supported.
  296. [discrete]
  297. [[eql-syntax-escape-characters]]
  298. ==== Escape characters in a string
  299. When used within a string, special characters, such as a carriage return or
  300. double quote (`"`), must be escaped with a preceding backslash (`\`).
  301. [source,eql]
  302. ----
  303. "example \r of \" escaped \n characters"
  304. ----
  305. [options="header"]
  306. |====
  307. | Escape sequence | Literal character
  308. |`\n` | Newline (linefeed)
  309. |`\r` | Carriage return
  310. |`\t` | Tab
  311. |`\\` | Backslash (`\`)
  312. |`\"` | Double quote (`"`)
  313. |====
  314. You can escape Unicode characters using a hexadecimal `\u{XXXXXXXX}` escape
  315. sequence. The hexadecimal value can be 2-8 characters and is case-insensitive.
  316. Values shorter than 8 characters are zero-padded. You can use these escape
  317. sequences to include non-printable or right-to-left (RTL) characters in your
  318. strings. For example, you can escape a
  319. {wikipedia}/Right-to-left_mark[right-to-left mark (RLM)] as `\u{200f}`,
  320. `\u{200F}`, or `\u{0000200f}`.
  321. IMPORTANT: The single quote (`'`) character is reserved for future use. You
  322. cannot use an escaped single quote (`\'`) for literal strings. Use an escaped
  323. double quote (`\"`) instead.
  324. [discrete]
  325. [[eql-syntax-raw-strings]]
  326. ==== Raw strings
  327. Raw strings treat special characters, such as backslashes (`\`), as literal
  328. characters. Raw strings are enclosed in three double quotes (`"""`).
  329. [source,eql]
  330. ----
  331. """Raw string with a literal double quote " and blackslash \ included"""
  332. ----
  333. A raw string cannot contain three consecutive double quotes (`"""`). Instead,
  334. use a regular string with the `\"` escape sequence.
  335. [source,eql]
  336. ----
  337. "String containing \"\"\" three double quotes"
  338. ----
  339. [discrete]
  340. [[eql-syntax-wildcards]]
  341. ==== Wildcards
  342. For string comparisons using the `:` operator or `like` keyword, you can use the
  343. `*` and `?` wildcards to match specific patterns. The `*` wildcard matches zero
  344. or more characters:
  345. [source,eql]
  346. ----
  347. my_field : "doc*" // Matches "doc", "docs", or "document" but not "DOS"
  348. my_field : "*doc" // Matches "adoc" or "asciidoc"
  349. my_field : "d*c" // Matches "doc" or "disc"
  350. my_field like "DOC*" // Matches "DOC", "DOCS", "DOCs", or "DOCUMENT" but not "DOS"
  351. my_field like "D*C" // Matches "DOC", "DISC", or "DisC"
  352. ----
  353. The `?` wildcard matches exactly one character:
  354. [source,eql]
  355. ----
  356. my_field : "doc?" // Matches "docs" but not "doc", "document", or "DOS"
  357. my_field : "?doc" // Matches "adoc" but not "asciidoc"
  358. my_field : "d?c" // Matches "doc" but not "disc"
  359. my_field like "DOC?" // Matches "DOCS" or "DOCs" but not "DOC", "DOCUMENT", or "DOS"
  360. my_field like "D?c" // Matches "DOC" but not "DISC"
  361. ----
  362. The `:` operator and `like` keyword also support wildcards in
  363. <<eql-syntax-lookup-operators,list lookups>>:
  364. [source,eql]
  365. ----
  366. my_field : ("doc*", "f*o", "ba?", "qux")
  367. my_field like ("Doc*", "F*O", "BA?", "QUX")
  368. ----
  369. [discrete]
  370. [[eql-sequences]]
  371. === Sequences
  372. You can use EQL sequences to describe and match an ordered series of events.
  373. Each item in a sequence is an event category and event condition,
  374. surrounded by square brackets (`[ ]`). Events are listed in ascending
  375. chronological order, with the most recent event listed last.
  376. [source,eql]
  377. ----
  378. sequence
  379. [ event_category_1 where condition_1 ]
  380. [ event_category_2 where condition_2 ]
  381. ...
  382. ----
  383. *Example* +
  384. The following EQL sequence query matches this series of ordered events:
  385. . Start with an event with:
  386. +
  387. --
  388. * An event category of `file`
  389. * A `file.extension` of `exe`
  390. --
  391. . Followed by an event with an event category of `process`
  392. [source,eql]
  393. ----
  394. sequence
  395. [ file where file.extension == "exe" ]
  396. [ process where true ]
  397. ----
  398. [discrete]
  399. [[eql-with-maxspan-keywords]]
  400. ==== `with maxspan` statement
  401. You can use `with maxspan` to constrain a sequence to a specified timespan. All
  402. events in a matching sequence must occur within this duration, starting at the
  403. first event's timestamp.
  404. `maxspan` accepts <<time-units,time value>> arguments.
  405. [source,eql]
  406. ----
  407. sequence with maxspan=30s
  408. [ event_category_1 where condition_1 ] by field_baz
  409. [ event_category_2 where condition_2 ] by field_bar
  410. ...
  411. ----
  412. *Example* +
  413. The following sequence query uses a `maxspan` value of `15m` (15 minutes).
  414. Events in a matching sequence must occur within 15 minutes of the first event's
  415. timestamp.
  416. [source,eql]
  417. ----
  418. sequence with maxspan=15m
  419. [ file where file.extension == "exe" ]
  420. [ process where true ]
  421. ----
  422. [discrete]
  423. [[eql-by-keyword]]
  424. ==== `by` keyword
  425. You can use the `by` keyword with sequences to only match events that share the
  426. same field values. If a field value should be shared across all events, you
  427. can use `sequence by`.
  428. [source,eql]
  429. ----
  430. sequence by field_foo
  431. [ event_category_1 where condition_1 ] by field_baz
  432. [ event_category_2 where condition_2 ] by field_bar
  433. ...
  434. ----
  435. *Example* +
  436. The following sequence query uses the `by` keyword to constrain matching events
  437. to:
  438. * Events with the same `user.name` value
  439. * `file` events with a `file.path` value equal to the following `process`
  440. event's `process.executable` value.
  441. [source,eql]
  442. ----
  443. sequence
  444. [ file where file.extension == "exe" ] by user.name, file.path
  445. [ process where true ] by user.name, process.executable
  446. ----
  447. Because the `user.name` field is shared across all events in the sequence, it
  448. can be included using `sequence by`. The following sequence is equivalent to the
  449. prior one.
  450. [source,eql]
  451. ----
  452. sequence by user.name
  453. [ file where file.extension == "exe" ] by file.path
  454. [ process where true ] by process.executable
  455. ----
  456. You can combine `sequence by` and `with maxspan` to constrain a sequence by both
  457. field values and a timespan.
  458. [source,eql]
  459. ----
  460. sequence by field_foo with maxspan=30s
  461. [ event_category_1 where condition_1 ] by field_baz
  462. [ event_category_2 where condition_2 ] by field_bar
  463. ...
  464. ----
  465. *Example* +
  466. The following sequence query uses `sequence by` and `with maxspan` to only match
  467. a sequence of events that:
  468. * Share the same `user.name` field values
  469. * Occur within `15m` (15 minutes) of the first matching event
  470. [source,eql]
  471. ----
  472. sequence by user.name with maxspan=15m
  473. [ file where file.extension == "exe" ] by file.path
  474. [ process where true ] by process.executable
  475. ----
  476. [discrete]
  477. [[eql-until-keyword]]
  478. ==== `until` keyword
  479. You can use the `until` keyword to specify an expiration event for a sequence.
  480. If this expiration event occurs _between_ matching events in a sequence, the
  481. sequence expires and is not considered a match. If the expiration event occurs
  482. _after_ matching events in a sequence, the sequence is still considered a
  483. match. The expiration event is not included in the results.
  484. [source,eql]
  485. ----
  486. sequence
  487. [ event_category_1 where condition_1 ]
  488. [ event_category_2 where condition_2 ]
  489. ...
  490. until [ event_category_3 where condition_3 ]
  491. ----
  492. *Example* +
  493. A dataset contains the following event sequences, grouped by shared IDs:
  494. [source,txt]
  495. ----
  496. A, B
  497. A, B, C
  498. A, C, B
  499. ----
  500. The following EQL query searches the dataset for sequences containing
  501. event `A` followed by event `B`. Event `C` is used as an expiration event.
  502. [source,eql]
  503. ----
  504. sequence by ID
  505. A
  506. B
  507. until C
  508. ----
  509. The query matches sequences `A, B` and `A, B, C` but not `A, C, B`.
  510. [TIP]
  511. ====
  512. The `until` keyword can be useful when searching for process sequences in
  513. Windows event logs.
  514. In Windows, a process ID (PID) is unique only while a process is running. After
  515. a process terminates, its PID can be reused.
  516. You can search for a sequence of events with the same PID value using the `by`
  517. and `sequence by` keywords.
  518. *Example* +
  519. The following EQL query uses the `sequence by` keyword to match a
  520. sequence of events that share the same `process.pid` value.
  521. [source,eql]
  522. ----
  523. sequence by process.pid
  524. [ process where event.type == "start" and process.name == "cmd.exe" ]
  525. [ process where file.extension == "exe" ]
  526. ----
  527. However, due to PID reuse, this can result in a matching sequence that
  528. contains events across unrelated processes. To prevent false positives, you can
  529. use the `until` keyword to end matching sequences before a process termination
  530. event.
  531. The following EQL query uses the `until` keyword to end sequences before
  532. `process` events with an `event.type` of `stop`. These events indicate a process
  533. has been terminated.
  534. [source,eql]
  535. ----
  536. sequence by process.pid
  537. [ process where event.type == "start" and process.name == "cmd.exe" ]
  538. [ process where file.extension == "exe" ]
  539. until [ process where event.type == "stop" ]
  540. ----
  541. ====
  542. [discrete]
  543. [[eql-with-runs-statement]]
  544. ==== `with runs` statement
  545. Use a `with runs` statement to run the same event criteria successively within a
  546. sequence query. For example:
  547. [source,eql]
  548. ----
  549. sequence
  550. [ process where event.type == "creation" ]
  551. [ library where process.name == "regsvr32.exe" ] with runs=3
  552. [ registry where true ]
  553. ----
  554. is equivalent to:
  555. [source,eql]
  556. ----
  557. sequence
  558. [ process where event.type == "creation" ]
  559. [ library where process.name == "regsvr32.exe" ]
  560. [ library where process.name == "regsvr32.exe" ]
  561. [ library where process.name == "regsvr32.exe" ]
  562. [ registry where true ]
  563. ----
  564. The `runs` value must be between `1` and `100` (inclusive).
  565. You can use a `with runs` statement with the <<eql-by-keyword,`by` keyword>>.
  566. For example:
  567. [source,eql]
  568. ----
  569. sequence
  570. [ process where event.type == "creation" ] by process.executable
  571. [ library where process.name == "regsvr32.exe" ] by dll.path with runs=3
  572. ----
  573. [discrete]
  574. [[eql-functions]]
  575. === Functions
  576. You can use EQL functions to convert data types, perform math, manipulate
  577. strings, and more. For a list of supported functions, see <<eql-function-ref>>.
  578. [discrete]
  579. [[eql-case-insensitive-functions]]
  580. ==== Case-insensitive functions
  581. Most EQL functions are case-sensitive by default. To make a function
  582. case-insensitive, use the `~` operator after the function name:
  583. [source,eql]
  584. ----
  585. stringContains(process.name,".exe") // Matches ".exe" but not ".EXE" or ".Exe"
  586. stringContains~(process.name,".exe") // Matches ".exe", ".EXE", or ".Exe"
  587. ----
  588. [discrete]
  589. [[eql-how-functions-impact-search-performance]]
  590. ==== How functions impact search performance
  591. Using functions in EQL queries can result in slower search speeds. If you
  592. often use functions to transform indexed data, you can speed up search by making
  593. these changes during indexing instead. However, that often means slower index
  594. speeds.
  595. *Example* +
  596. An index contains the `file.path` field. `file.path` contains the full path to a
  597. file, including the file extension.
  598. When running EQL searches, users often use the `endsWith` function with the
  599. `file.path` field to match file extensions:
  600. [source,eql]
  601. ----
  602. file where endsWith(file.path,".exe") or endsWith(file.path,".dll")
  603. ----
  604. While this works, it can be repetitive to write and can slow search speeds. To
  605. speed up search, you can do the following instead:
  606. . <<indices-put-mapping,Add a new field>>, `file.extension`, to the index. The
  607. `file.extension` field will contain only the file extension from the
  608. `file.path` field.
  609. . Use an <<ingest,ingest pipeline>> containing the <<grok-processor,`grok`>>
  610. processor or another preprocessor tool to extract the file extension from the
  611. `file.path` field before indexing.
  612. . Index the extracted file extension to the `file.extension` field.
  613. These changes may slow indexing but allow for faster searches. Users
  614. can use the `file.extension` field instead of multiple `endsWith` function
  615. calls:
  616. [source,eql]
  617. ----
  618. file where file.extension in ("exe", "dll")
  619. ----
  620. We recommend testing and benchmarking any indexing changes before deploying them
  621. in production. See <<tune-for-indexing-speed>> and <<tune-for-search-speed>>.
  622. [discrete]
  623. [[eql-pipes]]
  624. === Pipes
  625. EQL pipes filter, aggregate, and post-process events returned by
  626. an EQL query. You can use pipes to narrow down EQL query results or make them
  627. more specific.
  628. Pipes are delimited using the pipe (`|`) character.
  629. [source,eql]
  630. ----
  631. event_category where condition | pipe
  632. ----
  633. *Example* +
  634. The following EQL query uses the `tail` pipe to return only the 10 most recent
  635. events matching the query.
  636. [source,eql]
  637. ----
  638. authentication where agent.id == 4624
  639. | tail 10
  640. ----
  641. You can pass the output of a pipe to another pipe. This lets you use multiple
  642. pipes with a single query.
  643. For a list of supported pipes, see <<eql-pipe-ref>>.
  644. [discrete]
  645. [[eql-syntax-limitations]]
  646. === Limitations
  647. EQL has the following limitations.
  648. [discrete]
  649. [[eql-uses-fields-parameter]]
  650. ==== EQL uses the `fields` parameter
  651. EQL retrieves field values using the search API's <<search-fields-param,`fields`
  652. parameter>>. Any limitations on the `fields` parameter also apply to EQL
  653. queries. For example, if `_source` is disabled for any returned fields or at
  654. index level, the values cannot be retrieved.
  655. [discrete]
  656. [[eql-compare-fields]]
  657. ==== Comparing fields
  658. You cannot use EQL comparison operators to compare a field to
  659. another field. This applies even if the fields are changed using a
  660. <<eql-functions,function>>.
  661. [discrete]
  662. [[eql-text-fields]]
  663. ==== Text fields are not supported
  664. EQL searches do not support <<text,`text`>> fields. To a search a `text` field,
  665. use the EQL search API's <<eql-search-filter-query-dsl,Query DSL `filter`>>
  666. parameter.
  667. [discrete]
  668. [[eql-nested-fields]]
  669. ==== EQL search on nested fields
  670. You cannot use EQL to search the values of a <<nested,`nested`>> field or the
  671. sub-fields of a `nested` field. However, data streams and indices containing
  672. `nested` field mappings are otherwise supported.
  673. [discrete]
  674. [[eql-unsupported-syntax]]
  675. ==== Differences from Endgame EQL syntax
  676. {es} EQL differs from the {eql-ref}/index.html[Elastic Endgame EQL syntax] as
  677. follows:
  678. * In {es} EQL, most operators are case-sensitive. For example,
  679. `process_name == "cmd.exe"` is not equivalent to
  680. `process_name == "Cmd.exe"`.
  681. * In {es} EQL, functions are case-sensitive. To make a function
  682. case-insensitive, use `~`, such as `endsWith~(process_name, ".exe")`.
  683. * For case-insensitive equality comparisons, use the `:` operator. Both `*` and
  684. `?` are recognized wildcard characters.
  685. * The `==` and `!=` operators do not expand wildcard characters. For example,
  686. `process_name == "cmd*.exe"` interprets `*` as a literal asterisk, not a
  687. wildcard.
  688. * For wildcard matching, use the `like` keyword when case-sensitive and
  689. `like~` when case-insensitive. The `:` operator is equivalent to `like~`.
  690. * For regular expression matching, use `regex` or `regex~`.
  691. * `=` cannot be substituted for the `==` operator.
  692. * Strings enclosed in single quotes (`'`) are not supported. Enclose strings in
  693. double quotes (`"`) instead.
  694. * `?"` and `?'` do not indicate raw strings. Enclose raw strings in
  695. three double quotes (`"""`) instead.
  696. * {es} EQL does not support:
  697. ** Array functions:
  698. *** {eql-ref}/functions.html#arrayContains[`arrayContains`]
  699. *** {eql-ref}/functions.html#arrayCount[`arrayCount`]
  700. *** {eql-ref}/functions.html#arraySearch[`arraySearch`]
  701. ** The {eql-ref}//functions.html#match[`match`] function
  702. ** {eql-ref}/joins.html[Joins]
  703. ** {eql-ref}/basic-syntax.html#event-relationships[Lineage-related keywords]:
  704. *** `child of`
  705. *** `descendant of`
  706. *** `event of`
  707. ** The following {eql-ref}/pipes.html[pipes]:
  708. *** {eql-ref}/pipes.html#count[`count`]
  709. *** {eql-ref}/pipes.html#filter[`filter`]
  710. *** {eql-ref}/pipes.html#sort[`sort`]
  711. *** {eql-ref}/pipes.html#unique[`unique`]
  712. *** {eql-ref}/pipes.html#unique-count[`unique_count`]
  713. [discrete]
  714. [[eql-how-sequence-queries-handle-matches]]
  715. ==== How sequence queries handle matches
  716. <<eql-sequences,Sequence queries>> don't find all potential matches for a
  717. sequence. This approach would be too slow and costly for large event data sets.
  718. Instead, a sequence query handles pending sequence matches as a
  719. {wikipedia}/Finite-state_machine[state machine]:
  720. * Each event item in the sequence query is a state in the machine.
  721. * Only one pending sequence can be in each state at a time.
  722. * If two pending sequences are in the same state at the same time, the most
  723. recent sequence overwrites the older one.
  724. * If the query includes <<eql-by-keyword,`by` fields>>, the query uses a
  725. separate state machine for each unique `by` field value.
  726. .*Example*
  727. [%collapsible]
  728. ====
  729. A data set contains the following `process` events in ascending chronological
  730. order:
  731. [source,js]
  732. ----
  733. { "index" : { "_id": "1" } }
  734. { "user": { "name": "root" }, "process": { "name": "attrib" }, ...}
  735. { "index" : { "_id": "2" } }
  736. { "user": { "name": "root" }, "process": { "name": "attrib" }, ...}
  737. { "index" : { "_id": "3" } }
  738. { "user": { "name": "elkbee" }, "process": { "name": "bash" }, ...}
  739. { "index" : { "_id": "4" } }
  740. { "user": { "name": "root" }, "process": { "name": "bash" }, ...}
  741. { "index" : { "_id": "5" } }
  742. { "user": { "name": "root" }, "process": { "name": "bash" }, ...}
  743. { "index" : { "_id": "6" } }
  744. { "user": { "name": "elkbee" }, "process": { "name": "attrib" }, ...}
  745. { "index" : { "_id": "7" } }
  746. { "user": { "name": "root" }, "process": { "name": "attrib" }, ...}
  747. { "index" : { "_id": "8" } }
  748. { "user": { "name": "elkbee" }, "process": { "name": "bash" }, ...}
  749. { "index" : { "_id": "9" } }
  750. { "user": { "name": "root" }, "process": { "name": "cat" }, ...}
  751. { "index" : { "_id": "10" } }
  752. { "user": { "name": "elkbee" }, "process": { "name": "cat" }, ...}
  753. { "index" : { "_id": "11" } }
  754. { "user": { "name": "root" }, "process": { "name": "cat" }, ...}
  755. ----
  756. // NOTCONSOLE
  757. An EQL sequence query searches the data set:
  758. [source,eql]
  759. ----
  760. sequence by user.name
  761. [process where process.name == "attrib"]
  762. [process where process.name == "bash"]
  763. [process where process.name == "cat"]
  764. ----
  765. The query's event items correspond to the following states:
  766. * State A: `[process where process.name == "attrib"]`
  767. * State B: `[process where process.name == "bash"]`
  768. * Complete: `[process where process.name == "cat"]`
  769. image::images/eql/sequence-state-machine.svg[align="center"]
  770. To find matching sequences, the query uses separate state machines for each
  771. unique `user.name` value. Based on the data set, you can expect two state
  772. machines: one for the `root` user and one for `elkbee`.
  773. image::images/eql/separate-state-machines.svg[align="center"]
  774. Pending sequence matches move through each machine's states as follows:
  775. [source,txt]
  776. ----
  777. { "index" : { "_id": "1" } }
  778. { "user": { "name": "root" }, "process": { "name": "attrib" }, ...}
  779. // Creates sequence [1] in state A for the "root" user.
  780. //
  781. // +------------------------"root"------------------------+
  782. // | +-----------+ +-----------+ +------------+ |
  783. // | | State A | | State B | | Complete | |
  784. // | +-----------+ +-----------+ +------------+ |
  785. // | | [1] | | | | | |
  786. // | +-----------+ +-----------+ +------------+ |
  787. // +------------------------------------------------------+
  788. { "index" : { "_id": "2" } }
  789. { "user": { "name": "root" }, "process": { "name": "attrib" }, ...}
  790. // Creates sequence [2] in state A for "root", overwriting sequence [1].
  791. //
  792. // +------------------------"root"------------------------+
  793. // | +-----------+ +-----------+ +------------+ |
  794. // | | State A | | State B | | Complete | |
  795. // | +-----------+ +-----------+ +------------+ |
  796. // | | [2] | | | | | |
  797. // | +-----------+ +-----------+ +------------+ |
  798. // +------------------------------------------------------+
  799. { "index" : { "_id": "3" } }
  800. { "user": { "name": "elkbee" }, "process": { "name": "bash" }, ...}
  801. // Nothing happens. The "elkbee" user has no pending sequence to move
  802. // from state A to state B.
  803. //
  804. // +-----------------------"elkbee"-----------------------+
  805. // | +-----------+ +-----------+ +------------+ |
  806. // | | State A | | State B | | Complete | |
  807. // | +-----------+ +-----------+ +------------+ |
  808. // | | | | | | | |
  809. // | +-----------+ +-----------+ +------------+ |
  810. // +------------------------------------------------------+
  811. { "index" : { "_id": "4" } }
  812. { "user": { "name": "root" }, "process": { "name": "bash" }, ...}
  813. // Sequence [2] moves out of state A for "root".
  814. // State B for "root" now contains [2, 4].
  815. // State A for "root" is empty.
  816. //
  817. // +------------------------"root"------------------------+
  818. // | +-----------+ +-----------+ +------------+ |
  819. // | | State A | | State B | | Complete | |
  820. // | +-----------+ --> +-----------+ +------------+ |
  821. // | | | | [2, 4] | | | |
  822. // | +-----------+ +-----------+ +------------+ |
  823. // +------------------------------------------------------+
  824. { "index" : { "_id": "5" } }
  825. { "user": { "name": "root" }, "process": { "name": "bash" }, ...}
  826. // Nothing happens. State A is empty for "root".
  827. //
  828. // +------------------------"root"------------------------+
  829. // | +-----------+ +-----------+ +------------+ |
  830. // | | State A | | State B | | Complete | |
  831. // | +-----------+ +-----------+ +------------+ |
  832. // | | | | [2, 4] | | | |
  833. // | +-----------+ +-----------+ +------------+ |
  834. // +------------------------------------------------------+
  835. { "index" : { "_id": "6" } }
  836. { "user": { "name": "elkbee" }, "process": { "name": "attrib" }, ...}
  837. // Creates sequence [6] in state A for "elkbee".
  838. //
  839. // +-----------------------"elkbee"-----------------------+
  840. // | +-----------+ +-----------+ +------------+ |
  841. // | | State A | | State B | | Complete | |
  842. // | +-----------+ +-----------+ +------------+ |
  843. // | | [6] | | | | | |
  844. // | +-----------+ +-----------+ +------------+ |
  845. // +------------------------------------------------------+
  846. { "index" : { "_id": "7" } }
  847. { "user": { "name": "root" }, "process": { "name": "attrib" }, ...}
  848. // Creates sequence [7] in state A for "root".
  849. // Sequence [2, 4] remains in state B for "root".
  850. //
  851. // +------------------------"root"------------------------+
  852. // | +-----------+ +-----------+ +------------+ |
  853. // | | State A | | State B | | Complete | |
  854. // | +-----------+ +-----------+ +------------+ |
  855. // | | [7] | | [2, 4] | | | |
  856. // | +-----------+ +-----------+ +------------+ |
  857. // +------------------------------------------------------+
  858. { "index" : { "_id": "8" } }
  859. { "user": { "name": "elkbee" }, "process": { "name": "bash" }, ...}
  860. // Sequence [6, 8] moves to state B for "elkbee".
  861. // State A for "elkbee" is now empty.
  862. //
  863. // +-----------------------"elkbee"-----------------------+
  864. // | +-----------+ +-----------+ +------------+ |
  865. // | | State A | | State B | | Complete | |
  866. // | +-----------+ --> +-----------+ +------------+ |
  867. // | | | | [6, 8] | | | |
  868. // | +-----------+ +-----------+ +------------+ |
  869. // +------------------------------------------------------+
  870. { "index" : { "_id": "9" } }
  871. { "user": { "name": "root" }, "process": { "name": "cat" }, ...}
  872. // Sequence [2, 4, 9] is complete for "root".
  873. // State B for "root" is now empty.
  874. // Sequence [7] remains in state A.
  875. //
  876. // +------------------------"root"------------------------+
  877. // | +-----------+ +-----------+ +------------+ |
  878. // | | State A | | State B | | Complete | |
  879. // | +-----------+ +-----------+ --> +------------+ |
  880. // | | [7] | | | | [2, 4, 9] |
  881. // | +-----------+ +-----------+ +------------+ |
  882. // +------------------------------------------------------+
  883. { "index" : { "_id": "10" } }
  884. { "user": { "name": "elkbee" }, "process": { "name": "cat" }, ...}
  885. // Sequence [6, 8, 10] is complete for "elkbee".
  886. // State A and B for "elkbee" are now empty.
  887. //
  888. // +-----------------------"elkbee"-----------------------+
  889. // | +-----------+ +-----------+ +------------+ |
  890. // | | State A | | State B | | Complete | |
  891. // | +-----------+ +-----------+ --> +------------+ |
  892. // | | | | | | [6, 8, 10] |
  893. // | +-----------+ +-----------+ +------------+ |
  894. // +------------------------------------------------------+
  895. { "index" : { "_id": "11" } }
  896. { "user": { "name": "root" }, "process": { "name": "cat" }, ...}
  897. // Nothing happens.
  898. // The machines for "root" and "elkbee" remain the same.
  899. //
  900. // +------------------------"root"------------------------+
  901. // | +-----------+ +-----------+ +------------+ |
  902. // | | State A | | State B | | Complete | |
  903. // | +-----------+ +-----------+ +------------+ |
  904. // | | [7] | | | | [2, 4, 9] |
  905. // | +-----------+ +-----------+ +------------+ |
  906. // +------------------------------------------------------+
  907. //
  908. // +-----------------------"elkbee"-----------------------+
  909. // | +-----------+ +-----------+ +------------+ |
  910. // | | State A | | State B | | Complete | |
  911. // | +-----------+ +-----------+ +------------+ |
  912. // | | | | | | [6, 8, 10] |
  913. // | +-----------+ +-----------+ +------------+ |
  914. // +------------------------------------------------------+
  915. ----
  916. ====