File size: 10,079 Bytes
8377638
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Detecting Datper for Volatility
#
# LICENSE
# Please refer to the LICENSE.txt in the https://github.com/JPCERTCC/MalConfScan/
#
# How to use:
# 1. cd "Volatility Folder"
# 2. mv datperscan.py volatility/plugins/malware
# 3. python vol.py datperconfig -f images.mem --profile=Win7SP1x64

import volatility.plugins.taskmods as taskmods
import volatility.win32.tasks as tasks
import volatility.utils as utils
import volatility.debug as debug
import volatility.plugins.malware.malfind as malfind
import re
import pefile
from struct import unpack, unpack_from
from collections import OrderedDict

try:
    import yara
    has_yara = True
except ImportError:
    has_yara = False

datper_sig = {
    'namespace1' : 'rule Datper { \
                    strings: \
                       $a1 = { E8 03 00 00 } \
                       $b1 = "|||" \
                       $c1 = "Content-Type: application/x-www-form-urlencoded" \
                       $delphi = "Borland\\Delphi" ascii wide \
                       $push7530h64 = { C7 C1 30 75 00 00 } \
                       $push7530h = { 68 30 75 00 00 } \
                    condition: $a1 and $b1 and $c1 and $delphi and ($push7530h64 or $push7530h)}'
}

CONFIG_PATTERNS = [
    re.compile("\xB8(....)(\xBA\xE8\x03\x00\x00)", re.DOTALL),             # mov eax, qword ptr config_offset;mov edx 0x3e8
    re.compile("\xB8(....)(\x75\x00\xBA\xE8\x03\x00\x00)", re.DOTALL),     # mov eax, qword ptr config_offset;jnz short $+2;mov edx 0x3e8
    re.compile("\x48\x8D\x0D(....)(\xC7\xC2\xE8\x03\x00\x00)", re.DOTALL)  # lea rax, qword ptr config_offset;mov edx 0x3e8
]

RC4KEY = ["d4n[6h}8o<09,d(21i`t4n$]hx%.h,hd",
          "B3uT16@qs\l,!GdSevH=Y(;7Ady$jl\e",
          "V7oT1@@qr\\t,!GOSKvb=p(;3Akb$rl\\a"
          ]

idx_list = {
    0: "ID",
    1: "URL",
    2: "Sleep time(s)",
    3: "Mutex",
    4: "Proxy server",
    5: "Proxy port",
    6: "Unknown",
    7: "Unknown",
    8: "Startup time(h)",
    9: "End time(h)",
    10: "Unknown",
    11: "User-Agent",
    12: "RSA key(e + modules)"
}

CONFSIZE = 0x3F8
config_delimiter = ["|||", "[|-]"]


class datperConfig(taskmods.DllList):
    """Parse the Datper configuration"""

    @staticmethod
    def is_valid_profile(profile):
        return (profile.metadata.get('os', 'unknown') == 'windows'), profile.metadata.get('memory_model', '32bit')

    def get_vad_base(self, task, address):
        for vad in task.VadRoot.traverse():
            if address >= vad.Start and address < vad.End:
                return vad.Start, vad.End

        return None

    # Custom RC4 use sbox seed
    def custom_rc4(self, data, key, box_seed):
        x = 0
        box = range(256)
        if box_seed != 0:
            for i in range(256):
                box[i] = (i + box_seed) & 0xFF

        for i in range(256):
            x = (x + box[i] + ord(key[i % len(key)])) % 256
            box[i], box[x] = box[x], box[i]
        x = 0
        y = 0
        out = []
        for char in data:
            x = (x + 1) % 256
            y = (y + box[x]) % 256
            box[x], box[y] = box[y], box[x]
            out.append(chr(ord(char) ^ box[(box[x] + box[y]) % 256]))

        return ''.join(out)

    def get_config_data_64(self, data, pe):
        for pattern in CONFIG_PATTERNS:
            m = re.search(pattern, data)
            if m:
                #print("found pattern")
                rva_offset_config = pe.get_rva_from_offset(m.start(2)) + unpack("<L", data[m.start(1):m.start(1) + 4])[0]
                config = pe.get_memory_mapped_image()[rva_offset_config:rva_offset_config + CONFSIZE]
                return config
        return None

    def get_config_data_32(self, data, pe, start):
        for pattern in CONFIG_PATTERNS:
            m = re.search(pattern, data)
            if m:
                #print("found pattern")
                rva_offset_config = unpack("<L", data[m.start(1):m.start(1) + 4])[0]
                rva_config = rva_offset_config - start
                config = data[rva_config:rva_config + CONFSIZE]
                return config
        return None

    def decompress(self, data):
        if ord(data[0]) == 0x80:
            return data[1:]
        length = unpack("<H", data[:2])[0]
        if length > len(data[2:]):
            print("[!] invalid length")
            return ""
        data = data[2:2 + length]
        tmp = ""
        for i, c in enumerate(data):
            val = (((i >> 5) + (i << 7) + length + ~i) & 0xFF)
            tmp += chr(ord(c) ^ (((i >> 5) + (i << 7) + length + ~i) & 0xFF))

        tmp = map(ord, list(tmp))[1:]
        i = 0
        block_len = 16
        dec = ""
        try:
            while i < len(tmp):
                if block_len == 16:
                    block_flag = (tmp[i] << 8) + tmp[i + 1]
                    block_len = 0
                    i += 2

                if block_flag & (0x8000 >> block_len) != 0:
                    char_flag = (tmp[i + 1] >> 4) + (16 * tmp[i])
                    if char_flag != 0:
                        loop_count = (tmp[i + 1] & 0xF) + 3
                        for n in range(loop_count):
                            dec += dec[-char_flag]
                        i += 2
                    else:
                        loop_count = (tmp[i + 1] << 8) + tmp[i + 2] + 16
                        for n in range(loop_count):
                            #data += chr(tmp[i + 3])
                            pass
                        i += 4
                else:
                    dec += chr(tmp[i])
                    i += 1

                block_len += 1
        except:
            raise
            return ""
        return dec

    def decrypt(self, dec):
        decrypted_len = len(dec)
        decomp = []
        processed_len = 0
        while (decrypted_len > processed_len):
            enc_compressed_len = unpack("<H", dec[processed_len:processed_len + 2])[0]
            enc_compressed = dec[processed_len + 2:processed_len + 2 + enc_compressed_len]
            if enc_compressed_len > len(enc_compressed):
                break
            processed_len += enc_compressed_len + 2
            tmp = []
            for i in range(enc_compressed_len):
                xor_key = (i >> 5) & 0xff
                xor_key += (i << 7) & 0xff
                xor_key += enc_compressed_len
                xor_key += ~i
                xor_key = xor_key & 0xff
                tmp.append(chr(ord(enc_compressed[i]) ^ xor_key))
            compressed = "".join(tmp)
            decompressed = self.decompress(compressed)
            decomp.append(decompressed)
        return "".join(decomp)

    def calculate(self):

        if not has_yara:
            debug.error("Yara must be installed for this plugin")

        addr_space = utils.load_as(self._config)

        os, memory_model = self.is_valid_profile(addr_space.profile)
        if not os:
            debug.error("This command does not support the selected profile.")

        rules = yara.compile(sources=datper_sig)

        for task in self.filter_tasks(tasks.pslist(addr_space)):
            scanner = malfind.VadYaraScanner(task=task, rules=rules)

            for hit, address in scanner.scan():

                vad_base_addr, end = self.get_vad_base(task, address)
                proc_addr_space = task.get_process_address_space()
                data = proc_addr_space.zread(vad_base_addr, end - vad_base_addr)

                config_data = []

                try:
                    pe = pefile.PE(data=data)
                except:
                    # print("[!] could not parse as a PE file")
                    break

                config_size = CONFSIZE

                if pe.FILE_HEADER.Machine in (pefile.MACHINE_TYPE['IMAGE_FILE_MACHINE_IA64'], pefile.MACHINE_TYPE['IMAGE_FILE_MACHINE_AMD64']):
                    enc = self.get_config_data_64(data, pe)
                else:
                    enc = self.get_config_data_32(data, pe, vad_base_addr)

                dec = ""
                for key in RC4KEY:
                    for rc4key_seed in range(0xFF):
                        dec = self.custom_rc4(enc, key, rc4key_seed)
                        dec = self.decrypt(dec)
                        for dline in config_delimiter:
                            if dline in dec:
                                break
                        else:
                            continue
                        break
                    else:
                        continue
                    break

                if dec == "":
                    dec = self.decrypt(enc)
                    for dline in config_delimiter:
                        if dline in dec:
                            key = "NULL"
                            rc4key_seed = "NULL"
                            break

                p_data = OrderedDict()
                if dec != "":
                    p_data["RC4 key"]          = key
                    p_data["RC4 Sbox seed"]    = rc4key_seed
                    p_data["Config delimiter"] = dline
                    idx = 0
                    for e in (dec.split(dline)):
                        try:
                            p_data[idx_list[idx]] = e
                        except:
                            p_data["Unknown " + str(idx)] = e
                        idx += 1
                else:
                    outfd.write("[!] failed to decrypt\n")

                config_data.append(p_data)

                yield task, vad_base_addr, end, hit, memory_model, config_data
                break

    def render_text(self, outfd, data):

        delim = '-' * 70

        for task, start, end, malname, memory_model, config_data in data:
            outfd.write("{0}\n".format(delim))
            outfd.write("Process: {0} ({1})\n\n".format(task.ImageFileName, task.UniqueProcessId))

            outfd.write("[Config Info]\n")
            for p_data in config_data:
                for id, param in p_data.items():
                    outfd.write("{0:<16}: {1}\n".format(id, param))