|
10 | 10 | by Bastian Schroll |
11 | 11 |
|
12 | 12 | @file: descriptor.py |
13 | | -@date: 27.10.2019 |
| 13 | +@date: 04.08.2025 |
14 | 14 | @author: Bastian Schroll |
15 | | -@description: Module to add descriptions to bwPackets |
| 15 | +@description: Module to add descriptions to bwPackets with CSV and Regex support |
16 | 16 | """ |
17 | 17 | import logging |
| 18 | +import csv |
| 19 | +import re |
| 20 | +import os |
18 | 21 | from module.moduleBase import ModuleBase |
19 | 22 |
|
20 | 23 | # ###################### # |
|
26 | 29 |
|
27 | 30 |
|
28 | 31 | class BoswatchModule(ModuleBase): |
29 | | - r"""!Adds descriptions to bwPackets""" |
| 32 | + r"""!Adds descriptions to bwPackets with CSV and Regex support""" |
30 | 33 | def __init__(self, config): |
31 | 34 | r"""!Do not change anything here!""" |
32 | 35 | super().__init__(__name__, config) # you can access the config class on 'self.config' |
33 | 36 |
|
34 | 37 | def onLoad(self): |
35 | 38 | r"""!Called by import of the plugin""" |
36 | | - for descriptor in self.config: |
37 | | - if descriptor.get("wildcard", default=None): |
38 | | - self.registerWildcard(descriptor.get("wildcard"), descriptor.get("descrField")) |
| 39 | + # Initialize unified cache for all descriptors |
| 40 | + self.unified_cache = {} |
| 41 | + |
| 42 | + # Process each descriptor configuration |
| 43 | + for descriptor_config in self.config: |
| 44 | + scan_field = descriptor_config.get("scanField") |
| 45 | + descr_field = descriptor_config.get("descrField") |
| 46 | + descriptor_key = f"{scan_field}_{descr_field}" |
| 47 | + |
| 48 | + # Register wildcard if specified |
| 49 | + if descriptor_config.get("wildcard", default=None): |
| 50 | + self.registerWildcard(descriptor_config.get("wildcard"), descr_field) |
| 51 | + |
| 52 | + # Initialize cache for this descriptor |
| 53 | + self.unified_cache[descriptor_key] = [] |
| 54 | + |
| 55 | + # Load YAML descriptions first (for backward compatibility) |
| 56 | + yaml_descriptions = descriptor_config.get("descriptions", default=None) |
| 57 | + if yaml_descriptions: |
| 58 | + # yaml_descriptions is a Config object, we need to iterate properly |
| 59 | + for desc in yaml_descriptions: |
| 60 | + entry = { |
| 61 | + 'for': str(desc.get("for", default="")), |
| 62 | + 'add': desc.get("add", default=""), |
| 63 | + 'isRegex': desc.get("isRegex", default=False) # Default: False |
| 64 | + } |
| 65 | + # Handle string 'true'/'false' values |
| 66 | + if isinstance(entry['isRegex'], str): |
| 67 | + entry['isRegex'] = entry['isRegex'].lower() == 'true' |
| 68 | + |
| 69 | + self.unified_cache[descriptor_key].append(entry) |
| 70 | + logging.debug("Added YAML entry: %s -> %s", entry['for'], entry['add']) |
| 71 | + logging.info("Loaded %d YAML descriptions for %s", len(yaml_descriptions), descriptor_key) |
| 72 | + |
| 73 | + # Load CSV descriptions if csvPath is specified |
| 74 | + csv_path = descriptor_config.get("csvPath", default=None) |
| 75 | + if csv_path: |
| 76 | + self._load_csv_data(csv_path, descriptor_key) |
| 77 | + |
| 78 | + logging.info("Total entries for %s: %d", descriptor_key, len(self.unified_cache[descriptor_key])) |
| 79 | + |
| 80 | + def _load_csv_data(self, csv_path, descriptor_key): |
| 81 | + r"""!Load CSV data for a descriptor and add to unified cache""" |
| 82 | + try: |
| 83 | + if not os.path.isfile(csv_path): |
| 84 | + logging.error("CSV file not found: %s", csv_path) |
| 85 | + return |
| 86 | + |
| 87 | + csv_count = 0 |
| 88 | + with open(csv_path, 'r', encoding='utf-8') as csvfile: |
| 89 | + reader = csv.DictReader(csvfile) |
| 90 | + for row in reader: |
| 91 | + # Set default values if columns are missing |
| 92 | + entry = { |
| 93 | + 'for': str(row.get('for', '')), |
| 94 | + 'add': row.get('add', ''), |
| 95 | + 'isRegex': row.get('isRegex', 'false').lower() == 'true' # Default: False |
| 96 | + } |
| 97 | + self.unified_cache[descriptor_key].append(entry) |
| 98 | + csv_count += 1 |
| 99 | + |
| 100 | + logging.info("Loaded %d entries from CSV: %s for %s", csv_count, csv_path, descriptor_key) |
| 101 | + |
| 102 | + except Exception as e: |
| 103 | + logging.error("Error loading CSV file %s: %s", csv_path, str(e)) |
| 104 | + |
| 105 | + def _find_description(self, descriptor_key, scan_value, bw_packet): |
| 106 | + r"""!Find matching description for a scan value with Regex group support.""" |
| 107 | + descriptions = self.unified_cache.get(descriptor_key, []) |
| 108 | + scan_value_str = str(scan_value) |
| 109 | + |
| 110 | + # Search for matching description |
| 111 | + for desc in descriptions: |
| 112 | + description_text = desc.get('add', '') |
| 113 | + match_pattern = desc.get('for', '') |
| 114 | + is_regex = desc.get('isRegex', False) |
| 115 | + |
| 116 | + if is_regex: |
| 117 | + # Regex matching |
| 118 | + try: |
| 119 | + match = re.search(match_pattern, scan_value_str) |
| 120 | + if match: |
| 121 | + # Expand regex groups (\1, \2) in the description |
| 122 | + expanded_description = match.expand(description_text) |
| 123 | + |
| 124 | + # Replace standard wildcards like {TONE} |
| 125 | + final_description = self._replace_wildcards(expanded_description, bw_packet) |
| 126 | + |
| 127 | + logging.debug("Regex match '%s' -> '%s' for descriptor '%s'", |
| 128 | + match_pattern, final_description, descriptor_key) |
| 129 | + return final_description |
| 130 | + except re.error as e: |
| 131 | + logging.error("Invalid regex pattern '%s': %s", match_pattern, str(e)) |
| 132 | + continue |
| 133 | + else: |
| 134 | + # Exact match |
| 135 | + if match_pattern == scan_value_str: |
| 136 | + # Replace standard wildcards like {TONE} |
| 137 | + final_description = self._replace_wildcards(description_text, bw_packet) |
| 138 | + logging.debug("Exact match '%s' -> '%s' for descriptor '%s'", |
| 139 | + match_pattern, final_description, descriptor_key) |
| 140 | + return final_description |
| 141 | + |
| 142 | + return None |
| 143 | + |
| 144 | + def _replace_wildcards(self, text, bw_packet): |
| 145 | + r"""!Replace all available wildcards in description text dynamically.""" |
| 146 | + if not text or '{' not in text: |
| 147 | + return text |
| 148 | + |
| 149 | + result = text |
| 150 | + |
| 151 | + # Search for wildcards in the format {KEY} and replace them with values from the bw_packet |
| 152 | + found_wildcards = re.findall(r"\{([A-Z0-9_]+)\}", result) |
| 153 | + |
| 154 | + for key in found_wildcards: |
| 155 | + key_lower = key.lower() |
| 156 | + value = bw_packet.get(key_lower) |
| 157 | + |
| 158 | + if value is not None: |
| 159 | + result = result.replace(f"{{{key}}}", str(value)) |
| 160 | + logging.debug("Replaced wildcard {%s} with value '%s'", key, value) |
| 161 | + |
| 162 | + return result |
39 | 163 |
|
40 | 164 | def doWork(self, bwPacket): |
41 | 165 | r"""!start an run of the module. |
42 | 166 |
|
43 | 167 | @param bwPacket: A BOSWatch packet instance""" |
44 | | - for descriptor in self.config: |
45 | | - if not bwPacket.get(descriptor.get("scanField")): |
46 | | - break # scanField is not available in this packet |
47 | | - bwPacket.set(descriptor.get("descrField"), bwPacket.get(descriptor.get("scanField"))) |
48 | | - for description in descriptor.get("descriptions"): |
49 | | - if str(description.get("for")) == bwPacket.get(descriptor.get("scanField")): |
50 | | - logging.debug("Description '%s' added in packet field '%s'", |
51 | | - description.get("add"), descriptor.get("descrField")) |
52 | | - bwPacket.set(descriptor.get("descrField"), description.get("add")) |
53 | | - break # this descriptor has found a description - run next descriptor |
| 168 | + logging.debug("Processing packet with mode: %s", bwPacket.get("mode")) |
| 169 | + |
| 170 | + # Process each descriptor configuration |
| 171 | + for descriptor_config in self.config: |
| 172 | + scan_field = descriptor_config.get("scanField") |
| 173 | + descr_field = descriptor_config.get("descrField") |
| 174 | + descriptor_key = f"{scan_field}_{descr_field}" |
| 175 | + |
| 176 | + logging.debug("Processing descriptor: scanField='%s', descrField='%s'", scan_field, descr_field) |
| 177 | + |
| 178 | + # Check if scanField is present in packet |
| 179 | + scan_value = bwPacket.get(scan_field) |
| 180 | + if scan_value is None: |
| 181 | + logging.debug("scanField '%s' not found in packet, skipping", scan_field) |
| 182 | + continue # scanField not available in this packet - try next descriptor |
| 183 | + |
| 184 | + # Set default value (content of scanField) |
| 185 | + bwPacket.set(descr_field, str(scan_value)) |
| 186 | + logging.debug("Set default value '%s' for field '%s'", scan_value, descr_field) |
| 187 | + |
| 188 | + # Search for matching description in unified cache |
| 189 | + description = self._find_description(descriptor_key, scan_value, bwPacket) |
| 190 | + |
| 191 | + if description: |
| 192 | + bwPacket.set(descr_field, description) |
| 193 | + logging.info("Description set: '%s' -> '%s'", scan_value, description) |
| 194 | + else: |
| 195 | + logging.debug("No description found for value '%s' in field '%s'", scan_value, scan_field) |
| 196 | + |
| 197 | + logging.debug("Returning modified packet") |
54 | 198 | return bwPacket |
55 | 199 |
|
56 | 200 | def onUnload(self): |
|
0 commit comments