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
| Word | What it means | Example |
|---|---|---|
| Sample | one copy of a suspicious file kept for study | |
| Isolation | keeping a sample away from real machines and networks | |
| Hash | a fingerprint of a file's exact bytes | sha256sum updater |
| Case log | a dated record of what was done to the evidence | |
| noexec | a mount option that stops any file there running | |
| Defang | to change an address so it cannot be clicked | 127[.]66[.]0[.]23 |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsThe 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
mkdir -p ~/malab && cd ~/malab && sudo apt-get install -y -qq yara upx-ucl zip unzipShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the sample's source, part 1
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.cShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the sample's source, part 2
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.cShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the sample's source, part 3
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.cShown here, not run. Press Start lab to type it into your own lab’s terminal.
Build the sample, without running it
gcc -O2 -o updater updater.c && file updaterShown 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
echo "$(date -u +%Y-%m-%dT%H:%M:%SZ) received updater sha256=$(sha256sum updater | cut -c1-64)" >> notes.txt && cat notes.txtShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsStore 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
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 vaultShown here, not run. Press Start lab to type it into your own lab’s terminal.
Zip it with the usual password
zip -q -P infected -j updater.zip updater && unzip -l updater.zip | grep updater && file updater.zipShown 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 questionsDefang 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
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.
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