# ret2libc

## Theory

If the function is dynamically linked, it is possible to utilize gadgets from libc. For example, if the binary calls `puts()`, we can control EIP/RIP and then leak an address from GOT. If `libc.so` is given, just attach it and find the offset; otherwise, use libc database to search the offset. We compute the libc base address:

```
libc_base_address = leaked_address_from_GOT - offset
```

Then we compute the system() address and "/bin/sh" address:

```
system_address = libc_base_address + offset
bin_sh_address = libc_base_address + offset
```

In the end we call `system("/bin/sh")`. Read this writeup to learn more:

{% embed url="<https://www.ctfwriteup.com/pwn/jarvis-oj-pwn-xman-series>" %}
Jarvis OJ Pwn Xman Series - ctfwriteup.com
{% endembed %}

## Template:


---

# Agent Instructions: 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:

```
GET https://ret2basic.gitbook.io/ctfnote/pwn/linux-exploitation/rop/ret2libc.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
