-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfigmatter-linux.go
More file actions
284 lines (237 loc) · 8.84 KB
/
configmatter-linux.go
File metadata and controls
284 lines (237 loc) · 8.84 KB
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
/*
Copyright (C) <2021-2022> <Marius Genheimer>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>
*/
package main
import (
"bytes"
"compress/zlib"
"crypto/md5"
"crypto/sha256"
"debug/elf"
"encoding/base64"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"text/tabwriter"
"github.com/fatih/color"
)
// check errors as they occur and panic :o
func check(e error) {
if e != nil {
panic(e)
}
}
// ioReader acts as a wrapper function to make opening the file even easier
func ioReader(file string) io.ReaderAt {
r, err := os.Open(file)
check(err)
return r
}
// calcSHA256 reads the sample file and calculates its SHA-256 hashsum
func calcSHA256(file string) string {
f, readErr := os.Open(file)
check(readErr)
defer f.Close()
h := sha256.New()
_, hashErr := io.Copy(h, f)
check(hashErr)
return hex.EncodeToString(h.Sum(nil))
}
// calcMD5 reads the sample file and calculates its SHA-256 hashsum
func calcMD5(file string) string {
f, readErr := os.Open(file)
check(readErr)
defer f.Close()
h := md5.New()
_, hashErr := io.Copy(h, f)
check(hashErr)
return hex.EncodeToString(h.Sum(nil))
}
// getFileInfo returns the size on disk of the specified file
func getFileInfo(file string) int64 {
f, readErr := os.Open(file)
check(readErr)
defer f.Close()
fileInfo, fileErr := f.Stat()
check(fileErr)
return fileInfo.Size()
}
// base64Decode decodes base64 data passed as a byte array; returns a byte array
func base64Decode(message []byte) (b []byte) {
b = make([]byte, base64.StdEncoding.DecodedLen(len(message)))
l, base64Err := base64.StdEncoding.Decode(b, message)
check(base64Err)
return b[:l]
}
// zlibDecompress decompresses raw zlib data passed as a byte array; returns a byte array
func zlibDecompress(data []byte) []byte {
reader := bytes.NewReader(data)
zr, zlibErr := zlib.NewReader(reader)
check(zlibErr)
defer zr.Close()
contents, readErr := ioutil.ReadAll(zr)
check(readErr)
return contents
}
// Flag variables for commandline arguments
var debugFlag bool
var verboseFlag bool
var jsonFlag bool
func main() {
fmt.Printf("\n * +")
fmt.Printf("\n ' | ___ __ _ __ __ _ _ ")
fmt.Printf("\n () .-.,=''''=. - o - / __|___ _ _ / _(_)__ _| \\/ |__ _| |_| |_ ___ _ _ ")
fmt.Printf("\n '=/_ \\ | | (__/ _ \\ ' \\| _| / _` | |\\/| / _` | _| _/ -_) '_|")
fmt.Printf("\n * | '=._ | \\___\\___/_||_|_| |_\\__, |_| |_\\__,_|\\__|\\__\\___|_| ")
fmt.Printf("\n \\ `=./`, ' |___/ ")
fmt.Printf("\n . '=.__.=' `=' *")
fmt.Printf("\n + + BlackMatter Linux Ransomware Configuration Extractor")
fmt.Printf("\n O * ' . Marius 'f0wL' Genheimer | https://dissectingmalwa.re\n\n")
// parse passed flags
flag.BoolVar(&jsonFlag, "j", false, "Write extracted config to a JSON file")
flag.BoolVar(&verboseFlag, "v", false, "Verbose output")
flag.BoolVar(&debugFlag, "d", false, "More verbose output than -v. Useful for debugging this config extractor.")
flag.Parse()
// check passed arguments
if flag.NArg() == 0 {
color.Red("✗ No path to sample provided.\n\n")
os.Exit(1)
}
// calculate hash sums of the sample
md5sum := calcMD5(flag.Args()[0])
sha256sum := calcSHA256(flag.Args()[0])
// print useful sample metadata
w1 := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
fmt.Fprintln(w1, "→ File size (bytes): \t", getFileInfo(flag.Args()[0]))
fmt.Fprintln(w1, "→ Sample MD5: \t", md5sum)
fmt.Fprintln(w1, "→ Sample SHA-256: \t", sha256sum)
w1.Flush()
// open the sample in an ioReader
sample := ioReader(flag.Args()[0])
// parse the ELF
f, parseErr := elf.NewFile(sample)
check(parseErr)
// dump out the contents of the .app.version section
versionSection, dumpErr := f.Section(".app.version").Data()
check(dumpErr)
mwVersion := string(bytes.Trim(versionSection, "\x00"))
// dump out the contents of the .cfgETD section
isDecryptor := false
var configData []byte
if f.Section(".cfgETD") != nil {
configData, dumpErr = f.Section(".cfgETD").Data()
check(dumpErr)
} else if f.Section(".cfgDTD") != nil {
color.Yellow("Couldn't find section .cfgETD, trying to dump .cfgDTD instead (Decryptor).")
configData, dumpErr = f.Section(".cfgDTD").Data()
check(dumpErr)
isDecryptor = true
} else {
color.Red("Couldn't find section .cfgETD or .cfgDTD.")
}
cleanRaw := bytes.Trim(configData, "\x00")
// decode the base64 encoded data
decodedCompressed := base64Decode(cleanRaw)
if debugFlag {
fmt.Printf("\nBase-64 decoded:\n")
fmt.Print(hex.Dump(decodedCompressed))
}
// decompress the zlib compressed data
decompressed := zlibDecompress(decodedCompressed)
if debugFlag {
fmt.Printf("\nDecompressed config blob:\n")
fmt.Print(hex.Dump(decompressed))
}
index := 0
key_pos := 0
key := decompressed[:32]
ciphertext := decompressed[32:]
length := len(ciphertext)
plaintext := make([]byte, length)
if debugFlag {
fmt.Printf("\nXOR Key: \n%v\n", hex.Dump(key))
fmt.Printf("Ciphertext Length: %v / %x\n", length, length)
}
// ┌───────────────────────────────────────────────────────────┐
// │ Decrypting the configuration │
// | The first 32 bytes of the config blob contain the XOR Key |
// └───────────────────────────────────────────────────────────┘
for index < length {
kbyte := key[key_pos]
if ciphertext[index] != kbyte {
plaintext[index] = ciphertext[index] ^ kbyte
key_pos = key_pos + 1
if key_pos == 32 {
key_pos = 0
}
} else {
plaintext[index] = ciphertext[index]
}
index = index + 1
}
if jsonFlag {
// write json config to a file
filename := "config-" + md5sum + ".json"
writeErr := ioutil.WriteFile(filename, plaintext, 0644)
check(writeErr)
color.Green("\n✓ Wrote decrypted configuration to %v\n", filename)
}
if verboseFlag {
// hexdump the whole config file
fmt.Printf("\nDecrypted config blob:\n%v", hex.Dump(plaintext))
}
// initialize a variable to store the config in; the structures are defined in blackmatter-linux_structs.go
var configEnc BlackmatterConfigEnc
var configDec BlackmatterConfigDec
if !isDecryptor {
// unmarshal the decrypted config into the struct
jsonErr := json.Unmarshal(plaintext, &configEnc)
check(jsonErr)
// print extracted configuration features
color.Green("\n✓ Extracted Configuration:\n")
fmt.Fprintln(w1, "\n→ Ransomware Version: \t", mwVersion)
fmt.Fprintln(w1, "→ RSA Public Key: \t", configEnc.RSAKey[0:64]+"...")
fmt.Fprintln(w1, "→ Self-Remove: \t", configEnc.SelfDelete)
fmt.Fprintln(w1, "→ Worker Concurrency: \t", configEnc.Concurrency)
fmt.Fprintln(w1, "→ Log Level: \t", configEnc.Log.Level)
fmt.Fprintln(w1, "→ Log Path: \t", configEnc.Log.Path)
fmt.Fprintln(w1, "→ Ransomnote Filename: \t", configEnc.Message.Name)
fmt.Fprintln(w1, "→ Bot ID: \t", configEnc.Landing.ID)
fmt.Fprintln(w1, "→ AES Key: \t", configEnc.Landing.Key)
fmt.Fprintln(w1, "→ C&C URLs: \t", configEnc.Landing.URLs)
fmt.Fprintln(w1, "→ Ignore VMs: \t", configEnc.KillVM.Ignore)
fmt.Fprintln(w1, "→ Kill Processes: \t", configEnc.KillProcess.List)
w1.Flush()
// print the ransomnote
color.Green("\n✓ Ransomnote:\n")
fmt.Printf("%v\n", configEnc.Message.Content)
} else {
// unmarshal the decrypted config into the struct
jsonErr := json.Unmarshal(plaintext, &configDec)
check(jsonErr)
// print extracted configuration features
color.Green("\n✓ Extracted Configuration:\n")
fmt.Fprintln(w1, "\n→ Ransomware Version: \t", mwVersion)
fmt.Fprintln(w1, "→ RSA Public Key: \t", configDec.RSAKey[0:64]+"...")
fmt.Fprintln(w1, "→ Self-Remove: \t", configDec.SelfDelete)
fmt.Fprintln(w1, "→ Worker Concurrency: \t", configDec.Concurrency)
fmt.Fprintln(w1, "→ Log Level: \t", configDec.Log.Level)
fmt.Fprintln(w1, "→ Log Path: \t", configDec.Log.Path)
fmt.Fprintln(w1, "→ Ransomnote Filename: \t", configDec.Message.Name)
w1.Flush()
}
}