Bace

picoCTF 2018 - leak me 본문

picoCTF 2018/Binary Exploitation

picoCTF 2018 - leak me

Bace 2020. 5. 22. 04:45

 

주어진 주소와 포트에 접속하면 다음과 같이 나온다.

이름과 password를 치면 맞지 않은 password라고 뜬다.

 

주어진 소스를 봐보았다.

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

int flag() {
  char flag[48];
  FILE *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);
  printf("%s", flag);
  return 0;
}


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

  setvbuf(stdout, NULL, _IONBF, 0);
  
  // Set the gid to the effective gid
  gid_t gid = getegid();
  setresgid(gid, gid, gid);
  
  // real pw: 
  FILE *file;
  char password[64];
  char name[256];
  char password_input[64];
  
  memset(password, 0, sizeof(password));
  memset(name, 0, sizeof(name));
  memset(password_input, 0, sizeof(password_input));
  
  printf("What is your name?\n");
  
  fgets(name, sizeof(name), stdin);
  char *end = strchr(name, '\n');
  if (end != NULL) {
    *end = '\x00';
  }

  strcat(name, ",\nPlease Enter the Password.");

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

  fgets(password, sizeof(password), file);

  printf("Hello ");
  puts(name);

  fgets(password_input, sizeof(password_input), stdin);
  password_input[sizeof(password_input)] = '\x00';
  
  if (!strcmp(password_input, password)) {
    flag();
  }
  else {
    printf("Incorrect Password!\n");
  }
  return 0;
}

 

password.txt 파일과 비교하여 입력한 password가 맞으면 flag를 띄워주는 프로그램이다.

 

지금 주어진 password는 없으므로 주어진 파일을 gdb로 분석해보았다.

 

password.txt 파일이 없으므로 오류가 뜬다.

password.txt 파일을 만들고 1234 를 저장 후 다시 해보았다.

이번엔 flag 파일이 없다고 한다.

flag.txt 파일을 만든 후 12345678로 저장했다.

이름을 아무거나 입력한 후 password만 주면 flag를 주는 프로그램이다.

 

gdb로 확인해보았다.

 

이 중 main 함수의 ret에 breakpoint를 걸고 메모리를 확인해보았다.

 

입력한 이름 값이 들어가있고, password는 이번에 입력한 password가 아닌 원래 password가 나오는 것을 확인할 수 있다.

 

둘 사이의 offset을 확인해보았다.

256이 나온다.

 

name의  버퍼 크기인 256 바로 다음에 실제 password가 나오는 것 같다.

 

문제에 주어진 주소와 포트에 접속하여 name 값에 a를 256개 줘보았다.

 

password가 뜬다.

이제 password를 입력해보았다.

flag가 뜬다.

 

picoCTF{aLw4y5_Ch3cK_tHe_bUfF3r_s1z3_d1667872}

'picoCTF 2018 > Binary Exploitation' 카테고리의 다른 글

picoCTF 2018 - buffer overflow 2  (0) 2020.05.22
picoCTF 2018 - shellcode  (0) 2020.05.22
picoCTF 2018 - buffer overflow 1  (0) 2020.05.22
picoCTF 2018 - buffer overflow 0  (0) 2020.05.21