Protostar stack0 시작

시스템 해킹|2024. 5. 15. 20:50

https://github.com/z3tta/Exploit-Exercises-Protostar

 

 

 

GitHub - z3tta/Exploit-Exercises-Protostar: Solutions for Exploit-Exercises Protostar

Solutions for Exploit-Exercises Protostar. Contribute to z3tta/Exploit-Exercises-Protostar development by creating an account on GitHub.

github.com

 

프로토스타 문제 관력 깃허브 사이트

꼭있어야함!

 

 

#### Protostar Stack0
##### About

This level introduces the concept that memory can be accessed outside of its allocated region, how the stack variables are laid out, and that modifying outside of the allocated memory can modify program execution.  
 
This level is at /opt/protostar/bin/stack0  

##### Source code
```c
#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");
  }
}
```

The value of the parameter "int modified" can be changed by overflowing char buffer[64].  
 
Because gets() is an unsafe function.

##### Solution
```
$ python -c "print 'A'*65" | ./stack0
```

##### Reference

 

 

stack0의 소스코드이다.

 

 

 

protostar 폴더를 만든뒤 그뒤에 stack0에관한 c 파일을 복사 붙여넣기한다 위 깃허브에 있는파일을!

 

 

 

 

 

스택에서는

 

ret -> ebp -> modified가 스택에 쌓이고 -> buffer가 쌓이게된다

 

 

modified로 선언이되고

 

아래 char buffer[64]에서 버퍼가 가득차게되면

밑에 있는 데이터들이 변조가 될 수있다

 

 

 

modified=0을 선언하고

gets(buffer); // scanf 

사용자 입력을 받는 것

buffer에 사용자 입력을 받는코드

 

modified가 0이 아니라면 밑에 you have changed~ 가 출력

modified가 0이라면 try again 이 출력된다

 

즉 modified가 0이 아니게 출력을해야하는것이다

 

buffer를 건들여서 modified를 변조하는 문제인듯.

 

------------------------------------------------------------------

 

gcc -o stack0 stack0.c
기존에는 이렇게하지만 아래와같이 해야한다.

칼리등 버전이올라가서 그럼

 

 

 

 

스택 공격에 취약한 컴파일 명령어

취약한 컴파일 하는 방법이다

gcc -z execstack -no-pie -w -o stack0 stack0.c

 

 

-z execstack 스택을 실행할수있는 권한을 주고

 

 

-no-pie 랜덤하게 메모리에 매핑이되는것을 없애게 한다.

 

64이상 버퍼를 넘치게하면 된다.

80바이트이상정도로 만들기위해 파이썬을 작성한다.

 

 

python -c "print('a'*90)"

 

 

이것을 stack0에서 사용해보자

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
를 복사 후 붙여넣는다

 

그럼 변조가되었다는 말이 나온다.

 

 

 

 

문제는 해결했으나 원리를 확인하기위해서 gdb로 분석을 확인해서 체크를 한다.

 

 

set disassembly-flavor intel

disas main

 

테스트하는 곳인 test eax,eax에

브레이크 포인트를 건다

 

b *main+42

브레이크 포인트가 잘 걸렸다

 

즉 값을 확인할때 modifed 가 eax라는 뜻이다.

 

run을 한뒤에 값을 a 90개를 넣어봣다

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

 

 

꽉차있는 것을 확인할 수 있었다

 

rbp-0x4가 modified가 있는곳인데 쓰레기 값으로 가득찾다 ( 버퍼를 많이 줘서 그런듯)

 

x/30gx $rsp

데이터가 쌓인것을 확인

 

 

info reg $rbp

 

rbb- 0x4 가 모디파이드고 그값은 rbp - 0x4여서 계산을 하게되면

rbp-0x4 = modified = 0x7fffffffddb0 - 0x4

 

 

 

rbp자리

 

rbp-4 인 이부분에 modified 가 들어가야할 자리에

우리는 a를 90개를 넣어서 범위를 초과해 그 자리에 데이터를 채워넣어서 버퍼의 변조에 성공하였다

중요하다!

 

 

a를 적게넣어 실행할 경우 해당있는곳엔 데이터가 없는걸로 증명이됨!

'시스템 해킹' 카테고리의 다른 글

Protostar stack - 2 peda 사용  (1) 2024.05.16
Protostar - stack 1  (1) 2024.05.16
프로토스타 설치  (0) 2024.05.15
스택 공격 기법 이해하기  (0) 2024.05.15
gdb와 ida를 통해 문제 풀이 1  (0) 2024.05.15

댓글()