OSSIM开源安全信息管理系统(十八)

2021SC@SDUSC




接上一篇:OSSIM开源安全信息管理系统(十七)


2、私有方法 __getSensorID(self) :获取传感器(探针)的 ID 号。传感器用来收集监控网段内各类资产信息,OSSIM系统中,把 Agent 和插件构成的具有网络行为监控功能的组合称为一个传感器。

def __getSensorID(self):
	sensor_id = ""
	system_file = DEFAULT_SYSTEM_FILE

re 模块使 Python 语言拥有全部的正则表达式功能。
compile 函数用于编译正则表达式,生成一个正则表达式(Pattern)对象,供 match() 和 search() 这两个函数使用。
re.M :多行模式

reg_str = "(?P<uuid>[a-zA-Z0-9]{8}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{4}-[a-zA-Z0-9]{12})"

pt = re.compile(reg_str, re.M)

检查option是否存在

#has_option 检查option是否存在
if self.conf.has_option("plugin-defaults", "system_uuid_file"):
	#conf.get() : 获取指定section中指定键对应的值
    #conf.get() 中调用了ConfigParser.get(self, section, option)方法
    #获取 plugin-defaults 中 system_uuid_file 对应的值
    system_file = self.conf.get("plugin-defaults", "system_uuid_file")
    logger.info("System UUID File: %s" % system_file)
else:
    logger.warning("system uuid file not configured, using default %s" % DEFAULT_SYSTEM_FILE)        

检查获取的值文件是否存在。
如果不存在,通过uuid模块生成sensor_id,并将其写入文件中
UUID: 通用唯一标识符, 对于所有的UUID它可以保证在空间和时间上的唯一性.

if not os.path.exists(system_file):
	logger.warning("System uuid file doesn't exist. ")
	thefile = open(system_file, 'w')
    
    #uuid.uuid4():通过随机数来生成UUID. 使用的是伪随机数有一定的重复概率. 
    sensor_id = uuid.uuid4()
    thefile.write(str(sensor_id))
    thefile.close()

如果存在,直接读取 sensor_id ,然后再判断获取的内容是否有效,如果无效,再次使用 uuid.uuid4 随机生成一个 sensor_id ,然后写入文件中

else:
	thefile = open(system_file, 'r')
    sensor_id = thefile.read()
    thefile.close()
    #扫描整个字符串并返回第一个成功的匹配。
    match_data = pt.search(sensor_id)
    #如果匹配内容为空,返回内容无效信息,并通过uuid.uuid4随机生成一个sensor_id,然后写入文件中
    if match_data is None:
    	logger.error("System UUID file exist but the contents are invalid.")
        thefile = open(system_file, 'w')
        sensor_id = uuid.uuid4()
        thefile.write(str(sensor_id))
        thefile.close()
os.chmod(system_file, 0644)
        
self.__systemUUIDFile = system_file
logger.info("SensorID: %s" % sensor_id)
self.__sensorID = str(sensor_id).rstrip('\n')

3、私有方法 __loadAliases

别名:如同在 Linux 中定义别名一样,区别在于这里使用 aliases 定义别名。为了方便插件内容的定义,插件中使用了经常用的到的内容来定义别名,这样的好处是今后在定义某一插件内容时,可以使用已定义的别名代替那个元素

加载别名配置文件

def __loadAliases(self, configuration_file):
	self.__aliases = Aliases()
    self.__aliases.read([os.path.join(os.path.dirname(configuration_file), "aliases.cfg")], 'latin1')
    local_aliases_fn = os.path.join(os.path.dirname(configuration_file), "aliases.local")
    #如果 aliases.local 存在,在加载别名默认文件后,加载 aliases.local 文件
    if os.path.isfile(local_aliases_fn):
        logger.info("Reading local aliases file: %s" % local_aliases_fn)
        self.__aliases.read(local_aliases_fn, 'latin1')

4、私有方法 __loadPluginConfigurations

加载插件配置

def __loadPluginConfigurations(self):
	#conf.hitems():与RawConfigParser.items()相同,但返回哈希而不是列表
    #返回给定section下的所有option的键值对
    #Suricata是一个免费、开源、成熟、快速、健壮的网络威胁检测引擎。
    #Suricata引擎能够进行实时入侵检测(IDS)、内联入侵预防(IPS)、网络安全监控(NSM)和离线pcap处理。
    #Suricata使用强大而广泛的规则和签名语言来检查网络流量,并提供强大的Lua脚本支持来检测复杂的威胁。
    if 'suricata' in self.conf.hitems("plugins"):
        self.conf.set('plugins', 'av-pulse', DEFAULT_PULSE_PLUGIN_PATH)

检查是否有编码信息

for name, path in self.conf.hitems("plugins").iteritems():
	data = path.split('|')
    path = data[0]
    encoding = 'latin1'
    #如果按照字符“|”分割得到的数组data的长度大于1,则编码encoding为data[1]
	if len(data) > 1:
        path = data[0]
        encoding = data[1]
        if data[1] != '':
			encoding = data[1]
        try:
            logger.info("Using encoding: %s for plugin: %s" % (encoding, path))  

codecs用作编码转换
codecs.lookup:接受一个字符编码名称的参数,
并返回指定字符编码对应的encoder、decoder、StreamReader和StreamWriter的函数对象和类对象引用
创建encoding编码器

	encoder = codecs.lookup(encoding)
	except LookupError, e:
		logger.warning("Invalid encoding:%s, using default encoding ...latin1")
    	encoding = 'latin1'

if os.path.exists(path):
	plugin = Plugin()
    #读取配置文件
    plugin.read([path], encoding)
    if not plugin.has_section('config'):
		logger.error("Plugin without [config] section?. If the plugin has the [config] section, please verify the encoding ")
        continue
        plugin.set('config', 'encoding', encoding)
        #有效性
   if not plugin.get_validConfig():
        logger.error("Invalid plugin. Please check it :%s" % path)
        continue

   try:
        int(plugin.get("DEFAULT", "plugin_id"))
   except ValueError, e:
        logger.error("Invalid plugin. Please check plugin_id in it:%s" % path)
        continue

   #check if custom plugin configuration exist
   #检查自定义插件配置是否存在,如果存在,加载插件配置
   custompath = "%s.local" % path
   if os.path.exists(custompath):
        logger.warning("Loading custom configuration for plugin: %s" % custompath)
        custom_plug = Plugin()
        custom_plug.read([custompath], encoding, False)
        #更新plugin的值
        for item in custom_plug.hitems("DEFAULT"):
        	new_value = custom_plug.get("DEFAULT", item)
            old_value = plugin.get("DEFAULT", item)
            if new_value != old_value:
            	plugin.set("DEFAULT", item, new_value)
                logger.warning("Loading custom value for %s--->%s. New value: %s - Old value: %s" % ("DEFAULT", item, new_value, old_value ))



本篇文章部分内容参考或转载自下列文章及书籍。侵权即删。

参考书籍:

  • 《开源安全运维平台OSSIM疑难解析(入门篇)》——李晨光著
  • 《开源安全运维平台OSSIM疑难解析(提高篇)》——李晨光著
  • 《开源安全运维平台:OSSIM最佳实践》——李晨光著

参考文章:

  • https://blog.51cto.com/chenguang/2426473
  • https://blog.csdn.net/lcgweb/article/details/101284949
  • https://blog.51cto.com/chenguang/1665012
  • https://www.cnblogs.com/lsdb/p/10000061.html
  • https://blog.51cto.com/chenguang/1691090
  • https://blog.51cto.com/chenguang/category10.html
  • https://blog.51cto.com/topic/ossim.html
  • https://blog.csdn.net/isinstance/article/details/53694361
  • https://blog.51cto.com/chenguang/1332329
  • https://www.cnblogs.com/airoot/p/8072727.html
  • https://blog.51cto.com/chenguang/1738731
  • https://blog.csdn.net/security_yj/article/details/120153992

上一篇:OSSIM开源安全信息管理系统(十七)

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陌兮_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值