Lesson 5.4

Stacking and counting

Stack values with AQL: group and aggregate, top and rare, to find the noisy client, the rare request and the odd one out in thousands of records.

15 minEveryone
At a glance for experienced analysts
  • top N col and rare N col: the most and least frequent values, with a count.
  • group {col} (aggregate {n = count this, first = (earliest timestamp), last = (latest timestamp)}): a count and a time span per value.
  • Sort the groups with sort {-n} and keep the head with take.
Command line
filter event_type == "logs_nginx" | top 3 remote_addr

Thousands of records look alike. Stacking turns them into a short list where the unusual values stand out.

Who was busiest

filter event_type == "logs_nginx" | top 3 remote_addr
remote_addr n
203.0.113.45 647
135.64.176.122 24
139.132.162.34 23

One address sent 27 times more requests than the next. That is worth a closer look.

When, as well as how many

filter event_type == "logs_nginx" | group {remote_addr} (aggregate {n = count this, first = (earliest timestamp), last = (latest timestamp)}) | sort {-n} | take 10
Requests per client with first and last seen
203.0.113.45 sent 647 requests between 02:11:05 and 02:47:19: a scanner, finishing at the minute of the upload.

The busy client was active for only 36 minutes, while the others spread a few requests over weeks. A burst like this is typical of an automated scanner.

What happened once

filter event_type == "logs_nginx" && http_method == "POST" | rare 5 url

The rarest values are where single actions hide: an upload, a login from a new address, a command run once. Rare is not the same as bad, so read what each one is.

Patterns to remember

Question AQL
Most frequent values top 10 col
Least frequent values rare 10 col
Count and time span per value group {col} (aggregate {n = count this, first = (earliest timestamp), last = (latest timestamp)})
Count per pair of values group {col_a, col_b} (aggregate {n = count this})
Only the groups above a threshold add filter n > 100 after the group
The first record per value group {col} (sort {timestamp} | take 1)

Check yourself

1. Which question does rare answer best?
2. What does first = (earliest timestamp) add to a group?

Try it

On web01, find the POST request that was made only once to the web server. Which URL was it, from which address, and when?

Show the answer

filter event_type == "logs_nginx" && http_method == "POST" | rare 5 url returns two URLs: /quote/ with 115 posts and /wp-content/plugins/wp-file-manager/lib/php/connector.minimal.php with 1. Filter on that URL to see the request: 203.0.113.45 at 02:47:19 UTC, status 200. That is the file upload through WP File Manager that planted the web shell.

Progress is saved in this browser only.