import warnings
from dataclasses import dataclass
from pathlib import Path
from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
from sigma.rule import SigmaRuleBase
[docs]
@dataclass
class SigmaRuleLocation:
"""Describes a Sigma source file and optionally a location inside it."""
path: Path
line: int | None = None
char: int | None = None
def __post_init__(self) -> None:
if isinstance(self.path, str):
self.path = Path(self.path)
def __str__(self) -> str:
s = str(self.path.resolve())
if self.line is not None:
s += ":" + str(self.line)
if self.char is not None:
s += ":" + str(self.char)
return s
[docs]
class SigmaError(ValueError):
"""Generic Sigma error and super-class of all Sigma exceptions"""
[docs]
def __init__(
self, *args: Any, source: SigmaRuleLocation | None = None, **kwargs: dict[str, Any]
) -> None:
self.source = source
super().__init__(*args, **kwargs)
def __str__(self) -> str:
if self.source is not None:
return super().__str__() + " in " + str(self.source)
else:
return super().__str__()
def __eq__(self, other: object) -> bool:
try:
return (
type(self) is type(other)
and hasattr(other, "source")
and self.source == other.source
and hasattr(other, "args")
and self.args == other.args
)
except AttributeError:
return False
[docs]
class SigmaValueError(SigmaError):
"""Error in Sigma rule value"""
pass
[docs]
class SigmaBackendError(SigmaError):
"""Error in Sigma backend."""
pass
[docs]
class SigmaCollectionError(SigmaError):
"""Error in Sigma collection, e.g. unknown action"""
pass
[docs]
class SigmaConditionError(SigmaError):
"""Error in Sigma rule condition"""
pass
[docs]
class SigmaConfigurationError(SigmaError):
"""Error in configuration of a Sigma processing pipeline"""
pass
[docs]
class SigmaConversionError(SigmaError):
"""Rule conversion failed."""
[docs]
def __init__(
self,
rule: "SigmaRuleBase",
source: SigmaRuleLocation | None = None,
*args: Any,
**kwargs: dict[str, Any],
) -> None:
self.rule = rule
super().__init__(*args, source=source, **kwargs)
def __str__(self) -> str:
return super().__str__() + " in rule " + str(self.rule)
[docs]
class SigmaDetectionError(SigmaError):
"""Error in Sigma rule detection"""
pass
[docs]
class SigmaFeatureNotSupportedByBackendError(SigmaError):
"""Sigma feature is not supported by the backend."""
pass
[docs]
class SigmaModifierError(SigmaError):
"""Error in Sigma rule value modifier"""
pass
class SigmaPipelineNotAllowedForBackendError(SigmaConfigurationError):
"""One or multiple processing pipelines doesn't matches the given backend."""
def __init__(
self,
spec: str,
backend: str,
source: SigmaRuleLocation | None = None,
*args: list[Any],
**kwargs: dict[str, Any],
):
self.wrong_pipeline = spec
self.backend = backend
super().__init__(*args, source=source, **kwargs)
def __str__(self) -> str:
return (
f"Processing pipelines not allowed for backend '{self.backend}': {self.wrong_pipeline}"
)
[docs]
class SigmaPipelineNotFoundError(SigmaError, ValueError):
"""An attempt to resolve a processing pipeline from a specifier failed because it was not
found."""
[docs]
def __init__(
self,
spec: str,
source: SigmaRuleLocation | None = None,
*args: list[Any],
**kwargs: dict[str, Any],
) -> None:
self.spec = spec
super().__init__(*args, source=source, **kwargs)
def __str__(self) -> str:
return f"Processing pipeline '{self.spec}' not found"
[docs]
class SigmaPipelineParsingError(SigmaError):
"""Error in parsing of a Sigma processing pipeline"""
pass
class SigmaPolicyError(SigmaError):
"""Error related to incompatible Sigma policy usage."""
pass
[docs]
class SigmaProcessingItemError(SigmaError):
"""Error in definition or state of processing item."""
pass
[docs]
class SigmaPlaceholderError(SigmaValueError):
"""Attempted to convert an unhandled Placeholder into a query"""
pass
[docs]
class SigmaPluginNotFoundError(SigmaError):
"""Plugin was not found."""
pass
[docs]
class SigmaRegularExpressionError(SigmaValueError):
"""Error in regular expression contained in Sigma rule"""
pass
class SigmaSecurityError(SigmaError):
"""Security-sensitive operation was attempted without explicit opt-in."""
pass
[docs]
class SigmaTypeError(SigmaModifierError):
"""Sigma modifier not applicable on value type"""
pass
[docs]
class SigmaValidatorConfigurationParsingError(SigmaError):
"""Error in parsing of a Sigma validation configuration file."""
pass
# Meta Rule Correlation Error
[docs]
class SigmaCorrelationRuleError(SigmaValueError):
"""Error in Sigma correlation rule."""
pass
[docs]
class SigmaCorrelationConditionError(SigmaCorrelationRuleError):
"""Error in Sigma correlation condition."""
pass
[docs]
class SigmaCorrelationTypeError(SigmaCorrelationRuleError):
"""Wrong Sigma correlation type."""
pass
[docs]
class SigmaTimespanError(SigmaCorrelationRuleError):
"""Raised when the timespan for calculating sigma is invalid."""
pass
[docs]
class SigmaRuleNotFoundError(SigmaCorrelationRuleError):
"""Sigma rule not found."""
pass
# Meta Filter Error
[docs]
class SigmaFilterError(SigmaValueError):
"""Error in Sigma rule filter"""
pass
[docs]
class SigmaFilterConditionError(SigmaFilterError):
"""Error in Sigma rule filter condition"""
pass
[docs]
class SigmaFilterRuleReferenceError(SigmaFilterError):
"""Error in Sigma rule filter condition"""
pass
# Rule Fields error
class SigmaAuthorError(SigmaError):
"""Error in Sigma rule author"""
pass
class SigmaDateError(SigmaError):
"""Error in Sigma rule date"""
pass
[docs]
class SigmaPipelineConditionError(SigmaConfigurationError):
"""Error in Sigma pipeline condition"""
[docs]
def __init__(
self,
error: str,
expression: str | None = None,
location: int | None = None,
*args: Any,
source: SigmaRuleLocation | None = None,
**kwargs: dict[str, Any],
):
self.expression = expression
self.location = location
super().__init__(error, *args, source=source, **kwargs)
def __str__(self) -> str:
location_info = f" at location {self.location}" if self.location is not None else ""
expression_info = f" in expression '{self.expression}'" if self.expression else ""
return f"{super().__str__()}{expression_info}{location_info}"
class SigmaDescriptionError(SigmaError):
"""Error in Sigma rule description"""
pass
class SigmaFalsePositivesError(SigmaError):
"""Error in Sigma rule falsepositives field"""
pass
class SigmaFieldsError(SigmaError):
"""Error in Sigma rule fields field"""
pass
class SigmaIdentifierError(SigmaError):
"""Error in Sigma rule identifier"""
pass
class SigmaTaxonomyError(SigmaError):
"""Error in Sigma rule identifier"""
pass
class SigmaLevelError(SigmaError):
"""Error in Sigma rule level"""
pass
class SigmaLicenseError(SigmaError):
"""Error in Sigma rule license"""
pass
[docs]
class SigmaLogsourceError(SigmaError):
"""Error in Sigma rule logsource"""
pass
class SigmaModifiedError(SigmaError):
"""Error in Sigma rule modified field"""
pass
class SigmaNameError(SigmaError):
"""Error in Sigma rule name"""
pass
class SigmaReferencesError(SigmaError):
"""Error in Sigma rule references"""
pass
class SigmaRelatedError(SigmaError):
"""Error in Sigma rule related field"""
pass
class SigmaScopeError(SigmaError):
"""Error in Sigma rule scope"""
pass
class SigmaStatusError(SigmaError):
"""Error in Sigma rule status"""
pass
class SigmaTitleError(SigmaError):
"""Error in Sigma rule title"""
pass
class SigmaTagError(SigmaError):
"""Error in Sigma rule tag"""
pass
@dataclass
class ExceptionOnUsage:
"""Raise an exception when the class is used."""
exception: Exception
def __getattribute__(self, item: str) -> Any:
deprecation_message: str = (
f"{type(self).__name__} is deprecated and will be removed in a future release."
)
warnings.warn(deprecation_message, DeprecationWarning)
raise object.__getattribute__(self, "exception")