Reading width
Wide uses the full column for everything, text, diagrams, code, and exercises. Narrow keeps the standard reading width.
Text size
Scales the body text. Headings and code blocks keep their size.
In this section
0.10 From a Sample to a Detection
Introduction
An analysis that ends with a report has helped one incident. An analysis that ends with a detection helps every incident after it, because the next copy of the same malware is caught where it lands, and the copies already in the fleet are found by a hunt. This lesson turns the module's findings into detections and measures each one.
It sets out which indicators last and which do not, writes a YARA-X rule from the structure of a sample rather than from its names, measures it against the specimens and a clean corpus, and shows why some findings belong in a behavior query rather than a file rule. It ends with an assistant-written rule that looks right and fails its measurement.
Which Indicators Last
From a single build to a whole familyEvery lesson in this module produced indicators, and they differ enormously in how long they stay useful. A hash identifies one build of one file; a behavior describes what a whole family does whatever it is built into. Set out by how long each kind lasts:
How long each indicator lasts
The cheap indicators are the short-lived ones. A hash takes seconds to search for and finds only the identical file; lesson 0.3 showed that panels build a fresh sample for each affiliate, so even one campaign produces many hashes. Server addresses last until the operator moves, and lesson 0.5 showed that fallbacks are designed to outlive them.
The durable indicators cost more to produce. A decryption loop had to be found and read in code, in lesson 0.9, but it survives every rebuild that does not change the code or the compiler, whatever key or servers the build carries.
Behavior lasts longest of all, because the goal stays the same across versions: a stealer has to read the browser's stores, a paste-to-run lure has to start a script host from the Run dialog.
The practical rule is to send the short-lived indicators to the search and block lists at once, because they are cheap and they work today, and to spend the analysis time on the durable ones, because they are what still catches the family next quarter.
The same split decides who receives each indicator. Hashes and addresses go to the teams that run blocking and search, the same day. Structural rules go to whoever maintains the file scanning pipeline, with their measurements.
Behavior detections go to the detection engineers who own the query library, with the false positive count from the organization's own data. An analysis that hands everything to one team in one list leaves most of it unused.
A Rule From Structure
What the family must do, in bytesYARA-X is a rewrite of YARA, the pattern-matching language used across the industry to describe malware by its content, and it ships in REMnux as the yr command. A rule names strings or byte patterns and a condition, and any file that satisfies the condition matches.
The tempting rule matches names: a string the sample prints, a section name, a path. A durable rule matches structure, something the code has to contain to do its job:
Writing a rule from structure and measuring it. A rule without the second measurement is a guess about false positives, and the clean corpus turns it into a number.
Lesson 0.9 read the stealer specimen's decryption loop instruction by instruction. Those instructions are a structure the program cannot do without, and their bytes make a rule. Where the bytes encode something that varies between builds, here the loop's length, the rule leaves them open with wildcards:
$ cat loop.yar
rule config_xor_loop_8byte_key
{
meta:
description = "XOR decryption loop with an 8-byte key, as in mw03"
strings:
$loop = { 48 89 C2 83 E2 07 41 0F B6 14 10 32 14 01 # ◄ mask and XOR
41 88 14 01 48 83 C0 01 48 3D ?? ?? 00 00 75 E2 } # ◄ open length
condition:
uint16(0) == 0x5A4D and $loop
}
The masked index, and edx, 7, and the XOR of a key byte with a data byte are the heart of it, and neither depends on which key or which configuration a build carries. The length is left open because a different configuration has a different length. The condition adds one cheap check, that the file starts with MZ, so the pattern is only searched in Windows executables.
Choosing how much of the loop to include is a judgment between two risks. Too few bytes, and the pattern turns up by chance in unrelated programs; too many, and a trivial change in the surrounding code breaks the match.
The loop body from the counter to the jump back is a natural unit: it is the whole algorithm, it is long enough to be specific, and nothing outside it needs to stay the same for the rule to match.
A rule like this is specific to the code as compiled. A different compiler, or different optimization settings, would produce different instructions for the same loop, and the rule would need a second pattern. That is a known limit, recorded with the rule, not a reason to fall back to names.
The rule's metadata matters as much as its pattern. A description of what it detects and why, the sample it was written from with its hash, the date, the author and the YARA-X version it was tested with all belong in the rule or beside it.
When the rule fires a year later, the analyst who reads the alert needs to know what it was meant to find, and when it stops firing, the maintainer needs to know what to re-test it against.
Measuring a Rule
Specimens and a clean corpusA rule is a claim: that it matches what it should and nothing else. Both halves are measured, and the lab pack carries what is needed for each, the specimens and a clean corpus of benign files with the same surface features:
$ ls specimens | wc -l
11 # ◄ files the rule should judge
$ ls clean | wc -l
7 # ◄ files it must not match
$ yr --version
yara-x-cli 1.24.0
Scanning both with the loop rule gives two numbers:
$ yr scan loop.yar specimens
config_xor_loop_8byte_key specimens/mw03-stealer-config.exe # ◄ only this one
$ yr scan loop.yar clean | wc -l
0
One specimen matches, the stealer whose loop the rule was written from, and none of the clean files do. Those two numbers are the rule's record: what it hits and what it falsely hits, on named sets, with the date and the YARA-X version. A rule that reaches the pipeline without the second number has had its false positive rate guessed rather than measured.
The clean corpus here is small, built for teaching. In real work it is as large as the organization can make it: installers, system files, the software its own users run, and anything packed or protected the way the malware is.
False positives come from files that share the malware's surface, which is why the course's clean corpus includes a packed tool and a PowerShell script, and why the next section's rule fails on one of them.
A single match on a clean file is not automatically a failure, but it is always a decision. If the clean file is genuinely unusual and the rule's value is high, the rule may be kept with an exclusion and a note.
If the clean file is the kind of thing users run every day, the rule will fire constantly in production, and it is rewritten before it ships. Either way the decision and its reason go into the record beside the two numbers.
Names and Packers
Why the easy rule failsNames are the first thing an analyst notices and usually the worst thing to write a rule on. A packer's section name is the extreme case, because it describes how a file was built rather than who built it. An assistant asked for a rule for the packed stage from lesson 0.2 might reasonably reach for the section name that identifies UPX:
$ cat upx.yar
rule mw02_loader_family
{
strings:
$upx = "UPX0"
condition:
uint16(0) == 0x5A4D and $upx
}
$ yr scan upx.yar specimens
mw02_loader_family specimens/mw02-stage-upx.exe # ◄ the file it was written for
$ yr scan upx.yar clean
mw02_loader_family clean/clean-tool-1-upx.exe # ◄ and a clean tool
The rule matches the packed stage it was written for, and it also matches the clean tool packed with the same packer, while missing the encrypted loader from the same lesson entirely. It is a working detection for UPX, and that is all it is. Labeled as a family detection, it would send analysts to investigate every legitimately packed utility in the fleet, and say nothing about the loader.
The measurements make the decision for each rule:
Two rules, measured
The same failure applies to strings a sample prints, file names it uses and the text of its messages: all are chosen by the author and changed at will. They make useful triage labels, and poor detections.
Assistants reach for names readily, because names are what an output shows first and what a rule about a sample most obviously mentions. Asking for a rule that matches the code a family must contain, and supplying the disassembly it should use, produces better rules than asking for a rule for the sample, and the measurement catches the cases where it still goes wrong.
What a File Rule Cannot See
Configuration values and behaviorSome of the most durable findings in this module are invisible to a file rule. Lesson 0.3's stealer stages its haul in a folder named rd0311, a name that lasts a campaign and makes a good host indicator. Searching the file for it shows why it cannot be a YARA string:
$ grep -a -o 'rd0311' specimens/mw03-stealer-config.exe | wc -l
0
$ grep -a -o 'rdg-0311' specimens/mw03-stealer-config.exe | wc -l
1
The staging name appears nowhere in the file, because it lives inside the encrypted configuration; only the key is in clear. A rule on rd0311 would never match the stealer that uses it. The name appears on the host when the stealer runs, as a folder created under the user's temporary directory, and that is where it is searched for.
The key, by contrast, is in the file in clear, and it could be a rule string. Whether it should be depends on how long it lasts: a key hard-coded in one affiliate's build changes with the next build, so it detects that build and little else.
The loop that uses the key, which does not change with it, is the better rule, which is why this lesson built that one.
That is the general split. What is in the file in clear, structure and code, goes to file rules. What exists only when the malware runs, decrypted configuration values, mutexes, staging folders, network destinations and the processes it starts, goes to behavior detections and searches over host and network records.
That split is easy to lose when the configuration has just been decrypted on the analyst's screen and every value is in plain sight. The test is where the value exists on a victim's machine before the malware runs.
If it exists only inside an encrypted block, a file rule cannot reach it; if it is written to disk, sent over the network or created as a mutex while the malware runs, the host and network records can.
Detecting Behavior
A query over what ranA behavior detection describes what the malware does rather than what it contains, so it catches the family whatever file it arrives in, including the ones with no file at all.
Lesson 0.1's paste-to-run lures and lesson 0.6's encoded PowerShell are the clearest cases: there may be no sample to scan, but the process that runs leaves a record. In Microsoft Defender XDR's advanced hunting, a query for the pattern those lessons showed looks like this:
DeviceProcessEvents
| where FileName =~ "powershell.exe"
| where InitiatingProcessFileName =~ "explorer.exe"
| where ProcessCommandLine has_any ("-enc", "-EncodedCommand")
and ProcessCommandLine has_any ("-w hidden", "-WindowStyle Hidden")
| project Timestamp, DeviceName, AccountName, ProcessCommandLine
It looks for PowerShell started by the Windows shell, the parent a Run dialog paste produces, with an encoded command and a hidden window. Each condition is one of the findings from earlier lessons, and together they describe a delivery route rather than a file.
Behavior detections are measured the same way as file rules, with different sets. The specimens become test events, from a lab or from an incident's own records, and the clean corpus becomes a period of normal activity from the organization's own fleet. Administrators do run encoded PowerShell, so the query's false positives are counted and its conditions narrowed, with the same two numbers kept in the record.
Narrowing works by adding conditions that the malicious pattern needs and legitimate use rarely has: the parent process, the time of day, the user's role, the destination of any network connection the process makes. Each condition added is a claim about what attackers cannot avoid, and each is checked against the test events, so the query keeps catching what it was written for while it stops catching the administrators.
The two kinds of detection cover each other's blind spots:
Three ways a finding becomes detection. Each sees what the others miss, so one analysis usually feeds all three.
The practical consequence is that one analysis feeds all three. The stealer from lesson 0.3 gives a file rule from its decryption loop, a behavior search for its staging folder and its upload destination, and a hunt for both across the fleet's history. Each catches cases the others cannot, and the record lists all three with their measurements, so whoever maintains them knows where each came from.
From Rule to Hunt
Finding the copies already thereA new detection catches the next infection. The infections that happened before it existed are found by running the same logic backwards, as a hunt over the data already collected. The YARA-X rule is compiled once and run across stored files, file shares and endpoint collections; the behavior query is run across as much history as the telemetry keeps:
$ yr compile loop.yar -o loop.yarc && ls -l loop.yarc | cut -c1-10,24-
-rw-r--r--69264 Nov 24 06:50 loop.yarc
Compiling turns the rule into a binary form that loads quickly and can be shipped to wherever files are scanned, and the compile step also catches errors in the rule before it reaches production.
Where the compiled rule runs decides what it can find. Endpoint agents and mail gateways scan files as they arrive, which catches the next copy.
Scanning stored collections, backups and file shares catches copies that arrived before the rule existed. Each place has its own limits on file size and scan time, and the rule's record notes where it has been deployed, so a gap in coverage is visible rather than assumed away.
Every hunt result is a lead, not a finding, and a hunt that returns many is first sorted by what kind of match each is.
A match goes through the same triage as the first sample: what is it, is it what it claims, what does it carry. And every hunt is recorded with its window and its scope, because "no matches" means something only alongside "searched these hosts, over these days, with this version of the rule."
Detections also need owners and dates, as the indicators in lesson 0.5 did. A rule that has not matched anything in months may be quietly broken by a change in the family, or it may be the reason the family has not landed; only a periodic test against the specimens says which.
That test is cheap when the specimens are kept. A small set of samples and test events, with the rules that should match them and the clean files that should not, can be run automatically whenever a rule or the scanning engine changes. The lab pack's specimens and clean corpus are a small version of exactly that, and the commands in this lesson are the test.
Working With the Assistant
Prompt, session, recordWriting rules is a task assistants do quickly and describe confidently, and a one-line request gets a rule with a description of measurements nobody took:
- The rule mw02_loader_family matches mw02-stage-upx.exe.
- The rule detects the mw02 loader family.
- The rule has no false positives.
- A rule on the XOR loop bytes would still catch rebuilds with a different key.
- The staging folder name rd0311 from 0.3 would make a good string for a file rule.
Held against the scans in this lesson, claims 2, 3 and 5 are refuted: the rule misses the loader, matches a clean packed tool, and the staging name it suggests is never in the file in clear.
Claim 4 is a reasonable expectation with nothing yet to test it on. The rule is a fair first draft; the description is where the time would have been lost. The prompt that works asks for a rule from code and puts the measurement before any description:
- YARA-Xnames the engine the rule is compiled and run with
- The specimens folder and a clean corpusgives it both sides of the measurement
- what the code must containthe test for a rule that lasts: structure the program cannot work without
- before you describe how well it worksputs measurement before any claim about the rule
- scan both folders with yr and show me the resultsthe two numbers every rule is kept or retired on
With that prompt the assistant measures its own first draft, retires it, and writes the rule that lasts once the analyst points it at the right code:
- YouWrite a YARA-X rule for the mw02 loader family. The specimens folder and a clean corpus are in REMnux. Base the rule on what the code must contain, not on names the author can change, and before you describe how well it works, scan both folders with yr and show me the results.
- AssistantThe packed stage carries UPX section names, so here is a first rule on UPX0, with both scans.
- REMnux
$ cat upx.yar rule mw02_loader_family { strings: $upx = "UPX0" condition: uint16(0) == 0x5A4D and $upx } $ yr scan upx.yar specimens mw02_loader_family specimens/mw02-stage-upx.exe $ yr scan upx.yar clean mw02_loader_family clean/clean-tool-1-upx.exe - AssistantIt matches the packed stage and a clean UPX-packed tool, and misses the loader. It detects the packer, not the family: retire it as a family rule, or keep it renamed as a triage label for UPX.
- YouAgreed, retire it. For a rule from code, start with the loop we already read in radare2: the eight-byte XOR loop in mw03-stealer-config.exe, with the length left open. The mw02 loader's own loop comes next.
- AssistantWritten as loop.yar from the loop bytes, with the length operand wildcarded so a different length still matches. Scanning both folders and compiling it.
- REMnux
$ yr scan loop.yar specimens config_xor_loop_8byte_key specimens/mw03-stealer-config.exe $ yr scan loop.yar clean | wc -l 0 $ yr compile loop.yar -o loop.yarc - AssistantIt matches mw03-stealer-config.exe and none of the 7 clean files, and it compiles. Whether it catches a rebuild with a different key is untested until one exists.
The first draft was the same packer rule, and the difference is what happened next. Asked to scan before describing, the assistant saw the false positive and the miss itself and recommended retiring the rule, which is the decision the record would have reached.
The analyst's contribution was knowledge from earlier in the module: the decryption loop read in lesson 0.9 is code its program cannot work without, and already read, so it is the quickest place to show the method.
Given that, the assistant wrote the rule, measured it on both folders and compiled it in one turn. The mw02 loader's own XOR loop is the next rule to write the same way, and the lab sets it.
The time saved is in the drafting and measuring loop. A rule, two scans and a compile are a few minutes by hand each time, and a rule usually takes more than one round. The assistant also kept the untested claim open in its last line, because the prompt asked for results instead of descriptions.
The rule goes into the detection pipeline on the measurements, not on the description:
What the rules rest on
The UPX rule leaves as a triage label under an honest name or not at all, and the loop rule goes in with its two numbers and the one question still open: a rebuild with a different key, when one turns up.
Practice
Keep, fix or retire the rule? prepared evidenceThe scenario below holds the weak prompt's rule and claims, the working session, both rules, the scans of the specimens and the clean corpus, and the search of the stealer's strings. Find the claims the scans refute, then decide what happens to the rule.
In the lab, write both rules in your REMnux machine and scan the pack's specimens and clean folders with yr scan. Then work a rule from the mw02 loader's own XOR loop with your own assistant, using the prompt above, and record both numbers before you keep it.
Detections are where analysis pays off twice: the next copy is caught when it lands, and the earlier copies are found by a hunt. The indicators that last are structure and behavior, not hashes and names, so file rules are written from what the code must contain, and the findings that exist only at run time go to behavior queries.
Every rule is measured on the specimens and on clean files before it is trusted, and an assistant's claims about a rule are claims about measurements, checked by running them.
Resources
Vendors and projects revise these pages; where a page and this lesson disagree, the page is newer.