Ten minutes to decide
Static triage is a quick look at a suspicious file without running it. A security team may get dozens of samples a day and cannot study each one for a week.
In a few minutes, triage says what a file is, whether anybody has seen it before and what it hints at. Then the team decides which sample deserves the deep look.
By the end of this lesson
Note
I can triage a file without running it and tell whether it has been packed.
Key words
| Word | What it means | Example |
|---|---|---|
| Static analysis | studying a file without running it | |
| Triage | a quick look to decide what needs attention first | |
| String | a run of readable characters inside a file | |
| Entropy | how random a file's bytes look, from 0 to 8 bits | |
| Packer | a tool that compresses a program to hide its contents | upx |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsThe one rule
Important
Work only on the samples you built in lesson 1, in your own lab VM. Nothing in this lesson runs them. Studying anybody else's files or systems needs their written permission, under the Computer Misuse Act 1990 in the UK.
Triage answers three questions
What is it? The file command reads the first bytes. Have we seen it? Its hash can be searched in your own records. What does it hint at? Strings often give away addresses, file paths and messages.
All three are static analysis: nothing runs. Lesson 1 built the sample, so start there.
Look at the sample's type and hash
cd ~/malab && ls -l updater && file updater && sha256sum updaterShown here, not run. Press Start lab to type it into your own lab’s terminal.
Search its strings for clues
strings -n 8 updater | grep -E "127\.|Beacon|LAB-|autostart|Desktop"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsEntropy measures how random bytes look
Entropy counts how evenly the 256 possible byte values are used. Text and code repeat a lot, so they score low. Compressed or encrypted data looks random and scores close to 8 bits a byte, the most possible.
A short script works it out, and counts the strings too.
Write the triage script, part 1
printf '%s\n' '# triage.py: a first look at files, without running them.' 'import hashlib' 'import math' 'import sys' '' 'def entropy(data):' ' counts = [0] * 256' ' for byte in data:' ' counts[byte] += 1' ' total = 0.0' ' for count in counts:' ' if count > 0:' ' share = count / len(data)' ' total = total - share * math.log2(share)' ' return total' '' 'def strings(data, least):' ' found = []' ' run = bytearray()' ' for byte in data + bytes(1):' > triage.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the triage script, part 2
printf '%s\n' ' if 32 <= byte < 127:' ' run.append(byte)' ' else:' ' if len(run) >= least:' ' found.append(run.decode())' ' run = bytearray()' ' return found' '' 'for name in sys.argv[1:]:' ' data = open(name, "rb").read()' ' print(name, len(data), "bytes")' ' print(" sha256 ", hashlib.sha256(data).hexdigest()[:16], "and 48 more")' ' print(" entropy", round(entropy(data), 2), "bits a byte (8 is the most)")' ' print(" strings", len(strings(data, 8)), "of 8 or more characters")' >> triage.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Triage the sample
python3 triage.py updaterShown here, not run. Press Start lab to type it into your own lab’s terminal.
A packer hides a program's contents
Malware is often squeezed by a packer. The packed file holds the program compressed, plus a little code that unpacks it in memory when it runs. On disk, the tell-tale strings are gone.
UPX is a common, legitimate packer. Build the same sample as one static file, then pack a copy.
Build a static copy and pack it
gcc -O2 -static -s -o updater-static updater.c && upx -qq --lzma -o updater.upx updater-static > /dev/null && ls -l updater updater-static updater.upxShown here, not run. Press Start lab to type it into your own lab’s terminal.
Triage all three and search the packed one
python3 triage.py updater-static updater.upx && strings -n 8 updater.upx | grep -E "127\.|Beacon|LAB-|autostart|Desktop" | wc -lShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA packer often signs its work
Packing hides the contents but not the packer. UPX leaves its own marker, which the strings show. Because UPX is a known packer, it can also undo its work.
Unpack a copy, then compare it with the static build byte for byte.
Find the packer's marker
strings updater.upx | grep UPXShown here, not run. Press Start lab to type it into your own lab’s terminal.
Unpack a copy and compare it
upx -qq -d -o unpacked updater.upx > /dev/null; cmp updater-static unpacked && echo identicalShown here, not run. Press Start lab to type it into your own lab’s terminal.
A common mistake
Warning
Many people think high entropy proves a file is malware. A photo, a zip file and an installer score close to 8 as well. High entropy says only that a file is compressed or encrypted, which is a reason to look closer, never a verdict.
Assignment: triage bytes in Python
Write the three parts of a triage script.
entropy(data) returns the Shannon entropy of a bytes value in bits a byte, rounded to 2 places: for each byte value that occurs, with share p of the bytes, subtract p * log2(p). Empty data has entropy 0.0.
printable(data, least) returns the runs of characters with codes 32 to 126 that are at least least long, as strings, in order.
verdict(data) returns "packed?" when the entropy is 7.0 or more and fewer than 3 runs of 6 or more characters are found, and "plain" otherwise.
import math
def entropy(data):
return 0.0
def printable(data, least):
return []
def verdict(data):
return ""
Make a free account to save your work and track your progress.
Exit quiz
Have a go