In this section

0.3 Finding What You Need in Data

Module 0

Introduction

Records come in quantity. A single workstation can write thousands of events an hour, and an investigation usually starts with a question about a handful of them. Finding the right handful, and being sure you found all of it, is a skill of its own, and it doesn't belong to any one tool.

This lesson answers one question several ways, with tools that work very differently: a text search, a structured filter and a table. The question is simple: on one workstation, which programs started, as which user, and from what? The point is to see that every tool does the same four things, and that each has its own way of giving you a confidently wrong answer. Module 3 teaches querying languages in depth, including KQL and PowerShell. This lesson is the ground underneath them.

The tools here are grep and jq on a Linux command line, because they're free, fast and show each step plainly. Every output on this page is the real output of the command shown, run on the file. Windows has equivalents for each step, and section 3 shows them side by side, so nothing here depends on which system you use at work.

1

The Data and the Question

A file of recorded events, and what we want from it

The data is a file of 95 Windows events from a public research dataset, the same test domain as lesson 0.2, recorded while a program ran a command on one workstation. Each line is one event, written as a JSON object: a set of named fields and their values.

$ wc -l net-local-users.json
95 net-local-users.json
$ head -1 net-local-users.json | jq 'keys | length'
40
$ head -1 net-local-users.json | jq '{EventID, Channel, Hostname, EventTime}'
{
  "EventID": 5156,
  "Channel": "security",
  "Hostname": "WORKSTATION5.theshire.local",
  "EventTime": "2020-09-21 19:26:06"
}

The first event is already informative. It has 40 fields. Four of them are shown: an EventID, a Channel saying which log it came from, a Hostname and a time. Different event types carry different fields, and that's the first difficulty with real data: the rows aren't all the same shape.

Now the question. On WORKSTATION5, which programs started, as which user, and from what parent program? A program starting is recorded two ways in this file: Sysmon's event 1, process creation, and Windows' own Security event 4688. Both are in here, mixed in with network events, logons and others.

Before running anything, it's worth saying what a complete answer would look like. A list of process starts on one machine, each with a time, a program, a user and a parent. That's four fields, from one machine, from one kind of event. Writing the shape down first is how you'll know whether a tool's output actually answers the question, or only looks like it does.

It also helps to say which machine and which period, precisely. "WORKSTATION5" here means the host recorded as WORKSTATION5.theshire.local, and the period is whatever the file covers. Vague questions get vague answers from every tool, and the vagueness is usually invisible in the output.

The file is small enough to read by eye, which makes it a good place to learn: you can check every answer against the rows. Real files are far too large for that, which is exactly why the habits in this lesson matter. On a large file, you'll have only the tools' answers, and the checks in section 6 are how you'll know whether to trust them.

2

Searching the Text

grep finds characters, counts lines, and has opinions about case and dots

The quickest tool is a text search. On Linux and macOS that's grep, on Windows it's PowerShell's Select-String or the find box in any editor. It looks for a string of characters anywhere in a line.

$ grep -c "net.exe" net-local-users.json
4
$ grep -c "NET.EXE" net-local-users.json
0
$ grep -c -i "NET.EXE" net-local-users.json
4

The first search finds 4 lines containing net.exe. That's a reasonable start, and it's already not quite what it seems. grep's -c option counts matching lines, not matching events of a kind: a line counts if net.exe appears anywhere in it, including as another program's parent. Some of those four lines are events for a different program, net1.exe, whose parent field happens to mention net.exe.

The second search finds nothing, because grep is case sensitive by default and the file never writes NET.EXE in capitals. The third, with -i to ignore case, finds the same four again. Windows itself treats file names without regard to case, so the same program can appear as net.exe, NET.EXE or Net.exe in different records. A case-sensitive search can miss it entirely and report zero, which looks exactly like "the program never ran".

There's one more trap in that pattern. In grep's default syntax a dot matches any character, so net.exe would also match a line containing netXexe. Here it doesn't matter. With an IP address such as 10.1.1.5, it can, because the dots would match other digits. The -F option makes grep treat the pattern as plain text.

Text search is still the right first move surprisingly often. It's fast, it works on any file in any format, and it tells you in seconds whether a string appears at all. What it can't do is understand the structure: it doesn't know which field the match was in, and it can't count per machine or per user.

A good use of text search, then, is reconnaissance. Search for the thing you care about, read a few of the matching lines in full, and learn which fields it appears in and how they're written. Then switch to a structured tool for the actual answer. The two minutes spent reading raw lines tell you the field names and value formats your query will need, and that's what prevents the empty-result problem in section 7.

3

Filtering on Fields

A structured filter keeps the events you mean and the fields that answer

A structured tool reads the fields, not just the characters. For JSON, jq is the standard command-line tool. Its select function keeps only the objects where a condition is true, and you can then pick which fields to show:

$ jq 'select(.EventID == 1 and .Hostname == "WORKSTATION5.theshire.local")
    | {UtcTime, User, Image, ParentImage}' net-local-users.json
{
  "UtcTime": "2020-09-21 23:26:20.869",
  "User": "THESHIRE\\pgustavo",
  "Image": "C:\\Windows\\System32\\net.exe",
  "ParentImage": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
}
{
  "UtcTime": "2020-09-21 23:26:20.885",
  "User": "THESHIRE\\pgustavo",
  "Image": "C:\\Windows\\System32\\net1.exe",
  "ParentImage": "C:\\Windows\\System32\\net.exe"
}

That's the answer to the question, for this machine, from Sysmon's events: two process starts. The user THESHIRE\pgustavo started net.exe from PowerShell, and net.exe started net1.exe. Each object carries exactly the four fields the question asked for, and nothing else. The doubled backslashes in the paths are how JSON writes a single backslash, so C:\\Windows in the output is C:\Windows on the machine.

Three things made this better than the text search. The filter compared EventID to 1 exactly, so no other event type crept in. It tested the Hostname field, so a mention of WORKSTATION5 elsewhere in a line couldn't match. And it picked fields by name, so the output is the shape the question asked for.

The price is that you need to know the field names. Every structured tool, whether jq, a spreadsheet, KQL or PowerShell, filters on fields you name, and a field name that's wrong doesn't usually cause an error. It causes an empty result. That's the most important thing to remember about structured queries, and section 7 shows it happening.

The value format matters as much as the name. EventID here is a number, so it's compared with 1, not "1". The host name is the full domain name, so the filter compares it with WORKSTATION5.theshire.local; a test for WORKSTATION5 alone would equal nothing. Getting either wrong returns nothing, silently. Reading a row first, as section 1 did, is how you find out what form the values take.

The same filter, written in two other tools, looks like this:

Get-Content net-local-users.json | ConvertFrom-Json |
  Where-Object { $_.EventID -eq 1 -and $_.Hostname -like 'WORKSTATION5*' } |
  Select-Object UtcTime, User, Image, ParentImage
# the same filter in KQL, against a table of these events
SysmonEvents
| where EventID == 1 and Hostname startswith "WORKSTATION5"
| project UtcTime, User, Image, ParentImage

The words differ: Where-Object, where, select. The idea is identical: keep the rows where these fields have these values, then keep these columns. Once you can see that, a new query language is mostly a matter of new spelling.

These two listings aren't run here: the PowerShell needs a Windows machine and the KQL needs a workspace holding these events, and the lab machine has neither. Module 3 runs both, on Windows and in a workspace. Read them now for the shape of the filter, which matches the jq above clause for clause.

4

Counting and Sorting

Which machines and event types the file holds, most common first

Before trusting any answer, it helps to know what's in the data overall. Counting and sorting do that. This command takes each event's machine and event ID, counts how often each pair appears, and sorts the result, most common first:

$ jq -r '[.Hostname, .EventID] | @tsv' net-local-users.json | sort | uniq -c \
    | sort -rn | head -8
     33 MORDORDC.theshire.local	5156
     24 WORKSTATION6.theshire.local	5156
      8 WORKSTATION5.theshire.local	5156
      7 WORKSTATION6.theshire.local	3
      4 MORDORDC.theshire.local	4627
      4 MORDORDC.theshire.local	4624
      3 WORKSTATION6.theshire.local	4688
      3 WORKSTATION6.theshire.local	1

The most common pair is MORDORDC.theshire.local with event 5156, 33 times. That's a Windows Filtering Platform event, recording a network connection the firewall allowed. Process starts, the events we want, are a small minority. That's normal: most records in any log are routine, and the interesting ones are a few among many.

Counts answer useful questions on their own. Which machines are in the data at all? Is one producing far more events than the others? Does an event type you expected appear zero times? A count of zero for an event you need is a finding about the data, not about the machine, and it's much better to discover it here than after writing a conclusion.

Counting also shows the shape a query result should have. If you expect a few process starts on WORKSTATION5 and the count says thousands, the filter is too loose. If you expect some and the count says none, the filter is wrong or the data doesn't hold them. Either way, you found out before relying on the result.

Most data tools have the same pair of operations: a way to group and count, and a way to sort. In a spreadsheet it's a pivot table, in KQL it's summarize and order by, and in PowerShell it's Group-Object and Sort-Object. On the command line it's sort and uniq -c.

A count is also the quickest way to learn an unfamiliar data set. Count by event type, by machine and by hour before you go looking for anything specific. Five minutes spent on counts tells you what the data covers, which sources are quiet and which are noisy, and where the gaps are. Module 3 calls this profiling the data, and it comes before every serious query.

Notice too that the count is the first time the whole file has been summarized. Everything before it looked at selected lines. Summaries like this are where surprises show up: a machine you didn't know was in the data, an event type nobody mentioned, a source that wrote nothing at all.

5

From Events to a Table

Exporting the rows to CSV, and reading them as a spreadsheet would

Investigations often end up in a table: a spreadsheet passed to a colleague, a CSV attached to a case, a timeline in a report. Converting structured events to CSV is a routine step, and it's where data quietly changes shape.

$ jq -r 'select(.EventID == 1 and .Hostname == "WORKSTATION5.theshire.local")
    | [.UtcTime, .User, .Image] | @csv' net-local-users.json > procs.csv
$ cat procs.csv
"2020-09-21 23:26:20.869","THESHIRE\pgustavo","C:\Windows\System32\net.exe"
"2020-09-21 23:26:20.885","THESHIRE\pgustavo","C:\Windows\System32\net1.exe"
$ rm procs.csv

The two process starts, each on one row with three columns, every value quoted. Opened in a spreadsheet, the same file becomes a grid you can sort and filter by clicking. The table is easier to read than JSON, easier to sort by hand and easier to share with someone who doesn't use a command line.

It's also lossy. Only the three fields chosen survived, and the rest of each event was left behind. The user name lost its escaped backslash and now reads THESHIRE\pgustavo as plain text, which is right here but is the kind of change that can break a value elsewhere. Commas inside a value would need quoting, which the CSV format handles and some tools reading it don't.

Filterkeep the rows that matterPick fieldskeep the columns that answerCounthow many, per whatSortmost, least, first, lastCheckdoes it answer the question?

Four operations every data tool performs, and the check that makes the answer usable

Those are the four operations every data tool performs, whatever it looks like: filter the rows, pick the fields, count, and sort. The fifth step is the one that makes the answer usable, and it's the subject of the next section.

When you export data for a case, keep the command or query that made the export with it, as lesson 1.7 describes for evidence. A table without the query behind it can't be reproduced, and a reviewer can't tell what was left out.

Tables also lose type. In JSON, EventID was a number and the time was clearly a time. In CSV everything is text, and a spreadsheet will guess what each column holds when it opens the file. Spreadsheets are known to turn long numbers into rounded scientific notation and some text into dates. For an evidence export, open the CSV as text first, or import it with each column set to text, so the values you cite are the values that were exported.

6

Checking the Answer

Two sources, one question, and the result that should make you suspicious
CHECK BEFORE YOU USE THE ANSWER1Filterkeep the events youmean2Pickkeep the fields thatanswer3Countper machine, in eachsourceAgree?Use the answerboth sources give the same countsInvestigate firstconfiguration, collection or field namesHere they agree: Sysmon and 4688 both count 2 starts on WORKSTATION5 and 3 on WORKSTATION6.

The check comes before the answer is used, and the fork decides what happens next

The figure puts the check where it belongs: after filtering, picking and counting, and before the answer is used. An answer from a tool isn't checked until you've tested it against something independent. The best check is a second source that should agree. This file records process starts twice, in Sysmon's events and in Windows' own 4688 events, so ask both:

$ jq -r 'select(.EventID == 1) | .Hostname' net-local-users.json | sort | uniq -c
      2 WORKSTATION5.theshire.local
      3 WORKSTATION6.theshire.local
$ jq -r 'select(.EventID == 4688) | .Hostname' net-local-users.json | sort | uniq -c
      2 WORKSTATION5.theshire.local
      3 WORKSTATION6.theshire.local

They agree: two process starts on WORKSTATION5 and three on WORKSTATION6, in both sources. That's good evidence the answer is complete for these machines and this period. Had they disagreed, the difference would itself be worth investigating: one source configured differently, events lost in collection, or a filter that matched one source's field names and not the other's.

Notice that the two sources are different kinds of record. Sysmon is a separate tool someone installed and configured. Event 4688 is Windows' own audit log, which also has to be switched on. Agreement between two independently configured sources is stronger evidence than any amount of re-running the same query on one of them.

Searching textgrep, Select-String, a find boxFast and everywhereworks on any fileMatches charactersnot fieldsCounts linesnot events or matchesQuerying fieldsjq, KQL, SPL, a spreadsheet filterKnows the structurefilter on one fieldCounts and groupsper machine, per userNeeds the schemafield names must be right

Two ways to find things in data, and what each is good for

The two kinds of tool fail differently, and the checks follow from that. Text search fails by matching too much or too little: other fields, other cases, the dot. Check it by looking at a few matched lines. Structured queries fail by asking for a field that doesn't exist, or holds values in a form you didn't expect. Check them by counting first, as section 4 does, and by planting a case: search for something you know is there and confirm it comes back.

That last check matters most. An empty result is only a finding if the query was capable of returning something. Before reporting that a program never ran, run the same query for a program you know did run. If that comes back empty too, the query is broken, not the machine clean.

Three more questions are worth asking of any result. Does it cover the time window you care about? Does it cover all the machines you care about? And does the field you filtered on mean what you think it means in this data? Each has caught experienced analysts out.

Write the checks you ran into your notes alongside the answer. "Two process starts on WORKSTATION5 in Sysmon event 1; the same two in 4688" is a finding with its own evidence of completeness. Without the second half, a reader can't tell whether you checked.

7

When an Assistant Writes the Query

A command that runs, returns nothing, and looks like an answer

Assistants are useful for writing queries in tools you don't use every day, and jq's syntax is a good example of one worth getting help with. Here's an assistant asked for exactly the filter from section 3:

AI assistantNew chat

I have a file of Windows events as JSON lines. Give me a jq command to list process creations on WORKSTATION5.

Use this: jq 'select(.EventId == 1 and .Computer == "WORKSTATION5")' events.json

This selects Sysmon process creation events (Event ID 1) for that computer.

Reply to AI assistant...

Check before you use it

It runs without an error and returns nothing. This file names the field EventID, not EventId, and the machine is in Hostname as WORKSTATION5.theshire.local, not in a Computer field. Both guesses are reasonable for other exports. An empty result from a wrong field name looks exactly like "no such events".

An illustrative exchange, written for this lesson: an assistant you ask will word it differently.

The command is syntactically fine and would run. It would also return nothing, because it guesses two field names: EventId instead of EventID, and Computer instead of Hostname. Both guesses are reasonable, because other exports of Windows events do use those names. This file doesn't.

That's the most common way an assistant's query goes wrong, and the most dangerous, because it doesn't fail loudly. An analyst who runs it sees an empty result and may conclude no processes started on WORKSTATION5, which is false. Nothing on the screen says the field names were wrong.

The fix is the habit from section 6, applied to every query an assistant writes. Before trusting an empty result, check the field names against a real row: the head command in section 1 shows them. Then plant a case: run the query for something you know is there. If an assistant can see a sample row, give it one, and its field names will usually be right first time.

Assistants are good at explaining a query you don't understand, too. Asked what each part of the jq in section 4 does, an assistant will usually explain it well, and that explanation is often the quickest way to learn a new tool's syntax. Check its explanation by running the parts one at a time and reading what each produces.

The same discipline applies to queries in any language an assistant writes for you: KQL, SPL, PowerShell, SQL or a spreadsheet formula. The syntax may be perfect. The field names, value formats and table names are where it guesses, because it hasn't seen your data. Every guess that's wrong produces silence rather than an error.

So the routine is short and the same every time. Read a real row. Compare the assistant's field names with it. Run the query on something you know is there. Only then run it on the question you care about, and treat its answer, including an empty one, as a result.

Practice

Do

Practice

Answer one question three ways
the event file

Use the same file, shown in section 1. If you have a terminal with jq installed, run the commands yourself. If not, work from the outputs shown, and write the commands you would run.

Either way, write down for each answer what the tool counted: lines, events, or distinct values. That one note prevents most of the misreadings this lesson describes.

Do this Answer one question three ways
  1. Pick the question. Which programs started on WORKSTATION6, and as which user?
  2. Search the text. Use the grep from section 2 and say what its count does and does not tell you.
  3. Filter the fields. Adapt the jq from section 3 to WORKSTATION6, and list the program and user for each process start.
  4. Check the answer. Ask the 4688 events the same question, as section 6 does, and say whether the two sources agree.
You will have one answer you have checked two ways, and a note of what each tool counted.

Keep the commands you used with the answer. Module 3 starts from exactly this habit: every result in a case travels with the query that produced it.