-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgogreen.go
379 lines (329 loc) · 10.9 KB
/
gogreen.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
package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"github.com/leoloobeek/GoGreen/lib"
)
func main() {
conf := flag.String("config", "", "Config file to read. Default ./config.json.")
flagHttpKey := flag.String("httpkey", "", "Manual HttpKey to use. GoGreen will retrieve it's own by default.")
flag.Parse()
config := parseConfig(*conf)
if config == nil {
return
}
payloadWriter := setupPayloadWriter(config.Language)
if payloadWriter == nil {
fmt.Println("[!] Specified language in config.json not supported")
return
}
// get payload from config or file
payload := config.Payload
if payload == "" {
pFile, err := readFile(config.PayloadPath)
if err != nil {
fmt.Printf("[!] Error reading payload from %s\n", config.PayloadPath)
return
}
payload = string(pFile)
if payload == "" {
fmt.Printf("[!] Payload file %s was empty", config.PayloadPath)
return
}
}
// Apply httpkey here (if needed)
httpKey := ""
if config.HttpKeyUrl != "" {
var err error
payload, httpKey, err = httpKeyCode(*flagHttpKey, payload, config, payloadWriter)
if err != nil {
return
}
}
// generate payload hash
mb, err := strconv.Atoi(config.MinusBytes)
if (err != nil) || (mb > len(payload) || mb < 0) {
fmt.Println("[!] MinusBytes invalid. Is it less than 0 or greater than payload size? Setting to 1...")
config.MinusBytes = "1"
mb = 1
}
payloadHash := lib.GenerateSHA512(payload[:(len(payload) - mb)])
// generate key
if config.StartDir == "" {
config.PathKey = ""
}
key, envVarOrder := buildKey(config.EnvVars, config.PathKey)
// read in base code
result, err := baseCode(config.WSHAutoVersion, payloadWriter)
if err != nil {
return
}
// if a StartDir was specified in config.json, add in directory walking
result, err = directoryCode(result, config.StartDir, config.Depth, payloadWriter)
if err != nil {
return
}
// set up env vars code
result, err = envVarCode(result, envVarOrder, payloadWriter)
if err != nil {
return
}
// TODO: handle multiple SHA512 iterations
keyHash := lib.GenerateSHA512(strings.ToLower(key))[:32]
text, iv, err := lib.AESEncrypt([]byte(keyHash), []byte(payload))
if err != nil {
fmt.Printf("[!] Error received encrypting: %s", err)
return
}
result = bytes.Replace(result, []byte("~AESIVBASE64~"), []byte(iv), 1)
result = bytes.Replace(result, []byte("~ENCRYPTEDBASE64~"), []byte(text), 1)
result = bytes.Replace(result, []byte("~PAYLOADHASH~"), []byte(payloadHash), 1)
result = bytes.Replace(result, []byte("~MINUSBYTES~"), []byte(string(config.MinusBytes)), 1)
outfile := "payload" + payloadWriter.Extension
writeFile(outfile, result)
fmt.Println("\nPAYLOAD DETAILS-----------------------")
fmt.Println("Output File: " + outfile)
fmt.Println("Environmental Keys: " + key)
fmt.Println("Decryption Key: " + keyHash)
fmt.Println("Payload Hash: " + payloadHash)
if httpKey != "" {
fmt.Println("HTTP Key: " + httpKey)
}
}
// Config defines structure to be read from config.json
type Config struct {
Language string
WSHAutoVersion string
StartDir string
Depth string
PathKey string
EnvVars map[string]string
Payload string
PayloadPath string
MinusBytes string
HttpKeyUrl string
HttpKeyUA string
HttpKeyRetry string
}
// PayloadWriter for handling what to write
type PayloadWriter struct {
Language string
WSHAutoVersion string
BaseTemplate string
DirTemplate string
EnvKeyTemplate string
AutoVersionTemplate string
HttpKeyTemplate string
HttpKeyTestTemplate string
EnvKeyCode string
Extension string
}
func setupPayloadWriter(lang string) *PayloadWriter {
switch lang {
case "vbscript":
return &PayloadWriter{
Language: "vbscript",
BaseTemplate: "data/vbscript/base.vbs",
DirTemplate: "data/vbscript/directory.vbs",
EnvKeyTemplate: "data/vbscript/envkey.vbs",
AutoVersionTemplate: "data/vbscript/autoversion.vbs",
HttpKeyTemplate: "data/vbscript/httpkey.vbs",
HttpKeyTestTemplate: "data/vbscript/httpkey-test.vbs",
EnvKeyCode: "oEnv(\"%s\")",
Extension: ".vbs"}
case "jscript":
return &PayloadWriter{
Language: "jscript",
BaseTemplate: "data/jscript/base.js",
DirTemplate: "data/jscript/directory.js",
EnvKeyTemplate: "data/jscript/envkey.js",
AutoVersionTemplate: "data/jscript/autoversion.js",
HttpKeyTemplate: "data/jscript/httpkey.js",
HttpKeyTestTemplate: "data/jscript/httpkey-test.js",
EnvKeyCode: "oEnv(\"%s\")",
Extension: ".js"}
case "powershell":
return &PayloadWriter{
Language: "powershell",
BaseTemplate: "data/powershell/base.ps1",
DirTemplate: "data/powershell/directory.ps1",
EnvKeyTemplate: "data/powershell/envkey.ps1",
AutoVersionTemplate: "",
HttpKeyTemplate: "data/powershell/httpkey.ps1",
HttpKeyTestTemplate: "data/powershell/httpkey-test.ps1",
EnvKeyCode: "$oEnv.Invoke(\"%s\")",
Extension: ".ps1"}
default:
return nil
}
}
func parseConfig(configPath string) *Config {
if configPath == "" {
configPath = "config.json"
}
file, err := os.Open(configPath)
if err != nil {
fmt.Println("[!] Error reading config.json")
return nil
}
decoder := json.NewDecoder(file)
config := Config{}
err = decoder.Decode(&config)
if err != nil {
fmt.Println("[!] Error parsing config.json:", err)
return nil
}
return &config
}
func readFile(path string) ([]byte, error) {
fileBytes, err := ioutil.ReadFile(path)
if err != nil {
return nil, errors.New("[!] readFile: Error reading file")
}
return fileBytes, nil
}
func writeFile(filename string, contents []byte) {
f, err := os.Create(filename)
if err != nil {
panic(err)
}
defer f.Close()
_, err = f.Write(contents)
if err != nil {
panic(err)
}
}
// Get base code to start
func baseCode(autoVersion string, pw *PayloadWriter) ([]byte, error) {
baseCode, err := readFile(pw.BaseTemplate)
if err != nil {
fmt.Printf("[!] Unable to read %s\n", (pw.BaseTemplate))
return nil, err
}
if pw.AutoVersionTemplate != "" {
if strings.ToLower(autoVersion) == "yes" {
av, err := readFile(pw.AutoVersionTemplate)
if err != nil {
fmt.Printf("[!] Unable to read %s\n", (pw.AutoVersionTemplate))
return nil, err
}
baseCode = bytes.Replace(baseCode, []byte("~AUTOVERSION~"), []byte(av), 1)
} else {
baseCode = bytes.Replace(baseCode, []byte("~AUTOVERSION~"), []byte(""), 1)
}
}
return baseCode, nil
}
// Get directory code for directory walking
func directoryCode(text []byte, startDir, depth string, pw *PayloadWriter) ([]byte, error) {
if startDir != "" {
contents, err := readFile(pw.DirTemplate)
if err != nil {
fmt.Printf("[!] Error reading %s\n", pw.DirTemplate)
return nil, err
}
// JScript needs escaped slashes, VBS does not
if pw.Language == "jscript" {
startDir = strings.Replace(startDir, "\\", "\\\\", -1)
}
// To save on payload code size just assigning
// a huge number for now
if depth == "" {
depth = "100000"
}
result := bytes.Replace(text, []byte("~WALKOS~"), contents, 1)
result = bytes.Replace(result, []byte("~STARTDIR~"), []byte(startDir), 1)
result = bytes.Replace(result, []byte("~DEPTH~"), []byte(depth), 1)
return result, nil
}
result := bytes.Replace(text, []byte("~WALKOS~"), []byte(""), 1)
return result, nil
}
// Set up the environmental vars code within the script
// TODO: This got overly complex, need to simplify
func envVarCode(text []byte, envVarCode []string, pw *PayloadWriter) ([]byte, error) {
if len(envVarCode) > 0 {
contents, err := readFile(pw.EnvKeyTemplate)
if err != nil {
fmt.Printf("[!] Error reading %s\n", pw.EnvKeyTemplate)
return nil, err
}
result := bytes.Replace(text, []byte("~ENVVAR~"), contents, 1)
envs := fmt.Sprintf(pw.EnvKeyCode, envVarCode[0])
if len(envVarCode) > 1 {
for _, envVar := range envVarCode[1:] {
envs += fmt.Sprintf(", "+pw.EnvKeyCode, envVar)
}
}
result = bytes.Replace(result, []byte("~ENVVARS~"), []byte(envs), 1)
return result, nil
}
result := bytes.Replace(text, []byte("~ENVVAR~"), []byte(""), 1)
return result, nil
}
// returns the new payload, httpkey, and err
func httpKeyCode(httpKey, payload string, config *Config, pw *PayloadWriter) (string, string, error) {
if httpKey == "" {
var err error
httpKey, err = lib.GenerateHttpKey(config.HttpKeyUrl, config.HttpKeyUA)
if err != nil {
fmt.Printf("[!] Error accessing %s\n%s\n", config.HttpKeyUrl, err)
return "", "", err
}
}
payloadHash := lib.GenerateSHA512(payload)
text, iv, err := lib.AESEncrypt([]byte(httpKey), []byte(payload))
if err != nil {
fmt.Printf("[!] Error received encrypting: %s\n", err)
return "", "", err
}
contents, err := readFile(pw.HttpKeyTemplate)
if err != nil {
return "", "", err
}
if config.HttpKeyUA == "" {
config.HttpKeyUA = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)"
}
result := bytes.Replace(contents, []byte("~HKPAYLOADHASH~"), []byte(payloadHash), 1)
result = bytes.Replace(result, []byte("~HKPAYLOAD~"), []byte(text), 1)
result = bytes.Replace(result, []byte("~RETRYNUM~"), []byte(config.HttpKeyRetry), 1)
result = bytes.Replace(result, []byte("~HKURL~"), []byte(config.HttpKeyUrl), 1)
result = bytes.Replace(result, []byte("~HKUSERAGENT~"), []byte(config.HttpKeyUA), 1)
result = bytes.Replace(result, []byte("~HKIV~"), []byte(iv), 1)
httpKeyTestCode(config.HttpKeyUrl, config.HttpKeyUA, httpKey, pw)
return string(result), httpKey, nil
}
func httpKeyTestCode(url, ua, httpKey string, pw *PayloadWriter) {
contents, err := readFile(pw.HttpKeyTestTemplate)
if err != nil {
return
}
result := bytes.Replace(contents, []byte("~HTTPKEY~"), []byte(httpKey), 1)
result = bytes.Replace(result, []byte("~HKURL~"), []byte(url), 1)
result = bytes.Replace(result, []byte("~HKUSERAGENT~"), []byte(ua), 1)
outfile := "httpkey-tester" + pw.Extension
fmt.Printf("[*] Make sure to test the httpkey with %s!\n", outfile)
writeFile(outfile, result)
}
// buildKey builds the final key with env vars, path, etc. and also returns
// the order of the env vars (maps don't always come out in same order)
// key = [env var 1][env var 2][env var N][file path]
func buildKey(envVars map[string]string, filePath string) (string, []string) {
result := ""
envVarOrder := make([]string, len(envVars))
i := 0
for k, v := range envVars {
result += v
envVarOrder[i] = k
i++
}
result += filePath
return result, envVarOrder
}