본문으로 바로가기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#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, NULLNULLNULL>= 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);
}   
 
cs

코드가 좀 길다..


문제에서 변수가 선언된 순서를 보면, 우리가 풀던 방법대로 문제를 풀 수 없음을 알 수 있다.

왜냐하면 string함수가 맨 위기때문에 오버플로우를 내도 check값을 변경 시킬수가 없다.

그리고 혹시나 오버플로우가 나도 cnt값이 100을 넘어가면 오류를 출력시킨다.


하지만, string함수의 인덱스를 +가 아닌 -의 위치로 조작시킨다면?

string[-100]과 같이말이다.


gdb로 메모리를 분석해보자.

string 문자열은 두번의 printf함수 후에 다뤄진다. 그래서 두번에 printf함수 후에 프로그램 흐름을 살펴보자.

lea eas,ebp-100부분을 보아 string은 ebp-100에 있음을 알 수 있다.

하지만, check변수는 ebp-104에 있다. 위에서 설명한 취약점을 사용할 수 있을것이다.


이러한 취약점을 OOB ( Out Of Bount ) 취약점이라고 한다. 이 취약점을 이용하면 문제를 풀 수 있다.

100 - 104 = -4이므로, 인덱스를 조작해서 -4로 만들어주고, check에 deadbeaf를 넣자.







뾰로롱!




'System Hacking ( pwnable ) > hackerschool FTZ Write-up' 카테고리의 다른 글

[FTZ] level20 ( write - up )  (0) 2019.05.28
[FTZ] level19 ( write - up )  (0) 2019.05.28
[FTZ] level17 ( write - up )  (0) 2019.05.28
[FTZ] level16 ( write - up )  (0) 2019.05.28
[FTZ] level15 ( write - up )  (0) 2019.05.28