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

Free lesson

Handling a malware sample safely

After this lesson: I can store and record a suspicious file so that it cannot run by accident.

60 min lesson9 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

A file nobody should open

Malware analysis is the work of finding out what a harmful program does, so others can detect it and clean it up. It starts with one suspicious file, a sample.

Before anybody looks inside, the sample must be kept where it cannot run by accident and recorded so it can be proved later. One careless double-click undoes the whole job.

By the end of this lesson

Note

I can store and record a suspicious file so that it cannot run by accident.

Key words

WordWhat it meansExample
Sampleone copy of a suspicious file kept for study
Isolationkeeping a sample away from real machines and networks
Hasha fingerprint of a file's exact bytessha256sum updater
Case loga dated record of what was done to the evidence
noexeca mount option that stops any file there running
Defangto change an address so it cannot be clicked127[.]66[.]0[.]23

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 of this module

Important

Every sample in this module is a harmless program you build from the lesson's own text, and it stays in your own lab VM. Never fetch real malware, never run a sample on your own computer, never analyse another person's machine without written permission. In the UK, the Computer Misuse Act 1990 makes unauthorised access a crime.

Your sample is a harmless imitation

Real samples are dangerous, so this module uses one written for the lab. updater.c imitates three things malware does. It carries tell-tale text, it writes a file so it starts again at login, and it calls home to a server.

Its server, 127.66.0.23, is an address of your own VM, so nothing leaves it. Read the source as you write it: you should never trust a file you have not read.

Make the folder and install the tools

Command
mkdir -p ~/malab && cd ~/malab && sudo apt-get install -y -qq yara upx-ucl zip unzip

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

Write the sample's source, part 1

Command
printf '%s\n' '/* updater.c: a harmless sample written for this lab. */' '/* It imitates malware traits only: it writes one file in your home */' '/* and talks only to 127.66.0.23, an address on your own VM. */' '#include <stdio.h>' '#include <stdlib.h>' '#include <string.h>' '#include <unistd.h>' '#include <sys/stat.h>' '#include <sys/socket.h>' '#include <arpa/inet.h>' '' 'const char *SERVER = "127.66.0.23";' 'const int PORT = 8443;' 'const char *AGENT = "LabBeacon/1.0";' 'const char *MARK = "MT-LAB-SAMPLE-7F3A";' '' 'static void persist(const char *home, const char *self) {' '    char path[512];' '    snprintf(path, sizeof path, "%s/.config", home);' '    mkdir(path, 0700);' > updater.c

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

Write the sample's source, part 2

Command
printf '%s\n' '    snprintf(path, sizeof path, "%s/.config/autostart", home);' '    mkdir(path, 0700);' '    snprintf(path, sizeof path, "%s/.config/autostart/lab-updater.desktop", home);' '    FILE *f = fopen(path, "w");' '    if (f == NULL) return;' '    fprintf(f, "[Desktop Entry]%cType=Application%cName=Lab updater%cExec=%s%c", 10, 10, 10, self, 10);' '    fclose(f);' '}' '' 'static void beacon(int n) {' '    struct sockaddr_in to;' '    char line[128];' '    int s = socket(AF_INET, SOCK_STREAM, 0);' '    memset(&to, 0, sizeof to);' '    to.sin_family = AF_INET;' '    to.sin_port = htons(PORT);' '    inet_pton(AF_INET, SERVER, &to.sin_addr);' '    if (connect(s, (struct sockaddr *) &to, sizeof to) == 0) {' '        int len = snprintf(line, sizeof line, "BEACON %d id=%s agent=%s%c", n, MARK, AGENT, 10);' '        send(s, line, len, 0);' >> updater.c

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

Write the sample's source, part 3

Command
printf '%s\n' '    }' '    close(s);' '}' '' 'int main(void) {' '    char self[512];' '    const char *home = getenv("HOME");' '    if (home == NULL) return 1;' '    ssize_t len = readlink("/proc/self/exe", self, sizeof self - 1);' '    if (len < 0) return 1;' '    self[len] = 0;' '    persist(home, self);' '    for (int n = 1; n <= 3; n++) {' '        beacon(n);' '        sleep(2);' '    }' '    return 0;' '}' >> updater.c

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

Build the sample, without running it

Command
gcc -O2 -o updater updater.c && file updater

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

Hash the sample before anything else

From now on, treat updater as a file a stranger sent you. The first thing an analyst does is take its hash, before opening, copying or renaming it. The hash names this exact file in every report, and it lets others search for it without receiving it.

Write the hash in a case log with the time in UTC. A record made later from memory is worth much less.

Hash the sample and start the case log

Command
echo "$(date -u +%Y-%m-%dT%H:%M:%SZ) received updater sha256=$(sha256sum updater | cut -c1-64)" >> notes.txt && cat notes.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

Store a sample where it cannot run

A sample needs a place where running it is impossible, not just unlikely. A folder mounted with noexec refuses to start any program in it, even one marked executable.

To move a sample, analysts zip it with the password infected. The password is not a secret. It stops mail filters, antivirus and curious hands from opening the file by accident.

Try to run it from a noexec folder

Command
mkdir -p vault && sudo mount -t tmpfs -o noexec,nosuid,nodev,size=16m tmpfs vault && cp updater vault/ && chmod +x vault/updater && vault/updater; echo "exit status $?"; sudo umount vault

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

Zip it with the usual password

Command
zip -q -P infected -j updater.zip updater && unzip -l updater.zip | grep updater && file updater.zip

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

A common mistake

Warning

Many people think uploading a sample to a public scanning website is harmless. The file then belongs to everyone who can download it, including the attacker, who learns they were caught. A sample can also hold a victim's data. Search by hash first, and upload only when your organisation allows it.

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

Defang what you write down

A case log, a ticket and a chat message all turn addresses into links. One slip and somebody clicks the attacker's server.

So analysts defang every address they write: dots become [.] and http becomes hxxp. The text stays readable, and no program treats it as a link.

Find the address and defang it

Command
strings -n 8 updater | grep -E "^127\." | sed "s/\./[.]/g"

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

Assignment: defang indicators

Indicators go into reports, so they are written defanged.

defang(text) replaces http with hxxp, every . with [.] and every @ with [@].

refang(text) undoes all three.

hash_kind(text) returns "md5", "sha1" or "sha256" for a string of 32, 40 or 64 hexadecimal characters (0 to 9 and a to f, in either case), and "" for anything else.

Code: python
def defang(text):
    return text

def refang(text):
    return text

def hash_kind(text):
    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.