The label on the box
Every Linux program starts with an ELF header and every Windows program with a PE header. A header is the label on the box: what machine the program is for, where it starts and what it needs from the system.
An analyst reads it in seconds, and it often says what a program can do before anybody runs it.
By the end of this lesson
Note
I can read an ELF or PE header and say what the program needs in order to run.
Key words
| Word | What it means | Example |
|---|---|---|
| Header | the fixed fields at the start of a file that describe it | |
| ELF | the format of programs on Linux | |
| PE | the format of programs on Windows | |
| Entry point | the address where a program starts running | |
| Import | a function a program borrows from a library | connect |
| Section | a named part of a program file, such as code or text |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsThe one rule
Important
Read only the files this module builds, in your own lab VM. The Windows file you will write is a header for study and is never run. Analysing anybody else's programs or machines needs their written permission, under the Computer Misuse Act 1990 in the UK.
An ELF header starts with four magic bytes
An ELF file begins with the bytes 7f 45 4c 46, which is a byte then ELF in letters. The header after them says 32 or 64 bit, the byte order, the type of file and the machine.
It also holds the entry point, where the program starts. readelf prints the fields by name, and od shows the raw bytes behind them.
Read the sample's ELF header
cd ~/malab && ls -l updater && readelf -h updater | grep -E "Class|Type|Machine|Entry"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
See the same fields as raw bytes
od -A d -t x1 -N 20 updaterShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsImports say what a program can do
A program rarely talks to the network or the disk itself. It imports functions from a library, and the names are listed for the loader to find.
So the import list is a list of abilities. socket and connect mean networking, and fopen and mkdir mean writing files. A packed program hides its list, and even its sections.
List the library and the imports
readelf -d updater | grep NEEDED && readelf --dyn-syms -W updater | grep -o -E "(socket|connect|send|fopen|mkdir|readlink|getenv)@[A-Z0-9_.]+"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Read the packed copy's headers
readelf -S updater.upx; readelf -l -W updater.upx | grep -E "Entry|LOAD"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA PE header points to itself
A PE file starts with MZ, an old DOS header. At byte 60 it holds a number: where the real header starts, with the letters PE and two zero bytes.
Then come the machine, the number of sections, a time stamp and the subsystem, and an import table naming each DLL. The script writes a small one to study. It is never run.
Write the PE header script, part 1
printf '%s\n' '# mkpe.py: writes invoice.exe, a made-up Windows program header to study.' '# Its only code is "return 0". It imports names and never calls them.' 'import struct' '' 'STAMP = 1700000000' 'DLLS = [["WS2_32.dll", ["socket", "connect", "send"]], ["KERNEL32.dll", ["Sleep"]]]' 'TEXT = 0x1000' 'RDATA = 0x2000' '' 'def pad(data, size):' ' return data + bytes(size - len(data))' '' 'words = [b"127.66.0.23", b"LabBeacon/1.0", b"MT-LAB-SAMPLE-7F3A"]' 'rdata = bytearray(pad(bytes(1).join(words), 0x40))' 'table = len(rdata)' 'rdata += bytes(20 * (len(DLLS) + 1))' 'names = {}' 'for dll in DLLS:' ' for func in dll[1]:' ' names[func] = len(rdata)' > mkpe.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the PE header script, part 2
printf '%s\n' ' rdata += bytes(2) + func.encode() + bytes(2 - len(func) % 2)' ' names[dll[0]] = len(rdata)' ' rdata += dll[0].encode() + bytes(2 - len(dll[0]) % 2)' 'rdata += bytes(-len(rdata) % 8)' 'for at in range(len(DLLS)):' ' lists = []' ' for copy in range(2):' ' lists.append(RDATA + len(rdata))' ' for func in DLLS[at][1]:' ' rdata += struct.pack("<Q", RDATA + names[func])' ' rdata += bytes(8)' ' entry = struct.pack("<IIIII", lists[0], 0, 0, RDATA + names[DLLS[at][0]], lists[1])' ' rdata[table + 20 * at:table + 20 * at + 20] = entry' '' 'dos = bytearray(64)' 'dos[0:2] = b"MZ"' 'struct.pack_into("<I", dos, 60, 64)' 'coff = struct.pack("<4sHHIIIHH", b"PE" + bytes(2), 0x8664, 2, STAMP, 0, 0, 240, 0x22)' 'opt = struct.pack("<HBBIIIIIQIIHHHHHHIIIIHHQQQQII", 0x20B, 14, 0, 0x200, 0x200, 0,' ' TEXT, TEXT, 0x140000000, 0x1000, 0x200, 6, 0, 0, 0, 6, 0,' >> mkpe.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the PE header script, part 3
printf '%s\n' ' 0, 0x3000, 0x200, 0, 3, 0x8160, 0x100000, 0x1000, 0x100000, 0x1000, 0, 16)' 'dirs = bytearray(16 * 8)' 'struct.pack_into("<II", dirs, 8, RDATA + table, 20 * (len(DLLS) + 1))' 'sections = struct.pack("<8sIIIIIIHHI", b".text", 3, TEXT, 0x200, 0x200, 0, 0, 0, 0, 0x60000020)' 'sections += struct.pack("<8sIIIIIIHHI", b".rdata", len(rdata), RDATA, 0x200, 0x400, 0, 0, 0, 0, 0x40000040)' 'headers = pad(bytes(dos) + coff + opt + bytes(dirs) + sections, 0x200)' 'code = pad(bytes([0x31, 0xC0, 0xC3]), 0x200)' 'with open("invoice.exe", "wb") as out:' ' out.write(headers + code + pad(bytes(rdata), 0x200))' 'print("invoice.exe written,", len(headers + code) + 0x200, "bytes")' >> mkpe.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the file and find its header
python3 mkpe.py && file invoice.exe && od -A d -t x1 -j 60 -N 4 invoice.exeShown here, not run. Press Start lab to type it into your own lab’s terminal.
Read its time stamp and imports
objdump -p invoice.exe | grep -E "Time/Date|^Subsystem|DLL Name|socket|connect|send|Sleep"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
A common mistake
Warning
Many people think a PE time stamp says when a program was built. It is four bytes anyone can write, and this one is made up. Treat it as a clue that must agree with other evidence, such as when the file first appeared on your systems.
Check
Have a go
Check
3 questionsAssignment: read a file's header
Write a small header reader. Numbers in both formats are little-endian here: int.from_bytes(data[a:b], "little") reads one.
kind(data) returns "elf" when the data starts with the bytes 7f 45 4c 46, "pe" when it starts with MZ and the 4 bytes at the offset stored in bytes 60 to 63 are PE and two zero bytes, and "unknown" otherwise (also when that offset points past the end).
elf_machine(data) reads the 2-byte field at offset 18 and returns "x86-64" for 62, "aarch64" for 183 and "other" for anything else.
pe_sections(data) returns the section names of a PE file, in order. With p the PE offset, the number of sections is 2 bytes at p + 6 and the size of the optional header 2 bytes at p + 20. The table starts at p + 24 plus that size, each entry is 40 bytes, and a name is its first 8 bytes without the zero bytes at the end.
def kind(data):
return "unknown"
def elf_machine(data):
return "other"
def pe_sections(data):
return []
Make a free account to save your work and track your progress.
Exit quiz
Have a go