|
| 1 | +#!/usr/bin/env python |
| 2 | +from builtins import str as unicode |
| 3 | + |
| 4 | +import re |
| 5 | + |
| 6 | + |
| 7 | +class ExtractionError(Exception): |
| 8 | + pass |
| 9 | + |
| 10 | + |
| 11 | +class Extractor: |
| 12 | + """ |
| 13 | + The extractor class tries to detect ioc attribute types using regex-matching. Two functions are provided: |
| 14 | + - ``check_string(str)`` which checks a string for a regex match and just returns the type |
| 15 | + - ``check_iterable(itr)`` that iterates over a list or a dictionary and returns a list of {type, value} dicts |
| 16 | +
|
| 17 | + Currently, this is not a fulltext search, so the the ioc's must be isolated strings, to get found. |
| 18 | + This can be iterated for ioc's. |
| 19 | +
|
| 20 | + :param ignore: List of strings or a single string to ignore when matching artifacts to type |
| 21 | + :type ignore: list, str |
| 22 | + """ |
| 23 | + |
| 24 | + def __init__(self, ignore=None): |
| 25 | + self.ignore = ignore |
| 26 | + self.regex = self.__init_regex() |
| 27 | + |
| 28 | + @staticmethod |
| 29 | + def __init_regex(): |
| 30 | + """ |
| 31 | + Returns compiled regex list. |
| 32 | +
|
| 33 | + :return: List of {type, regex} dicts |
| 34 | + :rtype: list |
| 35 | + """ |
| 36 | + |
| 37 | + # IPv4 |
| 38 | + regex = [{ |
| 39 | + 'type': 'ip', |
| 40 | + 'regex': re.compile(r'[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}') |
| 41 | + }] |
| 42 | + |
| 43 | + # IPv6 |
| 44 | + # RegEx from https://stackoverflow.com/questions/53497/regular-expression-that-matches-valid-ipv6-addresses |
| 45 | + r = '(' + \ |
| 46 | + '([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|' + \ |
| 47 | + '([0-9a-fA-F]{1,4}:){1,7}:|' + \ |
| 48 | + '([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|' + \ |
| 49 | + '([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|' + \ |
| 50 | + '([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|' + \ |
| 51 | + '([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|' + \ |
| 52 | + '([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|' + \ |
| 53 | + '[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|' + \ |
| 54 | + ':((:[0-9a-fA-F]{1,4}){1,7}|:)|' + \ |
| 55 | + 'fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|' + \ |
| 56 | + '::(ffff(:0{1,4}){0,1}:){0,1}' + \ |
| 57 | + '((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}' + \ |
| 58 | + '(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|' + \ |
| 59 | + '([0-9a-fA-F]{1,4}:){1,4}:' + \ |
| 60 | + '((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}' + \ |
| 61 | + '(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])' + \ |
| 62 | + ')' |
| 63 | + regex.append({ |
| 64 | + 'type': 'ip', |
| 65 | + 'regex': re.compile(r'{}'.format(r)) |
| 66 | + }) |
| 67 | + |
| 68 | + # URL |
| 69 | + regex.append({ |
| 70 | + 'type': 'url', |
| 71 | + 'regex': re.compile(r'^(http://|https://)') |
| 72 | + }) |
| 73 | + |
| 74 | + # domain |
| 75 | + regex.append({ |
| 76 | + 'type': 'domain', |
| 77 | + 'regex': re.compile(r'^(?!http://|https://)^[\w\-]+\.[a-zA-Z]+$') |
| 78 | + }) |
| 79 | + |
| 80 | + # hash |
| 81 | + regex.append({ |
| 82 | + 'type': 'hash', |
| 83 | + 'regex': re.compile(r'^([0-9a-fA-F]{32}|[0-9a-fA-F]{40}|[0-9a-fA-F]{64})$') |
| 84 | + }) |
| 85 | + |
| 86 | + # user-agent |
| 87 | + regex.append({ |
| 88 | + 'type': 'user-agent', |
| 89 | + 'regex': re.compile(r'^(Mozilla/[45]\.0 |AppleWebKit/[0-9]{3}\.[0-9]{2} |Chrome/[0-9]{2}\.[0-9]\.' |
| 90 | + r'[0-9]{4}\.[0-9]{3} |Safari/[0-9]{3}\.[0-9]{2} ).*?$') |
| 91 | + }) |
| 92 | + |
| 93 | + # uri_path |
| 94 | + regex.append({ |
| 95 | + 'type': 'uri_path', |
| 96 | + 'regex': re.compile(r'^(?!http://|https://)[A-Za-z]*://') |
| 97 | + }) |
| 98 | + |
| 99 | + # regkey |
| 100 | + regex.append({ |
| 101 | + 'type': 'registry', |
| 102 | + 'regex': re.compile(r'^(HKEY|HKLM|HKCU|HKCR|HKCC)' |
| 103 | + r'(_LOCAL_MACHINE|_CURRENT_USER|_CURRENT_CONFIG|_CLASSES_ROOT|)[\\a-zA-Z0-9]+$') |
| 104 | + }) |
| 105 | + |
| 106 | + # mail |
| 107 | + regex.append({ |
| 108 | + 'type': 'mail', |
| 109 | + 'regex': re.compile(r'[\w.\-]+@\w+\.[\w.]+') |
| 110 | + }) |
| 111 | + |
| 112 | + # fqdn |
| 113 | + regex.append({ |
| 114 | + 'type': 'fqdn', |
| 115 | + 'regex': re.compile(r'^(?!http://|https://)^[\w\-.]+\.[\w\-]+\.[a-zA-Z]+$') |
| 116 | + }) |
| 117 | + |
| 118 | + return regex |
| 119 | + |
| 120 | + def __checktype(self, value): |
| 121 | + """Checks if the given value is a known datatype |
| 122 | +
|
| 123 | + :param value: The value to check |
| 124 | + :type value: str or number |
| 125 | + :return: Data type of value, if known, else empty string |
| 126 | + :rtype: str |
| 127 | + """ |
| 128 | + if self.ignore: |
| 129 | + if isinstance(value, str) and self.ignore in value: |
| 130 | + return '' |
| 131 | + if self.ignore == value: |
| 132 | + return '' |
| 133 | + |
| 134 | + if isinstance(value, (str, unicode)): |
| 135 | + for r in self.regex: |
| 136 | + if r.get('regex').match(value): |
| 137 | + return r.get('type') |
| 138 | + return '' |
| 139 | + |
| 140 | + def check_string(self, value): |
| 141 | + """ |
| 142 | + Checks if a string matches a datatype. |
| 143 | +
|
| 144 | + :param value: String to test |
| 145 | + :type value: str |
| 146 | + :return: Data type or empty string |
| 147 | + :rtype: str |
| 148 | + """ |
| 149 | + return self.__checktype(value) |
| 150 | + |
| 151 | + def check_iterable(self, iterable): |
| 152 | + """ |
| 153 | + Checks values of a list or a dict on ioc's. Returns a list of dict {type, value}. Raises TypeError, if iterable |
| 154 | + is not an expected type. |
| 155 | +
|
| 156 | + :param iterable: List or dict of values |
| 157 | + :type iterable: list dict str |
| 158 | + :return: List of ioc's matching the regex |
| 159 | + :rtype: list |
| 160 | + """ |
| 161 | + results = [] |
| 162 | + # Only the string left |
| 163 | + if isinstance(iterable, (str, unicode)): |
| 164 | + dt = self.__checktype(iterable) |
| 165 | + if len(dt) > 0: |
| 166 | + results.append({ |
| 167 | + 'type': dt, |
| 168 | + 'value': iterable |
| 169 | + }) |
| 170 | + elif isinstance(iterable, list): |
| 171 | + for item in iterable: |
| 172 | + if isinstance(item, list) or isinstance(item, dict): |
| 173 | + results.extend(self.check_iterable(item)) |
| 174 | + else: |
| 175 | + dt = self.__checktype(item) |
| 176 | + if len(dt) > 0: |
| 177 | + results.append({ |
| 178 | + 'type': dt, |
| 179 | + 'value': item |
| 180 | + }) |
| 181 | + elif isinstance(iterable, dict): |
| 182 | + for _, item in iterable.items(): |
| 183 | + if isinstance(item, list) or isinstance(item, dict): |
| 184 | + results.extend(self.check_iterable(item)) |
| 185 | + else: |
| 186 | + dt = self.__checktype(item) |
| 187 | + if len(dt) > 0: |
| 188 | + results.append({ |
| 189 | + 'type': dt, |
| 190 | + 'value': item |
| 191 | + }) |
| 192 | + else: |
| 193 | + raise TypeError('Not supported type.') |
| 194 | + |
| 195 | + return results |
0 commit comments