2018-12-11 15:29:09 +01:00
|
|
|
import io
|
|
|
|
import json
|
2018-07-11 23:43:42 +02:00
|
|
|
try:
|
2019-01-21 14:14:19 +01:00
|
|
|
from sigma.parser.collection import SigmaCollectionParser
|
|
|
|
from sigma.configuration import SigmaConfiguration
|
|
|
|
from sigma.backends.discovery import getBackend
|
2018-09-08 02:53:15 +02:00
|
|
|
except ImportError:
|
2018-07-11 23:43:42 +02:00
|
|
|
print("sigma or yaml is missing, use 'pip3 install sigmatools' to install it.")
|
|
|
|
|
|
|
|
misperrors = {'error': 'Error'}
|
|
|
|
mispattributes = {'input': ['sigma'], 'output': ['text']}
|
|
|
|
moduleinfo = {'version': '0.1', 'author': 'Christian Studer', 'module-type': ['expansion', 'hover'],
|
|
|
|
'description': 'An expansion hover module to display the result of sigma queries.'}
|
|
|
|
moduleconfig = []
|
2020-07-03 10:10:24 +02:00
|
|
|
sigma_targets = ('es-dsl', 'es-qs', 'graylog', 'kibana', 'xpack-watcher', 'logpoint', 'splunk', 'grep', 'mdatp', 'splunkxml', 'arcsight', 'qualys')
|
2018-07-11 23:43:42 +02:00
|
|
|
|
2018-12-11 15:29:09 +01:00
|
|
|
|
2018-07-11 23:43:42 +02:00
|
|
|
def handler(q=False):
|
|
|
|
if q is False:
|
|
|
|
return False
|
|
|
|
request = json.loads(q)
|
|
|
|
if not request.get('sigma'):
|
|
|
|
misperrors['error'] = 'Sigma rule missing'
|
|
|
|
return misperrors
|
|
|
|
config = SigmaConfiguration()
|
|
|
|
f = io.TextIOWrapper(io.BytesIO(request.get('sigma').encode()), encoding='utf-8')
|
2019-06-15 08:06:47 +02:00
|
|
|
parser = SigmaCollectionParser(f, config)
|
2018-07-11 23:43:42 +02:00
|
|
|
targets = []
|
2019-06-15 08:06:47 +02:00
|
|
|
results = []
|
2018-07-11 23:43:42 +02:00
|
|
|
for t in sigma_targets:
|
2019-06-15 08:06:47 +02:00
|
|
|
backend = getBackend(t)(config, {'rulecomment': False})
|
2018-07-11 23:43:42 +02:00
|
|
|
try:
|
|
|
|
parser.generate(backend)
|
2019-06-15 08:06:47 +02:00
|
|
|
result = backend.finalize()
|
|
|
|
if result:
|
|
|
|
results.append(result)
|
|
|
|
targets.append(t)
|
2019-06-15 08:17:29 +02:00
|
|
|
except Exception:
|
2018-07-11 23:43:42 +02:00
|
|
|
continue
|
2018-12-11 15:29:09 +01:00
|
|
|
d_result = {t: r.strip() for t, r in zip(targets, results)}
|
2018-07-11 23:43:42 +02:00
|
|
|
return {'results': [{'types': mispattributes['output'], 'values': d_result}]}
|
|
|
|
|
2018-12-11 15:29:09 +01:00
|
|
|
|
2018-07-11 23:43:42 +02:00
|
|
|
def introspection():
|
|
|
|
return mispattributes
|
|
|
|
|
2018-12-11 15:29:09 +01:00
|
|
|
|
2018-07-11 23:43:42 +02:00
|
|
|
def version():
|
|
|
|
moduleinfo['config'] = moduleconfig
|
|
|
|
return moduleinfo
|