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

Free lesson

Static triage of a suspicious file

After this lesson: I can triage a file without running it and tell whether it has been packed.

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

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

WordWhat it meansExample
Static analysisstudying a file without running it
Triagea quick look to decide what needs attention first
Stringa run of readable characters inside a file
Entropyhow random a file's bytes look, from 0 to 8 bits
Packera tool that compresses a program to hide its contentsupx

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

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

Command
cd ~/malab && ls -l updater && file updater && sha256sum updater

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

Search its strings for clues

Command
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 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

Entropy 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

Command
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.py

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

Write the triage script, part 2

Command
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.py

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

Triage the sample

Command
python3 triage.py updater

Shown 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

Command
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.upx

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

Triage all three and search the packed one

Command
python3 triage.py updater-static updater.upx && strings -n 8 updater.upx | grep -E "127\.|Beacon|LAB-|autostart|Desktop" | wc -l

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 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

Command
strings updater.upx | grep UPX

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

Unpack a copy and compare it

Command
upx -qq -d -o unpacked updater.upx > /dev/null; cmp updater-static unpacked && echo identical

Shown 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.

Code: python
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

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.