esql-examples.asciidoc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. [[esql-examples]]
  2. == Examples
  3. ++++
  4. <titleabbrev>Examples</titleabbrev>
  5. ++++
  6. [discrete]
  7. === Aggregating and enriching windows event logs
  8. [source,esql]
  9. ----
  10. FROM logs-*
  11. | WHERE event.code IS NOT NULL
  12. | STATS event_code_count = count(event.code) by event.code,host.name
  13. | ENRICH win_events on event.code with event_description
  14. | WHERE event_description IS NOT NULL and host.name IS NOT NULL
  15. | RENAME event_description as event.description
  16. | SORT event_code_count desc
  17. | KEEP event_code_count,event.code,host.name,event.description
  18. ----
  19. * It starts by querying logs from indices that match the pattern "logs-*".
  20. * Filters events where the "event.code" field is not null.
  21. * Aggregates the count of events by "event.code" and "host.name."
  22. * Enriches the events with additional information using the "EVENT_DESCRIPTION" field.
  23. * Filters out events where "EVENT_DESCRIPTION" or "host.name" is null.
  24. * Renames "EVENT_DESCRIPTION" as "event.description."
  25. * Sorts the result by "event_code_count" in descending order.
  26. * Keeps only selected fields: "event_code_count," "event.code," "host.name," and "event.description."
  27. [discrete]
  28. === Summing outbound traffic from a process `curl.exe`
  29. [source,esql]
  30. ----
  31. FROM logs-endpoint
  32. | WHERE process.name == "curl.exe"
  33. | STATS bytes = SUM(destination.bytes) BY destination.address
  34. | EVAL kb = bytes/1024
  35. | SORT kb desc
  36. | LIMIT 10
  37. | KEEP kb,destination.address
  38. ----
  39. * Queries logs from the "logs-endpoint" source.
  40. * Filters events where the "process.name" field is "curl.exe."
  41. * Calculates the sum of bytes sent to destination addresses and converts it to kilobytes (KB).
  42. * Sorts the results by "kb" (kilobytes) in descending order.
  43. * Limits the output to the top 10 results.
  44. * Keeps only the "kb" and "destination.address" fields.
  45. [discrete]
  46. === Manipulating DNS logs to find a high number of unique dns queries per registered domain
  47. [source,esql]
  48. ----
  49. FROM logs-*
  50. | GROK dns.question.name "%{DATA}\\.%{GREEDYDATA:dns.question.registered_domain:string}"
  51. | STATS unique_queries = count_distinct(dns.question.name) by dns.question.registered_domain, process.name
  52. | WHERE unique_queries > 10
  53. | SORT unique_queries DESC
  54. | RENAME unique_queries AS `Unique Queries`, dns.question.registered_domain AS `Registered Domain`, process.name AS `Process`
  55. ----
  56. * Queries logs from indices matching "logs-*."
  57. * Uses the "grok" pattern to extract the registered domain from the "dns.question.name" field.
  58. * Calculates the count of unique DNS queries per registered domain and process name.
  59. * Filters results where "unique_queries" are greater than 10.
  60. * Sorts the results by "unique_queries" in descending order.
  61. * Renames fields for clarity: "unique_queries" to "Unique Queries," "dns.question.registered_domain" to "Registered Domain," and "process.name" to "Process."
  62. [discrete]
  63. === Identifying high-numbers of outbound user connections
  64. [source,esql]
  65. ----
  66. FROM logs-*
  67. | WHERE NOT CIDR_MATCH(destination.ip, "10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16")
  68. | STATS destcount = COUNT(destination.ip) BY user.name, host.name
  69. | ENRICH ldap_lookup_new ON user.name
  70. | WHERE group.name IS NOT NULL
  71. | EVAL follow_up = CASE(destcount >= 100, "true","false")
  72. | SORT destcount desc
  73. | KEEP destcount, host.name, user.name, group.name, follow_up
  74. ----
  75. * Queries logs from indices matching "logs-*."
  76. * Filters out events where the destination IP address falls within private IP address ranges (e.g., 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16).
  77. * Calculates the count of unique destination IPs by "user.name" and "host.name."
  78. * Enriches the "user.name" field with LDAP group information.
  79. * Filters out results where "group.name" is not null.
  80. * Uses a "CASE" statement to create a "follow_up" field, setting it to "true" when "destcount" is greater than or equal to 100 and "false" otherwise.
  81. * Sorts the results by "destcount" in descending order.
  82. * Keeps selected fields: "destcount," "host.name," "user.name," "group.name," and "follow_up."