Advertisement
Madmouse

ELF header black magic, with ROP for shiggles

Jun 5th, 2015
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [bits 64]
  2. section .text
  3. global _start
  4.  
  5. org 0x08048000
  6. ehdr:                                      ; Elf64_Ehdr
  7.     db 0x7F, "ELF", 2, 1, 1, 0         ;   e_ident
  8. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  9. ;   times 8 db      0
  10. ; We can replace ^ this with the following for a code cave inside the elf header itself
  11. exit:
  12.     mov al, 60
  13.     xor rdi, rdi
  14.     syscall
  15.     db 0    ; the code in the cave is 7 bytes long, so we need one byte of padding
  16. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  17.     dw 2                               ;   e_type
  18.     dw 62                              ;   e_machine
  19.     dd 1                               ;   e_version
  20.     dq _start                          ;   e_entry
  21.     dq phdr - $$                       ;   e_phoff
  22.     dq 0                               ;   e_shoff
  23.     dd 0                               ;   e_flags
  24.     dw ehdrsize                        ;   e_ehsize
  25.     dw phdrsize                        ;   e_phentsize
  26.     dw 1                               ;   e_phnum
  27.     dw 0                               ;   e_shentsize
  28.     dw 0                               ;   e_shnum
  29.     dw 0                               ;   e_shstrndx
  30.     ehdrsize equ $ - ehdr
  31. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  32. ; we can put things in between the ELF header and the program header like so:
  33. str:
  34.     push qword exit ; cleverly disguised push instruction for use with the ret instruction in main
  35.     call main
  36. ;;;;;;;;;;;;;;;;;;GARBAGE;;;;;;;;;;;;;;;;;;;;;;
  37. ; fake code to trick people who are not familiar with rop into believing that the ret actually returns after the call
  38.     xor rax, rax
  39.     mov rcx, 27
  40.     mov rsi, garbage
  41. decode:
  42.     mov al, byte [rsi+30]
  43.     xor byte [rsi], al
  44.     inc rsi
  45. loop decode
  46.     jmp rsi
  47. ;;;;;;;;;;;;;;;;;;GARBAGE;;;;;;;;;;;;;;;;;;;;;;
  48.     db "Meow...", 0xa   ; silly message
  49. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  50. phdr:                                      ; Elf64_Phdr
  51.     dd 1                               ;   p_type
  52.     dd 7                               ;   p_flags
  53.     dq 0                               ;   p_offset
  54.     dq $$                              ;   p_vaddr
  55.     dq $$                              ;   p_paddr
  56.     dq filesize                        ;   p_filesz
  57.     dq filesize                        ;   p_memsz
  58.     dq 0x1000                          ;   p_align
  59.     phdrsize equ $ - phdr
  60. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  61. _start:
  62.     jmp str
  63. main:
  64.     pop rsi
  65.     add rsi, 30
  66.     xor rax, rax
  67.     xor rdx, rdx
  68.     xor rdi, rdi
  69.     mov al, 0x1
  70.     mov dil, al
  71.     mov dl, 8
  72.     syscall
  73.     xor rax, rax
  74.     ret      ; secretly, this is a rop jmp as described above lol
  75. ;;;;;;;;;;;;;;;;;;GARBAGE;;;;;;;;;;;;;;;;;;;;;;
  76. ; fake encrypted code
  77. garbage:
  78.     db 0x48, 0x31, 0xc0, 0xb0, 0x39, 0xcd, 0x80, 0xeb, 0xfa
  79.     db 0xe7, 0x57, 0x21, 0xd4, 0xad, 0xb5, 0x0e, 0xa8, 0x82
  80.     db 0x48, 0x31, 0xc0, 0xb0, 0x39, 0xcd, 0x80, 0xeb, 0xfa
  81.     db 0x43, 0x82, 0xb9, 0x7c, 0x9a, 0x1d, 0xcd, 0x71, 0xdc
  82.     db 0x48, 0x31, 0xc0, 0xb0, 0x39, 0xcd, 0x80, 0xeb, 0xfa
  83.     db 0xa8, 0x9d, 0xc9, 0xce, 0x02, 0x19, 0x1c, 0x57, 0xfd
  84.     db 0x48, 0x31, 0xc0, 0xb0, 0x39, 0xcd, 0x80, 0xeb, 0xfa
  85.     db 0x9a,0xc0,0x22,0x63,0xb4,0xad
  86. ;;;;;;;;;;;;;;;;;;GARBAGE;;;;;;;;;;;;;;;;;;;;;;
  87.  
  88. filesize equ $ - $$
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement