2017-02-13 23:14:40 +01:00
|
|
|
# Output backends for sigmac
|
2018-07-24 00:01:16 +02:00
|
|
|
# Copyright 2016-2018 Thomas Patzke
|
2017-12-07 21:55:43 +01:00
|
|
|
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-02-13 23:14:40 +01:00
|
|
|
|
2017-02-22 22:47:12 +01:00
|
|
|
import sigma
|
2017-02-13 23:14:40 +01:00
|
|
|
|
2018-07-10 23:42:38 +02:00
|
|
|
### Mixins
|
2017-10-18 19:03:38 +02:00
|
|
|
class QuoteCharMixin:
|
|
|
|
|
"""
|
|
|
|
|
This class adds the cleanValue method that quotes and filters characters according to the configuration in
|
|
|
|
|
the attributes provided by the mixin.
|
|
|
|
|
"""
|
|
|
|
|
reEscape = None # match characters that must be quoted
|
|
|
|
|
escapeSubst = "\\\\\g<1>" # Substitution that is applied to characters/strings matched for escaping by reEscape
|
|
|
|
|
reClear = None # match characters that are cleaned out completely
|
|
|
|
|
|
|
|
|
|
def cleanValue(self, val):
|
|
|
|
|
if self.reEscape:
|
|
|
|
|
val = self.reEscape.sub(self.escapeSubst, val)
|
|
|
|
|
if self.reClear:
|
|
|
|
|
val = self.reClear.sub("", val)
|
|
|
|
|
return val
|
|
|
|
|
|
2018-01-27 23:48:10 +01:00
|
|
|
class RulenameCommentMixin:
|
|
|
|
|
"""Prefixes each rule with the rule title."""
|
|
|
|
|
prefix = "# "
|
2018-03-21 01:13:10 +01:00
|
|
|
options = (
|
|
|
|
|
("rulecomment", False, "Prefix generated query with comment containing title", None),
|
|
|
|
|
)
|
2018-01-27 23:48:10 +01:00
|
|
|
|
|
|
|
|
def generateBefore(self, parsed):
|
2018-03-21 01:13:10 +01:00
|
|
|
if self.rulecomment:
|
2018-01-27 23:48:10 +01:00
|
|
|
try:
|
2018-05-13 22:36:51 +02:00
|
|
|
return "%s%s\n" % (self.prefix, parsed.sigmaParser.parsedyaml['title'])
|
2018-01-27 23:48:10 +01:00
|
|
|
except KeyError:
|
|
|
|
|
return ""
|
|
|
|
|
|
2018-05-13 22:36:51 +02:00
|
|
|
def generateAfter(self, parsed):
|
|
|
|
|
if self.rulecomment:
|
|
|
|
|
return "\n"
|
|
|
|
|
|
2017-09-30 01:03:08 +02:00
|
|
|
class MultiRuleOutputMixin:
|
|
|
|
|
"""Mixin with common for multi-rule outputs"""
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
self.rulenames = set()
|
|
|
|
|
|
|
|
|
|
def getRuleName(self, sigmaparser):
|
|
|
|
|
"""
|
|
|
|
|
Generate a rule name from the title of the Sigma rule with following properties:
|
|
|
|
|
|
|
|
|
|
* Spaces are replaced with -
|
|
|
|
|
* Unique name by addition of a counter if generated name already in usage
|
|
|
|
|
|
|
|
|
|
Generated names are tracked by the Mixin.
|
2018-04-06 17:36:11 +02:00
|
|
|
|
2017-09-30 01:03:08 +02:00
|
|
|
"""
|
2018-03-17 00:44:50 +01:00
|
|
|
rulename = sigmaparser.parsedyaml["title"].replace(" ", "-").replace("(", "").replace(")", "")
|
2017-09-30 01:03:08 +02:00
|
|
|
if rulename in self.rulenames: # add counter if name collides
|
|
|
|
|
cnt = 2
|
|
|
|
|
while "%s-%d" % (rulename, cnt) in self.rulenames:
|
|
|
|
|
cnt += 1
|
|
|
|
|
rulename = "%s-%d" % (rulename, cnt)
|
|
|
|
|
self.rulenames.add(rulename)
|
|
|
|
|
|
|
|
|
|
return rulename
|