In this section

0.9 Reading Code to Answer One Question

Module 0

Introduction

Most malware analysis never needs to read a single line of code.

The earlier lessons in this module reached verdicts, configurations and response tasks from strings, structure, tool output and decoding, and that is how most analysts spend most of their time. But some questions only the code can answer: whether a key really is the key, whether a capability belongs to the program or its runtime, whether a decrypted list is actually used.

This lesson sets out when reading code is worth it and how to keep it to one question. It finds a function from the data it uses, reads that function far enough to answer, and stops, using radare2 on REMnux, with Ghidra and its MCP connections described for the same job.

It confirms in code the key and length that lesson 0.3 found by decryption, and checks an assistant's reading of the same function.

1

When Code Reading Is Needed

And when it is not

Code reading earns its time when a question matters to the verdict or the response and nothing cheaper can settle it. Lesson 0.3 decrypted a stealer's configuration with a key found among its strings, and the result was valid JSON.

That is strong evidence, but it is indirect: it shows the key works, not that the program uses it the same way, over the same length, for the same purpose. If a decision rests on those details, the code is where they are settled.

The opposite case is more common. A question already answered by a tool's output does not need the code, and neither does a question whose answer would not change the response. Reading code to confirm that a packed file is packed, or that a known family behaves as its reports describe, spends hours on what is already known.

Set out as a list, the earlier lessons' questions and the cheapest thing that settles each look like this:

The cheapest thing that settles each question

Is it packed?

Section names, entropy and imports; the packer's own test (0.2)

What does its configuration say?

A decode from the strings and the bytes beside them (0.3, 0.5)

Is that really the key, and the length?

The code that uses the key

Does it use what it decrypts?

The code after the decryption, or the program running

Is a capability its own or the runtime's?

A clean file built the same way, then the code (0.2)

Code appears only where nothing cheaper reaches the answer. The discipline is to name the question before opening the disassembler, in one sentence that can be answered yes or no or with a value.

For this lesson: does the program decrypt its configuration with the eight-byte key rdg-0311, over 488 bytes, and does it then read the targets it decrypts? Everything in the code that does not bear on that sentence is skipped.

Naming the question also decides what a finished answer looks like. A value to confirm is finished when the code shows the value; a yes-or-no question is finished when the code shows one or the other; and a question the code cannot answer is finished when the record says so and names what would.

Without that, there is no point at which the reading is obviously complete, and it tends to continue.

2

Tools for Reading Code

radare2, Ghidra and their MCP connections

Two free tools do most of this work. radare2 is a command-line framework that disassembles, analyzes functions and follows references, and it ships with REMnux.

Ghidra, released by the US National Security Agency, adds a decompiler that turns machine code back into readable C-like source, which makes longer functions much faster to read. Both have MCP connections, so an assistant can run them: REMnux includes r2mcp for radare2 and GhidrAssistMCP for Ghidra, and GhidraMCP is another bridge for Ghidra.

The difference between the two outputs matters for checking an assistant. A disassembly is what the processor executes, one instruction per line; it is exact but slow to read.

A decompilation is Ghidra's reconstruction of what the source probably looked like; it is fast to read but it is an interpretation, and it can be wrong about types, loop bounds and which variable is which. An assistant working from decompiled output inherits those errors, so a claim that matters is checked against the instructions themselves.

The amount of code is the reason for discipline. Even this small specimen gives radare2 a long list of functions after analysis, almost all of them the compiler's runtime:

$ r2 -v | head -n 1
radare2 5.5.0 0 @ linux-x86-64 git.5.5.0
$ r2 -q -c 'aaa; afl' mw03-stealer-config.exe 2>/dev/null | wc -l
93  # ◄ functions to choose from

Only one of those functions bears on the question, and a real sample of any size has hundreds or thousands. The build that produced these listings used radare2 5.5.0; REMnux ships a newer release, whose output differs in small details of layout but not in the instructions themselves.

This course uses radare2 in every demonstration, because it runs in the build environment and in every student's REMnux, and its output is plain text that can be shown in full. Ghidra is the tool many analysts prefer for longer functions, and everything in this lesson's method applies to it unchanged.

In Ghidra the same steps are a search for the string, the references window on it, and the function the reference sits in, opened in the decompiler.

The decompiled loop for this specimen reads as a short for loop with an XOR and a modulo, and it is a good way to see the shape quickly. The disassembly is still where a claim about a register, a bound or an address is settled.

3

Starting From the Data

The shortest path to the right function

A program of any size has hundreds of functions, most of them from the compiler's runtime. Searching for the right one by reading is hopeless; starting from the data the question is about is quick. The question names a key, and the key is a string in the file, so the first step is to find the string and ask which code refers to it:

1. Start from a questionone claim the other tools could not settle2. Start from the dataa string, a constant or a buffer the claim is about3. Follow the referenceto the one function that uses it4. Read that functiononly as far as the question needs5. Stop and recordconfirmed, refuted or not shown, with addressesIn the toolradare2 or Ghidra

Reading code for one answer. The work is bounded by the question at the start and the record at the end, so it never turns into reverse engineering the whole program.

radare2 lists the string with its address, and its cross-reference command lists every instruction that refers to it. The listings in this lesson are plain text, with instruction bytes and automatic comments turned off in the analyst's radare2 settings file so that each line fits:

$ r2 -q -c 'iz~rdg' mw03-stealer-config.exe
1   0x00007258 0x140009058 8   9    .rdata  ascii   rdg-0311  # ◄ the key from 0.3
$ r2 -q -c 'aaa; axt @ str.rdg_0311' mw03-stealer-config.exe 2>/dev/null
fcn.1400014c2 0x1400014d7 [DATA] lea r8, str.rdg_0311  # ◄ one user

One function uses the key, at one instruction, loading its address into a register. That function is the only one worth opening, and the address of the reference is the first line of the record for this question. The same approach works from any data a question is about: a mutex name, a server address, a file path or a constant from a configuration format.

When the data is not stored as a string, the reference is found another way: from an imported function the behavior needs, such as one that opens files or connections, or from a constant typical of an algorithm. The principle is the same. Start from something concrete the question names, and let the references lead to the code.

Sometimes the reference leads somewhere unexpected: to a function that only copies the string elsewhere, or to a table of many strings used by one routine. Following one more reference from there is normal. What matters is that each step is chosen by the question, so the path stays short and every function opened has a reason in the record.

4

Reading the Loop

Six instructions answer the question

The function is short. Its first lines set up three addresses and then run a loop, and those lines contain the whole answer:

$ r2 -q -c 'aaa; pdf @ fcn.1400014c2' mw03-stealer-config.exe 2>/dev/null \
    | sed -n '4,16p' | cut -c1-62
│           0x1400014cb      mov eax, 0
│           0x1400014d0      lea r9, [0x14000c040]  # ◄ where the result goes
│           0x1400014d7      lea r8, str.rdg_0311  # ◄ the key
│           0x1400014de      lea rcx, [0x140009080]  # ◄ the encrypted data
│       ┌─> 0x1400014e5      mov rdx, rax
│       ╎   0x1400014e8      and edx, 7  # ◄ index mod 8
│       ╎   0x1400014eb      movzx edx, byte [r8 + rdx]
│       ╎   0x1400014f0      xor dl, byte [rcx + rax]  # ◄ key byte XOR data byte
│       ╎   0x1400014f3      mov byte [r9 + rax], dl
│       ╎   0x1400014f7      add rax, 1
│       ╎   0x1400014fb      cmp rax, 0x1e8  # ◄ 488 bytes
│       └─< 0x140001501      jne 0x1400014e5
│           0x140001503      lea rax, [0x14000c040]

Read with the annotations, the loop says: take the counter, keep its low three bits to pick a byte of the key, combine that key byte with the data byte at the same position, store the result, and repeat until the counter reaches 0x1e8, which is 488. Each instruction that matters, set beside what it means here:

The loop, instruction by instruction

lea r9, [0x14000c040]

r9 holds the address the decrypted bytes are written to

lea r8, str.rdg_0311

r8 holds the key

lea rcx, [0x140009080]

rcx holds the encrypted data

and edx, 7

the position in the key: the loop counter, modulo 8

xor dl, byte [rcx + rax]

a key byte combined with a data byte

cmp rax, 0x1e8 / jne

repeat until 488 bytes are done

That confirms every part of the question about the key.

The key is rdg-0311, it cycles every eight bytes because the index is masked with 7, the encrypted data starts at 0x140009080, which is file offset 0x7280 in the section listing that lesson 0.3 used, and the loop runs for exactly 488 bytes, the length the JSON parser found in lesson 0.3. Two independent methods, decryption and code reading, now agree.

Reading a loop like this needs only a handful of instruction types: loading an address, moving a byte, a logical operation, an addition, a comparison and a conditional jump. Most decryption, decoding and checksum loops in malware are built from the same few, and an analyst who can read them can confirm most keys and lengths without knowing much more assembly than this.

5

What Happens to the Result

The second half of the question

The other half of the question is what the program does with the decrypted list. The code after the loop answers it:

$ r2 -q -c 'aaa; pdf @ fcn.1400014c2' mw03-stealer-config.exe 2>/dev/null \
    | sed -n '17,30p' | cut -c1-62
│           0x14000150a      lea rcx, [rax + 0x1e8]
│           0x140001511      mov r8d, 0
│       ┌─> 0x140001517      movzx edx, byte [rax]
│       ╎   0x14000151a      add r8d, edx  # ◄ adds up the bytes
│       ╎   0x14000151d      add rax, 1
│       ╎   0x140001521      cmp rax, rcx
│       └─< 0x140001524      jne 0x140001517
│           0x140001526      mov edx, 0x1e8
│           0x14000152b      lea rcx, str.RIDGELINE_SPECIMEN
│           0x140001532      call fcn.140001480  # ◄ prints the message
│           0x140001537      mov eax, 0
│           0x14000153c      add rsp, 0x28
└           0x140001540      ret  # ◄ and returns

A second loop adds up the 488 decrypted bytes, and then a single call prints the specimen's message with the count and the sum, and the function returns. Counting the calls in the whole function confirms there is nothing else, and the section list shows where the decrypted bytes were put:

$ r2 -q -c 'aaa; pdf @ fcn.1400014c2' mw03-stealer-config.exe 2>/dev/null \
    | grep -c call
2
$ r2 -q -c 'iS~.bss' mw03-stealer-config.exe
5   0x00000000     0x0 0x14000c000  0x1000 -rw- .bss  # ◄ the result lands here

Two calls: one at the start into runtime setup, one to print. The decrypted configuration is written to .bss, a writable section that occupies no space on disk, and it is read only to sum its bytes.

Nothing in the function opens a file, reads a target or connects anywhere. A program that wanted to read a target would need a call that opens it, and there is no such call here, which is as strong as negative evidence in code gets:

Encrypted488 bytes at0x140009080readKeyrdg-0311, bytei mod 8combinedXORone byteat a timestoredDecryptedwritten to0x14000c040usedThensummed, printed,returned

The function in one line of reasoning. Each box is an instruction or two in the listing, and nothing between them reads a file or opens a connection.

In this specimen that is by design: it is the inert stand-in for a stealer, built to decode its targets and stop. In a real stealer the same reading would show the decrypted list passed to a routine that walks the paths and copies the files, and the call into that routine would be the next address in the record.

That difference is why the second half of the question was worth asking. A configuration says what a program could do; the code that uses it says what this build does.

Two samples of one family can carry the same configuration format and differ entirely in whether a feature is switched on in code, and the response for each follows what the code shows. The record therefore keeps the configuration and the code reading together, each with its own evidence, rather than letting one stand in for the other.

6

Stopping

The discipline that keeps this short

The question is answered, so the reading stops here, deliberately. The function calls a runtime setup routine at its start, and the program has hundreds of other functions; none of them bears on the question, and opening them would turn a ten-minute confirmation into an open-ended reverse engineering project.

Stopping is easier when the record is written as the reading goes, because the record shows when the question is answered.

For this lesson it reads: key rdg-0311, used once, at the reference from fcn.1400014c2; eight-byte cycle, from the mask with 7; 488 bytes from 0x140009080, from the loop bound; output to 0x14000c040 in .bss; result summed and printed, no file access, from the code after the loop. Every line has an address, so anyone can check it.

Each claim in such a record takes one of three verdicts, and the definitions keep them honest:

The three verdicts

Confirmed

The code does exactly what the claim says, at an address you can cite

Refuted

The code does something the claim contradicts, at an address you can cite

Not shown

The code read does not bear on the claim; say what would

Not shown is not a failure, and it is often the most useful line in a record, because it tells the next analyst exactly where the open question is. It means the code read does not bear on the claim, and the record says what would: another function, a capture of the program running, or the network records. A code reading that marks everything confirmed or refuted has usually overreached.

The record also notes what was not read, in one line: the function at the start of this one, the program's other functions, and anything that runs before or after. That line protects the next reader from assuming the reading covered more than it did, which is the same honesty about scope that the earlier lessons applied to configurations and sign-in records.

Time is part of the record too. A confirmation like this one takes minutes, and noting how long it took tells a team lead what the next one will cost. When a question starts to need many functions and much longer, that is the signal to step back and ask whether running the sample in a sandbox, or finding a published analysis of the family, would answer it faster.

7

Code Reading With an Assistant

Where it helps and where it misleads

An assistant connected to radare2 or Ghidra can do the mechanical parts of this quickly: list the strings, find the references, disassemble or decompile the function, and describe the loop. That is real help, and most of it can be checked by looking at the same output.

The risk is in the step from instructions to meaning.

A register that holds an output address can be read as an input; a loop that sums bytes can be described as one that reads files, because stealers usually do; a decompiled variable can be named after what such code normally does rather than what this code does. Each of those produces a fluent claim with an address attached, and the address makes it look checked when it is not.

A citation is only as good as the reading of what it points at.

The defense is the one the course has used throughout, applied to code, and it fits in five steps:

Checking a claim about code

Method

Needs

the address the claim rests on

Read

the instructions there, not a summary

Steps

  • 1. Ask for the address behind each claim.
  • 2. Read the instructions at that address yourself.
  • 3. Check which register or variable is input and which is output.
  • 4. Check what the code does next, not what such code usually does.
  • 5. Mark confirmed, refuted or not shown, with the address.

The method is the same whether the claim came from an assistant, a colleague or a report.

Step 3 catches the most common slip in reading code, by people and assistants alike. Instructions that load addresses look much the same whether the address is read from or written to, and only the later instructions, the ones that use the registers, say which is which.

In this function the write goes through r9 and the reads through r8 and rcx, and that one detail separates a correct description from a wrong one. Ask for the address behind each claim, then read the instructions at that address and see whether they say what the claim says.

For a short function that takes minutes; for a long one, the claims that matter to the verdict get the reading, and the rest are marked as the assistant's description rather than a finding.

Assistants are also useful at the other end of the work, in explaining an unfamiliar instruction or idiom once the analyst has found it. Asking what a specific instruction at a specific address does is a narrow question with a checkable answer, and it is a much safer use than asking for a summary of a whole function and taking it as read.

8

Working With the Assistant

Prompt, session, record

An assistant connected to a disassembler can read code quickly, and the question decides whether it reads the code at all. Asked what the program does, it summarizes from the strings:

OpenCode with r2mcp, file on REMnuxPrepared example
What does mw03-stealer-config.exe do?
From its strings and imports, it is a stealer that:
  1. Decrypts an embedded configuration listing browser and wallet paths.
  2. Copies those files to a staging folder, zips them and uploads them.
Numbered for checking against the tool output

The first line is right and the second is what stealers usually do next, which this one does not. The prompt that works asks the one question lesson 0.3 left resting on a successful decryption, and asks for addresses:

PromptThe prompt that works
Using radare2 through MCP, find how mw03-stealer-config.exe decrypts its configuration and what it does with the result. Give the address behind each claim.
  1. Using radare2 through MCPit reads the code itself, instead of guessing from strings
  2. how mw03-stealer-config.exe decrypts its configurationone question, the one lesson 0.3 left resting on a guess
  3. what it does with the resultthe second half, which settles whether the targets are read
  4. Give the address behind each claimmakes every claim checkable in the listing

With that prompt, the assistant runs three radare2 commands through r2mcp and answers from them, and the analyst does not need to steer:

Reading one function with the assistantOpenCode with r2mcp, mw03-stealer-config.exePrepared example
  1. You
    Using radare2 through MCP, find how mw03-stealer-config.exe decrypts its configuration and what it does with the result. Give the address behind each claim.
  2. Assistant
    Analyzing the binary and finding what references the key string.
  3. REMnux
    $ r2 -q -c 'aaa; axt @ str.rdg_0311' mw03-stealer-config.exe 2>/dev/null
    fcn.1400014c2 0x1400014d7 [DATA] lea r8, str.rdg_0311
  4. Assistant
    One function, fcn.1400014c2, references it. Reading the instructions that load addresses, index the key and bound the loop.
  5. REMnux
    $ r2 -q -c 'aaa; pdf @ fcn.1400014c2' mw03-stealer-config.exe 2>/dev/null \
        | grep -E 'lea r(8|9|cx)|and edx|xor dl|cmp rax' | cut -c1-62
    │           0x1400014d0      lea r9, [0x14000c040]
    │           0x1400014d7      lea r8, str.rdg_0311
    │           0x1400014de      lea rcx, [0x140009080]
    │       ╎   0x1400014e8      and edx, 7
    │       ╎   0x1400014f0      xor dl, byte [rcx + rax]
    │       ╎   0x1400014fb      cmp rax, 0x1e8
    │           0x14000150a      lea rcx, [rax + 0x1e8]
    │       ╎   0x140001521      cmp rax, rcx
    │           0x14000152b      lea rcx, str.RIDGELINE_SPECIMEN
  6. Assistant
    r9 points to the output, r8 to the key and rcx to the encrypted data; the index is masked with 7 and the loop runs to 0x1e8. Now the code after the loop, and which section 0x14000c040 is in.
  7. REMnux
    $ r2 -q -c 'aaa; pdf @ fcn.1400014c2' mw03-stealer-config.exe 2>/dev/null \
        | sed -n '17,30p' | grep -E 'add r8d|call|ret' | cut -c1-62
    │       ╎   0x14000151a      add r8d, edx
    │           0x140001532      call fcn.140001480
    └           0x140001540      ret
    $ r2 -q -c 'iS~.bss' mw03-stealer-config.exe | cut -c1-62
    5   0x00000000     0x0 0x14000c000  0x1000 -rw- .bss
  8. Assistant
    Here is what the code does, with the addresses: The key string rdg-0311 is used by one function, fcn.1400014c2. That function XORs 488 bytes at 0x140009080 with the key, cycling through its 8 bytes. The result is written to a separate buffer at 0x14000c040, in .bss; the encrypted data is left unchanged. After the loop the function sums the decrypted bytes, prints a message and returns; it opens no files. This function has no network or upload code; whether another function does is outside what was read.
REMnux turns are the commands as run, with their output

This is code reading as section 1 describes it, done at the assistant's speed: from the string to its one user, to the handful of instructions that answer the question, and no further. Its final turn is a set of claims with addresses, which is the form a claim about code has to take before anyone can rely on it, so it goes into the record claim by claim:

Verification logWhat the code says5 confirmedmw03-stealer-config.exe · fcn.1400014c2
  1. Confirmed01The key string rdg-0311 is used by one function, fcn.1400014c2.checked againstthe cross-reference from the key string
  2. Confirmed02That function XORs 488 bytes at 0x140009080 with the key, cycling through its 8 bytes.checked againstthe loop: and edx, 7; xor; cmp rax, 0x1e8
  3. Confirmed03The result is written to a separate buffer at 0x14000c040, in .bss; the encrypted data is left unchanged.checked againstlea r9, [0x14000c040] and the store through r9; the section listing for .bss
  4. Confirmed04After the loop the function sums the decrypted bytes, prints a message and returns; it opens no files.checked againstthe code after the loop: an add, one call, ret
  5. Confirmed05This function has no network or upload code; whether another function does is outside what was read.checked againstthe calls in the function, and the claim keeps to the function it read
DecisionAccept: key, length and buffer confirmed in code; the list is never usedRight throughout, and the record is what shows it. Lesson 0.3's key and length now rest on the code.

Every claim holds. Claims 1 and 2 are the cross-reference and the loop, and they are the claims the question was about: the assistant found the function from the key, read the mask and the bound correctly, and saved the analyst the mechanical work of the first two steps.

Claim 3 reads the registers the right way round, the step 3 detail that separates a correct description from a wrong one: the store goes through r9 to 0x14000c040, in .bss, and rcx, the input, is only read.

Claim 4 describes the instructions after the loop rather than what stealers usually do next, and claim 5 says what the function lacks and leaves every other function open, which is the honest edge of a reading that answered one question and stopped.

This answer was right for reasons worth copying. The question was narrow, it asked for the address behind every claim, and the assistant read one short function through a disassembler instead of summarizing a whole program from its strings. Each of those keeps an answer close to the instructions.

The check still took the same reading as for a wrong answer, because nothing in a right answer looks different from a wrong one until the address is read, and the record is what lets the lead accept it: five claims, five addresses, no inference left in.

Do

Practice

Confirmed, refuted or not shown?
prepared evidence

The scenario below holds the working session, its summary, the instructions that matter from the function and the cross-reference and section facts. Mark each claim, and answer what the function shows about reading the targets.

In the lab, run the same radare2 commands on mw03-stealer-config.exe in your REMnux machine, and if you have Ghidra, open the same function there and compare the decompiled loop with the disassembly. Then connect your assistant through r2mcp or a Ghidra MCP bridge, give it the same request, and mark its claims the same way.

Code reading is worth its time when a question matters and nothing cheaper settles it, and it stays short when it starts from that question and from the data the question names.

A string leads to a cross-reference, the cross-reference to one function, and a handful of instructions answer the question: here, the eight-byte key, the 488-byte length and the fact that the decoded list is never used. Each claim in the record gets confirmed, refuted or not shown with the address behind it, and the reading stops when the question is answered.

Resources

Vendors and projects revise these pages; where a page and this lesson disagree, the page is newer.