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
| Word | What it means | Example |
|---|---|---|
| Dynamic analysis | studying a program by running it | |
| Sandbox | an isolated machine where a sample may run | |
| System call | a request a program makes to the kernel | connect |
| Beacon | a regular call home from malware to its server | |
| Sinkhole | a 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 questionsThe 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
cd ~/malab && ls -l updater && rm -f ~/.config/autostart/lab-updater.desktop && touch before.mark && ls ~/.config/autostart 2>/dev/null | wc -lShown 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
strace -f -s 80 -e trace=socket,connect,sendto,openat -o trace1.txt ./updater && grep -E "connect|autostart" trace1.txtShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA 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
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.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Start the sinkhole and run it again
python3 sinkhole.py > sinkhole.log & sleep 1 && strace -f -s 80 -e trace=socket,connect,sendto,openat -o trace2.txt ./updater; wait; cat sinkhole.logShown here, not run. Press Start lab to type it into your own lab’s terminal.
Read what it sent in the trace
grep sendto trace2.txtShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsCompare 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
find ~ -newer before.mark -type f -not -path "*/malab/*" && cat ~/.config/autostart/lab-updater.desktopShown 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 questionsAssignment: 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.
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