Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Watching malware behaviour in a sandbox

After this lesson: I can run a sample in an isolated lab and record the files and connections it makes.

65 min lesson6 commandsReverse engineering lab: reading programs with gdb, radare2, strace and ltrace on one VM

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

Let it run, but on a lead

Dynamic analysis means running a sample on purpose and recording everything it does. Static triage guesses from the outside; running it shows which files it writes and which servers it calls.

It is only safe in a sandbox: a machine you can throw away, with no way out to the internet.

By the end of this lesson

Note

I can run a sample in an isolated lab and record the files and connections it makes.

Key words

WordWhat it meansExample
Dynamic analysisstudying a program by running it
Sandboxan isolated machine where a sample may run
System calla request a program makes to the kernelconnect
Beacona regular call home from malware to its server
Sinkholea stand-in server that catches a sample's calls

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

The one rule

Important

Run a sample only in your own lab VM, never on your own computer or a college machine. This sample is harmless and calls only an address of your VM. Running anything on somebody else's systems needs their written permission, under the Computer Misuse Act 1990 in the UK.

Record the starting point first

Your lab VM is the sandbox: a private copy, reset when you like, that cannot reach the internet. Before the run, note the state so you can see what changed.

Here that means removing any file a past run left, and touching a mark file whose time is "before".

Clean up and set the mark

Command
cd ~/malab && ls -l updater && rm -f ~/.config/autostart/lab-updater.desktop && touch before.mark && ls ~/.config/autostart 2>/dev/null | wc -l

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

strace records every system call

A program asks the kernel for everything: to open a file, to make a socket, to connect. Each request is a system call, and strace writes each one down.

This trace keeps four kinds, to keep it short. Nobody is listening at the sample's address yet.

Run it traced, with nobody listening

Command
strace -f -s 80 -e trace=socket,connect,sendto,openat -o trace1.txt ./updater && grep -E "connect|autostart" trace1.txt

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A sinkhole catches the beacon

A refused connection hides what the sample would send. So analysts give it a sinkhole: a small server of their own at the address it calls.

This one listens on 127.66.0.23, port 8443, on your own VM, and prints each line that arrives. It answers nothing.

Write the sinkhole

Command
printf '%s\n' '# sinkhole.py: stands in for the server the sample calls, on your own VM.' '# It answers nothing, prints each line that arrives and stops after three.' 'import socket' 'import time' '' 'server = socket.socket()' 'server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)' 'server.bind(("127.66.0.23", 8443))' 'server.listen(5)' 'server.settimeout(20)' 'start = time.time()' 'for n in range(3):' '    conn, peer = server.accept()' '    line = conn.recv(200).decode().strip()' '    print("+" + str(round(time.time() - start)) + "s", peer[0], line, flush=True)' '    conn.close()' > sinkhole.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Start the sinkhole and run it again

Command
python3 sinkhole.py > sinkhole.log & sleep 1 && strace -f -s 80 -e trace=socket,connect,sendto,openat -o trace2.txt ./updater; wait; cat sinkhole.log

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Read what it sent in the trace

Command
grep sendto trace2.txt

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Compare the system with the mark

The trace shows calls; the disk shows what stayed. find with -newer lists every file changed since the mark. Leaving out your own work folder leaves only what the sample did.

A file in .config/autostart runs at each desktop login. That is persistence, the next lesson's subject.

Find what changed since the mark

Command
find ~ -newer before.mark -type f -not -path "*/malab/*" && cat ~/.config/autostart/lab-updater.desktop

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

A common mistake

Warning

Many people think a sample that did nothing in a sandbox is harmless. Malware may wait, check for a sandbox, or act only when its server answers. A quiet run means it did nothing you saw, so check the strings and headers again and run it longer.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Assignment: read a system call trace

Each line of a trace is a string like the lines in trace1.txt.

connections(lines) returns "address:port" for each connect( line, in order, with no repeats. The port is between htons( and the next ), and the address between inet_addr(" and the next ".

refused(lines) returns how many lines contain ECONNREFUSED.

files_written(lines) returns the path of each openat( line whose flags contain O_WRONLY, O_RDWR or O_CREAT, in order. The path is between the first two " characters.

Code: python
def connections(lines):
    return []

def refused(lines):
    return 0

def files_written(lines):
    return []

Make a free account to save your work and track your progress.

Exit quiz

Have a go

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.