Week 7 - shorten

Updated 4 Oct 2026

Week 7 — DEP & ROP (NEW material only, builds on Wk 5–6)

The one new idea: DEP doesn't stop the hijack (Wk6 still works identically) — it stops what you can do with it. This adds one check after EIP is already hijacked.


1. DEP / NX / W^X — the new mechanism

  • Every memory page has R/W/X bits, checked by the MMU on every access
  • W^X rule: a page is writable OR executable — never both
  • NX bit in the page table — checked on every instruction fetch, no partial execution
  • Stack/heap/data = writable, never exec. .text + libraries = exec, never writable

On violation: attacker overwrites RET (same as Wk6) → EIP jumps into stack → NX bit set → hardware exception → process killed → zero attacker bytes ever run


2. The gap DEP leaves (why this lecture exists)

  • DEP checks where code runs, never why — no concept of intent
  • Stops injected code — does nothing about jumping to code that already exists & is trusted (libc, kernel32)
  • New question it forces: "Can't run new code — what existing code can I reuse?"

3. ret2libc

  • Overwrite RET → address of a real library function (e.g. system())
  • Craft stack so it receives chosen args (e.g. ptr to "/bin/sh")
  • No injected code — DEP satisfied, legit function called
  • Limitation: whole functions only, rigid conventions, can't chain → motivates ROP

4. ROP (the core new concept)

  • CPU has no concept of "function" — execution can start mid-instruction-sequence
  • Gadget = short sequence (1–5 instrs) ending in ret, found in existing exec code
  • Each gadget's ret pops the next address off attacker's stack → stack becomes the program
  • Turing-complete, never violates W^X → DEP has nothing to catch

⚠️ What ROP is actually FOR (exam point):

  • NOT the final payload — it's a setup phase
  • Real goal: mprotect/VirtualProtect → flip a page executable → write shellcode → jump in → back to classic code execution

5. Tooling & practical gotcha

  • mona — automates gadget-finding, filters bad-character addresses, assembles chains
  • Bad chars: 0x00 truncates string copies → trick: push negative value, then NEG EAX to avoid null bytes in addresses