티스토리 뷰

Pwnable/Heap

[Protostar] Heap1

D41JUNG0D 2018. 3. 11. 20:45

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

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <stdio.h>

#include <sys/types.h>

 

struct internet {

 int priority;

 char *name;

};

void winner()

{

 printf("and we have a winner @ %d\n", time(NULL));

}

int main(int argc, char **argv)

{

 struct internet *i1, *i2, *i3;

 i1 = malloc(sizeof(struct internet));

 i1->priority = 1;

 i1->name = malloc(8);

 i2 = malloc(sizeof(struct internet));

 i2->priority = 2;

 i2->name = malloc(8);

 strcpy(i1->name, argv[1]);

 strcpy(i2->name, argv[2]);

 printf("and that's a wrap folks!\n");

}

Colored by Color Scripter

cs



코드를 보게되면 internet이라는 구조체를 선언하고있고, i1과 i2라는 구조체 포인터에 internet 구조체의 크기만큼 메모리를 할당해주고 있습니다.


그리고 각 internet구조체의 name멤버 변수에는 8byte만큼 메모리를 할당해주고 있습니다.


메모리를 할당한 후 name멤버변수에 순서대로 argv[1], argv[2]의 값을 strcpy함수를 이용하여 복사해주고 있습니다.


strcpy함수를 사용하는 걸로보아 heap over flow가 일어나겠네요. 이 부분을 활용하여 exploit 해보도록 하겠습니다.


3. Exploit


heap over flow를 이용해 name의 포인터 주소를 printf의 got로 조작해주고 그 부분에 winner 함수의 주소를 넣어주면 마지막에 printf함수를 호출하면서 exploit이 가능할 것 같습니다.



strcpy(i2->name, argv[2]); 의 다음부분에 breakpoint를 걸고 값을 넣어주고 어떻게 heap에 할당되었는지 보겠습니다.



malloc 이후에 breakpoint를 걸고 그때의 eax  - 8 의 값이 malloc으로 할당된 heap 주소입니다.


그림을 보면 차례대로 | prev_size | size | priority | *name | 이런식으로 할당되어있고 이후에는 *name이 가르키고 있는 실질적인 name의 값이 들어가 있는 것을 볼 수 있습니다.


그리고 다음 구조체 포인터인 i2의 메모리가 i1과 똑같이 할당되어 있는 것을 볼 수 있습니다.


i1->name에서 20byte만큼 dummy값을 채워주면 i2의 name포인터에 접근할 수 있을 것 같습니다.



winner 함수의 주소를 구해주고



printf 함수의 탈을 쓴 puts함수의 got를 구해줍니다.(최적화 때문에 printf가 puts함수로 잠수함 패치,,)



puts 함수의 got가 winner 함수로 바뀌어 winner 함수가 호출된 것을 볼 수 있습니다.




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

[Protostar] Heap3  (1) 2018.03.15
[Protostar] Heap2  (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
글 보관함