def write_config(self): source_configuration_file_path = os.path.join(self.install_directory, 'etc', 'networks.cfg') if self.backup_configuration_directory: destination_configuration_path = os.path.join( self.backup_configuration_directory, 'networks.cfg.d') try: utilities.backup_configuration_file( source_configuration_file_path, destination_configuration_path, destination_file_prefix='networks.cfg.backup') except general_exceptions.WriteConfigError: raise zeek_exceptions.WriteZeekConfigError( 'Zeek configuration failed to write [networks.cfg].') except general_exceptions.ReadConfigError: raise zeek_exceptions.ReadsZeekConfigError( 'Zeek configuration failed to read [networks.cfg].') try: with open(source_configuration_file_path, 'w') as net_config: lines = [] for k, v in self.network_config.items(): if v: line = '{0: <64} {1}\n'.format(k, v) else: line = '{0: <64} {1}\n'.format(k, 'Undocumented network') lines.append(line) net_config.writelines(lines) except IOError as e: raise zeek_exceptions.WriteZeekConfigError( "General error while attempting to write new networks.cfg file to {}; {}" .format(self.install_directory, e))
def get_raw_config(self): """ Get the raw text of the config file :return: Config file contents """ zeek_node_cfg = os.path.join(self.install_directory, 'etc', 'node.cfg') try: with open(zeek_node_cfg) as config_f: raw_text = config_f.read() except IOError: raise zeek_exceptions.ReadsZeekConfigError( "Could not locate config at {}".format(zeek_node_cfg)) except Exception as e: raise zeek_exceptions.ReadsZeekConfigError( "General exception when opening/parsing config at {}; {}". format(zeek_node_cfg, e)) return raw_text
def get_raw_config(self): """ Get the raw text of the config file :return: Config file contents """ zeeklocalsite_path = os.path.join(self.configuration_directory, 'site', 'local.zeek') try: with open(zeeklocalsite_path) as config_f: raw_text = config_f.read() except IOError: raise zeek_exceptions.ReadsZeekConfigError( "Could not locate config at {}".format(zeeklocalsite_path)) except Exception as e: raise zeek_exceptions.ReadsZeekConfigError( "General exception when opening/parsing config at {}; {}". format(zeeklocalsite_path, e)) return raw_text
def _parse_zeek_scripts(self): """ Parse the local.zeek configuration file, and determine which scripts are enabled/disabled """ zeeklocalsite_path = os.path.join(self.configuration_directory, 'site', 'local.zeek') try: with open(zeeklocalsite_path) as config_f: for line in config_f.readlines(): line = line.replace(' ', '').strip() if '@load-sigs' in line: if line.startswith('#'): enabled = False line = line[1:] else: enabled = True sigs = line.split('@load-sigs')[1] self.zeek_sigs[sigs] = enabled elif '@load' in line: if line.startswith('#'): enabled = False line = line[1:] else: enabled = True script = line.split('@load')[1] self.zeek_scripts[script] = enabled elif line.startswith('redef'): definition, value = line.split('redef')[1].split('=') self.zeek_redefs[definition] = value except IOError: raise zeek_exceptions.ReadsZeekConfigError( "Could not locate config at {}".format(zeeklocalsite_path)) except Exception as e: raise zeek_exceptions.ReadsZeekConfigError( "General exception when opening/parsing config at {}; {}". format(zeeklocalsite_path, e))
def write_config(self): """ Overwrite the existing local.zeek config with changed values """ output_str = '' # Backup old configuration first source_configuration_file_path = os.path.join( self.configuration_directory, 'site', 'local.zeek') if self.backup_configuration_directory: destination_configuration_path = os.path.join( self.backup_configuration_directory, 'local.zeek.d') try: utilities.backup_configuration_file( source_configuration_file_path, destination_configuration_path, destination_file_prefix='local.zeek.backup') except general_exceptions.WriteConfigError: raise zeek_exceptions.WriteZeekConfigError( 'Zeek configuration failed to write [local.zeek].') except general_exceptions.ReadConfigError: raise zeek_exceptions.ReadsZeekConfigError( 'Zeek configuration failed to read [local.zeek].') for e_script in self.list_enabled_scripts(): output_str += '@load {}\n'.format(e_script) for d_script in self.list_disabled_scripts(): output_str += '#@load {}\n'.format(d_script) for e_sig in self.list_enabled_sigs(): output_str += '@load-sigs {}\n'.format(e_sig) for d_sig in self.list_disabled_sigs(): output_str += '@load-sigs {}\n'.format(d_sig) for rdef, val in self.list_redefinitions(): output_str += 'redef {} = {}\n'.format(rdef, val) try: with open(source_configuration_file_path, 'w') as f: f.write(output_str) except IOError: raise zeek_exceptions.WriteZeekConfigError( "Could not locate {}".format(self.configuration_directory)) except Exception as e: raise zeek_exceptions.WriteZeekConfigError( "General error while attempting to write new local.zeek file to {}; {}" .format(self.configuration_directory, e))
def write_config(self): """ Overwrite the existing node.cfg with changed values """ source_configuration_file_path = os.path.join(self.install_directory, 'etc', 'node.cfg') if self.backup_configuration_directory: destination_configuration_path = os.path.join( self.backup_configuration_directory, 'node.cfg.d') try: utilities.backup_configuration_file( source_configuration_file_path, destination_configuration_path, destination_file_prefix='node.cfg.backup') except general_exceptions.WriteConfigError: raise zeek_exceptions.WriteZeekConfigError( 'Zeek configuration failed to write [node.cfg].') except general_exceptions.ReadConfigError: raise zeek_exceptions.ReadsZeekConfigError( 'Zeek configuration failed to read [node.cfg].') config = ConfigParser() for section in self.node_config.keys(): for k, v in self.node_config[section].items(): try: config.add_section(section) except Exception: # Duplicate section pass config.set(section, k, str(v)) try: with open(source_configuration_file_path, 'w') as configfile: config.write(configfile) except IOError: raise zeek_exceptions.WriteZeekConfigError( "Could not locate {}".format(self.install_directory)) except Exception as e: raise zeek_exceptions.WriteZeekConfigError( "General error while attempting to write new node.cfg file to {}; {}" .format(self.install_directory, e))