syntax.asciidoc 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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-optional-fields]]
  272. === Optional fields
  273. By default, an EQL query can only contain fields that exist in the dataset
  274. you're searching. A field exists in a dataset if it has an
  275. <<explicit-mapping,explicit>>, <<dynamic-mapping,dynamic>>, or
  276. <<eql-use-runtime-fields,runtime>> mapping. If an EQL query contains a field
  277. that doesn't exist, it returns an error.
  278. If you aren't sure if a field exists in a dataset, use the `?` operator to mark
  279. the field as optional. If an optional field doesn't exist, the query replaces it
  280. with `null` instead of returning an error.
  281. *Example* +
  282. In the following query, the `user.id` field is optional.
  283. [source,eql]
  284. ----
  285. network where ?user.id != null
  286. ----
  287. If the `user.id` field exists in the dataset you're searching, the query matches
  288. any `network` event that contains a `user.id` value. If the `user.id` field
  289. doesn't exist in the dataset, EQL interprets the query as:
  290. [source,eql]
  291. ----
  292. network where null != null
  293. ----
  294. In this case, the query matches no events.
  295. [discrete]
  296. [[eql-syntax-check-field-exists]]
  297. ==== Check if a field exists
  298. To match events containing any value for a field, compare the field to `null`
  299. using the `!=` operator:
  300. [source,eql]
  301. ----
  302. ?my_field != null
  303. ----
  304. To match events that do not contain a field value, compare the field to `null`
  305. using the `==` operator:
  306. [source,eql]
  307. ----
  308. ?my_field == null
  309. ----
  310. [discrete]
  311. [[eql-syntax-strings]]
  312. === Strings
  313. Strings are enclosed in double quotes (`"`).
  314. [source,eql]
  315. ----
  316. "hello world"
  317. ----
  318. Strings enclosed in single quotes (`'`) are not supported.
  319. [discrete]
  320. [[eql-syntax-escape-characters]]
  321. ==== Escape characters in a string
  322. When used within a string, special characters, such as a carriage return or
  323. double quote (`"`), must be escaped with a preceding backslash (`\`).
  324. [source,eql]
  325. ----
  326. "example \r of \" escaped \n characters"
  327. ----
  328. [options="header"]
  329. |====
  330. | Escape sequence | Literal character
  331. |`\n` | Newline (linefeed)
  332. |`\r` | Carriage return
  333. |`\t` | Tab
  334. |`\\` | Backslash (`\`)
  335. |`\"` | Double quote (`"`)
  336. |====
  337. You can escape Unicode characters using a hexadecimal `\u{XXXXXXXX}` escape
  338. sequence. The hexadecimal value can be 2-8 characters and is case-insensitive.
  339. Values shorter than 8 characters are zero-padded. You can use these escape
  340. sequences to include non-printable or right-to-left (RTL) characters in your
  341. strings. For example, you can escape a
  342. {wikipedia}/Right-to-left_mark[right-to-left mark (RLM)] as `\u{200f}`,
  343. `\u{200F}`, or `\u{0000200f}`.
  344. IMPORTANT: The single quote (`'`) character is reserved for future use. You
  345. cannot use an escaped single quote (`\'`) for literal strings. Use an escaped
  346. double quote (`\"`) instead.
  347. [discrete]
  348. [[eql-syntax-raw-strings]]
  349. ==== Raw strings
  350. Raw strings treat special characters, such as backslashes (`\`), as literal
  351. characters. Raw strings are enclosed in three double quotes (`"""`).
  352. [source,eql]
  353. ----
  354. """Raw string with a literal double quote " and blackslash \ included"""
  355. ----
  356. A raw string cannot contain three consecutive double quotes (`"""`). Instead,
  357. use a regular string with the `\"` escape sequence.
  358. [source,eql]
  359. ----
  360. "String containing \"\"\" three double quotes"
  361. ----
  362. [discrete]
  363. [[eql-syntax-wildcards]]
  364. ==== Wildcards
  365. For string comparisons using the `:` operator or `like` keyword, you can use the
  366. `*` and `?` wildcards to match specific patterns. The `*` wildcard matches zero
  367. or more characters:
  368. [source,eql]
  369. ----
  370. my_field : "doc*" // Matches "doc", "docs", or "document" but not "DOS"
  371. my_field : "*doc" // Matches "adoc" or "asciidoc"
  372. my_field : "d*c" // Matches "doc" or "disc"
  373. my_field like "DOC*" // Matches "DOC", "DOCS", "DOCs", or "DOCUMENT" but not "DOS"
  374. my_field like "D*C" // Matches "DOC", "DISC", or "DisC"
  375. ----
  376. The `?` wildcard matches exactly one character:
  377. [source,eql]
  378. ----
  379. my_field : "doc?" // Matches "docs" but not "doc", "document", or "DOS"
  380. my_field : "?doc" // Matches "adoc" but not "asciidoc"
  381. my_field : "d?c" // Matches "doc" but not "disc"
  382. my_field like "DOC?" // Matches "DOCS" or "DOCs" but not "DOC", "DOCUMENT", or "DOS"
  383. my_field like "D?c" // Matches "DOC" but not "DISC"
  384. ----
  385. The `:` operator and `like` keyword also support wildcards in
  386. <<eql-syntax-lookup-operators,list lookups>>:
  387. [source,eql]
  388. ----
  389. my_field : ("doc*", "f*o", "ba?", "qux")
  390. my_field like ("Doc*", "F*O", "BA?", "QUX")
  391. ----
  392. [discrete]
  393. [[eql-sequences]]
  394. === Sequences
  395. You can use EQL sequences to describe and match an ordered series of events.
  396. Each item in a sequence is an event category and event condition,
  397. surrounded by square brackets (`[ ]`). Events are listed in ascending
  398. chronological order, with the most recent event listed last.
  399. [source,eql]
  400. ----
  401. sequence
  402. [ event_category_1 where condition_1 ]
  403. [ event_category_2 where condition_2 ]
  404. ...
  405. ----
  406. *Example* +
  407. The following EQL sequence query matches this series of ordered events:
  408. . Start with an event with:
  409. +
  410. --
  411. * An event category of `file`
  412. * A `file.extension` of `exe`
  413. --
  414. . Followed by an event with an event category of `process`
  415. [source,eql]
  416. ----
  417. sequence
  418. [ file where file.extension == "exe" ]
  419. [ process where true ]
  420. ----
  421. [discrete]
  422. [[eql-with-maxspan-keywords]]
  423. ==== `with maxspan` statement
  424. You can use `with maxspan` to constrain a sequence to a specified timespan. All
  425. events in a matching sequence must occur within this duration, starting at the
  426. first event's timestamp.
  427. `maxspan` accepts <<time-units,time value>> arguments.
  428. [source,eql]
  429. ----
  430. sequence with maxspan=30s
  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 a `maxspan` value of `15m` (15 minutes).
  437. Events in a matching sequence must occur within 15 minutes of the first event's
  438. timestamp.
  439. [source,eql]
  440. ----
  441. sequence with maxspan=15m
  442. [ file where file.extension == "exe" ]
  443. [ process where true ]
  444. ----
  445. [discrete]
  446. [[eql-by-keyword]]
  447. ==== `by` keyword
  448. Use the `by` keyword in a sequence query to only match events that share the
  449. same values, even if those values are in different fields. These shared values
  450. are called join keys. If a join key should be in the same field across all
  451. events, use `sequence by`.
  452. [source,eql]
  453. ----
  454. sequence by field_foo
  455. [ event_category_1 where condition_1 ] by field_baz
  456. [ event_category_2 where condition_2 ] by field_bar
  457. ...
  458. ----
  459. *Example* +
  460. The following sequence query uses the `by` keyword to constrain matching events
  461. to:
  462. * Events with the same `user.name` value
  463. * `file` events with a `file.path` value equal to the following `process`
  464. event's `process.executable` value.
  465. [source,eql]
  466. ----
  467. sequence
  468. [ file where file.extension == "exe" ] by user.name, file.path
  469. [ process where true ] by user.name, process.executable
  470. ----
  471. Because the `user.name` field is shared across all events in the sequence, it
  472. can be included using `sequence by`. The following sequence is equivalent to the
  473. prior one.
  474. [source,eql]
  475. ----
  476. sequence by user.name
  477. [ file where file.extension == "exe" ] by file.path
  478. [ process where true ] by process.executable
  479. ----
  480. You can combine `sequence by` and `with maxspan` to constrain a sequence by both
  481. field values and a timespan.
  482. [source,eql]
  483. ----
  484. sequence by field_foo with maxspan=30s
  485. [ event_category_1 where condition_1 ]
  486. [ event_category_2 where condition_2 ]
  487. ...
  488. ----
  489. *Example* +
  490. The following sequence query uses `sequence by` and `with maxspan` to only match
  491. a sequence of events that:
  492. * Share the same `user.name` field values
  493. * Occur within `15m` (15 minutes) of the first matching event
  494. [source,eql]
  495. ----
  496. sequence by user.name with maxspan=15m
  497. [ file where file.extension == "exe" ]
  498. [ process where true ]
  499. ----
  500. [discrete]
  501. [[eql-syntax-optional-by-fields]]
  502. ==== Optional `by` fields
  503. By default, a join key must be a non-`null` field value. To allow `null` join
  504. keys, use the `?` operator to mark the `by` field as
  505. <<eql-syntax-optional-fields,optional>>. This is also helpful if you aren't sure
  506. the dataset you're searching contains the `by` field.
  507. *Example* +
  508. The following sequence query uses `sequence by` to constrain matching events
  509. to:
  510. * Events with the same `process.pid` value, excluding `null` values. If the
  511. `process.pid` field doesn't exist in the dataset you're searching, the query
  512. returns an error.
  513. * Events with the same `process.entity_id` value, including `null` values. If
  514. an event doesn't contain the `process.entity_id` field, its
  515. `process.entity_id` value is considered `null`. This applies even if the
  516. `process.pid` field doesn't exist in the dataset you're searching.
  517. [source,eql]
  518. ----
  519. sequence by process.pid, ?process.entity_id
  520. [process where process.name == "regsvr32.exe"]
  521. [network where true]
  522. ----
  523. [discrete]
  524. [[eql-until-keyword]]
  525. ==== `until` keyword
  526. You can use the `until` keyword to specify an expiration event for a sequence.
  527. If this expiration event occurs _between_ matching events in a sequence, the
  528. sequence expires and is not considered a match. If the expiration event occurs
  529. _after_ matching events in a sequence, the sequence is still considered a
  530. match. The expiration event is not included in the results.
  531. [source,eql]
  532. ----
  533. sequence
  534. [ event_category_1 where condition_1 ]
  535. [ event_category_2 where condition_2 ]
  536. ...
  537. until [ event_category_3 where condition_3 ]
  538. ----
  539. *Example* +
  540. A dataset contains the following event sequences, grouped by shared IDs:
  541. [source,txt]
  542. ----
  543. A, B
  544. A, B, C
  545. A, C, B
  546. ----
  547. The following EQL query searches the dataset for sequences containing
  548. event `A` followed by event `B`. Event `C` is used as an expiration event.
  549. [source,eql]
  550. ----
  551. sequence by ID
  552. A
  553. B
  554. until C
  555. ----
  556. The query matches sequences `A, B` and `A, B, C` but not `A, C, B`.
  557. [TIP]
  558. ====
  559. The `until` keyword can be useful when searching for process sequences in
  560. Windows event logs.
  561. In Windows, a process ID (PID) is unique only while a process is running. After
  562. a process terminates, its PID can be reused.
  563. You can search for a sequence of events with the same PID value using the `by`
  564. and `sequence by` keywords.
  565. *Example* +
  566. The following EQL query uses the `sequence by` keyword to match a
  567. sequence of events that share the same `process.pid` value.
  568. [source,eql]
  569. ----
  570. sequence by process.pid
  571. [ process where event.type == "start" and process.name == "cmd.exe" ]
  572. [ process where file.extension == "exe" ]
  573. ----
  574. However, due to PID reuse, this can result in a matching sequence that
  575. contains events across unrelated processes. To prevent false positives, you can
  576. use the `until` keyword to end matching sequences before a process termination
  577. event.
  578. The following EQL query uses the `until` keyword to end sequences before
  579. `process` events with an `event.type` of `stop`. These events indicate a process
  580. has been terminated.
  581. [source,eql]
  582. ----
  583. sequence by process.pid
  584. [ process where event.type == "start" and process.name == "cmd.exe" ]
  585. [ process where file.extension == "exe" ]
  586. until [ process where event.type == "stop" ]
  587. ----
  588. ====
  589. [discrete]
  590. [[eql-with-runs-statement]]
  591. ==== `with runs` statement
  592. Use a `with runs` statement to run the same event criteria successively within a
  593. sequence query. For example:
  594. [source,eql]
  595. ----
  596. sequence
  597. [ process where event.type == "creation" ]
  598. [ library where process.name == "regsvr32.exe" ] with runs=3
  599. [ registry where true ]
  600. ----
  601. is equivalent to:
  602. [source,eql]
  603. ----
  604. sequence
  605. [ process where event.type == "creation" ]
  606. [ library where process.name == "regsvr32.exe" ]
  607. [ library where process.name == "regsvr32.exe" ]
  608. [ library where process.name == "regsvr32.exe" ]
  609. [ registry where true ]
  610. ----
  611. The `runs` value must be between `1` and `100` (inclusive).
  612. You can use a `with runs` statement with the <<eql-by-keyword,`by` keyword>>.
  613. For example:
  614. [source,eql]
  615. ----
  616. sequence
  617. [ process where event.type == "creation" ] by process.executable
  618. [ library where process.name == "regsvr32.exe" ] by dll.path with runs=3
  619. ----
  620. [discrete]
  621. [[eql-functions]]
  622. === Functions
  623. You can use EQL functions to convert data types, perform math, manipulate
  624. strings, and more. For a list of supported functions, see <<eql-function-ref>>.
  625. [discrete]
  626. [[eql-case-insensitive-functions]]
  627. ==== Case-insensitive functions
  628. Most EQL functions are case-sensitive by default. To make a function
  629. case-insensitive, use the `~` operator after the function name:
  630. [source,eql]
  631. ----
  632. stringContains(process.name,".exe") // Matches ".exe" but not ".EXE" or ".Exe"
  633. stringContains~(process.name,".exe") // Matches ".exe", ".EXE", or ".Exe"
  634. ----
  635. [discrete]
  636. [[eql-how-functions-impact-search-performance]]
  637. ==== How functions impact search performance
  638. Using functions in EQL queries can result in slower search speeds. If you
  639. often use functions to transform indexed data, you can speed up search by making
  640. these changes during indexing instead. However, that often means slower index
  641. speeds.
  642. *Example* +
  643. An index contains the `file.path` field. `file.path` contains the full path to a
  644. file, including the file extension.
  645. When running EQL searches, users often use the `endsWith` function with the
  646. `file.path` field to match file extensions:
  647. [source,eql]
  648. ----
  649. file where endsWith(file.path,".exe") or endsWith(file.path,".dll")
  650. ----
  651. While this works, it can be repetitive to write and can slow search speeds. To
  652. speed up search, you can do the following instead:
  653. . <<indices-put-mapping,Add a new field>>, `file.extension`, to the index. The
  654. `file.extension` field will contain only the file extension from the
  655. `file.path` field.
  656. . Use an <<ingest,ingest pipeline>> containing the <<grok-processor,`grok`>>
  657. processor or another preprocessor tool to extract the file extension from the
  658. `file.path` field before indexing.
  659. . Index the extracted file extension to the `file.extension` field.
  660. These changes may slow indexing but allow for faster searches. Users
  661. can use the `file.extension` field instead of multiple `endsWith` function
  662. calls:
  663. [source,eql]
  664. ----
  665. file where file.extension in ("exe", "dll")
  666. ----
  667. We recommend testing and benchmarking any indexing changes before deploying them
  668. in production. See <<tune-for-indexing-speed>> and <<tune-for-search-speed>>.
  669. [discrete]
  670. [[eql-pipes]]
  671. === Pipes
  672. EQL pipes filter, aggregate, and post-process events returned by
  673. an EQL query. You can use pipes to narrow down EQL query results or make them
  674. more specific.
  675. Pipes are delimited using the pipe (`|`) character.
  676. [source,eql]
  677. ----
  678. event_category where condition | pipe
  679. ----
  680. *Example* +
  681. The following EQL query uses the `tail` pipe to return only the 10 most recent
  682. events matching the query.
  683. [source,eql]
  684. ----
  685. authentication where agent.id == 4624
  686. | tail 10
  687. ----
  688. You can pass the output of a pipe to another pipe. This lets you use multiple
  689. pipes with a single query.
  690. For a list of supported pipes, see <<eql-pipe-ref>>.
  691. [discrete]
  692. [[eql-syntax-limitations]]
  693. === Limitations
  694. EQL has the following limitations.
  695. [discrete]
  696. [[eql-uses-fields-parameter]]
  697. ==== EQL uses the `fields` parameter
  698. EQL retrieves field values using the search API's <<search-fields-param,`fields`
  699. parameter>>. Any limitations on the `fields` parameter also apply to EQL
  700. queries. For example, if `_source` is disabled for any returned fields or at
  701. index level, the values cannot be retrieved.
  702. [discrete]
  703. [[eql-compare-fields]]
  704. ==== Comparing fields
  705. You cannot use EQL comparison operators to compare a field to
  706. another field. This applies even if the fields are changed using a
  707. <<eql-functions,function>>.
  708. [discrete]
  709. [[eql-text-fields]]
  710. ==== Text fields are not supported
  711. EQL searches do not support <<text,`text`>> fields. To a search a `text` field,
  712. use the EQL search API's <<eql-search-filter-query-dsl,Query DSL `filter`>>
  713. parameter.
  714. [discrete]
  715. [[eql-nested-fields]]
  716. ==== EQL search on nested fields
  717. You cannot use EQL to search the values of a <<nested,`nested`>> field or the
  718. sub-fields of a `nested` field. However, data streams and indices containing
  719. `nested` field mappings are otherwise supported.
  720. [discrete]
  721. [[eql-unsupported-syntax]]
  722. ==== Differences from Endgame EQL syntax
  723. {es} EQL differs from the {eql-ref}/index.html[Elastic Endgame EQL syntax] as
  724. follows:
  725. * In {es} EQL, most operators are case-sensitive. For example,
  726. `process_name == "cmd.exe"` is not equivalent to
  727. `process_name == "Cmd.exe"`.
  728. * In {es} EQL, functions are case-sensitive. To make a function
  729. case-insensitive, use `~`, such as `endsWith~(process_name, ".exe")`.
  730. * For case-insensitive equality comparisons, use the `:` operator. Both `*` and
  731. `?` are recognized wildcard characters.
  732. * The `==` and `!=` operators do not expand wildcard characters. For example,
  733. `process_name == "cmd*.exe"` interprets `*` as a literal asterisk, not a
  734. wildcard.
  735. * For wildcard matching, use the `like` keyword when case-sensitive and
  736. `like~` when case-insensitive. The `:` operator is equivalent to `like~`.
  737. * For regular expression matching, use `regex` or `regex~`.
  738. * `=` cannot be substituted for the `==` operator.
  739. * Strings enclosed in single quotes (`'`) are not supported. Enclose strings in
  740. double quotes (`"`) instead.
  741. * `?"` and `?'` do not indicate raw strings. Enclose raw strings in
  742. three double quotes (`"""`) instead.
  743. * {es} EQL does not support:
  744. ** Array functions:
  745. *** {eql-ref}/functions.html#arrayContains[`arrayContains`]
  746. *** {eql-ref}/functions.html#arrayCount[`arrayCount`]
  747. *** {eql-ref}/functions.html#arraySearch[`arraySearch`]
  748. ** The {eql-ref}//functions.html#match[`match`] function
  749. ** {eql-ref}/joins.html[Joins]
  750. ** {eql-ref}/basic-syntax.html#event-relationships[Lineage-related keywords]:
  751. *** `child of`
  752. *** `descendant of`
  753. *** `event of`
  754. ** The following {eql-ref}/pipes.html[pipes]:
  755. *** {eql-ref}/pipes.html#count[`count`]
  756. *** {eql-ref}/pipes.html#filter[`filter`]
  757. *** {eql-ref}/pipes.html#sort[`sort`]
  758. *** {eql-ref}/pipes.html#unique[`unique`]
  759. *** {eql-ref}/pipes.html#unique-count[`unique_count`]
  760. [discrete]
  761. [[eql-how-sequence-queries-handle-matches]]
  762. ==== How sequence queries handle matches
  763. <<eql-sequences,Sequence queries>> don't find all potential matches for a
  764. sequence. This approach would be too slow and costly for large event data sets.
  765. Instead, a sequence query handles pending sequence matches as a
  766. {wikipedia}/Finite-state_machine[state machine]:
  767. * Each event item in the sequence query is a state in the machine.
  768. * Only one pending sequence can be in each state at a time.
  769. * If two pending sequences are in the same state at the same time, the most
  770. recent sequence overwrites the older one.
  771. * If the query includes <<eql-by-keyword,`by` fields>>, the query uses a
  772. separate state machine for each unique `by` field value.
  773. .*Example*
  774. [%collapsible]
  775. ====
  776. A data set contains the following `process` events in ascending chronological
  777. order:
  778. [source,js]
  779. ----
  780. { "index" : { "_id": "1" } }
  781. { "user": { "name": "root" }, "process": { "name": "attrib" }, ...}
  782. { "index" : { "_id": "2" } }
  783. { "user": { "name": "root" }, "process": { "name": "attrib" }, ...}
  784. { "index" : { "_id": "3" } }
  785. { "user": { "name": "elkbee" }, "process": { "name": "bash" }, ...}
  786. { "index" : { "_id": "4" } }
  787. { "user": { "name": "root" }, "process": { "name": "bash" }, ...}
  788. { "index" : { "_id": "5" } }
  789. { "user": { "name": "root" }, "process": { "name": "bash" }, ...}
  790. { "index" : { "_id": "6" } }
  791. { "user": { "name": "elkbee" }, "process": { "name": "attrib" }, ...}
  792. { "index" : { "_id": "7" } }
  793. { "user": { "name": "root" }, "process": { "name": "attrib" }, ...}
  794. { "index" : { "_id": "8" } }
  795. { "user": { "name": "elkbee" }, "process": { "name": "bash" }, ...}
  796. { "index" : { "_id": "9" } }
  797. { "user": { "name": "root" }, "process": { "name": "cat" }, ...}
  798. { "index" : { "_id": "10" } }
  799. { "user": { "name": "elkbee" }, "process": { "name": "cat" }, ...}
  800. { "index" : { "_id": "11" } }
  801. { "user": { "name": "root" }, "process": { "name": "cat" }, ...}
  802. ----
  803. // NOTCONSOLE
  804. An EQL sequence query searches the data set:
  805. [source,eql]
  806. ----
  807. sequence by user.name
  808. [process where process.name == "attrib"]
  809. [process where process.name == "bash"]
  810. [process where process.name == "cat"]
  811. ----
  812. The query's event items correspond to the following states:
  813. * State A: `[process where process.name == "attrib"]`
  814. * State B: `[process where process.name == "bash"]`
  815. * Complete: `[process where process.name == "cat"]`
  816. image::images/eql/sequence-state-machine.svg[align="center"]
  817. To find matching sequences, the query uses separate state machines for each
  818. unique `user.name` value. Based on the data set, you can expect two state
  819. machines: one for the `root` user and one for `elkbee`.
  820. image::images/eql/separate-state-machines.svg[align="center"]
  821. Pending sequence matches move through each machine's states as follows:
  822. [source,txt]
  823. ----
  824. { "index" : { "_id": "1" } }
  825. { "user": { "name": "root" }, "process": { "name": "attrib" }, ...}
  826. // Creates sequence [1] in state A for the "root" user.
  827. //
  828. // +------------------------"root"------------------------+
  829. // | +-----------+ +-----------+ +------------+ |
  830. // | | State A | | State B | | Complete | |
  831. // | +-----------+ +-----------+ +------------+ |
  832. // | | [1] | | | | | |
  833. // | +-----------+ +-----------+ +------------+ |
  834. // +------------------------------------------------------+
  835. { "index" : { "_id": "2" } }
  836. { "user": { "name": "root" }, "process": { "name": "attrib" }, ...}
  837. // Creates sequence [2] in state A for "root", overwriting sequence [1].
  838. //
  839. // +------------------------"root"------------------------+
  840. // | +-----------+ +-----------+ +------------+ |
  841. // | | State A | | State B | | Complete | |
  842. // | +-----------+ +-----------+ +------------+ |
  843. // | | [2] | | | | | |
  844. // | +-----------+ +-----------+ +------------+ |
  845. // +------------------------------------------------------+
  846. { "index" : { "_id": "3" } }
  847. { "user": { "name": "elkbee" }, "process": { "name": "bash" }, ...}
  848. // Nothing happens. The "elkbee" user has no pending sequence to move
  849. // from state A to state B.
  850. //
  851. // +-----------------------"elkbee"-----------------------+
  852. // | +-----------+ +-----------+ +------------+ |
  853. // | | State A | | State B | | Complete | |
  854. // | +-----------+ +-----------+ +------------+ |
  855. // | | | | | | | |
  856. // | +-----------+ +-----------+ +------------+ |
  857. // +------------------------------------------------------+
  858. { "index" : { "_id": "4" } }
  859. { "user": { "name": "root" }, "process": { "name": "bash" }, ...}
  860. // Sequence [2] moves out of state A for "root".
  861. // State B for "root" now contains [2, 4].
  862. // State A for "root" is empty.
  863. //
  864. // +------------------------"root"------------------------+
  865. // | +-----------+ +-----------+ +------------+ |
  866. // | | State A | | State B | | Complete | |
  867. // | +-----------+ --> +-----------+ +------------+ |
  868. // | | | | [2, 4] | | | |
  869. // | +-----------+ +-----------+ +------------+ |
  870. // +------------------------------------------------------+
  871. { "index" : { "_id": "5" } }
  872. { "user": { "name": "root" }, "process": { "name": "bash" }, ...}
  873. // Nothing happens. State A is empty for "root".
  874. //
  875. // +------------------------"root"------------------------+
  876. // | +-----------+ +-----------+ +------------+ |
  877. // | | State A | | State B | | Complete | |
  878. // | +-----------+ +-----------+ +------------+ |
  879. // | | | | [2, 4] | | | |
  880. // | +-----------+ +-----------+ +------------+ |
  881. // +------------------------------------------------------+
  882. { "index" : { "_id": "6" } }
  883. { "user": { "name": "elkbee" }, "process": { "name": "attrib" }, ...}
  884. // Creates sequence [6] in state A for "elkbee".
  885. //
  886. // +-----------------------"elkbee"-----------------------+
  887. // | +-----------+ +-----------+ +------------+ |
  888. // | | State A | | State B | | Complete | |
  889. // | +-----------+ +-----------+ +------------+ |
  890. // | | [6] | | | | | |
  891. // | +-----------+ +-----------+ +------------+ |
  892. // +------------------------------------------------------+
  893. { "index" : { "_id": "7" } }
  894. { "user": { "name": "root" }, "process": { "name": "attrib" }, ...}
  895. // Creates sequence [7] in state A for "root".
  896. // Sequence [2, 4] remains in state B for "root".
  897. //
  898. // +------------------------"root"------------------------+
  899. // | +-----------+ +-----------+ +------------+ |
  900. // | | State A | | State B | | Complete | |
  901. // | +-----------+ +-----------+ +------------+ |
  902. // | | [7] | | [2, 4] | | | |
  903. // | +-----------+ +-----------+ +------------+ |
  904. // +------------------------------------------------------+
  905. { "index" : { "_id": "8" } }
  906. { "user": { "name": "elkbee" }, "process": { "name": "bash" }, ...}
  907. // Sequence [6, 8] moves to state B for "elkbee".
  908. // State A for "elkbee" is now empty.
  909. //
  910. // +-----------------------"elkbee"-----------------------+
  911. // | +-----------+ +-----------+ +------------+ |
  912. // | | State A | | State B | | Complete | |
  913. // | +-----------+ --> +-----------+ +------------+ |
  914. // | | | | [6, 8] | | | |
  915. // | +-----------+ +-----------+ +------------+ |
  916. // +------------------------------------------------------+
  917. { "index" : { "_id": "9" } }
  918. { "user": { "name": "root" }, "process": { "name": "cat" }, ...}
  919. // Sequence [2, 4, 9] is complete for "root".
  920. // State B for "root" is now empty.
  921. // Sequence [7] remains in state A.
  922. //
  923. // +------------------------"root"------------------------+
  924. // | +-----------+ +-----------+ +------------+ |
  925. // | | State A | | State B | | Complete | |
  926. // | +-----------+ +-----------+ --> +------------+ |
  927. // | | [7] | | | | [2, 4, 9] |
  928. // | +-----------+ +-----------+ +------------+ |
  929. // +------------------------------------------------------+
  930. { "index" : { "_id": "10" } }
  931. { "user": { "name": "elkbee" }, "process": { "name": "cat" }, ...}
  932. // Sequence [6, 8, 10] is complete for "elkbee".
  933. // State A and B for "elkbee" are now empty.
  934. //
  935. // +-----------------------"elkbee"-----------------------+
  936. // | +-----------+ +-----------+ +------------+ |
  937. // | | State A | | State B | | Complete | |
  938. // | +-----------+ +-----------+ --> +------------+ |
  939. // | | | | | | [6, 8, 10] |
  940. // | +-----------+ +-----------+ +------------+ |
  941. // +------------------------------------------------------+
  942. { "index" : { "_id": "11" } }
  943. { "user": { "name": "root" }, "process": { "name": "cat" }, ...}
  944. // Nothing happens.
  945. // The machines for "root" and "elkbee" remain the same.
  946. //
  947. // +------------------------"root"------------------------+
  948. // | +-----------+ +-----------+ +------------+ |
  949. // | | State A | | State B | | Complete | |
  950. // | +-----------+ +-----------+ +------------+ |
  951. // | | [7] | | | | [2, 4, 9] |
  952. // | +-----------+ +-----------+ +------------+ |
  953. // +------------------------------------------------------+
  954. //
  955. // +-----------------------"elkbee"-----------------------+
  956. // | +-----------+ +-----------+ +------------+ |
  957. // | | State A | | State B | | Complete | |
  958. // | +-----------+ +-----------+ +------------+ |
  959. // | | | | | | [6, 8, 10] |
  960. // | +-----------+ +-----------+ +------------+ |
  961. // +------------------------------------------------------+
  962. ----
  963. ====