2017-02-13 22:14:40 +00:00
|
|
|
# Output backends for sigmac
|
|
|
|
|
|
|
|
import json
|
2017-02-22 21:47:12 +00:00
|
|
|
import re
|
|
|
|
import sigma
|
2017-02-13 22:14:40 +00:00
|
|
|
|
|
|
|
def getBackendList():
|
|
|
|
"""Return list of backend classes"""
|
|
|
|
return list(filter(lambda cls: type(cls) == type and issubclass(cls, BaseBackend) and cls.active, [item[1] for item in globals().items()]))
|
|
|
|
|
|
|
|
def getBackendDict():
|
|
|
|
return {cls.identifier: cls for cls in getBackendList() }
|
|
|
|
|
2017-02-22 21:47:12 +00:00
|
|
|
def getBackend(name):
|
|
|
|
try:
|
|
|
|
return getBackendDict()[name]
|
|
|
|
except KeyError as e:
|
|
|
|
raise LookupError("Backend not found") from e
|
|
|
|
|
2017-02-13 22:14:40 +00:00
|
|
|
class BaseBackend:
|
|
|
|
"""Base class for all backends"""
|
|
|
|
identifier = "base"
|
|
|
|
active = False
|
|
|
|
|
2017-02-22 21:47:12 +00:00
|
|
|
def generate(self, parsed):
|
|
|
|
raise NotImplementedError("Backend is not implemented yet")
|
|
|
|
|
2017-02-13 22:14:40 +00:00
|
|
|
class ElasticsearchQuerystringBackend(BaseBackend):
|
|
|
|
"""Converts Sigma rule into Elasticsearch query string. Only searches, no aggregations."""
|
|
|
|
identifier = "es-qs"
|
|
|
|
active = True
|
2017-02-22 21:47:12 +00:00
|
|
|
reEscape = re.compile("([+\\-=!(){}\\[\\]^\"~*?:\\\\/]|&&|\\|\\|)")
|
|
|
|
reClear = re.compile("[<>]")
|
|
|
|
|
|
|
|
def generate(self, parsed):
|
|
|
|
return self.generateNode(parsed.getParseTree())
|
|
|
|
|
|
|
|
def cleanValue(self, val):
|
|
|
|
val = self.reEscape.sub("\\\\\g<1>", val)
|
|
|
|
return self.reClear.sub("", val)
|
|
|
|
|
|
|
|
def generateNode(self, node):
|
|
|
|
if type(node) == sigma.ConditionAND:
|
|
|
|
return " AND ".join([self.generateNode(val) for val in node])
|
|
|
|
elif type(node) == sigma.ConditionOR:
|
|
|
|
return " OR ".join([self.generateNode(val) for val in node])
|
|
|
|
elif type(node) == sigma.ConditionNOT:
|
|
|
|
return "NOT " + self.generateNode(node.item)
|
|
|
|
elif type(node) == sigma.NodeSubexpression:
|
|
|
|
return "(%s)" % self.generateNode(node.items)
|
|
|
|
elif type(node) == tuple:
|
|
|
|
key, value = node
|
|
|
|
if type(value) not in (str, int, list):
|
|
|
|
raise TypeError("Map values must be strings, numbers or lists, not " + str(type(value)))
|
|
|
|
return "%s:%s" % (key, self.generateNode(value))
|
|
|
|
elif type(node) in (str, int):
|
|
|
|
return "\"%s\"" % (self.cleanValue(str(node)))
|
|
|
|
elif type(node) == list:
|
|
|
|
if not set([type(value) for value in node]).issubset({str, int}):
|
|
|
|
raise TypeError("List values must be strings or numbers")
|
|
|
|
return "(%s)" % (" ".join([self.generateNode(value) for value in node]))
|
|
|
|
else:
|
|
|
|
raise TypeError("Node type %s was not expected in Sigma parse tree" % (str(type(node))))
|
2017-02-13 22:14:40 +00:00
|
|
|
|
|
|
|
class ElasticsearchDSLBackend(BaseBackend):
|
|
|
|
"""Converts Sigma rule into Elasticsearch DSL query (JSON)."""
|
|
|
|
identifier = "es-dsl"
|
|
|
|
active = True
|
|
|
|
|
|
|
|
class KibanaBackend(ElasticsearchDSLBackend):
|
|
|
|
"""Converts Sigma rule into Kibana JSON Configurations."""
|
|
|
|
identifier = "kibana"
|
|
|
|
active = True
|
|
|
|
|
|
|
|
class SplunkBackend(BaseBackend):
|
|
|
|
"""Converts Sigma rule into Splunk Search Processing Language (SPL)."""
|
|
|
|
identifier = "splunk"
|
|
|
|
active = True
|
|
|
|
|
|
|
|
class NullBackend(BaseBackend):
|
|
|
|
"""Does nothing, for debugging purposes."""
|
|
|
|
identifier = "null"
|
|
|
|
active = True
|
2017-02-22 21:47:12 +00:00
|
|
|
|
|
|
|
def generate(self, parsed):
|
|
|
|
pass
|