> For the complete documentation index, see [llms.txt](https://www.ctfrecipes.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.ctfrecipes.com/pwn/stack-exploitation/arbitrary-code-execution/stack-pivoting.md).

# Stack pivoting

**Stack pivoting** is a technique used when there is **lack of space after RIP** to conduct a full[ ROP chain](/pwn/stack-exploitation/arbitrary-code-execution/code-reuse-attack/return-oriented-programming-rop.md).

## How it works

**Stack pivoting** consist to take the control of the **`RSP`** register and then *"fake"* the location of the [stack frame](/pwn/general-knowledge/operation-of-the-stack.md).

The general target is to set **`RSP`** at the beginning of the vulnerable buffer.

There is some ways to do this.

### POP RSP gadget

The simplest but also the least likely to exist.

If there is one `pop rsp; ret` gadget then the attacker can use it to pop the bytes after overwriting RIP.

{% hint style="info" %}
This gadget requires 8 bytes after RIP.

```
ret       <-- overwrite RIP
pop rsp   <-- set RSP arbitrary value
```

{% endhint %}

### XCHG \<reg>, RSP

`xchg` gadget swap the values between the two registers.

{% hint style="info" %}
This gadget requires 16 bytes after RIP.

```
ret                 <-- overwrite RIP
pop <reg>           <-- set a register to an arbitrary value
xchg <reg>, rsp     <-- swap rsp and register values
```

{% endhint %}

### LEAVE; RET

This is the most likely to exist and interesting way to stack pivoting. **And it do not requires bytes space** after RIP

Every function, excepted `main`, end with a `leave; ret` gadget.

`leave` is equivalent to

```
mov rsp, rbp
pop rbp
```

A function ending therefore looks like

```
mov rsp, rbp
pop rbp
pop rip
```

That means, when RIP is overwrite, the 8 bytes before overwrite RBP. Then, using `leave` it's possible to overwrite `rbp` .

Note that before `pop rbp` there is a `mov rsp, rbp` instruction. This instruction set the `rsp` value at the `rbp` value. So, if RIP point to `leave; ret` again, the value of the overwrited `rbp` gets moved to `RSP`.

{% hint style="success" %}
This gadget do not requires byte space after RIP.
{% endhint %}


---

# 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://www.ctfrecipes.com/pwn/stack-exploitation/arbitrary-code-execution/stack-pivoting.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.
