|
| 1 | +import logging |
| 2 | +from Crypto.Cipher import ARC4 |
| 3 | +from ipaddress import ip_address |
| 4 | + |
| 5 | +log = logging.getLogger(__name__) |
| 6 | + |
| 7 | + |
| 8 | +def _chunk_stuff(stuff, group_size=20): |
| 9 | + # really just need to chunk out the ip into groups of....20? |
| 10 | + # https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks |
| 11 | + for i in range(0, len(stuff), group_size): |
| 12 | + yield ','.join(stuff[i:i + group_size]) |
| 13 | + |
| 14 | + |
| 15 | +def _build_rc4_rule(passphrase): |
| 16 | + hex_plain_text = "5b4461746153746172745d00000000" |
| 17 | + |
| 18 | + cipher = ARC4.new(passphrase) |
| 19 | + value = bytes.fromhex(hex_plain_text) |
| 20 | + enc_value = cipher.encrypt(value) |
| 21 | + |
| 22 | + # conver the encrypted form if the plain text to a hex string |
| 23 | + enc_hex_value = enc_value.hex() |
| 24 | + |
| 25 | + first_value = "" |
| 26 | + second_value = "" |
| 27 | + # split the first part into two char groups as hex, we need these for the rules |
| 28 | + # skip over the last 8 bytes |
| 29 | + for i in range(0, len(enc_hex_value) - 8, 2): |
| 30 | + first_value += f"{enc_hex_value[i:i + 2]} " |
| 31 | + |
| 32 | + # only take the last 4 bytes |
| 33 | + for i in range(len(enc_hex_value) - 4, len(enc_hex_value), 2): |
| 34 | + second_value += f"{enc_hex_value[i:i + 2]} " |
| 35 | + |
| 36 | + return first_value.rstrip(), second_value.rstrip() |
| 37 | + |
| 38 | + |
| 39 | +def _parse_mwcp(remcos_config): |
| 40 | + remcos_config_list = [] |
| 41 | + control = remcos_config.get('control', []) |
| 42 | + for c in control: |
| 43 | + if c and c.startswith("tcp://"): |
| 44 | + # maxsplit here incase the passphrase includes : |
| 45 | + tmp = c.replace("tcp://", "").split(":", maxsplit=2) |
| 46 | + if tmp: |
| 47 | + # if we don't have a password, just add a blank one, |
| 48 | + if len(tmp) == 2: |
| 49 | + remcos_config_list.append( |
| 50 | + {"Version": remcos_config.get("version", ""), "C2": tmp[0], "Port": tmp[1], "Password": ""} |
| 51 | + ) |
| 52 | + elif len(tmp) == 3 and tmp[2] != "": |
| 53 | + # we can include the passprhase |
| 54 | + remcos_config_list.append( |
| 55 | + {"Version": remcos_config.get("version", ""), "C2": tmp[0], "Port": tmp[1], "Password": tmp[2]} |
| 56 | + ) |
| 57 | + else: |
| 58 | + log.debug(f"[CENTS - Remcos] MWCP config - found to be invalid --> {c}") |
| 59 | + |
| 60 | + return remcos_config_list |
| 61 | + |
| 62 | + |
| 63 | +def _parse_ratdecoders(remcos_config): |
| 64 | + domains = remcos_config.get("domains", []) |
| 65 | + remcos_config_list = [] |
| 66 | + for domain in domains: |
| 67 | + # why is this a list of lists |
| 68 | + for nested_domain in domain: |
| 69 | + remcos_config_list.append( |
| 70 | + # notice the typo here including the colon after c2: |
| 71 | + # https://github.com/kevthehermit/RATDecoders/blob/master/malwareconfig/decoders/remcos.py#L56 |
| 72 | + {"C2": nested_domain.get("c2:", ""), |
| 73 | + "Port": nested_domain.get("port", ""), |
| 74 | + "Password": nested_domain.get("password", ""), |
| 75 | + } |
| 76 | + ) |
| 77 | + |
| 78 | + return remcos_config_list |
| 79 | + |
| 80 | + |
| 81 | +def cents_remcos(config_dict, sid_counter, md5, date, task_link): |
| 82 | + """Creates Suricata rules from extracted Remcos malware configuration. |
| 83 | +
|
| 84 | + :param config_dict: Dictionary with the extracted Remcos configuration. |
| 85 | + :type config_dict: `dict` |
| 86 | +
|
| 87 | + :param sid_counter: Signature ID of the next Suricata rule. |
| 88 | + :type sid_counter: `int` |
| 89 | +
|
| 90 | + :param md5: MD5 hash of the source sample. |
| 91 | + :type md5: `int` |
| 92 | +
|
| 93 | + :param date: Timestamp of the analysis run of the source sample. |
| 94 | + :type date: `str` |
| 95 | +
|
| 96 | + :param task_link: Link to analysis task of the source sample. |
| 97 | + :type task_link: `str` |
| 98 | +
|
| 99 | + :return List of Suricata rules (`str`) or empty list if no rule has been created. |
| 100 | + """ |
| 101 | + if not config_dict or not sid_counter or not md5 or not date or not task_link: |
| 102 | + return [] |
| 103 | + |
| 104 | + next_sid = sid_counter |
| 105 | + # build out an array to store the parsed configs |
| 106 | + remcos_config_list = [] |
| 107 | + |
| 108 | + # lowercase the key names in the configs for constancy |
| 109 | + remcos_config = dict((k.lower(), v) for k, v in config_dict.items()) |
| 110 | + |
| 111 | + if not remcos_config: |
| 112 | + return [] |
| 113 | + |
| 114 | + # there are two remcos parsers that could be at work here |
| 115 | + # 1) RATDecoders - https://github.com/kevthehermit/RATDecoders/blob/master/malwareconfig/decoders/remcos.py |
| 116 | + # which is an optional configuration that can be enabled in the processing.conf file |
| 117 | + # 2) MWCP - https://github.com/kevoreilly/CAPEv2/blob/master/modules/processing/parsers/mwcp/Remcos.py |
| 118 | + # which is an optional configuration that can be enabled in the processing.conf file |
| 119 | + if 'control' in remcos_config and 'domains' not in remcos_config: |
| 120 | + # we have an MWCP config |
| 121 | + log.debug("[CENTS - Remcos] Parsing DC3-MWCP based config") |
| 122 | + parsed_remcos_config = _parse_mwcp(remcos_config) |
| 123 | + for _config in parsed_remcos_config: |
| 124 | + if _config not in remcos_config_list: |
| 125 | + remcos_config_list.append(_config) |
| 126 | + |
| 127 | + if 'domains' in remcos_config and 'control' not in remcos_config: |
| 128 | + # we have a RATDecoders config |
| 129 | + log.debug("[CENTS - Remcos] Parsing RATDecoders based config") |
| 130 | + parsed_remcos_config = _parse_ratdecoders(remcos_config) |
| 131 | + for _config in parsed_remcos_config: |
| 132 | + if _config not in remcos_config_list: |
| 133 | + remcos_config_list.append(_config) |
| 134 | + |
| 135 | + # if we don't have a parsed config, drop out |
| 136 | + log.debug("[CENTS - Remcos] Done Parsing Config") |
| 137 | + if not remcos_config_list: |
| 138 | + log.debug("[CENTS - Remcos] No parsed configs found") |
| 139 | + return [] |
| 140 | + |
| 141 | + # Now we want to create Suricata rules finally |
| 142 | + rule_list = [] |
| 143 | + ip_list = set() |
| 144 | + domain_list = set() |
| 145 | + for c2_server in list(map(lambda x: x.get('C2'), remcos_config_list)): |
| 146 | + try: |
| 147 | + c2_ip = ip_address(c2_server) |
| 148 | + except ValueError: |
| 149 | + domain_list.add(c2_server) |
| 150 | + else: |
| 151 | + # only create rules for "global" ip addresses |
| 152 | + if c2_ip.is_global: |
| 153 | + ip_list.add(c2_server) |
| 154 | + else: |
| 155 | + log.debug("[CENTS - Remcos] Skipping c2 server due to non-routable ip") |
| 156 | + |
| 157 | + log.debug("[CENTS - Remcos] Building IP based rules") |
| 158 | + for ip_group in _chunk_stuff(list(ip_list)): |
| 159 | + rule = f"alert tcp $HOME_NET any -> {ip_group} any (msg:\"ET CENTS Remcos RAT (C2 IP Address) " \ |
| 160 | + f"C2 Communication - CAPE sandbox config extraction\"; flow:established,to_server; " \ |
| 161 | + f"reference:md5,{md5}; reference:url,{task_link}; sid:{next_sid}; rev:1; " \ |
| 162 | + f"metadata:created_at {date};)" |
| 163 | + rule_list.append(rule) |
| 164 | + next_sid += 1 |
| 165 | + |
| 166 | + log.debug("[CENTS - Remcos] Building Domain based rules") |
| 167 | + for c2_domain in domain_list: |
| 168 | + rule = f"alert dns $HOME_NET any -> any any (msg:\"ET CENTS Remcos RAT (C2 Domain) " \ |
| 169 | + f"C2 Communication - CAPE sandbox config extraction\"; flow:established,to_server; " \ |
| 170 | + f"dns.query; content:\"{c2_domain}\"; " \ |
| 171 | + f"reference:md5,{md5}; reference:url,{task_link}; sid:{next_sid}; rev:1; " \ |
| 172 | + f"metadata:created_at {date};)" |
| 173 | + rule_list.append(rule) |
| 174 | + next_sid += 1 |
| 175 | + |
| 176 | + log.debug("[CENTS - Remcos] Building Password based rules") |
| 177 | + for parsed_config in remcos_config_list: |
| 178 | + # if we have a password, we should create a rule for the RC4 encrypted stuff |
| 179 | + if parsed_config.get("Password", ""): |
| 180 | + first, second = _build_rc4_rule(parsed_config.get('Password')) |
| 181 | + rule = f"alert tcp $HOME_NET any -> $EXTERNAL_NET any (msg:\"ET CENTS Remcos RAT " \ |
| 182 | + f"(passphrase {parsed_config.get('Password')}) " \ |
| 183 | + f"C2 Communication - CAPE sandbox config extraction\"; flow:established,to_server; " \ |
| 184 | + f"content:\"|{first}|\"; startswith; fast_pattern; content:\"|{second}|\"; distance:2; within:2; " \ |
| 185 | + f"reference:md5,{md5}; reference:url,{task_link}; sid:{next_sid}; rev:1; " \ |
| 186 | + f"metadata:created_at {date};)" |
| 187 | + rule_list.append(rule) |
| 188 | + next_sid += 1 |
| 189 | + |
| 190 | + log.debug("[CENTS - Remcos] Returning built rules") |
| 191 | + return rule_list |
0 commit comments