본문 바로가기
강의 및 교육/Inflearn - Protostar

Stack (1) 다른 변수 덮어씌우기

by 이우정 2022. 2. 11.
728x90

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

홈페이지 사라져서 github보고 해야 함.


1. 다른 변수 덮어씌우기


Protostar Stack0

About

이 레벨은 할당된 영역 밖에서 메모리에 접근할 수 있고 스택 변수가 어떻게 배치되는지, 할당된 메모리 외부에서 수정하면 프로그램 실행을 수정할 수 있다는 개념을 도입한다.

 

이 레벨은 /opt/protostar/bin/stack0에 있다.

 


Source code

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

소스코드 작성을 위해 Kali에 파일 생성 (protostar dir도 생성)

modified를 0이 아닌 다른 수로 바꾸면 성공

buffer
변수
ebp
ret

버퍼를 가득 채우면 변수가 덮어씌워질 수 있다. 

스택의 구조 => https://wogh8732.tistory.com/88

 

스택의 구조

지금까지 배운걸 간단히 정리하면 다음과 같다 (세그먼트4는 세그먼트 n이라고 보셈.. 숫자를 잘못 입력했다는..) 8086메모리는 4기가 메모리 기준으로 2기가 커널 영역과 2기가 2기가 유저 영역이

wogh8732.tistory.com

 

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

gcc -o stacko stack0.c (일반 컴파일)

gcc -z execstack -no-pie -w -o stack0 stack0.c (취약 컴파일)
     (스택 실행 가능 권한)
                      (pie : 랜덤화)

64 byte가 할당되어 있지만 컴파일 환경에 따라 더 많은 메모리 용량이 dump 될 수 있음.

 

 


Analysis

p.s. 문제를 해결했다고 넘어가선 안되고 이를 분석해야 이 다음 문제 해결을 위한 성장이 가능하다. 

 

 

gdb 분석
https://biji-jjigae.tistory.com/55

test로 비교 중인데 코드상 eax로 비교할만한게 modified가 0인지 판단하는 부분밖에 없다. 

je 확인 가능 그 부분에 break

breakpoint 확인 가능

현재 eax 값 확인

변조 전

 

변조를 위해 재시작

변조 성공

 


 

메모리(스택) 확인

- 원래 modified의 위치는 rbp-0x4이다. 

eax에 mov된 modified(DWORD PTR [rbp-0x4]
rbp에서 한 칸 위에 위치한 변수 modified가 덮어씌워진걸 확인 가능하다.&nbsp;


레나 튜토리얼 1, 2번 공부하기!!

https://www.youtube.com/watch?v=S6uFitUIYlc 

 

728x90

'강의 및 교육 > Inflearn - Protostar' 카테고리의 다른 글

개요  (0) 2022.02.10