fgallego

Raw ELF generation: x86 32-bit Assembly to 102-byte executable

May 20th, 2021
780
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;; Minimal binary program that returns 3*4
  3. ;;    - 102 bytes binary ELF 32-bits executable
  4. ;;
  5. ;; This assembly program is a raw binary generation of a minimal
  6. ;; program doing a simple task as a multiplication, and not taking
  7. ;; several Kilobytes for that. To prevent standard tools such as
  8. ;; GCC or LD to introduce unused headers and sections into the
  9. ;; program, we generate our custom ELF header by directly outputting
  10. ;; its raw binary values. This code can easily be assembled under
  11. ;; Linux using NASM with this command:
  12. ;;
  13. ;; $> nasm minimal_multi.asm -f bin -o mmulti
  14. ;;
  15. ;; Then it can be executed and tested as follows:
  16. ;;
  17. ;; $> chmod +x mmulti
  18. ;; $> ./mmulti && echo $?
  19. ;;
  20. ;; If all goes well, we shall see a 12 printed in the terminal.
  21. ;;
  22. ;; This has been possible thanks to the amazing explanations and code
  23. ;; from Brain Raiter. Check his site at http://www.muppetlabs.com/~breadbox/.
  24. ;;
  25. ;; LICENSE (GNU GPL v3)
  26. ;;
  27. ;; Copyright (c) 2001 Brian Raiter (http://www.muppetlabs.com/~breadbox/software/tiny/)
  28. ;; Copyright (C) 2021 Francisco J. Gallego-Durán (@FranGallegoBR / fjgallego@ua.es)
  29. ;;
  30. ;; This program is free software: you can redistribute it and/or modify
  31. ;; it under the terms of the GNU General Public License as published by
  32. ;; the Free Software Foundation, either version 3 of the License, or
  33. ;; (at your option) any later version.
  34. ;;
  35. ;; This program is distributed in the hope that it will be useful,
  36. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  38. ;; GNU General Public License for more details.
  39. ;;
  40. ;; You should have received a copy of the GNU General Public License
  41. ;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
  42. ;;
  43. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  44.  
  45.  
  46. ;; Nasm switches to 16-bit mode when outputting raw binary data
  47. ;; Tell it to change to 32 bits mode. Otherwise, instructions are
  48. ;; incorrectly translated to machine code
  49. BITS 32
  50.  
  51. ;;-------------------------------------------------------------------
  52. ;; WHOLE PROGRAM LOAD POINT: 0x0804800
  53. ;;    Standard Load Point for 32bits static executables
  54. ;;-------------------------------------------------------------------
  55. org 0x08048000
  56.  
  57. ;;-------------------------------------------------------------------
  58. ;; ELF HEADER (52 Bytes)
  59. ;;-------------------------------------------------------------------
  60. elf_header:                         ;; Elf32_Elf_Header
  61.         db  0x7F, "ELF", 1, 1, 1, 0 ;; - e_ident
  62.         dd  0, 0                    ;;  
  63.         dw  2                       ;; - e_type
  64.         dw  3                       ;; - e_machine
  65.         dd  1                       ;; - e_version
  66.         dd  _start                  ;; - e_entry
  67.         dd  program_header - $$     ;; - e_phoff
  68.         dd  0                       ;; - e_shoff
  69.         dd  0                       ;; - e_flags
  70.         dw  ehdr_size               ;; - e_ehsize
  71.         dw  phdr_size               ;; - e_phentsize
  72.         dw  1                       ;; - e_phnum
  73.         dw  0                       ;; - e_shentsize
  74.         dw  0                       ;; - e_shnum
  75.         dw  0                       ;; - e_shstrndx
  76.  
  77. ehdr_size equ ($ - elf_header)      ;; Size of Elf Header  
  78.  
  79. ;;-------------------------------------------------------------------
  80. ;; PROGRAM HEADER (32 bytes)
  81. ;;-------------------------------------------------------------------
  82. program_header:                     ;; Elf32_Program_Header
  83.         dd  1                       ;; - p_type
  84.         dd  0                       ;; - p_offset
  85.         dd  $$                      ;; - p_vaddr
  86.         dd  $$                      ;; - p_paddr
  87.         dd  filesize                ;; - p_filesz
  88.         dd  filesize                ;; - p_memsz
  89.         dd  5                       ;; - p_flags
  90.         dd  0x1000                  ;; - p_align
  91.  
  92. phdr_size equ ($ - program_header)  ;; Size of the Program Header
  93.  
  94. ;;-------------------------------------------------------------------
  95. ;; PROGRAM
  96. ;;-------------------------------------------------------------------
  97. _start:
  98.   mov    ebx, 3   ;; EBX = 3
  99.   imul   ebx, 4   ;; EBX = 4*EBX
  100.   mov    eax, 1   ;; EAX = 1 ( Return to system sys_call, with EBX as return value )
  101.   int    0x80     ;; System Call
  102.  
  103. ;;-------------------------------------------------------------------
  104. ;; SIZE OF THIS WHOLE FILE
  105. ;;-------------------------------------------------------------------
  106. filesize equ ($ - $$)
RAW Paste Data