This repository has been archived by the owner on Mar 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathremcos_decryptor.py
executable file
·345 lines (263 loc) · 9.76 KB
/
remcos_decryptor.py
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
#!/usr/bin/env python2
####################################################################################
# #
# Decryptor POC for Remcos RAT version 2.0.5 and earlier #
# #
# Disclaimer: This tool comes without any warranties. Use it at your own risk. #
# #
# Created July 2018 by Talos #
# #
####################################################################################
import sys
import string
import pefile
import magic
import getopt
import array
import re
import string
from pprint import pprint
PRG=None
VERBOSE=False
DECRYPT_ONLY=False
C2_ONLY=False
FATAL_ERROR=10
def print_hexdata(s,d,astart):
l=len(d)
print("%s Length:%d(0x%x)" % (s,l,l))
c=0
sys.stdout.write("%06x " % (c + astart))
a=[]
for c,byte in enumerate(d,1):
sys.stdout.write("%02x " % byte)
a.append(byte)
if not c % 16:
sys.stdout.write(" ")
for x in a:
if x < 127 and x > 33:
sys.stdout.write("%c" % x)
else:
sys.stdout.write(".")
sys.stdout.write("\n%06x " % (c + astart))
a = []
print("\n")
def print_hexdata_str(d):
for x in d:
if x < 127 and x > 33:
sys.stdout.write("%c" % x)
else:
sys.stdout.write(".")
print("\n")
def get_C2(d):
try:
a = array.array('b')
a.extend(d)
d_str = a.tostring()
fields=d_str.split("|")
C2=[]
for field in fields:
if bool(re.search('.*:.*(:.*)*', field)):
C2.append(field)
except:
C2 = None
return(C2)
def get_named_resource_from_PE(pefilename,ResourceName):
pe = pefile.PE(pefilename)
ResourceData = ""
offset = 0x0
size = 0x0
for rsrc in pe.DIRECTORY_ENTRY_RESOURCE.entries:
for entry in rsrc.directory.entries:
if entry.name is not None:
if entry.name.__str__() == ResourceName:
offset = entry.directory.entries[0].data.struct.OffsetToData
size = entry.directory.entries[0].data.struct.Size
ResourceData = pe.get_memory_mapped_image()[offset:offset+size]
return ResourceData
def RC4_build_S_array(key,keylen):
S = range(256)
b=0
for counter in range(256):
a = key[counter % keylen] + S[counter]
b = (a + b) % 256
S[counter],S[b] = S[b],S[counter]
return S
def RC4_stream_generator(PlainBytes,S):
plainLen = len(PlainBytes)
cipherList = []
i = 0
j = 0
for m in range(plainLen):
i = (i + 1) % 256
j = (j + S[i]) % 256
S[i], S[j] = S[j], S[i]
k = S[(S[i] + S[j]) % 256]
cipherList.append(k ^ PlainBytes[m])
return cipherList
def print_out(msg,errorlevel=0):
if not DECRYPT_ONLY:
print(msg)
if errorlevel == FATAL_ERROR:
exit(0)
def check_filetype(filename):
IMAGE_FILE_EXECUTABLE_IMAGE = 0x0002
IMAGE_FILE_DLL = 0x2000
try:
file_type = magic.from_file(filename)
if VERBOSE:
print_out("Filetype for %s:\n%s" % (filename,file_type))
if "PE32 executable" in file_type:
pe=pefile.PE(filename)
if (pe.FILE_HEADER.Characteristics & IMAGE_FILE_DLL):
if VERBOSE:
print_out("Subtype is: DLL\n")
return("PE32 DLL")
elif (pe.FILE_HEADER.Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE):
if VERBOSE:
print_out("Subtype is: EXE\n")
return("PE32 EXE")
except:
print_out("ERROR: Could not open file %s" % filename, FATAL_ERROR)
raise
return None
def check_version(remcos_binary_name):
printable = set(string.printable)
with open(remcos_binary_name, 'rb') as myfile:
fcontent=myfile.read()
s=""
slist=[]
# find strings in binary file
for c in fcontent:
if len(s) > 4 and ord(c) == 0: # no strings <= 4
slist.append(s)
s=""
continue
if c in printable:
s += c
version_found = False
# find and extract version string e.g. "2.0.5 Pro" or "1.7 Free"
for s in slist:
if bool(re.search('^[12]\.\d+\d{0,1}.*[FP].*', s)):
print_out("%s is version %s\n" % (remcos_binary_name,s))
version_found = True
break
if not version_found:
print_out("ERROR: %s no version found\n" % remcos_binary_name, FATAL_ERROR)
def usage(prg):
print
print("################################################################")
print("# Talos Decryptor POC for Remcos RAT version 2.0.5 and earlier #")
print("################################################################")
print
print("%s -f <remcos_executable_file> [-e <encrypted_data_file>] [-d] [-v] [-c] [-r]" % prg)
print
print("-f [--file] <remcos_executable_file> Remcos executable file")
print("-e [--encypted_data] <encrypted_data_file> Encrypted data file (optional)")
print("-d [--decrypted_only] Show only decrypted data strings (optional)")
print(" (-d is suppressing all error msg!)")
print("-c [--c2_only] Show only extracted C2 data (optional)")
print("-v [--verbose] Verbose output (optional)")
print("-r [--remcos_version] Print Remcos version info")
print("-k [--key] Provide key as string e.g. -k password")
print
print("e.g. %s -f Remcos205.exe -d" % prg)
print
print("Disclaimer: This tool comes without any warranties. Use it at your own risk.\n")
# --------------------- Main -------------------------
def main(argv):
global VERBOSE
global DECRYPT_ONLY
global C2_ONLY
REMCOS_VERSION_CHECK = False
key_via_cmd = None
if len(sys.argv) < 3:
usage(sys.argv[0])
exit(1)
pefilename = None
encrypted_data_file = None
try:
opts, args = getopt.getopt(argv,"hve:df:crk:",["help","verbose","encypted_data","decrypted_only","file","c2_only","remcos_version","key"])
except getopt.GetoptError:
print_out("\nWrong parameter. Check syntax:")
usage(sys.argv[0])
exit(1)
for opt, arg in opts:
if opt in ("-h", "--help"):
usage(sys.argv[0])
exit(0)
elif opt in ("-v", "--verbose"):
VERBOSE = True
elif opt in ("-e", "--encypted_data"):
encrypted_data_file = arg
elif opt in ("-d", "--decrypted_only"):
DECRYPT_ONLY = True
elif opt in ("-c", "--c2_only"):
C2_ONLY = True
DECRYPT_ONLY = True
elif opt in ("-r", "--remcos_version"):
REMCOS_VERSION_CHECK = True
elif opt in ("-k", "--key"):
key_via_cmd = arg
elif opt in ("-f", "--file"):
pefilename = arg
if not DECRYPT_ONLY:
print
if not pefilename:
usage(sys.argv[0]);
exit(1)
filetype = check_filetype(pefilename)
if filetype != "PE32 EXE":
print_out("ERROR: File %s is not a PE executable or it is a DLL. Try -v to see what it is." % pefilename, FATAL_ERROR)
if REMCOS_VERSION_CHECK:
check_version(pefilename)
exit(0)
print_out("Analysing file: %s\n" % pefilename)
try:
# Get data from the PE resource section
ResourceData = get_named_resource_from_PE(pefilename,"SETTINGS")
# Extact the key from the PE resource section data or get it from the cmd line
if key_via_cmd:
key = map(ord,key_via_cmd)
keylen = len(key)
else:
keylen = ord(ResourceData[0])
key = map(ord, list(ResourceData[1:keylen+1]))
if not DECRYPT_ONLY and VERBOSE:
print_hexdata("Key:",key,0x0)
except:
print_out("ERROR: File %s. Could not extract data from PE resource, maybe not a remcos file or it is packed?\n" % pefilename,FATAL_ERROR)
# Do we have the encryted data in a file or should we get it from the resource section
try:
if encrypted_data_file != None:
with open(encrypted_data_file, mode='rb') as file:
encrypted_str = file.read()
else:
encrypted_str = ResourceData[keylen+1:]
except:
print_out("ERROR: Could not read encrypted file %s or file has wrong format\n" % encrypted_data_file,FATAL_ERROR)
# Convert it into an list
encrypted = map(ord, list(encrypted_str))
if not DECRYPT_ONLY and VERBOSE:
print_hexdata("Encrypted data:",encrypted,0x0)
# Generate S
S = RC4_build_S_array(key,keylen)
if not DECRYPT_ONLY and VERBOSE:
print_hexdata("Generated S array:",S,0x0)
# Decode the encrypted data
clear_text = RC4_stream_generator(encrypted,S)
if DECRYPT_ONLY and not C2_ONLY:
print_hexdata_str(clear_text)
elif not DECRYPT_ONLY and not C2_ONLY:
print_hexdata("\nDecrypted data:",clear_text,0x0)
if C2_ONLY:
C2 = get_C2(clear_text)
print("C2 server for %s" % pefilename)
if C2 == None:
print("ERROR: C2 not decoded")
exit(1)
for C2_server in C2:
print("%s" % C2_server)
print
exit(0)
if __name__ == "__main__":
main(sys.argv[1:])