Investigation Queries Across Sources
Answer who, what, when and from where about the lab intrusion with OpenSearch Query DSL and PPL across SSH, web, DNS, DHCP and sign-in logs: turn each question into filters and aggregations, build a cross-source timeline, attribute an IP address to a person through the DHCP lease active at the time, and see how un-normalised user names break a join.
Contentsand lesson path
On this page
- Goal and setup
- From a question to a query
- Step 1: Who signed in over SSH, from where, and how often?
- Step 2: What did the failures look like?
- Step 3: What happened next, across every source?
- Step 4: Who else touched the script?
- Step 5: Which device held the address at that minute?
- Step 6: Who was signed in on that device?
- Step 7: Why the join needs normalised names
- Troubleshooting
- Check your understanding
- Apply it
- Review prompt
Lessons of Security Logging & SIEM
- Which Logs Answer Which Security Questions Concept · 20 min
- Standing Up a Lab SIEM and Onboarding Sources Lab · 30 min
- Parsing and Normalising to a Common Schema Worked example · 20 min
- Investigation Queries Across Sources Lab · 25 min
- Alert Triage, Case Notes and Playbook Automation Worked example · 30 min
- Logging & SIEM Check Knowledge check · 10 questions
What you will learn
- Write searches that answer investigation questions (who, what, when, from where) across several log sources.
01. Goal and setup
An alert says that web01 saw many failed SSH logins from 203.0.113.45 and then a success. In this lab you answer the questions an incident lead will ask: who logged in, from where and how often; what the attacker did next; who else touched what the attacker left behind; and which person sat behind an internal address at a given minute. Each answer is a search you can rerun, which is what makes it evidence.
You need the cluster and siem-lab/ folder from lesson 2 with the five pipelines of lesson 3. The block below rebuilds the indices from scratch, so it also repairs a lab you changed. Every search runs over the fixed day of 12 November 2026, and all times are UTC.
cd siem-lab
export OS=http://127.0.0.1:9200 OS_URL=http://127.0.0.1:9200
docker start siem-os > /dev/null # or the docker run command of lesson 2
until curl -s "$OS" > /dev/null; do sleep 3; done
.venv/bin/python make_logs.py > /dev/null && .venv/bin/python load.pypipelines: dhcp-lease, dns-query, idp-signin, linux-auth, nginx-access
logs/auth.log -> linux-auth sent 39, rejected 0
logs/access.log -> nginx-access sent 46, rejected 0
logs/idp.jsonl -> idp-signin sent 9, rejected 0
logs/dhcp.log -> dhcp-lease sent 7, rejected 0
logs/dns.jsonl -> dns-query sent 43, rejected 0To keep commands short, save this helper and load it into your shell with source q.sh. It sends the JSON query on standard input to INDEX/_search and uses filter_path to return only the parts you ask for.
# q INDEX FILTER_PATH: send the JSON query on stdin to INDEX/_search
q() {
curl -s "$OS/$1/_search?filter_path=$2" -H 'Content-Type: application/json' -d @-
}02. From a question to a query
Most investigation questions have the same parts. Who is a user or a host, what is an action and its outcome, when is a time range, and from where is an address, device or location. Each part becomes either a filter or a grouping. In OpenSearch Query DSL a bool query holds the filters: clauses under filter must all match and do not affect scoring, which is what you want for exact facts. Clauses under should are alternatives; when you combine them with filters, set minimum_should_match so that at least one must match. Groupings are aggregations: terms counts documents per value, cardinality counts distinct values, and min and max give the first and last time. Because @timestamp is a date field, ranges and sorting work across sources with different original time formats.
03. Step 1: Who signed in over SSH, from where, and how often?
The question has four parts: the source (SSH on web01), the outcome (success), the time (the whole day) and the grouping (per user, then per address). Each becomes one line of the query.
source q.sh
q 'logs-*' aggregations <<'JSON' | jq -r '.aggregations.user.buckets[]
| .key as $u | .ip.buckets[] | "\($u) \(.key) \(.doc_count)"'
{"size": 0,
"query": {"bool": {"filter": [
{"term": {"event.dataset": "linux.auth"}},
{"term": {"event.outcome": "success"}},
{"range": {"@timestamp": {"gte": "2026-11-12T00:00:00Z",
"lt": "2026-11-13T00:00:00Z"}}}]}},
"aggs": {"user": {"terms": {"field": "user.name", "order": {"_key": "asc"}},
"aggs": {"ip": {"terms": {"field": "source.ip"}}}}}}
JSONasmith 10.20.1.5 1
deploy 203.0.113.45 1
jdoe 10.20.1.5 1Two staff accounts logged in from the internal jump host 10.20.1.5, and deploy logged in once from the internet. Leave out the outcome filter and you count failures too; leave out the user grouping and you lose who did it. Small changes to a query answer a different question.
04. Step 2: What did the failures look like?
Group failed SSH logins by source address, and for each one count the distinct user names it tried and the first and last attempt.
source q.sh
q logs-auth aggregations <<'JSON' | jq -r '.aggregations.src.buckets[] | "\(.key)
failures=\(.doc_count) names=\(.names.value)
first=\(.first.value_as_string) last=\(.last.value_as_string)"'
{"size": 0,
"query": {"bool": {"filter": [{"term": {"event.action": "ssh_login"}},
{"term": {"event.outcome": "failure"}}]}},
"aggs": {"src": {"terms": {"field": "source.ip", "min_doc_count": 1},
"aggs": {"names": {"cardinality": {"field": "user.name"}},
"first": {"min": {"field": "@timestamp"}},
"last": {"max": {"field": "@timestamp"}}}}}}
JSON203.0.113.45
failures=30 names=8
first=2026-11-12T09:01:00.000Z last=2026-11-12T09:06:31.000Z
10.20.1.5
failures=1 names=1
first=2026-11-12T09:30:05.000Z last=2026-11-12T09:30:05.000ZThirty failures against eight names in five and a half minutes is password guessing. The MITRE ATT&CK page for T1110 Brute Force describes detection on this pattern: many failed logons followed by a successful one. One failure from the jump host is a typo by asmith, who logged in six seconds later. A detection threshold has to separate the two; check it in both directions.
source q.sh
for n in 10 1; do
printf 'at least %s failures: ' $n
q logs-auth aggregations <<JSON | jq -c '[.aggregations.src.buckets[].key]'
{"size": 0,
"query": {"bool": {"filter": [{"term": {"event.outcome": "failure"}},
{"range": {"@timestamp": {"gte": "2026-11-12T09:00:00Z",
"lt": "2026-11-12T10:00:00Z"}}}]}},
"aggs": {"src": {"terms": {"field": "source.ip", "min_doc_count": $n}}}}
JSON
doneat least 10 failures: ["203.0.113.45"]
at least 1 failures: ["203.0.113.45","10.20.1.5"]With a threshold of 10 failures in the hour, only the attacker remains. The benign typo must not alert, and with a threshold of 1 it would.
05. Step 3: What happened next, across every source?
Build one timeline from 09:06 UTC onwards of every event that mentions the attacker's address or the deploy account. The should clauses are the two alternatives, minimum_should_match makes one of them required, and the fields option returns @timestamp in UTC whatever offset the source used.
source q.sh
q 'logs-*' 'hits.hits._source,hits.hits.fields' <<'JSON' | jq -r '.hits.hits[]
| ._source as $s | [.fields["@timestamp"][0][11:19], $s.event.dataset,
($s.event.action // $s.url.original // $s.dns.question.name),
($s.event.outcome // "-"), ($s.source.ip // "-"), ($s.user.name // "-")]
| join(" ")'
{"size": 20,
"query": {"bool": {
"filter": [{"range": {"@timestamp": {"gte": "2026-11-12T09:06:00Z"}}}],
"should": [{"term": {"related.ip": "203.0.113.45"}},
{"term": {"related.user": "deploy"}}],
"minimum_should_match": 1}},
"sort": [{"@timestamp": "asc"}],
"fields": ["@timestamp"],
"_source": ["event.*", "url.original", "dns.question.name",
"source.ip", "user.name"]}
JSON09:06:10 linux.auth ssh_login failure 203.0.113.45 oracle
09:06:20 linux.auth ssh_login failure 203.0.113.45 ubuntu
09:06:31 linux.auth ssh_login failure 203.0.113.45 postgres
09:07:12 linux.auth ssh_login success 203.0.113.45 deploy
09:09:40 linux.auth sudo_command - - deploy
09:10:05 nginx.access /js/invoice.js success 203.0.113.45 -
09:15:02 dns.query cdn-update.example - 10.20.5.31 -
09:20:11 dns.query cdn-update.example - 10.20.5.44 -Read it as a story. The guessing ended at 09:06:31, deploy got in at 09:07:12, used sudo to copy invoice.js into the web root at 09:09:40, and the same address fetched it over HTTP at 09:10:05 to check it was served. Then two office laptops looked up cdn-update.example, which resolved to the attacker's address. The DNS events appear because the DNS pipeline copied answers into related.ip.
06. Step 4: Who else touched the script?
Ask nginx who downloaded /js/invoice.js, then ask the resolver who looked up cdn-update.example. The second search uses PPL, the Piped Processing Language of the OpenSearch SQL plugin, which reads like a shell pipeline. Backticks quote field names that contain dots.
source q.sh
q logs-nginx aggregations <<'JSON' | jq -r '.aggregations.ip.buckets[]
| "\(.key) \(.first.value_as_string)"'
{"size": 0,
"query": {"term": {"url.original": "/js/invoice.js"}},
"aggs": {"ip": {"terms": {"field": "source.ip", "order": {"first": "asc"}},
"aggs": {"first": {"min": {"field": "@timestamp"}}}}}}
JSON
PPL='source=logs-dns | where `dns.question.name` = "cdn-update.example"'
PPL="$PPL"' | stats count() as lookups, min(`@timestamp`) as first by `source.ip`'
jq -n --arg q "$PPL" '{query: $q}' \
| curl -s "$OS/_plugins/_ppl?format=jdbc" -H 'Content-Type: application/json' -d @- \
| jq -c '.datarows[]'203.0.113.45 2026-11-12T09:10:05.000Z
10.20.5.31 2026-11-12T09:14:07.000Z
10.20.5.44 2026-11-12T09:19:30.000Z
[1,"2026-11-12 09:15:02","10.20.5.31"]
[1,"2026-11-12 09:20:11","10.20.5.44"]Two internal addresses downloaded the script and, a minute later, looked up the attacker's domain. That points to a script that runs in a browser and calls home. The next question is who was using 10.20.5.31 at 09:14:07.
07. Step 5: Which device held the address at that minute?
DHCP gives addresses out for a limited time and reuses them, so the device that holds 10.20.5.31 now may not be the one that held it at 09:14. The helper below asks for the last DHCP event for an address at or before a given time. If that last event is a release, nobody held the address.
# lease_at IP TIME: the last DHCP event for IP at or before TIME (UTC)
source q.sh
lease_at() {
q logs-dhcp hits.hits._source <<JSON | jq -c '.hits.hits[]?._source
| {at: .["@timestamp"], action: .event.action, host: .related.hosts[0]}'
{"size": 1, "sort": [{"@timestamp": "desc"}],
"query": {"bool": {"filter": [{"term": {"client.ip": "$1"}},
{"range": {"@timestamp": {"lte": "$2"}}}]}},
"_source": ["@timestamp", "event.action", "related.hosts"]}
JSON
}source lease_at.sh
echo "now: $(lease_at 10.20.5.31 now)"
echo "at 09:14: $(lease_at 10.20.5.31 2026-11-12T09:14:07Z)"now: {"at":"2026-11-12T09:47:30.000Z","action":"dhcpack","host":"LT-0377"}
at 09:14: {"at":"2026-11-12T09:02:11.000Z","action":"dhcpack","host":"LT-0412"}Today's table says LT-0377, but at 09:14 the address belonged to LT-0412, which renewed it at 09:02:11 and released it at 09:31:05. Using the current lease would accuse the wrong person.
08. Step 6: Who was signed in on that device?
A device is not a person. Look for the last successful sign-in from LT-0412 before the download. On a Windows network, logon events such as 4624 on the workstation or EDR data answer the same question. Then repeat both steps for the second laptop.
# signin_before HOST TIME: the last successful sign-in from HOST at or before TIME
source q.sh
signin_before() {
q logs-idp hits.hits._source <<JSON | jq -c '[.hits.hits[]?._source
| {at: .["@timestamp"], user: .user.name, id: .related.user[0]}]'
{"size": 1, "sort": [{"@timestamp": "desc"}],
"query": {"bool": {"filter": [{"term": {"related.hosts": "$1"}},
{"term": {"event.outcome": "success"}},
{"range": {"@timestamp": {"lte": "$2"}}}]}},
"_source": ["@timestamp", "user.name", "related.user"]}
JSON
}source lease_at.sh; source signin_before.sh
signin_before LT-0412 2026-11-12T09:14:07Z
lease_at 10.20.5.44 2026-11-12T09:19:30Z
signin_before LT-0418 2026-11-12T09:19:30Z[{"at":"2026-11-12T08:31:10.000Z","user":"jdoe","id":"[email protected]"}]
{"at":"2026-11-12T08:10:02.000Z","action":"dhcpack","host":"LT-0418"}
[]jdoe signed in to mail from LT-0412 at 08:31, and the address was still leased to that laptop at 09:14, so jdoe is the likely user. For 10.20.5.44 the lease leads to LT-0418, but the sign-in log has nothing for that device. The honest answer is "user unknown from these sources", plus a request for the laptop's own logon events. An empty result is a finding, not a reason to guess.
09. Step 7: Why the join needs normalised names
Compare a search on the raw form of a name with a search on the normalised user.name, then see which sources each raw form of jdoe appears in.
source q.sh
for name in JDoe jdoe; do
printf 'user.name=%-5s ' $name
q 'logs-auth,logs-idp' aggregations <<JSON | jq -c '[.aggregations.ds.buckets[]?
| "\(.key)=\(.doc_count)"]'
{"size": 0, "query": {"term": {"user.name": "$name"}},
"aggs": {"ds": {"terms": {"field": "event.dataset", "order": {"_key": "asc"}}}}}
JSON
done
q 'logs-auth,logs-idp' aggregations <<'JSON' | jq -r '.aggregations.raw.buckets[]
| "related.user=\(.key) \([.ds.buckets[].key] | sort | join(" + "))"'
{"size": 0,
"aggs": {"raw": {"terms": {"field": "related.user", "order": {"_key": "asc"},
"include": ["CORP\\jdoe", "[email protected]", "jdoe"]},
"aggs": {"ds": {"terms": {"field": "event.dataset"}}}}}}
JSONuser.name=JDoe []
user.name=jdoe ["idp.signin=2","linux.auth=1"]
related.user=CORP\jdoe idp.signin
[email protected] idp.signin
related.user=jdoe idp.signin + linux.authJDoe finds nothing because keyword fields match exact strings. The raw forms CORP\jdoe and [email protected] exist only in the sign-in log, so a join on raw names between SSH and SSO finds no common user. Only the normalised jdoe appears in both. Without the work of lesson 3, step 6 would have found the person in one source and lost them in the other.
The answer for the incident lead: deploy on web01 was taken over from 203.0.113.45 at 09:07:12 UTC after password guessing; the attacker planted invoice.js; jdoe on LT-0412 downloaded it at 09:14:07; a second laptop did the same and its user is still unknown.
10. Troubleshooting
11. Check your understanding
12. Apply it
Practise on the skill project's question set:
- Write a query that finds every source address with at least 10 failed SSH logins followed by a success within 15 minutes. Test it against both the attacker and the
asmithtypo. - Rewrite step 1 in PPL and check that it gives the same three rows.
- Add a
lease_betweenhelper that lists every DHCP event for an address in a time window, and use it to show how10.20.5.31changed hands. - For each step, write one sentence of the answer and the exact query, as you would in a case note.
13. Review prompt
Without looking back:
- Which parts of an investigation question become filters, and which become aggregations?
- Why must DHCP attribution use the lease at the time of the event, and what does a release mean?
- What does
minimum_should_matchchange in aboolquery? - Why did a search for
JDoefind nothing, and how did normalisation fix the join?
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
10 questions · about 8 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.