워게임/root-me

ELF x64 - Basic heap overflow

zz! 2025. 7. 28. 22:09
728x90
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
 
void    checkArg(const char *a)
{
  while (*a)
    {
      if (   (*a == ';')
          || (*a == '&')
          || (*a == '|')
          || (*a == ',')
          || (*a == '$')
          || (*a == '(')
          || (*a == ')')
          || (*a == '{')
          || (*a == '}')
          || (*a == '`')
          || (*a == '>')
          || (*a == '<') ) {
        puts("Forbidden !!!");
        exit(2);
      }
        a++;
    }
}
 
int     main()
{
  char  *arg = malloc(0x20);
  char  *cmd = malloc(0x400);
  setreuid(geteuid(), geteuid());
 
  strcpy(cmd, "/bin/ls -l ");
 
  printf("Enter directory you want to display : ");
 
  gets(arg);
  checkArg(arg);
 
  strcat(cmd, arg);
  system(cmd);
 
  return 0;
}

gets 함수를 통해서 힙 오버플로우가 발생한다. 

gdb를 사용해서 힙의 정렬을 확인을 한다. 반환 값은 rax에 있다.

그래서 두 반환 값을 빼면 0x30이다. 즉, 0x30 바이트를 채우면 된다.

strcpy 함수에서 "/bin/ls -l" 문자열을 cat .passwd로 쓰면 된다.

최종 페이로드는 다음과 같다.

728x90