|
@@ -0,0 +1,213 @@
|
|
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, base64, md5sum, sha256sum, sha512sum
|
|
33
|
+; make dist
|
|
34
|
+;
|
|
35
|
+
|
|
36
|
+[bits 64]
|
|
37
|
+section .data
|
|
38
|
+
|
|
39
|
+ elf_head:
|
|
40
|
+ dw 0x457f, 0x464c, 0x0102, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000
|
|
41
|
+ dw 0x0002, 0x003e, 0x0001, 0x0000, 0x1000, 0x0040, 0x0000, 0x0000
|
|
42
|
+ dw 0x0040, 0x0000, 0x0000, 0x0000, 0x4230, 0x0000, 0x0000, 0x0000
|
|
43
|
+ dw 0x0000, 0x0000, 0x0040, 0x0038, 0x0004, 0x0040, 0x0005, 0x0004
|
|
44
|
+ dw 0x0001, 0x0000, 0x0004, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
|
|
45
|
+ dw 0x0000, 0x0040, 0x0000, 0x0000, 0x0000, 0x0040, 0x0000, 0x0000
|
|
46
|
+ dw 0x0140, 0x0000, 0x0000, 0x0000, 0x0140, 0x0000, 0x0000, 0x0000
|
|
47
|
+ dw 0x1000, 0x0000, 0x0000, 0x0000, 0x0001, 0x0000, 0x0005, 0x0000
|
|
48
|
+ dw 0x1000, 0x0000, 0x0000, 0x0000, 0x1000, 0x0040, 0x0000, 0x0000
|
|
49
|
+ dw 0x1000, 0x0040, 0x0000, 0x0000, 0x00c6, 0x0000, 0x0000, 0x0000
|
|
50
|
+ dw 0x00c6, 0x0000, 0x0000, 0x0000, 0x1000, 0x0000, 0x0000, 0x0000
|
|
51
|
+ dw 0x0001, 0x0000, 0x0006, 0x0000, 0x2000, 0x0000, 0x0000, 0x0000
|
|
52
|
+ dw 0x2000, 0x0040, 0x0000, 0x0000, 0x2000, 0x0040, 0x0000, 0x0000
|
|
53
|
+ dw 0x2200, 0x0000, 0x0000, 0x0000, 0x2200, 0x0000, 0x0000, 0x0000
|
|
54
|
+ dw 0x1000, 0x0000, 0x0000, 0x0000, 0x0004, 0x0000, 0x0004, 0x0000
|
|
55
|
+ dw 0x0120, 0x0000, 0x0000, 0x0000, 0x0120, 0x0040, 0x0000, 0x0000
|
|
56
|
+ dw 0x0120, 0x0040, 0x0000, 0x0000, 0x0020, 0x0000, 0x0000, 0x0000
|
|
57
|
+ dw 0x0020, 0x0000, 0x0000, 0x0000, 0x0008, 0x0000, 0x0000, 0x0000
|
|
58
|
+ dw 0x0004, 0x0000, 0x0010, 0x0000, 0x0005, 0x0000, 0x4e47, 0x0055
|
|
59
|
+ dw 0x0001, 0xc000, 0x0004, 0x0000, 0x0001
|
|
60
|
+ times 0xec6 db 0
|
|
61
|
+
|
|
62
|
+ code:
|
|
63
|
+ ; writing elf head
|
|
64
|
+ mov rax, 1 ; write
|
|
65
|
+ mov rdi, rax ; stdout
|
|
66
|
+ mov rsi, elf_head
|
|
67
|
+ mov rdx, 0x1000
|
|
68
|
+ syscall
|
|
69
|
+ cmp rax, 0
|
|
70
|
+ jl code.err
|
|
71
|
+ ; writing text section
|
|
72
|
+ ; _start
|
|
73
|
+ mov rax, 1 ; write
|
|
74
|
+ mov rdi, rax ; stdout
|
|
75
|
+ mov rsi, code
|
|
76
|
+ mov rdx, 0x1000
|
|
77
|
+ syscall
|
|
78
|
+ cmp rax, 0
|
|
79
|
+ jl code.err
|
|
80
|
+ ; writing data section
|
|
81
|
+ ; elf_head
|
|
82
|
+ mov rax, 1 ; write
|
|
83
|
+ mov rdi, rax ; stdout
|
|
84
|
+ mov rsi, elf_head
|
|
85
|
+ mov rdx, 0x1000
|
|
86
|
+ syscall
|
|
87
|
+ cmp rax, 0
|
|
88
|
+ jl code.err
|
|
89
|
+ ; code
|
|
90
|
+ mov rax, 1 ; write
|
|
91
|
+ mov rdi, rax ; stdout
|
|
92
|
+ mov rsi, code
|
|
93
|
+ mov rdx, 0x1000
|
|
94
|
+ syscall
|
|
95
|
+ cmp rax, 0
|
|
96
|
+ jl code.err
|
|
97
|
+ ; text section end
|
|
98
|
+ ; elf_foot
|
|
99
|
+ mov rax, 1
|
|
100
|
+ mov rdi, rax ; stdout
|
|
101
|
+ mov rsi, elf_foot
|
|
102
|
+ mov rdx, elf_foot_data_sz
|
|
103
|
+ syscall
|
|
104
|
+ ; data section end
|
|
105
|
+ ; elf_foot
|
|
106
|
+ mov rax, 1
|
|
107
|
+ mov rdi, rax ; stdout
|
|
108
|
+ mov rsi, elf_foot
|
|
109
|
+ mov rdx, elf_foot_sz
|
|
110
|
+ syscall
|
|
111
|
+
|
|
112
|
+ code.exit:
|
|
113
|
+ mov rax, 60 ; exit
|
|
114
|
+ xor rdi, rdi
|
|
115
|
+ syscall
|
|
116
|
+
|
|
117
|
+ code.err:
|
|
118
|
+ mov rdi, rax
|
|
119
|
+ mov rax, 60 ; exit
|
|
120
|
+ syscall
|
|
121
|
+ times 0xf3a db 0
|
|
122
|
+
|
|
123
|
+ elf_foot:
|
|
124
|
+ dw 0x2e00, 0x6873, 0x7473, 0x7472, 0x6261, 0x2e00, 0x6f6e, 0x6574
|
|
125
|
+ dw 0x672e, 0x756e, 0x702e, 0x6f72, 0x6570, 0x7472, 0x0079, 0x742e
|
|
126
|
+ dw 0x7865, 0x0074, 0x642e, 0x7461, 0x0061, 0x0000, 0x0000, 0x0000
|
|
127
|
+ dw 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
|
|
128
|
+ dw 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
|
|
129
|
+ dw 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
|
|
130
|
+ dw 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
|
|
131
|
+ dw 0x000b, 0x0000, 0x0007, 0x0000, 0x0002, 0x0000, 0x0000, 0x0000
|
|
132
|
+ dw 0x0120, 0x0040, 0x0000, 0x0000, 0x0120, 0x0000, 0x0000, 0x0000
|
|
133
|
+ dw 0x0020, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
|
|
134
|
+ dw 0x0008, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
|
|
135
|
+ dw 0x001e, 0x0000, 0x0001, 0x0000, 0x0006, 0x0000, 0x0000, 0x0000
|
|
136
|
+ dw 0x1000, 0x0040, 0x0000, 0x0000, 0x1000, 0x0000, 0x0000, 0x0000
|
|
137
|
+ dw 0x00c6, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
|
|
138
|
+ dw 0x0010, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
|
|
139
|
+ dw 0x0024, 0x0000, 0x0001, 0x0000, 0x0003, 0x0000, 0x0000, 0x0000
|
|
140
|
+ dw 0x2000, 0x0040, 0x0000, 0x0000, 0x2000, 0x0000, 0x0000, 0x0000
|
|
141
|
+ dw 0x2200, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
|
|
142
|
+ dw 0x0004, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
|
|
143
|
+ dw 0x0001, 0x0000, 0x0003, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
|
|
144
|
+ dw 0x0000, 0x0000, 0x0000, 0x0000, 0x4200, 0x0000, 0x0000, 0x0000
|
|
145
|
+ dw 0x002a, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
|
|
146
|
+ dw 0x0001
|
|
147
|
+ times 0xe db 0
|
|
148
|
+ elf_foot_sz: equ $ - elf_foot
|
|
149
|
+ times 0x90 db 0 ; .data allign
|
|
150
|
+ elf_foot_data_sz: equ $ - elf_foot
|
|
151
|
+
|
|
152
|
+section .text
|
|
153
|
+global _start
|
|
154
|
+_start:
|
|
155
|
+ ; writing elf head
|
|
156
|
+ mov rax, 1 ; write
|
|
157
|
+ mov rdi, rax ; stdout
|
|
158
|
+ mov rsi, elf_head
|
|
159
|
+ mov rdx, 0x1000
|
|
160
|
+ syscall
|
|
161
|
+ cmp rax, 0
|
|
162
|
+ jl .err
|
|
163
|
+ ; writing text section
|
|
164
|
+ ; _start
|
|
165
|
+ mov rax, 1 ; write
|
|
166
|
+ mov rdi, rax ; stdout
|
|
167
|
+ mov rsi, code
|
|
168
|
+ mov rdx, 0x1000
|
|
169
|
+ syscall
|
|
170
|
+ cmp rax, 0
|
|
171
|
+ jl .err
|
|
172
|
+ ; writing data section
|
|
173
|
+ ; elf_head
|
|
174
|
+ mov rax, 1 ; write
|
|
175
|
+ mov rdi, rax ; stdout
|
|
176
|
+ mov rsi, elf_head
|
|
177
|
+ mov rdx, 0x1000
|
|
178
|
+ syscall
|
|
179
|
+ cmp rax, 0
|
|
180
|
+ jl .err
|
|
181
|
+ ; code
|
|
182
|
+ mov rax, 1 ; write
|
|
183
|
+ mov rdi, rax ; stdout
|
|
184
|
+ mov rsi, code
|
|
185
|
+ mov rdx, 0x1000
|
|
186
|
+ syscall
|
|
187
|
+ cmp rax, 0
|
|
188
|
+ jl .err
|
|
189
|
+ ; text section end
|
|
190
|
+ ; elf_foot
|
|
191
|
+ mov rax, 1
|
|
192
|
+ mov rdi, rax ; stdout
|
|
193
|
+ mov rsi, elf_foot
|
|
194
|
+ mov rdx, elf_foot_data_sz
|
|
195
|
+ syscall
|
|
196
|
+ ; data section end
|
|
197
|
+ ; elf_foot
|
|
198
|
+ mov rax, 1
|
|
199
|
+ mov rdi, rax ; stdout
|
|
200
|
+ mov rsi, elf_foot
|
|
201
|
+ mov rdx, elf_foot_sz
|
|
202
|
+ syscall
|
|
203
|
+
|
|
204
|
+ .exit:
|
|
205
|
+ mov rax, 60 ; exit
|
|
206
|
+ xor rdi, rdi
|
|
207
|
+ syscall
|
|
208
|
+
|
|
209
|
+ .err:
|
|
210
|
+
|
|
211
|
+ mov rdi, rax
|
|
212
|
+ mov rax, 60 ; exit
|
|
213
|
+ syscall
|