> For the complete documentation index, see [llms.txt](https://alham-rizvi.gitbook.io/alhamrizvi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alham-rizvi.gitbook.io/alhamrizvi/binary-exp/05.-return-oriented-programming.md).

# ROP

> Return-Oriented Programming reference split into focused modules. Each file is self-contained and cross-linked.

***

## Files in This Series

| File                                                                                                                                                                     | Topic                                                               | When to Read            |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------- | ----------------------- |
| [`01_fundamentals.md`](https://github.com/alhamrizvi-cloud/Binary-Exploitation/blob/main/05.%20Return%20Oriented%20Programming/REDmw_ROP_01_fundamentals.md)             | What ROP is, why it works, when it's used, core mental model        | Start here              |
| [`02_gadgets.md`](https://github.com/alhamrizvi-cloud/Binary-Exploitation/blob/main/05.%20Return%20Oriented%20Programming/REDmw_ROP_02_gadgets.md)                       | Gadget types, full catalog, ret2csu, finding gadgets                | Before building a chain |
| [`03_chain_construction.md`](https://github.com/alhamrizvi-cloud/Binary-Exploitation/blob/main/05.%20Return%20Oriented%20Programming/REDmw_ROP_03_chain_construction.md) | Step-by-step chain building, offset finding, 32 vs 64-bit, ASLR+PIE | Core workflow           |
| [`04_techniques.md`](https://github.com/alhamrizvi-cloud/Binary-Exploitation/blob/main/05.%20Return%20Oriented%20Programming/REDmw_ROP_04_techniques.md)                 | ret2libc, ret2plt, ret2syscall, ret2mprotect, stack pivot           | Pick your technique     |
| [`05_examples.md`](https://github.com/alhamrizvi-cloud/Binary-Exploitation/blob/main/05.%20Return%20Oriented%20Programming/REDmw_ROP_05_examples.md)                     | Four full worked exploits with C source + exploit script            | Hands-on practice       |
| [`06_advanced.md`](https://github.com/alhamrizvi-cloud/Binary-Exploitation/blob/main/05.%20Return%20Oriented%20Programming/REDmw_ROP_06_advanced.md)                     | BROP, SROP, ret2dl-resolve, JOP/COP, defenses                       | After mastering basics  |

***

## Recommended Reading Order

```
New to ROP?
  01_fundamentals → 02_gadgets → 03_chain_construction → 05_examples

Know the basics, need a technique?
  04_techniques → 05_examples

CTF / competition?
  03_chain_construction → 04_techniques → 05_examples

Advanced research?
  06_advanced
```

***

## Quick Cheat Sheet

```
SYSCALL NUMBERS (x86-64 Linux):
  read     =  0    rdi=fd,       rsi=buf,      rdx=count
  write    =  1    rdi=fd,       rsi=buf,      rdx=count
  open     =  2    rdi=path,     rsi=flags,    rdx=mode
  mprotect = 10    rdi=addr,     rsi=len,      rdx=prot
  execve   = 59    rdi=filename, rsi=argv,     rdx=envp
  exit     = 60    rdi=status

x86-64 CALLING CONVENTION:
  arg1=rdi  arg2=rsi  arg3=rdx  arg4=rcx  arg5=r8  arg6=r9
  return value = rax

PROT FLAGS:   READ=1  WRITE=2  EXEC=4  RWX=7

STACK ALIGNMENT:
  system() requires RSP 16-byte aligned before call
  Fix: add a bare `ret` gadget before system()

TOOLS:
  ROPgadget --binary ./t --rop
  ropper -f ./t --search "pop rdi"
  one_gadget /lib/x86_64-linux-gnu/libc.so.6
  python3 -c "from pwn import *; r=ROP(ELF('./t')); print(r.dump())"
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://alham-rizvi.gitbook.io/alhamrizvi/binary-exp/05.-return-oriented-programming.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
