# Output backends for sigmac # Copyright 2016-2018 Thomas Patzke, Florian Roth, Roey, Karneades # 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 . import re import sigma from .base import SingleTextQueryBackend from .mixins import MultiRuleOutputMixin class PowerShellBackend(SingleTextQueryBackend): """Converts Sigma rule into PowerShell event log cmdlets.""" identifier = "powershell" active = True config_required = False default_config = ["sysmon", "powershell"] options = ( ("csv", False, "Return the results in CSV format instead of Powershell objects", None), ) reEscape = re.compile('("|(?"): return "-gt" elif(cond_op == "="): return "-eq" def generateAggregation(self, agg): if agg == None: return "" if agg.aggfunc != sigma.parser.condition.SigmaAggregationParser.AGGFUNC_COUNT: raise NotImplementedError("Only COUNT aggregation function is implemented for this backend") if agg.aggfunc == sigma.parser.condition.SigmaAggregationParser.AGGFUNC_NEAR: # python .\tools\sigmac -t splunk -c .\tools\config\splunk-windows-all.yml -r .\rules\windows\builtin\ # Example rule: .\sigma\rules\windows\builtin\win_susp_samr_pwset.yml raise NotImplementedError("The 'near' aggregation operator is not yet implemented for this backend") if agg.groupfield == None: # Example rule: .\sigma\rules\windows\builtin\win_multiple_suspicious_cli.yml powershell_cond_op = self.getPowerShellCondOp(agg.cond_op) return " | group-object %s | where { $_.count %s %s } | select name,count | sort -desc" % (agg.aggfield or "", powershell_cond_op, agg.condition) else: # Example rule: .\sigma\rules\windows\other\win_rare_schtask_creation.yml powershell_cond_op = self.getPowerShellCondOp(agg.cond_op) if (agg.aggfield == None): return " | group-object %s | where { $_.count %s %s } | select name,count | sort -desc" % (agg.groupfield or "", powershell_cond_op, agg.condition) else: return " | select %s, %s | group %s | foreach { [PSCustomObject]@{'%s'=$_.name;'Count'=($_.group.%s | sort -u).count} } | sort count -desc | where { $_.count %s %s }" % (agg.groupfield, agg.aggfield, agg.groupfield, agg.groupfield, agg.aggfield, powershell_cond_op, agg.condition)