misp-modules/misp_modules/modules/export_mod/cef_export.py

84 lines
2.5 KiB
Python
Raw Normal View History

2016-08-22 15:18:19 +02:00
import json
import base64
import datetime
misperrors = {'error': 'Error'}
# possible module-types: 'expansion', 'hover' or both
moduleinfo = {'version': '1', 'author': 'Hannah Ward',
'description': 'Export a module in CEF format',
'module-type': ['export']}
# config fields that your code expects from the site admin
moduleconfig = ["Default_Severity", "Device_Vendor", "Device_Product", "Device_Version"]
2018-12-11 15:29:09 +01:00
cefmapping = {"ip-src": "src", "ip-dst": "dst", "hostname": "dhost", "domain": "dhost",
"md5": "fileHash", "sha1": "fileHash", "sha256": "fileHash",
"url": "request"}
2016-08-22 15:18:19 +02:00
2018-12-11 15:29:09 +01:00
mispattributes = {'input': list(cefmapping.keys())}
2016-08-22 15:18:19 +02:00
outputFileExtension = "cef"
responseType = "application/txt"
2018-12-11 15:29:09 +01:00
2016-08-22 15:18:19 +02:00
def handler(q=False):
if q is False:
return False
request = json.loads(q)
if "config" in request:
2018-12-11 15:29:09 +01:00
config = request["config"]
2016-08-22 15:18:19 +02:00
else:
2018-12-11 15:29:09 +01:00
config = {"Default_Severity": 1, "Device_Vendor": "MISP",
"Device_Product": "MISP", "Device_Version": 1}
2016-08-22 15:18:19 +02:00
data = request["data"]
response = ""
for ev in data:
2018-12-11 15:29:09 +01:00
event = ev["Attribute"]
for attr in event:
if attr["type"] in cefmapping:
response += "{} host CEF:0|{}|{}|{}|{}|{}|{}|{}={}\n".format(
2016-08-22 15:18:19 +02:00
datetime.datetime.fromtimestamp(int(attr["timestamp"])).strftime("%b %d %H:%M:%S"),
config["Device_Vendor"],
config["Device_Product"],
config["Device_Version"],
attr["category"],
attr["category"],
config["Default_Severity"],
cefmapping[attr["type"]],
attr["value"],
2018-12-11 15:29:09 +01:00
)
r = {"response": [], "data": str(base64.b64encode(bytes(response, 'utf-8')), 'utf-8')}
2016-08-22 15:18:19 +02:00
return r
def introspection():
2018-12-11 15:29:09 +01:00
modulesetup = {}
try:
2016-08-22 15:18:19 +02:00
responseType
modulesetup['responseType'] = responseType
2018-12-11 15:29:09 +01:00
except NameError:
pass
try:
userConfig
modulesetup['userConfig'] = userConfig
except NameError:
pass
try:
outputFileExtension
modulesetup['outputFileExtension'] = outputFileExtension
except NameError:
pass
try:
inputSource
modulesetup['inputSource'] = inputSource
except NameError:
pass
return modulesetup
2016-08-22 15:18:19 +02:00
def version():
moduleinfo['config'] = moduleconfig
return moduleinfo