A very simple CEF parser.
Project description
pycef
A very simple CEF parser for Python 2/3
I originally wrote this because I wasn't able to find very many good Python CEF parsers out there. I did find one by Sooshie that got me started (thanks for sharing, sir!), but I elected to produce my own.
The parse
function takes a string containing a single CEF record and returns a dict containing the following keys, as defined in the CEF format documentation:
- CEFVersion
- DeviceVendor
- DeviceVersion
- DeviceEventClassID
- Name
- Severity
If there are any key=value
pairs in the "extensions" section (and face it, pretty much every CEF record has these), they'll also be in the dict, with the dict key name the same as the CEF record's key name. If it could not recognize any CEF data, the parse
function will return None
.
NOTE: Versions 1.10 and earlier used 'DeviceName' and 'DeviceSeverity' instead of just 'Name' and 'Severity'. Those old keys are still present in the returned dictionary for backwards-compatibility reasons, but are likely to be removed in the future without warning.
Example Usage
Parsing a well-formatted CEF record
>>> 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', 'Name': 'Test event 1', 'Severity': '3', 'CEFVersion': '0', 'field1': 'value1', 'field2': 'value2', 'field3': 'value3'}
Parsing a line of CEF from a source with header junk at the front (NOTE: this isn't specific to syslog headers as in the example. The parser just starts wherever 'CEF:0' is found):
>>> 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', 'Name': 'Test event 1', 'Severity': '3', 'CEFVersion': '0', 'field1': 'value1', 'field2': 'value2', 'field3': 'value3'}
Logging
Pycef
uses the standard Python logging
module. By default, you will not see any logs, but you can easily configure them within your own application. Here's an example:
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', 'Name': 'Test event 1', 'Severity': '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?
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pycef-1.11.tar.gz
.
File metadata
- Download URL: pycef-1.11.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca0389c5d9027cb7c3aefb94a8aef1ebdc8587319c03abea218b6ffb6f44bd7b |
|
MD5 | 482ce4db0ee42baf456be3db023bf5e0 |
|
BLAKE2b-256 | 20c94d690e94c074536315ba26f7833c5faffe5489bae8624bd2393bd0ba4993 |
File details
Details for the file pycef-1.11-py3-none-any.whl
.
File metadata
- Download URL: pycef-1.11-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 24e6b9a85cff9d636f18b20b15568d61f8fd7e89c66779ae8dbdbf33dfa80846 |
|
MD5 | 4da25417d387a790fec72549f098013f |
|
BLAKE2b-256 | c25804af3fc6c5e30cb3208301a5d78a41b28ffd419af3590be3ee193e8dea6a |