/
elfFromScratch
executable file
·233 lines (169 loc) · 6.58 KB
/
elfFromScratch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/bin/bash
/bin/date
echo "Starting Build"
#Exit if we don't have a file to save to
if [ $# -eq 0 ]; then
echo "Use: elfFromScratch [name of file]"
exit 1
fi
echo "Saving results to file: $1"
echo -e "\nConstructing elf header..."
#Elf magic numbers
echo -en "\x7fELF" >> $1
#Is 32 bit elf file
echo -en "\x01" >> $1
#Is big endian
echo -en "\x01" >> $1
#Original elf format
echo -en "\x01" >> $1
#Target operating system linux (we could leave this zero)
echo -en "\x03" >> $1
#We are conforming to the application binary interface specified above
#you will see this as ABI in man pages. For syscalls etc.
echo -en "\x00" >> $1
#seven bytes of padding
echo -en "\x00\x00\x00\x00\x00\x00\x00" >> $1
#This will be an executable file
echo -en "\x02\x00" >> $1
#We are targeting x86
echo -en "\x03\x00" >> $1
#This is the original version of an elf (some things just don't change ;))
echo -en "\x01\x00\x00\x00" >> $1
#Since we are compiling this program from scratch we can start
#wherever we want in memory. Lets stick with something normalish like
#0x08048000 we also need to add a4 to this to start at the actual
#instructions KEEP IN MIND LITTLE ENDIANNESS
echo -en "\xa4\x80\x04\x08" >> $1
#We will need a program header table. Start it right after the header
#file (52nd byte = 34 hex) because why waist space.
echo -en "\x34\x00\x00\x00" >> $1
#We also need a section header table. This is normally after the text
#segment but because we are compiling things by hand lets just stick it
#after the program header for the lulz.
echo -en "\x54\x00\x00\x00" >> $1
#set the eflags. Man pages say "Currently no flags have been defined"
#lets just pad this with 0s then
echo -en "\x00\x00\x00\x00" >> $1
#Since we are on 32bits we are using a 52 byte elf header (0x34)
echo -en "\x34\x00" >> $1
#The size of a single program header struct is going to be 32 bytes
#(0x20)
echo -en "\x20\x00" >> $1
#The number of entries in the program header table we will just use
#1 huge block of executable/writeable/readable memory. Time to live
#life on the edge!
echo -en "\x01\x00" >> $1
#The size of the section header table struct. We will make this
#40 bytes (0x28)
echo -en "\x28\x00" >> $1
#We will have two section header table entries. One will contain the
#section header names and the other will have our actual instructions.
echo -en "\x02\x00" >> $1
#Lets put the put the section header names in the second section header
#struct. Keeping in mind indexing starts at 0 of course.
echo -en "\x01\x00" >> $1
#########################PROGRAM HEADER TABLE#########################
#Every entry of the struct should be four bytes large according to the
#manual.
echo "Constructing program header table"
#Make this program header loaded from a file
echo -en "\x01\x00\x00\x00" >> $1
#Lets just load the entire executable into memory starting at 00000000
#The actual offset of the instructions in the file though is below.
#offset_of_instructions = 0x34 + 0x20 + 0x28 + 0x28
echo -en "\x00\x00\x00\x00" >> $1
#Start this data being loaded into memory at 08048000 because thats
#a pretty normal starting address
echo -en "\x00\x80\x04\x08" >> $1
#Most elf files seem to set the physical addressing value to the virtual
#address value
echo -en "\x00\x80\x04\x08" >> $1
#Since we are a bit lazy the in memory size will just be the same size
#as the file. We are loading the entire file into memory.
echo -en "\xEE\x00\x00\x00" >> $1
#Our file size should be the same as the memory size here
echo -en "\xEE\x00\x00\x00" >> $1
#Lets set everything to read write execute (What could possibly go wrong?)
#cough.. cough... cough..
echo -en "\x07\x00\x00\x00" >> $1
#Lets align things to a page size
echo -en "\x00\x10\x00\x00" >> $1
#########################SECTION HEADER TABLE 1#########################
#All fields of the section header struct should be 32 bits according
#to the manual. Although the name has been just nulled out, this section
#will just act as the text section of the program.
#since this is the first section header its name will just start at
#0 in the names array.
echo -en "\x00\x00\x00\x00" >> $1
#Lets set the SHT_PROGBITS type defined in the manual
echo -en "\x01\x00\x00\x00" >> $1
#Lets set the flags to read write excute again because why not cough..
#cough...
echo -en "\x07\x00\x00\x00" >> $1
#This section should start at the same spot as our instructions
echo -en "\xA4\x80\x04\x08" >> $1
#This offset should start at the same offset our instructions start
echo -en "\xA4\x00\x00\x00" >> $1
#This section will only include the size of our program. (0x21)
echo -en "\x2B\x00\x00\x00" >> $1
#Null out link
echo -en "\x00\x00\x00\x00" >> $1
#Null out info
echo -en "\x00\x00\x00\x00" >> $1
#Align things to a word
echo -en "\x10\x00\x00\x00" >> $1
#Null out fixed-sized entries
echo -en "\x00\x00\x00\x00" >> $1
#########################SECTION HEADER TABLE 2#########################
#This is the section header for the names. We don't really care about
#the names though.
#Name of section index (just start at 0x01 because who cares)
echo -en "\x01\x00\x00\x00" >> $1
#Will be of type string table
echo -en "\x03\x00\x00\x00" >> $1
#Lets not set any flags here
echo -en "\x00\x00\x00\x00" >> $1
#No address because not loaded into memory
echo -en "\x00\x00\x00\x00" >> $1
#The offset should be after our regular program instructions. Elf header,
#Program header, Section header *2, Text instructions
#string_table_offset = 0x34 + 0x20 + 0x28 + 0x28 + 0x2B
echo -en "\xCF\x00\x00\x00" >> $1
#The size is going to be 32 bytes
echo -en "\x20\x00\x00\x00" >> $1
#Null Link
echo -en "\x00\x00\x00\x00" >> $1
#Null info
echo -en "\x00\x00\x00\x00" >> $1
#Align to 1
echo -en "\x01\x00\x00\x00" >> $1
#Null out fixed sized entries
echo -en "\x00\x00\x00\x00" >> $1
#########################PROGRAM INSTRUCTIONS#########################
#push $0xa
echo -en "\x6a\x0a" >> $1
#push $0x21646c72
echo -en "\x68\x72\x6c\x64\x21" >> $1
#push $0x6f77206f
echo -en "\x68\x6f\x20\x77\x6f" >> $1
#push $0x6c6c6568
echo -en "\x68\x68\x65\x6c\x6c" >> $1
#mov $0xd,%edx
echo -en "\xba\x0d\x00\x00\x00" >> $1
#mov %esp,%ecx
echo -en "\x89\xe1" >> $1
#mov $0x1,%ebx
echo -en "\xbb\x01\x00\x00\x00" >> $1
#mov $0x4,%eax
echo -en "\xb8\x04\x00\x00\x00" >> $1
#int $0x80
echo -en "\xcd\x80" >> $1
#mov $0x1,%eax
echo -en "\xb8\x01\x00\x00\x00" >> $1
#int $0x80
echo -en "\xcd\x80" >> $1
#########################SECTION NAMES#########################
echo -en "\x00\x00\x00\x00\x00\x00\x00\x00" >> $1
echo -en "\x00\x00\x00\x00\x00\x00\x00\x00" >> $1
echo -en "\x00\x00\x00\x00\x00\x00\x00\x00" >> $1
echo -en "\x00\x00\x00\x00\x00\x00\x00\x00" >> $1