Which flags did the build really use?
A team ships a tool built by a script nobody has read for years. Is it protected? The script's flags might say one thing and an old Makefile another.
The binary itself is the only honest answer. Every protection leaves a mark you can read.
By the end of this lesson
Note
I can check which hardening protections a Linux binary was built with.
Key words
| Word | What it means | Example |
|---|---|---|
| Hardening | building a program with its protections turned on | |
| Hardening flag | a compiler or linker option that adds one | -fstack-protector-strong |
| FORTIFY_SOURCE | checked versions of copying functions where sizes are known | -D_FORTIFY_SOURCE=3 |
| ELF header | the start of a Linux binary that says what kind it is | |
| Audit | checking something against a list, from evidence |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsOne program, two builds
The same greet.c is built twice. The weak build turns every protection off. The hard build asks for each protection by name.
Write the program, then build both. Then read the marks each protection leaves.
Write greet.c again
mkdir -p ~/mem/audit && cd ~/mem/audit && cat > greet.c <<'END'
#include <stdio.h>
#include <string.h>
void greet(const char *name) {
char buf[8];
strcpy(buf, name);
printf("hello %s\n", buf);
}
int main(int argc, char **argv) {
greet(argc > 1 ? argv[1] : "ann");
return 0;
}
END
lsShown here, not run. Press Start lab to type it into your own lab’s terminal.
Build the weak version
cd ~/mem/audit && gcc -O2 -fno-stack-protector -U_FORTIFY_SOURCE -no-pie -Wl,-z,norelro,-z,execstack -o greet-weak greet.c && echo builtShown here, not run. Press Start lab to type it into your own lab’s terminal.
Build the hardened version
cd ~/mem/audit && gcc -O2 -fstack-protector-strong -D_FORTIFY_SOURCE=3 -fPIE -pie -Wl,-z,relro,-z,now -o greet-hard greet.c && echo builtShown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: the hardening flags
| Flag | Protection | The mark it leaves |
|---|---|---|
| -fstack-protector-strong | Stack canary | __stack_chk_fail in the symbols |
| -D_FORTIFY_SOURCE=3 (with -O1 or more) | Checked copying functions | __strcpy_chk, __printf_chk |
| -fPIE -pie | Code moves with ASLR | Type: DYN |
| default stack (no -z execstack) | NX | GNU_STACK flags RW, not RWE |
| -Wl,-z,relro,-z,now | Full RELRO | GNU_RELRO and BIND_NOW |
The cells students fill in are left blank in this preview.
Check 1: is it a PIE?
cd ~/mem/audit && for b in greet-weak greet-hard; do echo "$b: $(readelf -h $b | grep Type | awk '{print $2}')"; doneShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check 2: is the stack executable?
cd ~/mem/audit && for b in greet-weak greet-hard; do echo "$b: $(readelf -lW $b | grep GNU_STACK | awk '{print $(NF-1)}')"; doneShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check 3: RELRO and BIND_NOW
cd ~/mem/audit && for b in greet-weak greet-hard; do echo "$b: relro $(readelf -lW $b | grep -c GNU_RELRO), now $(readelf -d $b | grep -c BIND_NOW)"; doneShown here, not run. Press Start lab to type it into your own lab’s terminal.
Checks 4 and 5: canary and FORTIFY
cd ~/mem/audit && for b in greet-weak greet-hard; do echo "$b: $(readelf --dyn-syms -W $b | grep -oE '__(stack_chk_fail|strcpy_chk|printf_chk)' | sort -u | tr '\n' ' ')"; doneShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA common mistake
Warning
Many people think reading the build script proves a binary is hardened. A later flag can undo an earlier one, and some flags need others. FORTIFY does nothing without optimisation. Check the binary that ships.
Ubuntu turns most of these on for you
Ubuntu's gcc builds PIE, with a canary, FORTIFY at -O2 and full RELRO, even when you ask for nothing. Other compilers and other systems may not.
So a plain build here passes, and a build that turns things off fails. Check a plain gcc -O2 build.
Audit a plain build
cd ~/mem/audit && gcc -O2 -o greet-default greet.c && readelf -h greet-default | grep Type | awk '{print $2}' && echo "now $(readelf -d greet-default | grep -c BIND_NOW)" && readelf --dyn-syms -W greet-default | grep -oE '__(stack_chk_fail|strcpy_chk)' | sort -uShown here, not run. Press Start lab to type it into your own lab’s terminal.
FORTIFY stops a copy it can measure
In the hard build, gcc knew buf holds eight bytes, so strcpy became __strcpy_chk. It checks the length before copying and stops the program at once.
That is earlier than the canary, which only checks at the return.
Run the hard build with a long name
cd ~/mem/audit && ./greet-hard ann && ./greet-hard Christopher-Robinson-Smith; echo "exit $?"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsAssignment: write a hardening checker
checksec is a well-known tool that reads these marks. Write a small one. Each argument is the text readelf printed.
audit(header, segments, dynamic, symbols) returns a dictionary: pie (the header has a Type: line with DYN), nx (the GNU_STACK line's flags are RW, not RWE), relro ("full" with GNU_RELRO and BIND_NOW, "partial" with only GNU_RELRO, else "none"), canary (__stack_chk_fail in the symbols) and fortify (any symbol ending _chk other than __stack_chk_fail).
missing(report) returns the sorted names whose value is False or "none", with "relro" also listed when it is "partial".
def audit(header, segments, dynamic, symbols):
return {}
def missing(report):
return []
Make a free account to save your work and track your progress.
Exit quiz
Have a go