File size: 7,637 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
# Detecting QuasarRAT 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 quasarscan.py volatility/plugins/malware
# 3. python vol.py quasarconfig -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 hashlib
from base64 import b64decode
from collections import OrderedDict

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

try:
    from Crypto.Cipher import AES
    has_crypto = True
except ImportError:
    has_crypto = False

try:
    from pbkdf2 import PBKDF2
    has_pbkdf2 = True
except ImportError:
    has_pbkdf2 = False

quasar_sig = {
    'namespace1' : 'rule Quasar { \
                    strings: \
                       $quasarstr1 = "Client.exe" wide \
                       $quasarstr2 = "({0}:{1}:{2})" wide \
                       $sql1 = "SELECT * FROM Win32_DisplayConfiguration" wide \
                       $sql2 = "{0}d : {1}h : {2}m : {3}s" wide \
                       $sql3 = "SELECT * FROM FirewallProduct" wide \
                       $net1 = "echo DONT CLOSE THIS WINDOW!" wide \
                       $net2 = "freegeoip.net/xml/" wide \
                       $net3 = "http://api.ipify.org/" wide \
                       $resource = { 52 00 65 00 73 00 6F 00 75 00 72 00 63 00 65 00 73 00 00 17 69 00 6E 00 66 00 6F 00 72 00 6D 00 61 00 74 00 69 00 6F 00 6E 00 00 } \
                    condition: ((all of ($quasarstr*) or all of ($sql*)) and $resource) or all of ($net*)}'
}

# Config pattern
CONFIG_PATTERNS = [re.compile("\x52\x00\x65\x00\x73\x00\x6F\x00\x75\x00\x72\x00\x63\x00\x65\x00\x73\x00\x00\x17\x69\x00\x6E\x00\x66\x00\x6F\x00\x72\x00\x6D\x00\x61\x00\x74\x00\x69\x00\x6F\x00\x6E\x00\x00\x80", re.DOTALL),
                   re.compile("\x61\x00\x70\x00\x69\x00\x2E\x00\x69\x00\x70\x00\x69\x00\x66\x00\x79\x00\x2E\x00\x6F\x00\x72\x00\x67\x00\x2F\x00\x00\x03\x5C\x00\x00", re.DOTALL),
                   re.compile("\x3C\x00\x2F\x00\x73\x00\x74\x00\x79\x00\x6C\x00\x65\x00\x3E\x00\x00\x03\x5C\x00\x00\x80", re.DOTALL)]

idx_list = {
    0:  ["VERSION", True],
    1:  ["HOSTS", True],
    2:  ["KEY (Base64)", False],
    3:  ["AUTHKEY (Base64)", False],
    4:  ["SUBDIRECTORY", True],
    5:  ["INSTALLNAME",True],
    6:  ["MUTEX", True],
    7:  ["STARTUPKEY", True],
    8:  ["ENCRYPTIONKEY", False],
    9:  ["TAG", True],
    10: ["LOGDIRECTORYNAME",True ],
    11: ["unknown1", True],
    12: ["unknown2", True]
}

idx_list_2 = {
    0:  ["VERSION", True],
    1:  ["HOSTS", True],
    2:  ["KEY (Base64)", False],
    3:  ["SUBDIRECTORY", True],
    4:  ["INSTALLNAME",True],
    5:  ["MUTEX", True],
    6:  ["STARTUPKEY", True],
    7:  ["ENCRYPTIONKEY", False],
    8:  ["TAG", True]
}


class quasarConfig(taskmods.DllList):
    """Parse the QuasarRAT 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

    def decrypt_string(self, key, configs, mode, idx):
        p_data = OrderedDict()
        for i, config in enumerate(configs):
            if idx[i][1] == True:
                if len(configs) < 10:
                    config = b64decode(config)
                    aes_iv = config[:16]
                    cipher = AES.new(key, mode, IV=aes_iv)
                    value = re.sub("[\x00-\x19]" ,"" , cipher.decrypt(config[16:]))
                else:
                    config = b64decode(config)
                    aes_iv = config[32:48]
                    cipher = AES.new(key, mode, IV=aes_iv)
                    value = re.sub("[\x00-\x19]" ,"" , cipher.decrypt(config[48:]))
            else:
                value = config
            p_data[idx[i][0]] = value

        return p_data

    def parse_config(self, configs):
        if len(configs) > 10:
            idx = idx_list
            key, salt = configs[8], 'BFEB1E56FBCD973BB219022430A57843003D5644D21E62B9D4F180E7E6C33941'.decode('hex')

            generator = PBKDF2(key, salt, 50000)
            aes_key = generator.read(16)
        else:
            idx = idx_list_2
            aes_key = hashlib.md5(configs[7]).digest()

        if(len(configs) > 12):
            mode = AES.MODE_CFB
        else:
            mode = AES.MODE_CBC
        p_data = self.decrypt_string(aes_key, configs, mode, idx)

        return p_data

    def calculate(self):

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

        if not has_crypto:
            debug.error("pycrypto must be installed for this plugin")

        if not has_pbkdf2:
            debug.error("pbkdf2 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=quasar_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 = []

                offset = 0
                for pattern in CONFIG_PATTERNS:
                    mc = re.search(pattern, data)
                    if mc:
                        offset = mc.end()

                        if ord(data[offset]) == 0x0:
                            offset += 1

                configs = []
                if offset > 0:
                    while 1:
                        strings = []
                        string_len = ord(data[offset])
                        if ord(data[offset]) == 0x80 or ord(data[offset]) == 0x81:
                            string_len = ord(data[offset + 1]) + ((ord(data[offset]) - 0x80) * 256)
                            offset += 1

                        offset += 1
                        for i in range(string_len):
                            if data[offset + i] != "\x00":
                                strings.append(data[offset + i])
                        configs.append("".join(strings))
                        offset = offset + string_len

                        if ord(data[offset]) < 0x20:
                            break

                    config_data.append(self.parse_config(configs))

                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:<21}: {1}\n".format(id, param))