forked from safe6Sec/GolangBypassAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
311 lines (276 loc) · 9.25 KB
/
main.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
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/rc4"
"encoding/base64"
"encoding/hex"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
// X Packages
"golang.org/x/crypto/argon2"
// 3rd Party
"github.com/fatih/color"
)
func main() {
verbose := flag.Bool("v", false, "Enable verbose output")
encryptionType := flag.String("e", "xor", "The type of encryption to use [xor, aes256, rc4, null]")
key := flag.String("key", "1564", "Encryption key")
b64 := flag.Bool("base64", false, "Base64 encode the output. Can be used with or without encryption")
input := flag.String("i", "C:\\Users\\Administrator\\Desktop\\payload.bin", "Input file path of binary file")
output := flag.String("o", "./code.txt", "Output file path")
mode := flag.String("mode", "encrypt", "Mode of operation to perform on the input file [encrypt,decrypt]")
salt := flag.String("salt", "", "Salt, in hex, used to generate an AES256 32-byte key through Argon2. Only used during decryption")
inputNonce := flag.String("nonce", "", "Nonce, in hex, used to decrypt an AES256 input file. Only used during decryption")
flag.Usage = func() {
flag.PrintDefaults()
os.Exit(0)
}
flag.Parse()
// Check to make sure the input file exists
_, errInputFile := os.Stat(*input)
if os.IsNotExist(errInputFile) {
color.Red(fmt.Sprintf("[!]The file does not exist: %s", *input))
os.Exit(1)
}
shellcode, errShellcode := ioutil.ReadFile(*input)
if errShellcode != nil {
color.Red(fmt.Sprintf("[!]%s", errShellcode.Error()))
os.Exit(1)
}
// Check to make sure an output file was provided
/* if *output == "" {
color.Red("[!]The -o output argument is required")
os.Exit(1)
}*/
// Check to make sure the output directory exists
dir, outFile := filepath.Split(*output)
if *verbose {
color.Yellow(fmt.Sprintf("[-]Output directory: %s", dir))
color.Yellow(fmt.Sprintf("[-]Output file name: %s", outFile))
}
outDir, errOutDir := os.Stat(dir)
if errOutDir != nil {
color.Red(fmt.Sprintf("[!]%s", errOutDir.Error()))
os.Exit(1)
}
if !outDir.IsDir() {
color.Red(fmt.Sprintf("[!]The output directory does not exist: %s", dir))
}
if *verbose {
color.Yellow(fmt.Sprintf("[-]File contents (hex): %x", shellcode))
}
if strings.ToUpper(*mode) != "ENCRYPT" && strings.ToUpper(*mode) != "DECRYPT" {
color.Red("[!]Invalid mode provided. Must be either encrypt or decrypt")
os.Exit(1)
}
// Make sure a key was provided
if *encryptionType != "" {
if *key == "" {
color.Red("[!]A key must be provided with the -key parameter to encrypt the input file")
os.Exit(1)
}
}
var outputBytes []byte
switch strings.ToUpper(*mode) {
case "ENCRYPT":
var encryptedBytes []byte
switch strings.ToUpper(*encryptionType) {
case "XOR":
// https://kylewbanks.com/blog/xor-encryption-using-go
if *verbose {
color.Yellow(fmt.Sprintf("[-]XOR encrypting input file with key: %s", *key))
}
encryptedBytes = make([]byte, len(shellcode))
tempKey := *key
for k, v := range shellcode {
encryptedBytes[k] = v ^ tempKey[k%len(tempKey)]
}
case "AES256":
// https://github.com/gtank/cryptopasta/blob/master/encrypt.go
if *verbose {
color.Yellow("[-]AES256 encrypting input file")
}
// Generate a salt that is used to generate a 32 byte key with Argon2
salt := make([]byte, 32)
_, errReadFull := io.ReadFull(rand.Reader, salt)
if errReadFull != nil {
color.Red(fmt.Sprintf("[!]%s", errReadFull.Error()))
os.Exit(1)
}
color.Green(fmt.Sprintf("[+]Argon2 salt (hex): %x", salt))
// Generate Argon2 ID key from input password using a randomly generated salt
aesKey := argon2.IDKey([]byte(*key), salt, 1, 64*1024, 4, 32)
// I leave it up to the operator to use the password + salt for decryption or just the Argon2 key
color.Green(fmt.Sprintf("[+]AES256 key (32-bytes) derived from input password %s (hex): %x", *key, aesKey))
// Generate AES Cipher Block
cipherBlock, err := aes.NewCipher(aesKey)
if err != nil {
color.Red(fmt.Sprintf("[!]%s", err.Error()))
}
gcm, errGcm := cipher.NewGCM(cipherBlock)
if err != nil {
color.Red(fmt.Sprintf("[!]%s", errGcm.Error()))
os.Exit(1)
}
// Generate a nonce (or IV) for use with the AES256 function
nonce := make([]byte, gcm.NonceSize())
_, errNonce := io.ReadFull(rand.Reader, nonce)
if errNonce != nil {
color.Red(fmt.Sprintf("[!]%s", errNonce.Error()))
os.Exit(1)
}
color.Green(fmt.Sprintf("[+]AES256 nonce (hex): %x", nonce))
encryptedBytes = gcm.Seal(nil, nonce, shellcode, nil)
case "RC4":
if *verbose {
color.Yellow("[-]RC4 encrypting input file")
}
cipher, err := rc4.NewCipher([]byte(*key))
if err != nil {
color.Red(fmt.Sprintf("[!]%s", err.Error()))
os.Exit(1)
}
encryptedBytes = make([]byte, len(shellcode))
cipher.XORKeyStream(encryptedBytes, shellcode)
case "":
if *verbose {
color.Yellow("[-]No encryption type provided, continuing on...")
}
encryptedBytes = append(encryptedBytes, shellcode...)
default:
color.Red(fmt.Sprintf("[!]Invalid method type: %s", *encryptionType))
os.Exit(1)
}
if len(encryptedBytes) <= 0 {
color.Red("[!]Encrypted byte slice length is equal to or less than 0")
os.Exit(1)
}
if *b64 {
outputBytes = make([]byte, base64.StdEncoding.EncodedLen(len(encryptedBytes)))
base64.StdEncoding.Encode(outputBytes, encryptedBytes)
} else {
outputBytes = append(outputBytes, encryptedBytes...)
}
case "DECRYPT":
var decryptedBytes []byte
switch strings.ToUpper(*encryptionType) {
case "AES256":
// https://github.com/gtank/cryptopasta/blob/master/encrypt.go
if *verbose {
color.Yellow("[-]AES256 decrypting input file")
}
// I leave it up to the operator to use the password + salt for decryption or just the Argon2 key
if *salt == "" {
color.Red("[!]A 32-byte salt in hex format must be provided with the -salt argument to decrypt AES256 input file")
os.Exit(1)
}
if len(*salt) != 64 {
color.Red("[!]A 32-byte salt in hex format must be provided with the -salt argument to decrypt AES256 input file")
color.Red(fmt.Sprintf("[!]A %d byte salt was provided", len(*salt)/2))
os.Exit(1)
}
saltDecoded, errSaltDecoded := hex.DecodeString(*salt)
if errShellcode != nil {
color.Red(fmt.Sprintf("[!]%s", errSaltDecoded.Error()))
os.Exit(1)
}
if *verbose {
color.Yellow("[-]Argon2 salt (hex): %x", saltDecoded)
}
aesKey := argon2.IDKey([]byte(*key), saltDecoded, 1, 64*1024, 4, 32)
if *verbose {
color.Yellow("[-]AES256 key (hex): %x", aesKey)
}
cipherBlock, err := aes.NewCipher(aesKey)
if err != nil {
color.Red(fmt.Sprintf("[!]%s", err.Error()))
}
gcm, errGcm := cipher.NewGCM(cipherBlock)
if err != nil {
color.Red(fmt.Sprintf("[!]%s", errGcm.Error()))
os.Exit(1)
}
if len(shellcode) < gcm.NonceSize() {
color.Red("[!]Malformed ciphertext is larger than nonce")
os.Exit(1)
}
if len(*inputNonce) != gcm.NonceSize()*2 {
color.Red("[!]A nonce, in hex, must be provided with the -nonce argument to decrypt the AES256 input file")
color.Red(fmt.Sprintf("[!]A %d byte nonce was provided but %d byte nonce was expected", len(*inputNonce)/2, gcm.NonceSize()))
os.Exit(1)
}
decryptNonce, errDecryptNonce := hex.DecodeString(*inputNonce)
if errDecryptNonce != nil {
color.Red("[!]%s", errDecryptNonce.Error())
os.Exit(1)
}
if *verbose {
color.Yellow(fmt.Sprintf("[-]AES256 nonce (hex): %x", decryptNonce))
}
var errDecryptedBytes error
decryptedBytes, errDecryptedBytes = gcm.Open(nil, decryptNonce, shellcode, nil)
if errDecryptedBytes != nil {
color.Red("[!]%s", errDecryptedBytes.Error())
os.Exit(1)
}
case "XOR":
// https://kylewbanks.com/blog/xor-encryption-using-go
if *verbose {
color.Yellow(fmt.Sprintf("[-]XOR decrypting input file with key: %s", *key))
}
decryptedBytes = make([]byte, len(shellcode))
tempKey := *key
for k, v := range shellcode {
decryptedBytes[k] = v ^ tempKey[k%len(tempKey)]
}
case "RC4":
if *verbose {
color.Yellow("[-]RC4 decrypting input file")
}
cipher, err := rc4.NewCipher([]byte(*key))
if err != nil {
color.Red(fmt.Sprintf("[!]%s", err.Error()))
os.Exit(1)
}
decryptedBytes = make([]byte, len(shellcode))
cipher.XORKeyStream(decryptedBytes, shellcode)
default:
color.Red("[!]Invalid method")
os.Exit(1)
}
if len(decryptedBytes) <= 0 {
color.Red("[!]Decrypted byte slice length is equal to or less than 0")
os.Exit(1)
}
if *b64 {
outputBytes = make([]byte, base64.StdEncoding.EncodedLen(len(decryptedBytes)))
base64.StdEncoding.Encode(outputBytes, decryptedBytes)
} else {
outputBytes = append(outputBytes, decryptedBytes...)
//outputBytes = []byte(hex.EncodeToString(outputBytes))
}
}
if *verbose {
if *b64 {
color.Green("[+]Output (string):\r\n")
fmt.Println(fmt.Sprintf("%s", outputBytes))
} else {
color.Green("[+]Output (hex):\r\n")
fmt.Println(fmt.Sprintf("%x", outputBytes))
}
}
// Write the file
err := ioutil.WriteFile(*output, outputBytes, 0660)
if err != nil {
color.Red(fmt.Sprintf("[!]%s", err.Error()))
os.Exit(1)
}
color.Green(fmt.Sprintf("[+]%s %s input and wrote %d bytes to: %s", *encryptionType, *mode, len(outputBytes), *output))
}