一个非常简单的cef解析器。
pycef的Python项目详细描述
#Pycef公司 python 2/3的一个非常简单的cef解析器
我最初写这个是因为我找不到很多优秀的python cef解析器。我确实找到了[苏轼的一个](https://github.com/sooshie/cef_parser)让我开始(谢谢分享,先生!),但我选择了自己制作。
函数parse
接受一个包含单个cef记录的字符串,并返回一个dict,该dict包含[cef format documentation](https://www.protect724.hpe.com/docs/DOC-1072)中定义的以下键:
- 简化版
- 设备供应商
- 设备版本
- 设备事件ClassID
- 设备名
- 设备严重性
如果在“extensions”部分中有任何key=value
对(面对它,几乎每个cef记录都有这些),它们也将在dict中,dict密钥名与cef记录的密钥名相同。如果它不能识别任何cef数据,parse
函数将返回None
。
##示例用法 分析格式良好的CEF记录
>>> import pycef >>> cef = 'CEF:0|pycef|python CEF tests|1|2|Test event 1|3| field1=value1 field2=value2 field3=value3' >>> d = pycef.parse(cef) >>> d {'DeviceVendor': 'pycef', 'DeviceProduct': 'python CEF tests', 'DeviceVersion': '1', 'DeviceEventClassID': '2', 'DeviceName': 'Test event 1', 'DeviceSeverity': '3', 'CEFVersion': '0', 'field1': 'value1', 'field2': 'value2', 'field3': 'value3'}
从前端带有头垃圾邮件的源解析一行cef(注意:这并不像示例中那样特定于syslog头。解析器只在找到“cef:0”的地方启动:
>>> import pycef >>> cef_syslog = 'Nov 16 21:24:18 arcsightfwd.davidbianco.io CEF:0|pycef|python CEF tests|1|2|Test event 1|3| field1=value1 field2=value2 field3=value3' >>> d = pycef.parse(cef_syslog) >>> d {'DeviceVendor': 'pycef', 'DeviceProduct': 'python CEF tests', 'DeviceVersion': '1', 'DeviceEventClassID': '2', 'DeviceName': 'Test event 1', 'DeviceSeverity': '3', 'CEFVersion': '0', 'field1': 'value1', 'field2': 'value2', 'field3': 'value3'}
##日志记录
Pycef
使用标准的pythonlogging
模块。默认情况下,您不会看到任何日志,但可以在自己的应用程序中轻松配置它们。下面是一个示例:
import logging
# We log with the name ‘pycef’ logger = logging.getLogger(‘pycef’)
# set log level to DEBUG to get the most verbose output logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) formatter = logging.Formatter(‘%(asctime)s - %(name)s - %(levelname)s - %(message)s’) ch.setFormatter(formatter) logger.addHandler(ch)
# well-formatted CEF data will log the parsed values at DEBUG level cef = ‘CEF:0|pycef|python CEF tests|1|2|Test event 1|3| field1=value1 field2=value2 field3=value3’ d = pycef.parse(cef) 2018-11-23 08:49:39,827 - pycef - DEBUG - Returning values: {‘DeviceVendor’: ‘pycef’, ‘DeviceProduct’: ‘python CEF tests’, ‘DeviceVersion’: ‘1’, ‘DeviceEventClassID’: ‘2’, ‘DeviceName’: ‘Test event 1’, ‘DeviceSeverity’: ‘3’, ‘CEFVersion’: ‘0’, ‘field1’: ‘value1’, ‘field2’: ‘value2’, ‘field3’: ‘value3’}
# Parse errors in the data will log at WARNING level pycef.parse(‘kjlk’) 2018-11-23 08:47:42,853 - pycef - WARNING - Could not parse record. Is it valid CEF format?