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
| Word | What it means | Example |
|---|---|---|
| Persistence | a way for a program to start again by itself | |
| Autostart entry | a file that starts a program at login | |
| Indicator of compromise | a fact that shows a system was affected | a hash or an address |
| ATT&CK technique | a named, numbered attacker behaviour | T1547.013 |
| Remediation | removing the problem and checking it is gone |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsThe 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
| Place | Starts the program | ATT&CK technique |
|---|---|---|
| ~/.config/autostart/ | At each desktop login | T1547.013 |
| ~/.config/systemd/user/, /etc/systemd/system/ | As a service | T1543.002 |
| /etc/cron.d/, a crontab | On a timetable | T1053.003 |
| ~/.bashrc, ~/.profile | At each new shell | T1546.004 |
The cells students fill in are left blank in this preview.
Set a mark and run the sample once
cd ~/malab && ls -l updater && touch before.mark && sleep 1 && ./updater && echo finishedShown here, not run. Press Start lab to type it into your own lab’s terminal.
Sweep the persistence places
find ~/.config/autostart ~/.config/systemd/user ~/.bashrc ~/.profile /etc/cron.d /etc/systemd/system -newer before.mark -type f 2>/dev/nullShown here, not run. Press Start lab to type it into your own lab’s terminal.
Read the entry it left
grep -H Exec ~/.config/autostart/lab-updater.desktopShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsRemove 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
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 -lShown here, not run. Press Start lab to type it into your own lab’s terminal.
Hunt for copies with YARA
yara -r lab.yar ~ 2>/dev/nullShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA 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
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.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the report script, part 2
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.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the report and read it
python3 report.py && cat report.mdShown 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.
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