eql.asciidoc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[eql]]
  4. = EQL search
  5. ++++
  6. <titleabbrev>EQL</titleabbrev>
  7. ++++
  8. Event Query Language (EQL) is a query language for event-based time series
  9. data, such as logs, metrics, and traces.
  10. [discrete]
  11. [[eql-advantages]]
  12. == Advantages of EQL
  13. * *EQL lets you express relationships between events.* +
  14. Many query languages allow you to match single events. EQL lets you match a
  15. sequence of events across different event categories and time spans.
  16. * *EQL has a low learning curve.* +
  17. <<eql-syntax,EQL syntax>> looks like other common query languages, such as SQL.
  18. EQL lets you write and read queries intuitively, which makes for quick,
  19. iterative searching.
  20. * *EQL is designed for security use cases.* +
  21. While you can use it for any event-based data, we created EQL for threat
  22. hunting. EQL not only supports indicator of compromise (IOC) searches but can
  23. describe activity that goes beyond IOCs.
  24. [discrete]
  25. [[eql-required-fields]]
  26. == Required fields
  27. To run an EQL search, the searched data stream or index must contain a
  28. _timestamp_ and _event category_ field. By default, EQL uses the `@timestamp`
  29. and `event.category` fields from the {ecs-ref}[Elastic Common Schema
  30. (ECS)]. To use a different timestamp or event category field, see
  31. <<specify-a-timestamp-or-event-category-field>>.
  32. TIP: While no schema is required to use EQL, we recommend using the
  33. {ecs-ref}[ECS]. EQL searches are designed to work with core ECS fields by
  34. default.
  35. [discrete]
  36. [[run-an-eql-search]]
  37. == Run an EQL search
  38. Use the <<eql-search-api,EQL search API>> to run a <<eql-basic-syntax,basic EQL
  39. query>>. If the {es} {security-features} are enabled, you must have the `read`
  40. <<privileges-list-indices,index privilege>> for the target data stream, index,
  41. or index alias.
  42. [source,console]
  43. ----
  44. GET /my-index-000001/_eql/search
  45. {
  46. "query": """
  47. process where process.name == "regsvr32.exe"
  48. """
  49. }
  50. ----
  51. // TEST[setup:sec_logs]
  52. By default, basic EQL queries return the 10 most recent matching events in the
  53. `hits.events` property. These hits are sorted by timestamp, converted to
  54. milliseconds since the {wikipedia}/Unix_time[Unix epoch], in ascending order.
  55. [source,console-result]
  56. ----
  57. {
  58. "is_partial": false,
  59. "is_running": false,
  60. "took": 60,
  61. "timed_out": false,
  62. "hits": {
  63. "total": {
  64. "value": 2,
  65. "relation": "eq"
  66. },
  67. "events": [
  68. {
  69. "_index": "my-index-000001",
  70. "_id": "OQmfCaduce8zoHT93o4H",
  71. "_source": {
  72. "@timestamp": "2099-12-07T11:07:09.000Z",
  73. "event": {
  74. "category": "process",
  75. "id": "aR3NWVOs",
  76. "sequence": 4
  77. },
  78. "process": {
  79. "pid": 2012,
  80. "name": "regsvr32.exe",
  81. "command_line": "regsvr32.exe /s /u /i:https://...RegSvr32.sct scrobj.dll",
  82. "executable": "C:\\Windows\\System32\\regsvr32.exe"
  83. }
  84. }
  85. },
  86. {
  87. "_index": "my-index-000001",
  88. "_id": "xLkCaj4EujzdNSxfYLbO",
  89. "_source": {
  90. "@timestamp": "2099-12-07T11:07:10.000Z",
  91. "event": {
  92. "category": "process",
  93. "id": "GTSmSqgz0U",
  94. "sequence": 6,
  95. "type": "termination"
  96. },
  97. "process": {
  98. "pid": 2012,
  99. "name": "regsvr32.exe",
  100. "executable": "C:\\Windows\\System32\\regsvr32.exe"
  101. }
  102. }
  103. }
  104. ]
  105. }
  106. }
  107. ----
  108. // TESTRESPONSE[s/"took": 60/"took": $body.took/]
  109. // TESTRESPONSE[s/"_id": "OQmfCaduce8zoHT93o4H"/"_id": $body.hits.events.0._id/]
  110. // TESTRESPONSE[s/"_id": "xLkCaj4EujzdNSxfYLbO"/"_id": $body.hits.events.1._id/]
  111. Use the `size` parameter to get a smaller or larger set of hits:
  112. [source,console]
  113. ----
  114. GET /my-index-000001/_eql/search
  115. {
  116. "query": """
  117. process where process.name == "regsvr32.exe"
  118. """,
  119. "size": 50
  120. }
  121. ----
  122. // TEST[setup:sec_logs]
  123. [discrete]
  124. [[eql-search-sequence]]
  125. === Search for a sequence of events
  126. Use EQL's <<eql-sequences,sequence syntax>> to search for a series of
  127. ordered events. List the event items in ascending chronological order,
  128. with the most recent event listed last:
  129. [source,console]
  130. ----
  131. GET /my-index-000001/_eql/search
  132. {
  133. "query": """
  134. sequence
  135. [ process where process.name == "regsvr32.exe" ]
  136. [ file where stringContains(file.name, "scrobj.dll") ]
  137. """
  138. }
  139. ----
  140. // TEST[setup:sec_logs]
  141. The response's `hits.sequences` property contains the 10 most recent matching
  142. sequences.
  143. [source,console-result]
  144. ----
  145. {
  146. "is_partial": false,
  147. "is_running": false,
  148. "took": 60,
  149. "timed_out": false,
  150. "hits": {
  151. "total": {
  152. "value": 1,
  153. "relation": "eq"
  154. },
  155. "sequences": [
  156. {
  157. "events": [
  158. {
  159. "_index": "my-index-000001",
  160. "_id": "OQmfCaduce8zoHT93o4H",
  161. "_source": {
  162. "@timestamp": "2099-12-07T11:07:09.000Z",
  163. "event": {
  164. "category": "process",
  165. "id": "aR3NWVOs",
  166. "sequence": 4
  167. },
  168. "process": {
  169. "pid": 2012,
  170. "name": "regsvr32.exe",
  171. "command_line": "regsvr32.exe /s /u /i:https://...RegSvr32.sct scrobj.dll",
  172. "executable": "C:\\Windows\\System32\\regsvr32.exe"
  173. }
  174. }
  175. },
  176. {
  177. "_index": "my-index-000001",
  178. "_id": "yDwnGIJouOYGBzP0ZE9n",
  179. "_source": {
  180. "@timestamp": "2099-12-07T11:07:10.000Z",
  181. "event": {
  182. "category": "file",
  183. "id": "tZ1NWVOs",
  184. "sequence": 5
  185. },
  186. "process": {
  187. "pid": 2012,
  188. "name": "regsvr32.exe",
  189. "executable": "C:\\Windows\\System32\\regsvr32.exe"
  190. },
  191. "file": {
  192. "path": "C:\\Windows\\System32\\scrobj.dll",
  193. "name": "scrobj.dll"
  194. }
  195. }
  196. }
  197. ]
  198. }
  199. ]
  200. }
  201. }
  202. ----
  203. // TESTRESPONSE[s/"took": 60/"took": $body.took/]
  204. // TESTRESPONSE[s/"_id": "OQmfCaduce8zoHT93o4H"/"_id": $body.hits.sequences.0.events.0._id/]
  205. // TESTRESPONSE[s/"_id": "yDwnGIJouOYGBzP0ZE9n"/"_id": $body.hits.sequences.0.events.1._id/]
  206. Use the <<eql-with-maxspan-keywords,`with maxspan` keywords>> to constrain
  207. matching sequences to a timespan:
  208. [source,console]
  209. ----
  210. GET /my-index-000001/_eql/search
  211. {
  212. "query": """
  213. sequence with maxspan=1h
  214. [ process where process.name == "regsvr32.exe" ]
  215. [ file where stringContains(file.name, "scrobj.dll") ]
  216. """
  217. }
  218. ----
  219. // TEST[setup:sec_logs]
  220. Use the <<eql-by-keyword,`by` keyword>> to match events that share the
  221. same field values:
  222. [source,console]
  223. ----
  224. GET /my-index-000001/_eql/search
  225. {
  226. "query": """
  227. sequence with maxspan=1h
  228. [ process where process.name == "regsvr32.exe" ] by process.pid
  229. [ file where stringContains(file.name, "scrobj.dll") ] by process.pid
  230. """
  231. }
  232. ----
  233. // TEST[setup:sec_logs]
  234. If a field value should be shared across all events, use the `sequence by`
  235. keyword. The following query is equivalent to the previous one.
  236. [source,console]
  237. ----
  238. GET /my-index-000001/_eql/search
  239. {
  240. "query": """
  241. sequence by process.pid with maxspan=1h
  242. [ process where process.name == "regsvr32.exe" ]
  243. [ file where stringContains(file.name, "scrobj.dll") ]
  244. """
  245. }
  246. ----
  247. // TEST[setup:sec_logs]
  248. The `hits.sequences.join_keys` property contains the shared field values.
  249. [source,console-result]
  250. ----
  251. {
  252. "is_partial": false,
  253. "is_running": false,
  254. "took": 60,
  255. "timed_out": false,
  256. "hits": {
  257. "total": {
  258. "value": 1,
  259. "relation": "eq"
  260. },
  261. "sequences": [
  262. {
  263. "join_keys": [
  264. 2012
  265. ],
  266. "events": [
  267. {
  268. "_index": "my-index-000001",
  269. "_id": "OQmfCaduce8zoHT93o4H",
  270. "_source": {
  271. "@timestamp": "2099-12-07T11:07:09.000Z",
  272. "event": {
  273. "category": "process",
  274. "id": "aR3NWVOs",
  275. "sequence": 4
  276. },
  277. "process": {
  278. "pid": 2012,
  279. "name": "regsvr32.exe",
  280. "command_line": "regsvr32.exe /s /u /i:https://...RegSvr32.sct scrobj.dll",
  281. "executable": "C:\\Windows\\System32\\regsvr32.exe"
  282. }
  283. }
  284. },
  285. {
  286. "_index": "my-index-000001",
  287. "_id": "yDwnGIJouOYGBzP0ZE9n",
  288. "_source": {
  289. "@timestamp": "2099-12-07T11:07:10.000Z",
  290. "event": {
  291. "category": "file",
  292. "id": "tZ1NWVOs",
  293. "sequence": 5
  294. },
  295. "process": {
  296. "pid": 2012,
  297. "name": "regsvr32.exe",
  298. "executable": "C:\\Windows\\System32\\regsvr32.exe"
  299. },
  300. "file": {
  301. "path": "C:\\Windows\\System32\\scrobj.dll",
  302. "name": "scrobj.dll"
  303. }
  304. }
  305. }
  306. ]
  307. }
  308. ]
  309. }
  310. }
  311. ----
  312. // TESTRESPONSE[s/"took": 60/"took": $body.took/]
  313. // TESTRESPONSE[s/"_id": "OQmfCaduce8zoHT93o4H"/"_id": $body.hits.sequences.0.events.0._id/]
  314. // TESTRESPONSE[s/"_id": "yDwnGIJouOYGBzP0ZE9n"/"_id": $body.hits.sequences.0.events.1._id/]
  315. Use the <<eql-until-keyword,`until` keyword>> to specify an expiration
  316. event for sequences. Matching sequences must end before this event.
  317. [source,console]
  318. ----
  319. GET /my-index-000001/_eql/search
  320. {
  321. "query": """
  322. sequence by process.pid with maxspan=1h
  323. [ process where process.name == "regsvr32.exe" ]
  324. [ file where stringContains(file.name, "scrobj.dll") ]
  325. until [ process where event.type == "termination" ]
  326. """
  327. }
  328. ----
  329. // TEST[setup:sec_logs]
  330. [discrete]
  331. [[specify-a-timestamp-or-event-category-field]]
  332. === Specify a timestamp or event category field
  333. The EQL search API uses the `@timestamp` and `event.category` fields from the
  334. {ecs-ref}[ECS] by default. To specify different fields, use the
  335. `timestamp_field` and `event_category_field` parameters:
  336. [source,console]
  337. ----
  338. GET /my-index-000001/_eql/search
  339. {
  340. "timestamp_field": "file.accessed",
  341. "event_category_field": "file.type",
  342. "query": """
  343. file where (file.size > 1 and file.type == "file")
  344. """
  345. }
  346. ----
  347. // TEST[setup:sec_logs]
  348. The event category field must be mapped as a <<keyword,`keyword`>> family field
  349. type. The timestamp field should be mapped as a <<date,`date`>> field type.
  350. <<date_nanos,`date_nanos`>> timestamp fields are not supported. You cannot use a
  351. <<nested,`nested`>> field or the sub-fields of a `nested` field as the timestamp
  352. or event category field.
  353. [discrete]
  354. [[eql-search-specify-a-sort-tiebreaker]]
  355. === Specify a sort tiebreaker
  356. By default, the EQL search API returns matching hits by timestamp. If two or
  357. more events share the same timestamp, {es} uses a tiebreaker field value to sort
  358. the events in ascending order. {es} orders events with no
  359. tiebreaker value after events with a value.
  360. If you don't specify a tiebreaker field or the events also share the same
  361. tiebreaker value, {es} considers the events concurrent. Concurrent events cannot
  362. be part of the same sequence and may not be returned in a consistent sort order.
  363. To specify a tiebreaker field, use the `tiebreaker_field` parameter. If you use
  364. the {ecs-ref}[ECS], we recommend using `event.sequence` as the tiebreaker field.
  365. [source,console]
  366. ----
  367. GET /my-index-000001/_eql/search
  368. {
  369. "tiebreaker_field": "event.sequence",
  370. "query": """
  371. process where process.name == "cmd.exe" and stringContains(process.executable, "System32")
  372. """
  373. }
  374. ----
  375. // TEST[setup:sec_logs]
  376. [discrete]
  377. [[eql-search-filter-query-dsl]]
  378. === Filter using query DSL
  379. The `filter` parameter uses <<query-dsl,query DSL>> to limit the documents on
  380. which an EQL query runs.
  381. [source,console]
  382. ----
  383. GET /my-index-000001/_eql/search
  384. {
  385. "filter": {
  386. "range" : {
  387. "file.size" : {
  388. "gte" : 1,
  389. "lte" : 1000000
  390. }
  391. }
  392. },
  393. "query": """
  394. file where (file.type == "file" and file.name == "cmd.exe")
  395. """
  396. }
  397. ----
  398. // TEST[setup:sec_logs]
  399. [discrete]
  400. [[eql-search-async]]
  401. === Run an async EQL search
  402. By default, EQL search requests are synchronous and wait for complete results
  403. before returning a response. However, complete results can take longer for
  404. searches across <<frozen-indices,frozen indices>> or
  405. <<modules-cross-cluster-search,multiple clusters>>.
  406. To avoid long waits, run an async EQL search. Set the
  407. `wait_for_completion_timeout` parameter to a duration you'd like to wait for
  408. synchronous results.
  409. [source,console]
  410. ----
  411. GET /frozen-my-index-000001/_eql/search
  412. {
  413. "wait_for_completion_timeout": "2s",
  414. "query": """
  415. process where process.name == "cmd.exe"
  416. """
  417. }
  418. ----
  419. // TEST[setup:sec_logs]
  420. // TEST[s/frozen-my-index-000001/my-index-000001/]
  421. If the request doesn't finish within the timeout period, the search becomes async
  422. and returns a response that includes:
  423. * A search ID
  424. * An `is_partial` value of `true`, indicating the search results are
  425. incomplete
  426. * An `is_running` value of `true`, indicating the search is ongoing
  427. The async search continues to run in the background without blocking other
  428. requests.
  429. [source,console-result]
  430. ----
  431. {
  432. "id": "FmNJRUZ1YWZCU3dHY1BIOUhaenVSRkEaaXFlZ3h4c1RTWFNocDdnY2FSaERnUTozNDE=",
  433. "is_partial": true,
  434. "is_running": true,
  435. "took": 2000,
  436. "timed_out": false,
  437. "hits": ...
  438. }
  439. ----
  440. // TESTRESPONSE[s/FmNJRUZ1YWZCU3dHY1BIOUhaenVSRkEaaXFlZ3h4c1RTWFNocDdnY2FSaERnUTozNDE=/$body.id/]
  441. // TESTRESPONSE[s/"is_partial": true/"is_partial": $body.is_partial/]
  442. // TESTRESPONSE[s/"is_running": true/"is_running": $body.is_running/]
  443. // TESTRESPONSE[s/"took": 2000/"took": $body.took/]
  444. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]
  445. To check the progress of an async search, use the <<get-async-eql-search-api,get
  446. async EQL search API>> with the search ID. Specify how long you'd like for
  447. complete results in the `wait_for_completion_timeout` parameter. If the {es}
  448. {security-features} are enabled, only the user who first submitted the EQL
  449. search can retrieve the search using this API.
  450. [source,console]
  451. ----
  452. GET /_eql/search/FmNJRUZ1YWZCU3dHY1BIOUhaenVSRkEaaXFlZ3h4c1RTWFNocDdnY2FSaERnUTozNDE=?wait_for_completion_timeout=2s
  453. ----
  454. // TEST[skip: no access to search ID]
  455. If the response's `is_running` value is `false`, the async search has finished.
  456. If the `is_partial` value is `false`, the returned search results are
  457. complete.
  458. [source,console-result]
  459. ----
  460. {
  461. "id": "FmNJRUZ1YWZCU3dHY1BIOUhaenVSRkEaaXFlZ3h4c1RTWFNocDdnY2FSaERnUTozNDE=",
  462. "is_partial": false,
  463. "is_running": false,
  464. "took": 2000,
  465. "timed_out": false,
  466. "hits": ...
  467. }
  468. ----
  469. // TESTRESPONSE[s/FmNJRUZ1YWZCU3dHY1BIOUhaenVSRkEaaXFlZ3h4c1RTWFNocDdnY2FSaERnUTozNDE=/$body.id/]
  470. // TESTRESPONSE[s/"took": 2000/"took": $body.took/]
  471. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]
  472. [discrete]
  473. [[eql-search-store-async-eql-search]]
  474. === Change the search retention period
  475. By default, the EQL search API stores async searches for five days. After this
  476. period, any searches and their results are deleted. Use the `keep_alive`
  477. parameter to change this retention period:
  478. [source,console]
  479. ----
  480. GET /my-index-000001/_eql/search
  481. {
  482. "keep_alive": "2d",
  483. "wait_for_completion_timeout": "2s",
  484. "query": """
  485. process where process.name == "cmd.exe"
  486. """
  487. }
  488. ----
  489. // TEST[setup:sec_logs]
  490. You can use the <<get-async-eql-search-api,get async EQL search API>>'s
  491. `keep_alive` parameter to later change the retention period. The new retention
  492. period starts after the get request runs.
  493. [source,console]
  494. ----
  495. GET /_eql/search/FmNJRUZ1YWZCU3dHY1BIOUhaenVSRkEaaXFlZ3h4c1RTWFNocDdnY2FSaERnUTozNDE=?keep_alive=5d
  496. ----
  497. // TEST[skip: no access to search ID]
  498. Use the <<delete-async-eql-search-api,delete async EQL search API>> to
  499. manually delete an async EQL search before the `keep_alive` period ends. If the
  500. search is still ongoing, {es} cancels the search request. If the {es}
  501. {security-features} are enabled, only the user who first submitted the EQL
  502. search can delete the search using this API.
  503. [source,console]
  504. ----
  505. DELETE /_eql/search/FmNJRUZ1YWZCU3dHY1BIOUhaenVSRkEaaXFlZ3h4c1RTWFNocDdnY2FSaERnUTozNDE=?keep_alive=5d
  506. ----
  507. // TEST[skip: no access to search ID]
  508. [discrete]
  509. [[eql-search-store-sync-eql-search]]
  510. === Store synchronous EQL searches
  511. By default, the EQL search API only stores async searches. To save a synchronous
  512. search, set `keep_on_completion` to `true`:
  513. [source,console]
  514. ----
  515. GET /my-index-000001/_eql/search
  516. {
  517. "keep_on_completion": true,
  518. "wait_for_completion_timeout": "2s",
  519. "query": """
  520. process where process.name == "cmd.exe"
  521. """
  522. }
  523. ----
  524. // TEST[setup:sec_logs]
  525. The response includes a search ID. `is_partial` and `is_running` are `false`,
  526. indicating the EQL search was synchronous and returned complete results.
  527. [source,console-result]
  528. ----
  529. {
  530. "id": "FjlmbndxNmJjU0RPdExBTGg0elNOOEEaQk9xSjJBQzBRMldZa1VVQ2pPa01YUToxMDY=",
  531. "is_partial": false,
  532. "is_running": false,
  533. "took": 52,
  534. "timed_out": false,
  535. "hits": ...
  536. }
  537. ----
  538. // TESTRESPONSE[s/FjlmbndxNmJjU0RPdExBTGg0elNOOEEaQk9xSjJBQzBRMldZa1VVQ2pPa01YUToxMDY=/$body.id/]
  539. // TESTRESPONSE[s/"took": 52/"took": $body.took/]
  540. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]
  541. Use the <<get-async-eql-search-api,get async EQL search API>> to get the
  542. same results later:
  543. [source,console]
  544. ----
  545. GET /_eql/search/FjlmbndxNmJjU0RPdExBTGg0elNOOEEaQk9xSjJBQzBRMldZa1VVQ2pPa01YUToxMDY=
  546. ----
  547. // TEST[skip: no access to search ID]
  548. Saved synchronous searches are still subject to the `keep_alive` parameter's
  549. retention period. When this period ends, the search and its results are deleted.
  550. You can also manually delete saved synchronous searches using the
  551. <<delete-async-eql-search-api,delete async EQL search API>>.
  552. include::syntax.asciidoc[]
  553. include::functions.asciidoc[]
  554. include::pipes.asciidoc[]
  555. include::detect-threats-with-eql.asciidoc[]