티스토리 뷰

Pwnable/Heap

[Protostar] Heap2

D41JUNG0D 2018. 3. 11. 23:41

1. Introduce


heap을 얼마나 이해하고 있는지를 알기위해서 protostar의 heap시리즈의

          write-up을 작성해보겠습니다.


2. Analysis


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

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <sys/types.h>

#include <stdio.h>

struct auth {

 char name[32];

 int auth;

};

struct auth *auth;

char *service;

int main(int argc, char **argv)

{

 char line[128];

 while(1) {

     printf("[ auth = %p, service = %p ]\n", auth, service);

     if(fgets(line, sizeof(line), stdin) == NULL) break;

     

     if(strncmp(line, "auth ", 5) == 0) {

         auth = malloc(sizeof(auth));

         memset(auth, 0, sizeof(auth));

         if(strlen(line + 5) < 31) {

             strcpy(auth->name, line + 5);

         }

     }

     if(strncmp(line, "reset", 5) == 0) {

         free(auth);

     }

     if(strncmp(line, "service", 6) == 0) {

         service = strdup(line + 7);

     }

     if(strncmp(line, "login", 5) == 0) {

         if(auth->auth) {

             printf("you have logged in already!\n");

         } else {

             printf("please enter your password\n");

         }

     }

 }

}

Colored by Color Scripter

cs


코드를 보면 각 옵션들에 따라서 동작을 수행하고 auth구조체의 auth멤버 변수의 값이 존재하면 exploit이 되었다고 생각하면 될 것 같습니다.


strdup는 인자를 malloc해주고 malloc과 같이 메모리가 할당된 주소를 반환합니다.


3. Exploit


직접 프로그램을 실행시키고 auth와 service 옵션을 사용해서 메모리를 할당해보고 출력되는 주소의 값을 확인해보았습니다.


두 주소의 차이를 구해보니 0x10으로 16byte만큼 차이가 나있는데,  분명히 malloc은 구조체 auth 만큼 해주었는데 왜 0x10만큼 차이가 날까요? 그 부분은 아직 좀 더 살펴보아야 알 것 같습니다.


아마 memset 함수를 통해 할당한 영역을 0으로 초기화해주는 부분때문에 생기는 오류? 인듯 싶습니다.


아무튼 이 주소의 차이를 알았으므로 auth 구조체의 auth멤버 변수의 위치를 구하면 exploit이 가능할 것 같습니다.


auth가 0x0804c0008에 할당되어있고, 원래대로라면 name[32] + auth 할당이 되었을 것이므로 + 0x20 부분에 auth멤버 변수가 위치해 있을 것 입니다.


그렇다면 service의 strdup함수에서 heap over flow가 발생하기 때문에 service의 위치인 0x0804c018 에서 auth 멤버 변수의 위치인 0x0804c028 까지 채워주고 또 1byte를 더 채워주면 auth값이 들어가겠네요.



auth에 값이 존재해서 이미 로그인 되있다는 문구가 나오네요.



'Pwnable > Heap' 카테고리의 다른 글

[Protostar] Heap3  (1) 2018.03.15
[Protostar] Heap1  (0) 2018.03.11
[Protostar] Heap0  (0) 2018.03.11
Heap 이해하기  (0) 2018.03.11
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함