eql.asciidoc 17 KB

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