File size: 7,514 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
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
from struct import unpack, pack
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
    from Crypto.Protocol.KDF import PBKDF2
    has_crypto = True
except ImportError:
    has_crypto = False

asyncrat_sig = {
    'namespace1': 'rule asyncrat { \
                    strings: \
                        $salt = {BF EB 1E 56 FB CD 97 3B B2 19 02 24 30 A5 78 43 00 3D 56 44 D2 1E 62 B9 D4 F1 80 E7 E6 C3 39 41}\
                        $b1 = {00 00 00 0D 53 00 48 00 41 00 32 00 35 00 36 00 00}\
                        $b2 = {09 50 00 6F 00 6E 00 67 00 00}\
                        $s1 = "pastebin" ascii wide nocase \
                        $s2 = "pong" wide\
                        $s3 = "Stub.exe" ascii wide\
                    condition:  ($salt and (2 of ($s*) or 1 of ($b*))) or (all of ($b*) and 2 of ($s*)) }'
}

CONFIG_PATTERNS = [
    b"\x00\x00\x00\x0D\x53\x00\x48\x00\x41\x00\x32\x00\x35\x00\x36\x00\x00"]

## format  "index" : ("position_in_storage_stream","field_name","encryption_method")
config_index = {
    1: (2,"Server", "aes"),
    2: (1,"Ports", "aes"),
    3: (3,"Version", "aes"),
    4: (4,"Autorun", "aes"),
    5: (5,"Install_Folder", ""),
    6: (6,"Install_File", "aes"),
    7: (7,"AES_key", "base64"),
    8: (8,"Mutex", "aes"),
    9: (11,"AntiDetection", "aes"),
    10: (12,"External_config_on_Pastebin", "aes"),
    11: (13,"BDOS", "aes"),
    12: (14,"HWID", ""),
    13: (15,"Startup_Delay", ""),
    14: (9,"Certificate", "aes"),
    15: (10,"ServerSignature", "aes")
}


class asyncratConfig(taskmods.DllList):
    """Parse the asyncrat 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 printable(self, data):
        if len(data) < 1:
            return data
        cleaned = ""

        for d in data:
            if 0x20 <= ord(d) and ord(d) <= 0x7F:
                cleaned += d

        return cleaned

    def storage_stream_us_parser(self, data):
        """
        parse storage_stream for unicode strings in .NET assembly.
        unicode_strings chunk patterns
            pat1: [size of unicode strings(1byte)][unicode strings][terminate code(0x00 or 0x01)]
            pat2: [size of unicode strings(2byte)][unicode strings][terminate code(0x00 or 0x01)]
        """
        if len(data) < 2:
            return list()
        unicode_strings = list()

        while True:
            # first byte must be the size of unicode strings chunk.
            initial_byte = ord(data[0])
            if initial_byte == 0x00:
                break
            elif initial_byte < 0x80:
                size = initial_byte
                p = 1
            elif initial_byte >= 0x80:
                size = unpack(">H",pack("B",initial_byte-0x80)+data[1])[0]
                # size = int.from_bytes(bytes([data[0]-0x80, data[1]]), "big")
                p = 2

            if size < 0 or 0x7FFF < size or size > len(data)-3:
                debug.info("Invalid string size found in stroage stream.")
                break
            try:
                unicode_strings.append(
                    data[p:size + p - 1].decode().replace("\x00", ""))
            except UnicodeDecodeError:
                debug.info("Invalid unicode byte(s) found in storage stream.")
                pass
            # check the termination code.
            termination_byte = ord(data[size + p - 1])
            if termination_byte == 0x00 or termination_byte == 0x01:
                # goto next block
                data = data[size + p:]
                continue
            else:
                debug.info("Invalid termination code: {}".format(termination_byte))
                break

        return unicode_strings

    def parse_config(self, unicode_strings):

        if len(unicode_strings) < 7:
            debug.info("unicode strings list is too short.")
            return OrderedDict()

        config = OrderedDict()

        key = b64decode(unicode_strings[7])
        salt = "BFEB1E56FBCD973BB219022430A57843003D5644D21E62B9D4F180E7E6C33941".decode("hex")
        aes_key = PBKDF2(key, salt, 32, 50000)

        for _ , params in config_index.items():
            pos, field, enc_type = params
            if enc_type == "aes" and len(unicode_strings[pos]) > 48:
                enc_data = b64decode(unicode_strings[pos])
                # hmac = enc_data[:32]
                aes_iv = enc_data[32:48]
                cipher = AES.new(aes_key, AES.MODE_CBC, aes_iv)
                value = self.printable(cipher.decrypt(enc_data[48:]))
            elif enc_type == "base64":
                value = self.printable(b64decode(unicode_strings[pos]))
            else:
                value = unicode_strings[pos]
            config[field] = value
        return config

    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")

        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=asyncrat_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 = []
                dlist = OrderedDict()

                for pattern in CONFIG_PATTERNS:
                    m = data.find(pattern)
                    if m > 0:
                        unicode_strings = self.storage_stream_us_parser(
                            data[m + 3:])
                        dlist = self.parse_config(unicode_strings)
                        break
                    else:
                        debug.info(
                            "Asyncrat configuration signature not found.")

                config_data.append(dlist)

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