/
write.go
195 lines (163 loc) · 6.42 KB
/
write.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
package pe
import (
"bytes"
"encoding/binary"
"errors"
"os"
)
func (peFile *File) Bytes() ([]byte, error) {
var bytesWritten uint64
peBuf := bytes.NewBuffer(nil)
// write DOS header and stub
binary.Write(peBuf, binary.LittleEndian, peFile.DosHeader)
bytesWritten += uint64(binary.Size(peFile.DosHeader))
if peFile.DosExists {
binary.Write(peBuf, binary.LittleEndian, peFile.DosStub)
bytesWritten += uint64(binary.Size(peFile.DosStub))
}
// write Rich header
if peFile.RichHeader != nil {
binary.Write(peBuf, binary.LittleEndian, peFile.RichHeader)
bytesWritten += uint64(len(peFile.RichHeader))
}
// apply padding before PE header if necessary
if uint32(bytesWritten) != peFile.DosHeader.AddressOfNewExeHeader {
padding := make([]byte, peFile.DosHeader.AddressOfNewExeHeader-uint32(bytesWritten))
binary.Write(peBuf, binary.LittleEndian, padding)
bytesWritten += uint64(len(padding))
}
// write PE header
peMagic := []byte{'P', 'E', 0x00, 0x00}
binary.Write(peBuf, binary.LittleEndian, peMagic)
binary.Write(peBuf, binary.LittleEndian, peFile.FileHeader)
bytesWritten += uint64(binary.Size(peFile.FileHeader) + len(peMagic))
var (
is32bit bool
oldCertTableOffset, oldCertTableSize uint32
)
switch peFile.FileHeader.Machine {
case IMAGE_FILE_MACHINE_I386:
is32bit = true
optionalHeader := peFile.OptionalHeader.(*OptionalHeader32)
binary.Write(peBuf, binary.LittleEndian, peFile.OptionalHeader.(*OptionalHeader32))
bytesWritten += uint64(binary.Size(optionalHeader))
oldCertTableOffset = optionalHeader.DataDirectory[CERTIFICATE_TABLE].VirtualAddress
oldCertTableSize = optionalHeader.DataDirectory[CERTIFICATE_TABLE].Size
case IMAGE_FILE_MACHINE_AMD64:
is32bit = false
optionalHeader := peFile.OptionalHeader.(*OptionalHeader64)
binary.Write(peBuf, binary.LittleEndian, optionalHeader)
bytesWritten += uint64(binary.Size(optionalHeader))
oldCertTableOffset = optionalHeader.DataDirectory[CERTIFICATE_TABLE].VirtualAddress
oldCertTableSize = optionalHeader.DataDirectory[CERTIFICATE_TABLE].Size
default:
return nil, errors.New("architecture not supported")
}
// write section headers
sectionHeaders := make([]SectionHeader32, len(peFile.Sections))
for idx, section := range peFile.Sections {
// write section header
sectionHeader := SectionHeader32{
Name: section.OriginalName,
VirtualSize: section.VirtualSize,
VirtualAddress: section.VirtualAddress,
SizeOfRawData: section.Size,
PointerToRawData: section.Offset,
PointerToRelocations: section.PointerToRelocations,
PointerToLineNumbers: section.PointerToLineNumbers,
NumberOfRelocations: section.NumberOfRelocations,
NumberOfLineNumbers: section.NumberOfLineNumbers,
Characteristics: section.Characteristics,
}
// if the PE file was pulled from memory, the symbol table offset in the header will be wrong.
// Fix it up by picking the section that lines up, and use the raw offset instead.
if peFile.FileHeader.PointerToSymbolTable == sectionHeader.VirtualAddress {
peFile.FileHeader.PointerToSymbolTable = sectionHeader.PointerToRawData
}
sectionHeaders[idx] = sectionHeader
//log.Printf("section: %+v\nsectionHeader: %+v\n", section, sectionHeader)
binary.Write(peBuf, binary.LittleEndian, sectionHeader)
bytesWritten += uint64(binary.Size(sectionHeader))
}
// write sections' data
for idx, sectionHeader := range sectionHeaders {
section := peFile.Sections[idx]
sectionData, err := section.Data()
if err != nil {
return nil, err
}
if sectionData == nil { // for sections that weren't in the original file
sectionData = []byte{}
}
if section.Offset != 0 && bytesWritten < uint64(section.Offset) {
pad := make([]byte, uint64(section.Offset)-bytesWritten)
peBuf.Write(pad)
//log.Printf("Padding before section %s at %x: length:%x to:%x\n", section.Name, bytesWritten, len(pad), section.Offset)
bytesWritten += uint64(len(pad))
}
// if our shellcode insertion address is inside this section, insert it at the correct offset in sectionData
if peFile.InsertionAddr >= section.Offset && int64(peFile.InsertionAddr) < (int64(section.Offset)+int64(section.Size)-int64(len(peFile.InsertionBytes))) {
sectionData = append(sectionData, peFile.InsertionBytes[:]...)
datalen := len(sectionData)
if sectionHeader.SizeOfRawData > uint32(datalen) {
paddingSize := sectionHeader.SizeOfRawData - uint32(datalen)
padding := make([]byte, paddingSize, paddingSize)
sectionData = append(sectionData, padding...)
//log.Printf("Padding after section %s: length:%d\n", section.Name, paddingSize)
}
}
binary.Write(peBuf, binary.LittleEndian, sectionData)
bytesWritten += uint64(len(sectionData))
}
// write symbols
binary.Write(peBuf, binary.LittleEndian, peFile.COFFSymbols)
bytesWritten += uint64(binary.Size(peFile.COFFSymbols))
// write the string table
binary.Write(peBuf, binary.LittleEndian, peFile.StringTable)
bytesWritten += uint64(binary.Size(peFile.StringTable))
var newCertTableOffset, newCertTableSize uint32
// write the certificate table
if peFile.CertificateTable != nil {
newCertTableOffset = uint32(bytesWritten)
newCertTableSize = uint32(len(peFile.CertificateTable))
} else {
newCertTableOffset = 0
newCertTableSize = 0
}
binary.Write(peBuf, binary.LittleEndian, peFile.CertificateTable)
bytesWritten += uint64(len(peFile.CertificateTable))
peData := peBuf.Bytes()
// write the offset and size of the new Certificate Table if it changed
if newCertTableOffset != oldCertTableOffset || newCertTableSize != oldCertTableSize {
certTableInfo := &DataDirectory{
VirtualAddress: newCertTableOffset,
Size: newCertTableSize,
}
var certTableInfoBuf bytes.Buffer
binary.Write(&certTableInfoBuf, binary.LittleEndian, certTableInfo)
var certTableLoc int64
if is32bit {
certTableLoc = int64(peFile.DosHeader.AddressOfNewExeHeader) + 24 + 128
} else {
certTableLoc = int64(peFile.DosHeader.AddressOfNewExeHeader) + 24 + 144
}
peData = append(peData[:certTableLoc], append(certTableInfoBuf.Bytes(), peData[int(certTableLoc)+binary.Size(certTableInfo):]...)...)
}
return peData, nil
}
func (peFile *File) WriteFile(destFile string) error {
f, err := os.Create(destFile)
if err != nil {
return err
}
defer f.Close()
peData, err := peFile.Bytes()
if err != nil {
return err
}
_, err = f.Write(peData)
if err != nil {
return err
}
return nil
}