syntax.asciidoc 17 KB

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