Exploit › Module 5 › Lesson 2
Fortify Sanitize
FORTIFY_SOURCE wraps libc calls; AddressSanitizer catches heap bugs at test time — run on YOUR $PWN_LAB toys in CI and local test.
Visual · pwn_fortify_sanitize
Fortify/sanitize literacy. $PWN_LAB only. Original Cyberlium.
Opening
Compile-time FORTIFY and test-time ASan turn silent corruption into loud failures — use both.
_FORTIFY_SOURCE=2 with -O2 adds bounds checks to selected libc functions at compile time — failures abort with clear message. AddressSanitizer (-fsanitize=address) instruments allocations in test binaries — slows down, catches UAF/overflow during development. Cyberlium runs ASan builds on YOUR toys at $PWN_LAB before merging fixes — not as a step toward weaponization. Next: Safe APIs.
1. FORTIFY_SOURCE (named)
Requires -O2 (or -O3) and glibc. Catches some compile-time-known overflow sizes in strcpy-like wrappers — not a substitute for safe coding.
On $PWN_LAB, trigger FORTIFY abort on toy BOF — observe message; fix with bounded copy; rebuild clean.
Command guide
Try these commands — FORTIFY_SOURCE (named)
═══ TOOLS & WEBSITES ═══ Browse / read these (authorized learning only — stay in YOUR lab / program scope)
FORTIFY_SOURCE — https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html#index-D_005fFORTIFY_005fSOURCE AddressSanitizer — https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html#index-fsanitize_003daddress Microsoft SDL — https://learn.microsoft.com/en-us/security/sdl/ (safe CRT / /GS literacy)
═══ INSTALL ═══
Linux (Debian/Ubuntu):
Command — copy this
sudo apt install build-essential sudo apt install curl
macOS:
Command — copy this
xcode-select --install # or brew install gcc
Windows: Use WSL or MinGW Built-in (PowerShell: Invoke-WebRequest)
═══ LINUX / macOS ═══
Command — copy this
export LAB_PWN=${LAB_PWN:-$HOME/cyberlium-lab/t24-pwn}
gcc -Wall -D_FORTIFY_SOURCE=2 -O2 -g -o "$LAB_PWN/bin/strcpy_fortify" "$LAB_PWN/src/strcpy_safe.c"
gcc -Wall -fsanitize=address -O0 -g -o "$LAB_PWN/bin/uaf_asan" "$LAB_PWN/src/uaf_named.c" 2>/dev/null || echo 'ASan build optional — install libasan if missing'
curl -sS https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html | grep -i fortify | head -3
echo 'fortify/sanitize literacy: detect bugs on YOUR toys — not weaponize findings'Primary tools to practice this lesson: gcc, curl. Reference sites: FORTIFY_SOURCE (https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html#index-D_005fFORTIFY_005fSOURCE); AddressSanitizer (https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html#index-fsanitize_003daddress); Microsoft SDL (https://learn.microsoft.com/en-us/security/sdl/). Run every command in the box — install first, then the usage lines — only on YOUR lab / program scope.
2. AddressSanitizer (named)
Build with -fsanitize=address -g for test target. Run toy UAF demo — ASan prints allocation/free stack. Use ASAN_OPTIONS=log_path for CI artifacts.
Do not ship ASan binaries to production — test-only instrumentation.
3. CI habit
Pipeline: asan test job on toy suite, secure release job with FORTIFY + checksec gate. Failing ASan blocks merge — defender workflow.
Ship: fortify/sanitize card — flags, when to use, sample ASan one-liner from YOUR toy. Next: Safe APIs.
4. What you ship: FORTIFY/ASan card for $PWN_LAB
Flag lines for FORTIFY and ASan. Sample abort/ASan message from toy. Test vs release note. chmod 600.
5. What you record before the next lesson
Date. Fortify/sanitize card. $PWN_LAB named. File t24-m05-l02-fortify-sanitize.txt chmod 600.
6. Wrong vs right: weaponized exploits vs memory-safety literacy
Worked failure — same MSF word, opposite target. Right never needs a café Wi-Fi or classmate laptop.
Wrong
Disable FORTIFY to 'see raw overflow.' Ship ASan binary as production release.
Right
Document FORTIFY/ASan with sample output from YOUR toy tests. Next: Safe APIs.
Mission: run ASan on one toy
1) Build toy with -fsanitize=address. 2) Paste one ASan error block. 3) Note FORTIFY flag in release Makefile. 4) chmod 600.
Stuck? Ask Cyberlium AI Mentor
ASan in CI catches bugs before attackers do.
Knowledge Check
APPLY: FORTIFY_SOURCE helps by:
Multiple choice
Knowledge Check
APPLY: True or False: AddressSanitizer is for test builds, not typical production release.
True or False
Knowledge Check
APPLY: ASan report on YOUR toy primarily aids:
Multiple choice