ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [HackerSchool] Level18
    오래된/HackerSchool 2011. 5. 10. 05:40

    [level18@ftz level18]$ cat hint

    #include <stdio.h>
    #include <sys/time.h>
    #include <sys/types.h>
    #include <unistd.h>
    void shellout(void);
    int main()
    {
      char string[100];
      int check;
      int x = 0;
      int count = 0;
      fd_set fds;
      printf("Enter your command: ");
      fflush(stdout);
      while(1)
        {
          if(count >= 100)
            printf("what are you trying to do?\n");
          if(check == 0xdeadbeef)
            shellout();
          else
            {
              FD_ZERO(&fds);
              FD_SET(STDIN_FILENO,&fds);
     
              if(select(FD_SETSIZE, &fds, NULL, NULL, NULL) >= 1)
                {
                  if(FD_ISSET(fileno(stdin),&fds))
                    {
                      read(fileno(stdin),&x,1);
                      switch(x)
                        {
                          case '\r':
                          case '\n':
                            printf("\a");
                            break;
                          case 0x08:
                            count--;
                            printf("\b \b");
                            break;
                          default:
                            string[count] = x;
                            count++;
                            break;
                        }
                    }
                }
            }
        }
    }
     
    void shellout(void)
    {
      setreuid(3099,3099);
      execl("/bin/sh","sh",NULL);
    }  

    [ level18 hint ]

    Underflow 문제인데, 알고리즘을 정확히 분석하고 있다면 쉽게 해결 가능한 문제이다.

    메모리 상태를 확인해보면

    [ ebp-main ][ ret-main ][ fds ][ count ][ x ][ check ][ string ][ ebp-libc ][ ret-libc ]

    위와 같은 것이다.
    일단, check 와 string 사이에 dummy 영역이 존재하는지 확인하기 위해 gdb로 확인 해본다.

    [level18@ftz tmp]$ gdb -q ./attackme
    (no debugging symbols found)...(gdb) disas main
    ...[중략]...
    0x080485ab <main+91>:   cmpl   $0xdeadbeef,0xffffff98(%ebp)                    - if(check == 0xdeadbeef)
    ...[중략]...
    0x08048743 <main+499>:  lea    0xffffff9c(%ebp),%eax                               - string[count] = x
    0x08048746 <main+502>:  mov    %eax,0xffffff04(%ebp)

    0x0804874c <main+508>:  mov    0xffffff90(%ebp),%edx
    0x0804874f <main+511>:  mov    0xffffff94(%ebp),%cl
    0x08048752 <main+514>:  mov    %cl,0xffffff03(%ebp)
    0x08048758 <main+520>:  mov    0xffffff03(%ebp),%al
    0x0804875e <main+526>:  mov    0xffffff04(%ebp),%ecx
    0x08048764 <main+532>:  mov    %al,(%edx,%ecx,1)
    [ attackme 주요 코드 ]

    위 주요 코드에서 보면 check 는 0xffffff98(%ebp) 에 string 은 0xffffff9c(%ebp) 에 존재하는 것을 알 수 있다.
    즉 check 와 string 사이에 dummy 공간이 존재하지 않는 것이기 때문에 4 byte 뒤에서부터 0xdeadbeef 를 채우면 되겠다.

    '오래된 > HackerSchool' 카테고리의 다른 글

    [HackerSchool] Level20  (2) 2011.05.15
    [HackerSchool] Level19  (0) 2011.05.10
    [HackerSchool] Level17  (0) 2011.05.10
    [HackerSchool] Level16  (0) 2011.05.10
    [HackerSchool] Level15  (0) 2011.05.10

    댓글

Designed by Tistory.