|
| 1 | +from enum import Enum |
| 2 | +from typing import List |
| 3 | +from uuid import UUID |
| 4 | + |
| 5 | +from pydantic import BaseModel |
| 6 | +from thefuzz import fuzz |
| 7 | + |
| 8 | +from pyobas.signatures.signature_type import SignatureType |
| 9 | +from pyobas.signatures.types import MatchTypes, SignatureTypes |
| 10 | + |
| 11 | + |
| 12 | +class ExpectationTypeEnum(str, Enum): |
| 13 | + """Types of Expectations""" |
| 14 | + |
| 15 | + Detection = "DETECTION" |
| 16 | + Prevention = "PREVENTION" |
| 17 | + Other = "other" |
| 18 | + |
| 19 | + @classmethod |
| 20 | + def _missing_(cls, value): |
| 21 | + return cls.Other |
| 22 | + |
| 23 | + |
| 24 | +class ExpectationSignature(BaseModel): |
| 25 | + """An expectation signature describes a known marker potentially |
| 26 | + found in alerting data in security software. For example, an |
| 27 | + expectation signature can be a process image name, a command |
| 28 | + line, or any other relevant piece of data. |
| 29 | + """ |
| 30 | + |
| 31 | + type: SignatureTypes |
| 32 | + value: str |
| 33 | + |
| 34 | + |
| 35 | +class Expectation(BaseModel): |
| 36 | + """An expectation represents an expected outcome of a BAS run. |
| 37 | + For example, in the case of running an attack command line, the |
| 38 | + expectation may be that security software has _detected_ it, while |
| 39 | + another expectation may be that the attack was _prevented_. |
| 40 | + """ |
| 41 | + |
| 42 | + inject_expectation_id: UUID |
| 43 | + inject_expectation_signatures: List[ExpectationSignature] |
| 44 | + |
| 45 | + success_label: str = "Success" |
| 46 | + failure_label: str = "Failure" |
| 47 | + |
| 48 | + def __init__(self, *a, **kw): |
| 49 | + super().__init__(*a, **kw) |
| 50 | + self.__api_client = kw["api_client"] |
| 51 | + |
| 52 | + def update(self, success, sender_id, metadata): |
| 53 | + """Update the expectation object in OpenBAS with the supplied outcome. |
| 54 | +
|
| 55 | + :param success: whether the expectation was fulfilled (true) or not (false) |
| 56 | + :type success: bool |
| 57 | + :param sender_id: identifier of the collector that is updating the expectation |
| 58 | + :type sender_id: string |
| 59 | + :param metadata: arbitrary dictionary of additional data relevant to updating the expectation |
| 60 | + :type metadata: dict[string,string] |
| 61 | + """ |
| 62 | + self.__api_client.update( |
| 63 | + self.inject_expectation_id, |
| 64 | + inject_expectation={ |
| 65 | + "collector_id": sender_id, |
| 66 | + "result": (self.success_label if success else self.failure_label), |
| 67 | + "is_success": success, |
| 68 | + "metadata": metadata, |
| 69 | + }, |
| 70 | + ) |
| 71 | + |
| 72 | + def match_alert(self, relevant_signature_types: list[SignatureType], alert_data): |
| 73 | + """Matches an alert's data against the current expectation signatures |
| 74 | + to see if the alert is relevant to the current expectation's inject, |
| 75 | + i.e. this alert was triggered by the execution of the inject to which |
| 76 | + belongs the expectation. |
| 77 | +
|
| 78 | + :param relevant_signature_types: filter of signature types that we want to consider. |
| 79 | + Only the signature types listed in this collection may be checked for matching. |
| 80 | + :type relevant_signature_types: list[SignatureType] |
| 81 | + :param alert_data: list of possibly relevant markers found in an alert. |
| 82 | + :type alert_data: dict[SignatureTypes, dict] |
| 83 | +
|
| 84 | + :return: whether the alert matches the expectation signatures or not. |
| 85 | + :rtype: bool |
| 86 | + """ |
| 87 | + relevant_expectation_signatures = [ |
| 88 | + signature |
| 89 | + for signature in self.inject_expectation_signatures |
| 90 | + if signature.type in [type.label for type in relevant_signature_types] |
| 91 | + ] |
| 92 | + if not any(relevant_expectation_signatures): |
| 93 | + return False |
| 94 | + |
| 95 | + for relevant_expectation_signature in relevant_expectation_signatures: |
| 96 | + if not ( |
| 97 | + alert_signature_for_type := alert_data.get( |
| 98 | + relevant_expectation_signature.type.value |
| 99 | + ) |
| 100 | + ): |
| 101 | + return False |
| 102 | + |
| 103 | + if alert_signature_for_type[ |
| 104 | + "type" |
| 105 | + ] == MatchTypes.MATCH_TYPE_FUZZY and not self.match_fuzzy( |
| 106 | + alert_signature_for_type["data"], |
| 107 | + relevant_expectation_signature.value, |
| 108 | + alert_signature_for_type["score"], |
| 109 | + ): |
| 110 | + return False |
| 111 | + if alert_signature_for_type[ |
| 112 | + "type" |
| 113 | + ] == MatchTypes.MATCH_TYPE_SIMPLE and not self.match_simple( |
| 114 | + alert_signature_for_type["data"], relevant_expectation_signature.value |
| 115 | + ): |
| 116 | + return False |
| 117 | + |
| 118 | + return True |
| 119 | + |
| 120 | + @staticmethod |
| 121 | + def match_fuzzy(tested: list[str], reference: str, threshold: int): |
| 122 | + """Applies a fuzzy match against a known reference to a list of candidates |
| 123 | +
|
| 124 | + :param tested: list of strings candidate for fuzzy matching |
| 125 | + :type tested: list[str] |
| 126 | + :param reference: the reference against which to try to fuzzy match |
| 127 | + :type reference: str |
| 128 | + :param threshold: string overlap percentage threshold above which to declare a match |
| 129 | + :type threshold: int |
| 130 | +
|
| 131 | + :return: whether any of the candidate is a match against the reference |
| 132 | + :rtype: bool |
| 133 | + """ |
| 134 | + actual_tested = [tested] if isinstance(tested, str) else tested |
| 135 | + for value in actual_tested: |
| 136 | + ratio = fuzz.ratio(value, reference) |
| 137 | + if ratio >= threshold: |
| 138 | + return True |
| 139 | + return False |
| 140 | + |
| 141 | + @staticmethod |
| 142 | + def match_simple(tested: list[str], reference: str): |
| 143 | + """A simple strict, case-sensitive string matching between a list of |
| 144 | + candidates and a reference. |
| 145 | +
|
| 146 | + :param tested: list of strings candidate for fuzzy matching |
| 147 | + :type tested: list[str] |
| 148 | + :param reference: the reference against which to try to fuzzy match |
| 149 | + :type reference: str |
| 150 | +
|
| 151 | + :return: whether any of the candidate is a match against the reference |
| 152 | + :rtype: bool |
| 153 | + """ |
| 154 | + return Expectation.match_fuzzy(tested, reference, threshold=100) |
| 155 | + |
| 156 | + |
| 157 | +class DetectionExpectation(Expectation): |
| 158 | + """An expectation that is specific to Detection, i.e. that is used |
| 159 | + by OpenBAS to assert that an inject's execution was detected. |
| 160 | + """ |
| 161 | + |
| 162 | + success_label: str = "Detected" |
| 163 | + failure_label: str = "Not Detected" |
| 164 | + |
| 165 | + |
| 166 | +class PreventionExpectation(Expectation): |
| 167 | + """An expectation that is specific to Prevention, i.e. that is used |
| 168 | + by OpenBAS to assert that an inject's execution was prevented. |
| 169 | + """ |
| 170 | + |
| 171 | + success_label: str = "Prevented" |
| 172 | + failure_label: str = "Not Prevented" |
0 commit comments