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

Free lesson

Finding malware persistence and writing a report

After this lesson: I can find how a sample starts again and report it with its indicators.

65 min lesson8 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

It came back after the restart

Persistence is how malware makes sure it starts again after a restart or a new login. Deleting the program is not enough if an entry somewhere still starts it.

So an analysis ends with two jobs. Find every way the sample survives, then write down what others need to find and remove it.

By the end of this lesson

Note

I can find how a sample starts again and report it with its indicators.

Key words

WordWhat it meansExample
Persistencea way for a program to start again by itself
Autostart entrya file that starts a program at login
Indicator of compromisea fact that shows a system was affecteda hash or an address
ATT&CK techniquea named, numbered attacker behaviourT1547.013
Remediationremoving the problem and checking it is gone

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

Sweep and clean only your own lab VM. Searching or changing another computer needs its owner's written permission, under the Computer Misuse Act 1990 in the UK. The entry you remove is one your harmless sample made.

Linux has a few places to start again

A program that wants to return writes an entry in a place the system reads at start-up or login. The table lists common ones, each with its ATT&CK technique number, the shared names defenders use.

A sweep looks in all of them for anything changed since the sample arrived.

Where persistence hides on Linux

PlaceStarts the programATT&CK technique
~/.config/autostart/At each desktop loginT1547.013
~/.config/systemd/user/, /etc/systemd/system/As a serviceT1543.002
/etc/cron.d/, a crontabOn a timetableT1053.003
~/.bashrc, ~/.profileAt each new shellT1546.004

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

Set a mark and run the sample once

Command
cd ~/malab && ls -l updater && touch before.mark && sleep 1 && ./updater && echo finished

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

Sweep the persistence places

Command
find ~/.config/autostart ~/.config/systemd/user ~/.bashrc ~/.profile /etc/cron.d /etc/systemd/system -newer before.mark -type f 2>/dev/null

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

Read the entry it left

Command
grep -H Exec ~/.config/autostart/lab-updater.desktop

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

Remove it, then prove it is gone

Remediation has two halves: remove the entry, then sweep again to show it is gone. A clean-up nobody checked is a guess.

Then hunt for copies. The YARA rule from lesson 4 searches the whole home folder, and should find the sample only in your own work folder.

Remove the entry and sweep again

Command
rm ~/.config/autostart/lab-updater.desktop && find ~/.config/autostart ~/.config/systemd/user ~/.bashrc ~/.profile /etc/cron.d /etc/systemd/system -newer before.mark -type f 2>/dev/null | wc -l

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

Hunt for copies with YARA

Command
yara -r lab.yar ~ 2>/dev/null

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 report others can act on

A malware report gives the files and their hashes, and the indicators of compromise, defanged. It adds the behaviour with technique numbers, the detection rules and what was cleaned.

Write it from your notes, not from memory. The script gathers this module's findings and works out the hashes as it runs.

Write the report script, part 1

Command
printf '%s\n' '# report.py: writes report.md from what this module found.' 'import hashlib' '' 'def sha256(name):' '    return hashlib.sha256(open(name, "rb").read()).hexdigest()' '' 'def defang(text):' '    return text.replace(".", "[.]")' '' 'lines = ["# Report: the lab updater sample (harmless, built for the lab)", "", "## Files"]' 'for name in ["updater", "updater.upx", "invoice.exe"]:' '    lines.append("- " + name + " sha256 " + sha256(name))' 'lines += ["", "## Indicators",' '         "- " + defang("127.66.0.23") + " port 8443, plain TCP",' '         "- text MT-LAB-SAMPLE-7F3A and LabBeacon/1.0 in each beacon",' '         "- file ~/.config/autostart/lab-updater.desktop"]' 'lines += ["", "## Behaviour and ATT&CK",' '         "- T1547.013 XDG autostart entry: starts at each desktop login",' '         "- T1095 non-application layer protocol: 3 beacons, 2 seconds apart",' '         "- T1027.002 software packing: updater.upx, entropy about 8"]' > report.py

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

Write the report script, part 2

Command
printf '%s\n' 'lines += ["", "## Detection and clean-up",' '         "- YARA: lab.yar (strings), packed.yar (UPX and entropy), pe.yar (imports)",' '         "- alert on new files in ~/.config/autostart",' '         "- removed the autostart entry, the sweep found nothing else"]' 'open("report.md", "w").write(chr(10).join(lines) + chr(10))' 'print("report.md written,", len(lines), "lines")' >> report.py

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

Write the report and read it

Command
python3 report.py && cat report.md

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

A common mistake

Warning

Many people think a list of hashes is the whole report. A rebuilt or repacked copy has a new hash, as updater and updater.upx show. Behaviour, rules and techniques still describe the next copy, so they matter more than the hashes.

Assignment: sweep for new persistence

Two listings of file paths, before and after, were taken around a sample's run. home is the user's home folder, such as "/home/student".

technique(path, home) returns "T1547.013" for a path that starts with home + "/.config/autostart/", "T1543.002" for home + "/.config/systemd/user/" or "/etc/systemd/system/", "T1053.003" for "/etc/cron.d/", "T1546.004" for exactly home + "/.bashrc" or home + "/.profile", and "" otherwise.

new_entries(before, after, home) returns a list of [path, technique] pairs for each path in after that is not in before and has a technique, in the order of after.

summary(entries) returns one line per entry, "T1547.013 /home/student/...", sorted.

Code: python
def technique(path, home):
    return ""

def new_entries(before, after, home):
    return []

def summary(entries):
    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.