File size: 8,681 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
# Detecting RedLeaves 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 redleavesscan.py volatility/plugins/malware
# 3. python vol.py redleavesconfig -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
from struct import unpack, unpack_from
from collections import OrderedDict

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

redleaves_sig = {
    'namespace1' : 'rule RedLeaves { \
                    strings: \
                       $v1 = "red_autumnal_leaves_dllmain.dll" \
                       $b1 = { FF FF 90 00 } \
                    condition: $v1 and $b1 at 0}',
    'namespace2' : 'rule Himawari { \
                    strings: \
                       $h1 = "himawariA" \
                       $h2 = "himawariB" \
                       $h3 = "HimawariDemo" \
                    condition: $h1 and $h2 and $h3}',
    'namespace3' : 'rule Lavender { \
                    strings: \
                       $l1 = {C7 ?? ?? 4C 41 56 45} \
                       $l2 = {C7 ?? ?? 4E 44 45 52} \
                    condition: $l1 and $l2}',
    'namespace4' : 'rule Armadill { \
                    strings: \
                       $a1 = {C7 ?? ?? 41 72 6D 61 } \
                       $a2 = {C7 ?? ?? 64 69 6C 6C } \
                    condition: $a1 and $a2}',
    'namespace5' : 'rule zark20rk { \
                    strings: \
                       $a1 = {C7 ?? ?? 7A 61 72 6B } \
                       $a2 = {C7 ?? ?? 32 30 72 6B } \
                    condition: $a1 and $a2}'
}

CONF_PATTERNS = {"RedLeaves": re.compile("\x68\x88\x13\x00\x00\xFF", re.DOTALL),
                 "Himawari": re.compile("\x68\x70\x03\x00\x00\xBF", re.DOTALL),
                 "Lavender": re.compile("\x68\x70\x03\x00\x00\xBF", re.DOTALL),
                 "Armadill": re.compile("\x68\x70\x03\x00\x00\xBF", re.DOTALL),
                 "zark20rk": re.compile("\x68\x70\x03\x00\x00\x8D", re.DOTALL),
                 }

CONNECT_MODE = {1: 'TCP', 2: 'HTTP', 3: 'HTTPS', 4: 'TCP and HTTP'}


class redleavesConfig(taskmods.DllList):
    """Detect processes infected with redleaves malware"""

    @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 parse_config(self, cfg_blob, cfg_sz, cfg_addr):

        p_data = OrderedDict()

        p_data["Server1"]           = unpack_from('<64s', cfg_blob, 0x0)[0]
        p_data["Server2"]           = unpack_from('<64s', cfg_blob, 0x40)[0]
        p_data["Server3"]           = unpack_from('<64s', cfg_blob, 0x80)[0]
        p_data["Port"]              = unpack_from('<I', cfg_blob, 0xC0)[0]
        mode = unpack_from('<I', cfg_blob, 0x1D0)[0]
        p_data["Mode"]              = CONNECT_MODE[mode]
        p_data["ID"]                = unpack_from('<64s', cfg_blob, 0x1E4)[0]
        p_data["Mutex"]             = unpack_from('<550s', cfg_blob, 0x500)[0].replace('\0', '')
        p_data["Injection Process"] = unpack_from('<104s', cfg_blob, 0x726)[0].replace('\0', '')
        p_data["RC4 Key"]           = unpack_from('<10s', cfg_blob, 0x82A)[0]

        return p_data

    def parse_config_himawari(self, cfg_blob, cfg_sz, cfg_addr):

        p_data = OrderedDict()

        p_data["Server1"]        = unpack_from('<64s', cfg_blob, 0x4)[0]
        p_data["Server2"]        = unpack_from('<64s', cfg_blob, 0x44)[0]
        p_data["Server3"]        = unpack_from('<64s', cfg_blob, 0x84)[0]
        p_data["Server4"]        = unpack_from('<64s', cfg_blob, 0xC4)[0]
        p_data["Port"]           = unpack_from('<I', cfg_blob, 0x104)[0]
        mode = unpack_from('<I', cfg_blob, 0x1D8)[0]
        p_data["Mode"]           = CONNECT_MODE[mode]
        p_data["ID"]             = unpack_from('<64s', cfg_blob, 0x1E0)[0]
        p_data["Mutex"]          = unpack_from('<62s', cfg_blob, 0x224)[0].replace('\0', '')
        p_data["Key"]            = unpack_from('<10s', cfg_blob, 0x366)[0]
        p_data["UserAgent"]      = unpack_from('<260s', cfg_blob, 0x262)[0]
        p_data["Proxy server"]   = unpack_from('<64s', cfg_blob, 0x10C)[0]
        p_data["Proxy username"] = unpack_from('<64s', cfg_blob, 0x14C)[0]
        p_data["Proxy password"] = unpack_from('<64s', cfg_blob, 0x18C)[0]

        return p_data

    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=redleaves_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 = []

                c_pt = CONF_PATTERNS[str(hit)]
                if re.search(c_pt, data):
                    m_conf = re.search(c_pt, data)
                else:
                    continue

                offset_conf = m_conf.start()

                if "RedLeaves" in str(hit):
                    config_size = 2100

                    offset_conf -= 1
                    while data[offset_conf] != "\xC7" and data[offset_conf] != "\xBE" and data[offset_conf] != "\xBF":
                        offset_conf -= 1

                    if data[offset_conf] != "\xC7" and data[offset_conf] != "\xBE" and data[offset_conf] != "\xBF":
                        continue
                    if data[offset_conf] == "\xC7" and data[offset_conf + 1] != "\x85" and data[offset_conf + 1] != "\x45":
                        offset_conf -= 6

                    # get address
                    if data[offset_conf] == "\xC7" and data[offset_conf + 1] != "\x85":
                        (config_addr, ) = unpack("=I", data[offset_conf + 3:offset_conf + 7])
                    elif data[offset_conf] == "\xC7" and data[offset_conf + 1] == "\x85":
                        (config_addr, ) = unpack("=I", data[offset_conf + 6:offset_conf + 10])
                    else:
                        (config_addr, ) = unpack("=I", data[offset_conf + 1:offset_conf + 5])

                    if config_addr < vad_base_addr:
                        continue
                    config_addr -= vad_base_addr
                    config = data[config_addr:config_addr + config_size]
                    if len(config) > 0:
                        config_data.append(self.parse_config(config, config_size, config_addr))

                if str(hit) in ["Himawari", "Lavender", "Armadill", "zark20rk"]:
                    offset_conf += 6
                    if str(hit) in ["zark20rk"]:
                        offset_conf += 6
                    config_size = 880

                    # get address
                    (config_addr, ) = unpack("=I", data[offset_conf:offset_conf + 4])

                    if config_addr < vad_base_addr:
                        continue

                    config_addr -= vad_base_addr
                    config = data[config_addr:config_addr + config_size]
                    if len(config) > 0:
                        config_data.append(self.parse_config_himawari(config, config_size, config_addr))

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