; Copyright (C) 2020 Weber Yann ; ; This program is free software; you can redistribute it and/or modify ; it under the terms of the GNU General Public License as published by ; the Free Software Foundation; either version 3 of the License, or ; any later version. ; ; This program is distributed in the hope that it will be useful, ; but WITHOUT ANY WARRANTY; without even the implied warranty of ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; GNU General Public License for more details. ; ; You should have received a copy of the GNU General Public License ; along with this program. If not, see . ; ; amd64elfquine : A GNU/Linux AMD X86-64 ELF Quine. ; =============== ; Produce itself on stdout without reading outside .data section. ; ; Compile : ; --------- ; # Needs nasm, ld ; # And sh, hexdump, diff, for check.sh ; make ; ; Produce a copy : ; ---------------- ; ./amd64elfquine > amd64elfquine_copy ; ; Produce distributable copies : ; ------------------------------ ; # Needs gzip, zip, base64, md5sum, sha256sum, sha512sum ; make dist ; [bits 64] section .data elf_head: dw 0x457f, 0x464c, 0x0102, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000 dw 0x0002, 0x003e, 0x0001, 0x0000, 0x1000, 0x0040, 0x0000, 0x0000 dw 0x0040, 0x0000, 0x0000, 0x0000, 0x4230, 0x0000, 0x0000, 0x0000 dw 0x0000, 0x0000, 0x0040, 0x0038, 0x0004, 0x0040, 0x0005, 0x0004 dw 0x0001, 0x0000, 0x0004, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 dw 0x0000, 0x0040, 0x0000, 0x0000, 0x0000, 0x0040, 0x0000, 0x0000 dw 0x0140, 0x0000, 0x0000, 0x0000, 0x0140, 0x0000, 0x0000, 0x0000 dw 0x1000, 0x0000, 0x0000, 0x0000, 0x0001, 0x0000, 0x0005, 0x0000 dw 0x1000, 0x0000, 0x0000, 0x0000, 0x1000, 0x0040, 0x0000, 0x0000 dw 0x1000, 0x0040, 0x0000, 0x0000, 0x00c6, 0x0000, 0x0000, 0x0000 dw 0x00c6, 0x0000, 0x0000, 0x0000, 0x1000, 0x0000, 0x0000, 0x0000 dw 0x0001, 0x0000, 0x0006, 0x0000, 0x2000, 0x0000, 0x0000, 0x0000 dw 0x2000, 0x0040, 0x0000, 0x0000, 0x2000, 0x0040, 0x0000, 0x0000 dw 0x2200, 0x0000, 0x0000, 0x0000, 0x2200, 0x0000, 0x0000, 0x0000 dw 0x1000, 0x0000, 0x0000, 0x0000, 0x0004, 0x0000, 0x0004, 0x0000 dw 0x0120, 0x0000, 0x0000, 0x0000, 0x0120, 0x0040, 0x0000, 0x0000 dw 0x0120, 0x0040, 0x0000, 0x0000, 0x0020, 0x0000, 0x0000, 0x0000 dw 0x0020, 0x0000, 0x0000, 0x0000, 0x0008, 0x0000, 0x0000, 0x0000 dw 0x0004, 0x0000, 0x0010, 0x0000, 0x0005, 0x0000, 0x4e47, 0x0055 dw 0x0001, 0xc000, 0x0004, 0x0000, 0x0001 times 0xec6 db 0 code: ; writing elf head mov rax, 1 ; write mov rdi, rax ; stdout mov rsi, elf_head mov rdx, 0x1000 syscall cmp rax, 0 jl code.err ; writing text section ; _start mov rax, 1 ; write mov rdi, rax ; stdout mov rsi, code mov rdx, 0x1000 syscall cmp rax, 0 jl code.err ; writing data section ; elf_head mov rax, 1 ; write mov rdi, rax ; stdout mov rsi, elf_head mov rdx, 0x1000 syscall cmp rax, 0 jl code.err ; code mov rax, 1 ; write mov rdi, rax ; stdout mov rsi, code mov rdx, 0x1000 syscall cmp rax, 0 jl code.err ; text section end ; elf_foot mov rax, 1 mov rdi, rax ; stdout mov rsi, elf_foot mov rdx, elf_foot_data_sz syscall ; data section end ; elf_foot mov rax, 1 mov rdi, rax ; stdout mov rsi, elf_foot mov rdx, elf_foot_sz syscall code.exit: mov rax, 60 ; exit xor rdi, rdi syscall code.err: mov rdi, rax mov rax, 60 ; exit syscall times 0xf3a db 0 elf_foot: dw 0x2e00, 0x6873, 0x7473, 0x7472, 0x6261, 0x2e00, 0x6f6e, 0x6574 dw 0x672e, 0x756e, 0x702e, 0x6f72, 0x6570, 0x7472, 0x0079, 0x742e dw 0x7865, 0x0074, 0x642e, 0x7461, 0x0061, 0x0000, 0x0000, 0x0000 dw 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 dw 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 dw 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 dw 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 dw 0x000b, 0x0000, 0x0007, 0x0000, 0x0002, 0x0000, 0x0000, 0x0000 dw 0x0120, 0x0040, 0x0000, 0x0000, 0x0120, 0x0000, 0x0000, 0x0000 dw 0x0020, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 dw 0x0008, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 dw 0x001e, 0x0000, 0x0001, 0x0000, 0x0006, 0x0000, 0x0000, 0x0000 dw 0x1000, 0x0040, 0x0000, 0x0000, 0x1000, 0x0000, 0x0000, 0x0000 dw 0x00c6, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 dw 0x0010, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 dw 0x0024, 0x0000, 0x0001, 0x0000, 0x0003, 0x0000, 0x0000, 0x0000 dw 0x2000, 0x0040, 0x0000, 0x0000, 0x2000, 0x0000, 0x0000, 0x0000 dw 0x2200, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 dw 0x0004, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 dw 0x0001, 0x0000, 0x0003, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 dw 0x0000, 0x0000, 0x0000, 0x0000, 0x4200, 0x0000, 0x0000, 0x0000 dw 0x002a, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 dw 0x0001 times 0xe db 0 elf_foot_sz: equ $ - elf_foot times 0x90 db 0 ; .data allign elf_foot_data_sz: equ $ - elf_foot section .text global _start _start: ; writing elf head mov rax, 1 ; write mov rdi, rax ; stdout mov rsi, elf_head mov rdx, 0x1000 syscall cmp rax, 0 jl .err ; writing text section ; _start mov rax, 1 ; write mov rdi, rax ; stdout mov rsi, code mov rdx, 0x1000 syscall cmp rax, 0 jl .err ; writing data section ; elf_head mov rax, 1 ; write mov rdi, rax ; stdout mov rsi, elf_head mov rdx, 0x1000 syscall cmp rax, 0 jl .err ; code mov rax, 1 ; write mov rdi, rax ; stdout mov rsi, code mov rdx, 0x1000 syscall cmp rax, 0 jl .err ; text section end ; elf_foot mov rax, 1 mov rdi, rax ; stdout mov rsi, elf_foot mov rdx, elf_foot_data_sz syscall ; data section end ; elf_foot mov rax, 1 mov rdi, rax ; stdout mov rsi, elf_foot mov rdx, elf_foot_sz syscall .exit: mov rax, 60 ; exit xor rdi, rdi syscall .err: mov rdi, rax mov rax, 60 ; exit syscall