Advertisement
Madmouse

12 byte code cave in the ELF header :D

Jun 5th, 2015
554
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; [madmouse@yourmomsb0x ~]$ nasm -f bin -o test test.s&&chmod +x test&&./test;cat test|xxd
  2. ; 0000000: 7f45 4c46 31c0 31db 40cd 8041 4141 4141  .ELF1.1.@..AAAAA
  3. ; 0000010: 0200 0300 0100 0000 0480 0408 3400 0000  ............4...
  4. ; 0000020: 0000 0000 0000 0000 3400 2000 0100 0000  ........4. .....
  5. ; 0000030: 0000 0000 0100 0000 0000 0000 0080 0408  ................
  6. ; 0000040: 0080 0408 5400 0000 5400 0000 0700 0000  ....T...T.......
  7. ; 0000050: 0010 0000                                ....
  8. ; [madmouse@yourmomsb0x ~]$ readelf -e test
  9. ; ELF Header:
  10. ;   Magic:   7f 45 4c 46 31 c0 31 db 40 cd 80 41 41 41 41 41
  11. ;   Class:                             <unknown: 31>
  12. ;   Data:                              <unknown: c0>
  13. ;   Version:                           49 <unknown: %lx>
  14. ;   OS/ABI:                            <unknown: db>
  15. ;   ABI Version:                       64
  16. ;   Type:                              EXEC (Executable file)
  17. ;   Machine:                           Intel 80386
  18. ;   Version:                           0x1
  19. ;   Entry point address:               0x8048004
  20. ;   Start of program headers:          52 (bytes into file)
  21. ;   Start of section headers:          0 (bytes into file)
  22. ;   Flags:                             0x0
  23. ;   Size of this header:               52 (bytes)
  24. ;   Size of program headers:           32 (bytes)
  25. ;   Number of program headers:         1
  26. ;   Size of section headers:           0 (bytes)
  27. ;   Number of section headers:         0
  28. ;   Section header string table index: 0
  29. ;
  30. ; There are no sections in this file.
  31. ;
  32. ; Program Headers:
  33. ;   Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  34. ;   LOAD           0x000000 0x08048000 0x08048000 0x00054 0x00054 RWE 0x1000
  35. ; [madmouse@yourmomsb0x ~]$
  36.  
  37.  
  38. [bits 32]
  39. section .text
  40. global start
  41.  
  42. org 0x08048000
  43. ehdr:                                      ; Elf32_Ehdr
  44.     db 0x7F,"ELF" ;, 1, 1, 1, 0         ;   e_ident
  45. ;                   ^ 4 more bytes for code caving
  46. ;   times 8 db 0
  47. ; We can replace ^ this with the following for a code cave inside the elf header itself
  48. start:
  49.     xor eax, eax
  50.     xor ebx, ebx
  51.     inc eax
  52.     int 0x80
  53. end:
  54.     times 12-(end-start) db 'A' ; we need bytes for padding, the code cave here is 12 bytes long
  55. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  56.     dw 2                               ;   e_type
  57.     dw 3                               ;   e_machine
  58.     dd 1                               ;   e_version
  59.     dd start                           ;   e_entry
  60.     dd phdr - $$                       ;   e_phoff
  61.     dd 0                               ;   e_shoff
  62.     dd 0                               ;   e_flags
  63.     dw ehdrsize                        ;   e_ehsize
  64.     dw phdrsize                        ;   e_phentsize
  65.     dw 1                               ;   e_phnum
  66.     dw 0                               ;   e_shentsize
  67.     dw 0                               ;   e_shnum
  68.     dw 0                               ;   e_shstrndx
  69.     ehdrsize equ $ - ehdr
  70.  
  71. phdr:                                      ; Elf32_Phdr
  72.     dd 1                               ;   p_type
  73.     dd 0                               ;   p_offset
  74.     dd $$                              ;   p_vaddr
  75.     dd $$                              ;   p_paddr
  76.     dd filesize                        ;   p_filesz
  77.     dd filesize                        ;   p_memsz
  78.     dd 7                               ;   p_flags
  79.     dd 0x1000                          ;   p_align
  80.     phdrsize equ $ - phdr
  81.  
  82. filesize equ $ - $$
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement