Time buckets
12 min · 2 ejercicio(s)
Attacks are bursts in time. To see them you group by time buckets.
FROM lab-web
| STATS requests = COUNT(*) BY day = DATE_TRUNC(1 day, @timestamp)
| SORT dayDATE_TRUNC(interval, date) rounds a date down. Intervals are written as 1 hour, 15 minutes, 1 day and so on.
BUCKET does the same, but can also pick the bucket size for you from a target number of buckets and a time range. It is what Kibana uses for charts:
FROM lab-auth
| WHERE event.outcome == "failure"
| STATS failures = COUNT(*) BY hour = BUCKET(@timestamp, 1 hour)
| SORT failures DESC
| LIMIT 5Date functions
| Function | Example |
|---|---|
DATE_EXTRACT("hour_of_day", @timestamp) |
0–23, find logons at night |
DATE_EXTRACT("day_of_week", @timestamp) |
1 (Monday) – 7 (Sunday) |
DATE_FORMAT("yyyy-MM-dd", @timestamp) |
Format as a string |
NOW() |
Current time, e.g. WHERE @timestamp > NOW() - 1 day |
🔎 Profundiza: funciones, errores habituales y práctica
DATE_TRUNC vs. BUCKET
DATE_TRUNC(1 hour, @timestamp) |
BUCKET(@timestamp, 1 hour) |
|
|---|---|---|
| Fixed interval | yes | yes |
| Automatic interval | no | BUCKET(@timestamp, 50, start, end): about 50 buckets between two dates |
| Numeric buckets | no | BUCKET(bytes, 10, 0, 1000000) |
| Use in | EVAL and STATS … BY |
mainly STATS … BY |
Time spans
Write intervals as a number plus a unit: 1 minute, 15 minutes, 1 hour, 1 day, 1 week, 1 month. You can also do date arithmetic: WHERE @timestamp > NOW() - 7 days.
Useful date functions
| Function | Example |
|---|---|
DATE_EXTRACT("hour_of_day", @timestamp) |
logons outside business hours |
DATE_EXTRACT("day_of_week", @timestamp) |
weekend activity |
DATE_DIFF("minute", first, last) |
duration of a session or beacon |
DATE_FORMAT("yyyy-MM-dd HH:mm", @timestamp) |
readable output |
DATE_PARSE("dd.MM.yyyy", s) |
parse dates from strings |
Time zones
All dates are stored in UTC. Business hours in Germany are 07:00–16:00 UTC in winter and 06:00–15:00 UTC in summer. Keep that in mind for "after hours" detections.