A GNU/Linux AMD X86-64 ELF Quine. Produce itself on stdout without reading outside .data section.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

amd64elfquine.asm 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. ; Copyright (C) 2020 Weber Yann <amd64elfquine@yannweb.net>
  2. ;
  3. ; This program is free software; you can redistribute it and/or modify
  4. ; it under the terms of the GNU General Public License as published by
  5. ; the Free Software Foundation; either version 3 of the License, or
  6. ; any later version.
  7. ;
  8. ; This program is distributed in the hope that it will be useful,
  9. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ; GNU General Public License for more details.
  12. ;
  13. ; You should have received a copy of the GNU General Public License
  14. ; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;
  16. ; amd64elfquine : A GNU/Linux AMD X86-64 ELF Quine.
  17. ; ===============
  18. ; Produce itself on stdout without reading outside .data section.
  19. ;
  20. ; Compile :
  21. ; ---------
  22. ; # Needs nasm, ld
  23. ; # And sh, hexdump, diff, for check.sh
  24. ; make
  25. ;
  26. ; Produce a copy :
  27. ; ----------------
  28. ; ./amd64elfquine > amd64elfquine_copy
  29. ;
  30. ; Produce distributable copies :
  31. ; ------------------------------
  32. ; # Needs gzip, zip, base64, md5sum, sha256sum, sha512sum
  33. ; make dist
  34. ;
  35. [bits 64]
  36. section .data
  37. elf_head:
  38. dw 0x457f, 0x464c, 0x0102, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000
  39. dw 0x0002, 0x003e, 0x0001, 0x0000, 0x1000, 0x0040, 0x0000, 0x0000
  40. dw 0x0040, 0x0000, 0x0000, 0x0000, 0x4230, 0x0000, 0x0000, 0x0000
  41. dw 0x0000, 0x0000, 0x0040, 0x0038, 0x0004, 0x0040, 0x0005, 0x0004
  42. dw 0x0001, 0x0000, 0x0004, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
  43. dw 0x0000, 0x0040, 0x0000, 0x0000, 0x0000, 0x0040, 0x0000, 0x0000
  44. dw 0x0140, 0x0000, 0x0000, 0x0000, 0x0140, 0x0000, 0x0000, 0x0000
  45. dw 0x1000, 0x0000, 0x0000, 0x0000, 0x0001, 0x0000, 0x0005, 0x0000
  46. dw 0x1000, 0x0000, 0x0000, 0x0000, 0x1000, 0x0040, 0x0000, 0x0000
  47. dw 0x1000, 0x0040, 0x0000, 0x0000, 0x00c6, 0x0000, 0x0000, 0x0000
  48. dw 0x00c6, 0x0000, 0x0000, 0x0000, 0x1000, 0x0000, 0x0000, 0x0000
  49. dw 0x0001, 0x0000, 0x0006, 0x0000, 0x2000, 0x0000, 0x0000, 0x0000
  50. dw 0x2000, 0x0040, 0x0000, 0x0000, 0x2000, 0x0040, 0x0000, 0x0000
  51. dw 0x2200, 0x0000, 0x0000, 0x0000, 0x2200, 0x0000, 0x0000, 0x0000
  52. dw 0x1000, 0x0000, 0x0000, 0x0000, 0x0004, 0x0000, 0x0004, 0x0000
  53. dw 0x0120, 0x0000, 0x0000, 0x0000, 0x0120, 0x0040, 0x0000, 0x0000
  54. dw 0x0120, 0x0040, 0x0000, 0x0000, 0x0020, 0x0000, 0x0000, 0x0000
  55. dw 0x0020, 0x0000, 0x0000, 0x0000, 0x0008, 0x0000, 0x0000, 0x0000
  56. dw 0x0004, 0x0000, 0x0010, 0x0000, 0x0005, 0x0000, 0x4e47, 0x0055
  57. dw 0x0001, 0xc000, 0x0004, 0x0000, 0x0001
  58. times 0xec6 db 0
  59. code:
  60. ; writing elf head
  61. mov rax, 1 ; write
  62. mov rdi, rax ; stdout
  63. mov rsi, elf_head
  64. mov rdx, 0x1000
  65. syscall
  66. cmp rax, 0
  67. jl code.err
  68. ; writing text section
  69. ; _start
  70. mov rax, 1 ; write
  71. mov rdi, rax ; stdout
  72. mov rsi, code
  73. mov rdx, 0x1000
  74. syscall
  75. cmp rax, 0
  76. jl code.err
  77. ; writing data section
  78. ; elf_head
  79. mov rax, 1 ; write
  80. mov rdi, rax ; stdout
  81. mov rsi, elf_head
  82. mov rdx, 0x1000
  83. syscall
  84. cmp rax, 0
  85. jl code.err
  86. ; code
  87. mov rax, 1 ; write
  88. mov rdi, rax ; stdout
  89. mov rsi, code
  90. mov rdx, 0x1000
  91. syscall
  92. cmp rax, 0
  93. jl code.err
  94. ; text section end
  95. ; elf_foot
  96. mov rax, 1
  97. mov rdi, rax ; stdout
  98. mov rsi, elf_foot
  99. mov rdx, elf_foot_data_sz
  100. syscall
  101. ; data section end
  102. ; elf_foot
  103. mov rax, 1
  104. mov rdi, rax ; stdout
  105. mov rsi, elf_foot
  106. mov rdx, elf_foot_sz
  107. syscall
  108. code.exit:
  109. mov rax, 60 ; exit
  110. xor rdi, rdi
  111. syscall
  112. code.err:
  113. mov rdi, rax
  114. mov rax, 60 ; exit
  115. syscall
  116. times 0xf3a db 0
  117. elf_foot:
  118. dw 0x2e00, 0x6873, 0x7473, 0x7472, 0x6261, 0x2e00, 0x6f6e, 0x6574
  119. dw 0x672e, 0x756e, 0x702e, 0x6f72, 0x6570, 0x7472, 0x0079, 0x742e
  120. dw 0x7865, 0x0074, 0x642e, 0x7461, 0x0061, 0x0000, 0x0000, 0x0000
  121. dw 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
  122. dw 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
  123. dw 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
  124. dw 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
  125. dw 0x000b, 0x0000, 0x0007, 0x0000, 0x0002, 0x0000, 0x0000, 0x0000
  126. dw 0x0120, 0x0040, 0x0000, 0x0000, 0x0120, 0x0000, 0x0000, 0x0000
  127. dw 0x0020, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
  128. dw 0x0008, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
  129. dw 0x001e, 0x0000, 0x0001, 0x0000, 0x0006, 0x0000, 0x0000, 0x0000
  130. dw 0x1000, 0x0040, 0x0000, 0x0000, 0x1000, 0x0000, 0x0000, 0x0000
  131. dw 0x00c6, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
  132. dw 0x0010, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
  133. dw 0x0024, 0x0000, 0x0001, 0x0000, 0x0003, 0x0000, 0x0000, 0x0000
  134. dw 0x2000, 0x0040, 0x0000, 0x0000, 0x2000, 0x0000, 0x0000, 0x0000
  135. dw 0x2200, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
  136. dw 0x0004, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
  137. dw 0x0001, 0x0000, 0x0003, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
  138. dw 0x0000, 0x0000, 0x0000, 0x0000, 0x4200, 0x0000, 0x0000, 0x0000
  139. dw 0x002a, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
  140. dw 0x0001
  141. times 0xe db 0
  142. elf_foot_sz: equ $ - elf_foot
  143. times 0x90 db 0 ; .data allign
  144. elf_foot_data_sz: equ $ - elf_foot
  145. section .text
  146. global _start
  147. _start:
  148. ; writing elf head
  149. mov rax, 1 ; write
  150. mov rdi, rax ; stdout
  151. mov rsi, elf_head
  152. mov rdx, 0x1000
  153. syscall
  154. cmp rax, 0
  155. jl .err
  156. ; writing text section
  157. ; _start
  158. mov rax, 1 ; write
  159. mov rdi, rax ; stdout
  160. mov rsi, code
  161. mov rdx, 0x1000
  162. syscall
  163. cmp rax, 0
  164. jl .err
  165. ; writing data section
  166. ; elf_head
  167. mov rax, 1 ; write
  168. mov rdi, rax ; stdout
  169. mov rsi, elf_head
  170. mov rdx, 0x1000
  171. syscall
  172. cmp rax, 0
  173. jl .err
  174. ; code
  175. mov rax, 1 ; write
  176. mov rdi, rax ; stdout
  177. mov rsi, code
  178. mov rdx, 0x1000
  179. syscall
  180. cmp rax, 0
  181. jl .err
  182. ; text section end
  183. ; elf_foot
  184. mov rax, 1
  185. mov rdi, rax ; stdout
  186. mov rsi, elf_foot
  187. mov rdx, elf_foot_data_sz
  188. syscall
  189. ; data section end
  190. ; elf_foot
  191. mov rax, 1
  192. mov rdi, rax ; stdout
  193. mov rsi, elf_foot
  194. mov rdx, elf_foot_sz
  195. syscall
  196. .exit:
  197. mov rax, 60 ; exit
  198. xor rdi, rdi
  199. syscall
  200. .err:
  201. mov rdi, rax
  202. mov rax, 60 ; exit
  203. syscall