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

Free lesson

Checking a binary's hardening

After this lesson: I can check which hardening protections a Linux binary was built with.

60 min lesson9 commandsCompilers and computer architecture lab: gcc, flex, bison and RISC-V in 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

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

WordWhat it meansExample
Hardeningbuilding a program with its protections turned on
Hardening flaga compiler or linker option that adds one-fstack-protector-strong
FORTIFY_SOURCEchecked versions of copying functions where sizes are known-D_FORTIFY_SOURCE=3
ELF headerthe start of a Linux binary that says what kind it is
Auditchecking something against a list, from evidence

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

One 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

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

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

Build the weak version

Command
cd ~/mem/audit && gcc -O2 -fno-stack-protector -U_FORTIFY_SOURCE -no-pie -Wl,-z,norelro,-z,execstack -o greet-weak greet.c && echo built

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

Build the hardened version

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

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

Worked example: the hardening flags

FlagProtectionThe mark it leaves
-fstack-protector-strongStack canary__stack_chk_fail in the symbols
-D_FORTIFY_SOURCE=3 (with -O1 or more)Checked copying functions__strcpy_chk, __printf_chk
-fPIE -pieCode moves with ASLRType: DYN
default stack (no -z execstack)NXGNU_STACK flags RW, not RWE
-Wl,-z,relro,-z,nowFull RELROGNU_RELRO and BIND_NOW

The cells students fill in are left blank in this preview.

Check 1: is it a PIE?

Command
cd ~/mem/audit && for b in greet-weak greet-hard; do echo "$b: $(readelf -h $b | grep Type | awk '{print $2}')"; done

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

Check 2: is the stack executable?

Command
cd ~/mem/audit && for b in greet-weak greet-hard; do echo "$b: $(readelf -lW $b | grep GNU_STACK | awk '{print $(NF-1)}')"; done

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

Check 3: RELRO and BIND_NOW

Command
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)"; done

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

Checks 4 and 5: canary and FORTIFY

Command
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' ' ')"; done

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

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

Shown 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

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

Assignment: 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".

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

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.