Exemple #1
0
class SoSMacParser(SoSCleanerParser):
    """Handles parsing for MAC addresses"""

    name = 'MAC Parser'
    regex_patterns = [
        # IPv6
        r'(([^:|-])([0-9a-fA-F]{2}(:|-)){7}[0-9a-fA-F]{2}(\s|$))',
        r'(([^:|-])([0-9a-fA-F]{4}(:|-)){3}[0-9a-fA-F]{4}(\s|$))',
        # IPv4, avoiding matching a substring within IPv6 addresses
        r'(([^:|-])([0-9a-fA-F]{2}([:-])){5}([0-9a-fA-F]){2}(.)?(\s|$|\W))'
    ]
    obfuscated_patterns = (
        '53:4f:53',
        '534f:53'
    )
    map_file_key = 'mac_map'

    def __init__(self, config):
        self.mapping = SoSMacMap()
        super(SoSMacParser, self).__init__(config)

    def reduce_mac_match(self, match):
        """Strips away leading and trailing non-alphanum characters from any
        matched string to leave us with just the bare MAC addr
        """
        while not (match[0].isdigit() or match[0].isalpha()):
            match = match[1:]
        while not (match[-1].isdigit() or match[-1].isalpha()):
            match = match[0:-1]
        # just to be safe, call strip() to remove any padding
        return match.strip()

    def parse_line(self, line):
        count = 0
        for skip_pattern in self.skip_line_patterns:
            if re.match(skip_pattern, line, re.I):
                return line, count
        for pattern in self.regex_patterns:
            matches = [m[0] for m in re.findall(pattern, line, re.I)]
            if matches:
                count += len(matches)
                for match in matches:
                    if match.startswith(self.obfuscated_patterns):
                        # avoid double scrubbing
                        continue
                    stripped_match = self.reduce_mac_match(match)
                    new_match = self.mapping.get(stripped_match)
                    line = line.replace(stripped_match, new_match)
        return line, count
Exemple #2
0
class SoSMacParser(SoSCleanerParser):
    """Handles parsing for MAC addresses"""

    name = 'MAC Parser'
    regex_patterns = [IPV6_REG_8HEX, IPV6_REG_4HEX, IPV4_REG]
    obfuscated_patterns = ('53:4f:53', '534f:53')
    skip_files = ['sos_commands/kernel/modinfo.*']
    map_file_key = 'mac_map'
    compile_regexes = False

    def __init__(self, config):
        self.mapping = SoSMacMap()
        super(SoSMacParser, self).__init__(config)

    def reduce_mac_match(self, match):
        """Strips away leading and trailing non-alphanum characters from any
        matched string to leave us with just the bare MAC addr
        """
        while not (match[0].isdigit() or match[0].isalpha()):
            match = match[1:]
        while not (match[-1].isdigit() or match[-1].isalpha()):
            match = match[0:-1]
        # just to be safe, call strip() to remove any padding
        return match.strip()

    def _parse_line(self, line):
        count = 0
        for pattern in self.regex_patterns:
            matches = [m[0] for m in re.findall(pattern, line, re.I)]
            if matches:
                count += len(matches)
                for match in matches:
                    stripped_match = self.reduce_mac_match(match)
                    if stripped_match.startswith(self.obfuscated_patterns):
                        # avoid double scrubbing
                        continue
                    new_match = self.mapping.get(stripped_match)
                    line = line.replace(stripped_match, new_match)
        return line, count
Exemple #3
0
class SoSMacParser(SoSCleanerParser):
    """Handles parsing for MAC addresses"""

    name = 'MAC Parser'
    regex_patterns = [
        # IPv6
        r'(([^:|-])([0-9a-fA-F]{2}(:|-)){7}[0-9a-fA-F]{2}(\s|$))',
        r'(([^:|-])([0-9a-fA-F]{4}(:|-)){3}[0-9a-fA-F]{4}(\s|$))',
        # IPv4, avoiding matching a substring within IPv6 addresses
        r'(([^:|-])([0-9a-fA-F]{2}([:-])){5}([0-9a-fA-F]){2}(\.|,|!)?(\s|$))'
    ]
    map_file_key = 'mac_map'
    prep_map_file = 'sos_commands/networking/ip_-d_address'

    def __init__(self, conf_file=None):
        self.mapping = SoSMacMap()
        super(SoSMacParser, self).__init__(conf_file)

    def parse_line(self, line):
        """Override the base parse_line to account for MAC matches that end
        a line with punctuation, which may or may not be beneficial to have in
        other parsers
        """
        count = 0
        for skip_pattern in self.skip_line_patterns:
            if re.match(skip_pattern, line, re.I):
                return line, count
        for pattern in self.regex_patterns:
            matches = [m[0] for m in re.findall(pattern, line, re.I)]
            if matches:
                count += len(matches)
                for match in matches:
                    stripped_match = match.rstrip('.,!').strip()
                    new_match = self.mapping.get(stripped_match)
                    line = line.replace(stripped_match, new_match)
        return line, count
Exemple #4
0
 def setUp(self):
     self.mac_map = SoSMacMap()
     self.ip_map = SoSIPMap()
     self.host_map = SoSHostnameMap()
     self.host_map.load_domains_from_options(['redhat.com'])
     self.kw_map = SoSKeywordMap()
Exemple #5
0
class CleanerMapTests(unittest.TestCase):

    def setUp(self):
        self.mac_map = SoSMacMap()
        self.ip_map = SoSIPMap()
        self.host_map = SoSHostnameMap()
        self.host_map.load_domains_from_options(['redhat.com'])
        self.kw_map = SoSKeywordMap()

    def test_mac_map_obfuscate_valid_v4(self):
        _test = self.mac_map.get('12:34:56:78:90:ab')
        self.assertNotEqual(_test, '12:34:56:78:90:ab')

    def test_mac_map_obfuscate_valid_v6(self):
        _test = self.mac_map.get('12:34:56:ff:fe:78:90:ab')
        self.assertNotEqual(_test, '12:34:56:ff:fe:78:90:ab')

    def test_mac_map_obfuscate_valid_v6_quad(self):
        _test = self.mac_map.get('1234:56ff:fe78:90ab')
        self.assertNotEqual(_test, '1234:56ff:fe78:90ab')

    def test_mac_map_skip_ignores(self):
        _test = self.mac_map.get('ff:ff:ff:ff:ff:ff')
        self.assertEquals(_test, 'ff:ff:ff:ff:ff:ff')

    def test_mac_map_avoid_duplicate_obfuscation(self):
        _test = self.mac_map.get('ab:cd:ef:fe:dc:ba')
        _dup = self.mac_map.get(_test)
        self.assertEquals(_test, _dup)

    def test_ip_map_obfuscate_v4_with_cidr(self):
        _test = self.ip_map.get('192.168.1.0/24')
        self.assertNotEqual(_test, '192.168.1.0/24')

    def test_ip_map_obfuscate_no_cidr(self):
        _test = self.ip_map.get('192.168.2.2')
        self.assertNotEqual(_test, '192.168.2.2')

    def test_ip_map_obfuscate_same_subnet(self):
        _net = ip_interface(self.ip_map.get('192.168.3.0/24'))
        _test = ip_interface(self.ip_map.get('192.168.3.1'))
        self.assertTrue(_test.ip in _net.network)

    def test_ip_map_get_same_with_or_without_cidr(self):
        _hostwsub = self.ip_map.get('192.168.4.1/24')
        _hostnosub = self.ip_map.get('192.168.4.1')
        self.assertEqual(_hostwsub.split('/')[0], _hostnosub)

    def test_ip_skip_ignores(self):
        _test = self.ip_map.get('127.0.0.1')
        self.assertEquals(_test, '127.0.0.1')

    def test_hostname_obfuscate_domain_options(self):
        _test = self.host_map.get('www.redhat.com')
        self.assertNotEqual(_test, 'www.redhat.com')

    def test_hostname_obfuscate_same_item(self):
        _test1 = self.host_map.get('example.redhat.com')
        _test2 = self.host_map.get('example.redhat.com')
        self.assertEqual(_test1, _test2)

    def test_hostname_obfuscate_just_domain(self):
        _test = self.host_map.get('redhat.com')
        self.assertEqual(_test, 'obfuscateddomain0.com')

    def test_hostname_no_obfuscate_non_loaded_domain(self):
        _test = self.host_map.get('foobar.com')
        self.assertEqual(_test, 'foobar.com')

    def test_hostname_no_obfuscate_non_loaded_fqdn(self):
        _test = self.host_map.get('example.foobar.com')
        self.assertEqual(_test, 'example.foobar.com')

    def test_keyword_single(self):
        _test = self.kw_map.get('foobar')
        self.assertEqual(_test, 'obfuscatedword0')
Exemple #6
0
 def setUp(self):
     self.mac_map = SoSMacMap()
     self.ip_map = SoSIPMap()
     self.host_map = SoSHostnameMap(['redhat.com'])
     self.kw_map = SoSKeywordMap()
Exemple #7
0
 def __init__(self, config):
     self.mapping = SoSMacMap()
     super(SoSMacParser, self).__init__(config)
Exemple #8
0
 def __init__(self, conf_file=None):
     self.mapping = SoSMacMap()
     super(SoSMacParser, self).__init__(conf_file)