syntax.asciidoc 37 KB

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