워게임/root-me

ELF x86 - Stack buffer overflow basic 1

zz! 2025. 7. 28. 07:18
728x90
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
 
int main()
{
 
  int var;
  int check = 0x04030201;
  char buf[40];
 
  fgets(buf,45,stdin);
 
  printf("\n[buf]: %s\n", buf);
  printf("[check] %p\n", check);
 
  if ((check != 0x04030201) && (check != 0xdeadbeef))
    printf ("\nYou are on the right way!\n");
 
  if (check == 0xdeadbeef)
   {
     printf("Yeah dude! You win!\nOpening your shell...\n");
     setreuid(geteuid(), geteuid());
     system("/bin/bash");
     printf("Shell closed! Bye.\n");
   }
   return 0;
}

buf의 사이즈는 40이다. 하지만 fgets에서는 45만큼 입력을 받고 있다.

그리고 printf를 통해서 buf와 check를 출력을 해주고 있다. 첫 번째 조건문에서는 check가 0x04030201 이랑 0xdeadbeef 의 값이 아니면 "You are on the right way!" 라는 것을 출력을 한다.

그 다음 조건문에서는 check가 0xdeadbeef 일 경우에는 system함수를 통해서 shell을 실행을 할 수 있다.

pwntools만 사용하다가 막혀서, https://hackingstudypad.tistory.com/252 이 블로그에서 나온 방법대로 했다.

위에 방식으로도 되는 것을 알게 되었다.

728x90