Skip to content

Commit 8f102e8

Browse files
committed
[client] Implement detection helper
1 parent 8f8af8f commit 8f102e8

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

pyobas/helpers.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import pika
1313
import yaml
14+
from thefuzz import fuzz
1415

1516
from pyobas import OpenBAS, utils
1617

@@ -416,3 +417,51 @@ def listen(self, message_callback: Callable[[Dict], None]) -> None:
416417
self.config, self.injector_config, self.injector_logger, message_callback
417418
)
418419
self.listen_queue.start()
420+
421+
422+
class OpenBASDetectionHelper:
423+
def __init__(self, logger, relevant_signatures_types) -> None:
424+
self.logger = logger
425+
self.relevant_signatures_types = relevant_signatures_types
426+
427+
def match_alert_element_fuzzy(self, signature_value, alert_values, fuzzy_scoring):
428+
for alert_value in alert_values:
429+
self.logger.info(
430+
"Comparing alert value (" + alert_value + ", " + signature_value + ")"
431+
)
432+
ratio = fuzz.ratio(alert_value, signature_value)
433+
if ratio > fuzzy_scoring:
434+
self.logger.info("MATCHING! (score: " + str(ratio) + ")")
435+
return True
436+
return False
437+
438+
def match_alert_elements(self, signatures, alert_data):
439+
# Example for alert_data
440+
# {"process_name": {"list": ["xx", "yy"], "fuzzy": 90}}
441+
relevant_signatures = [
442+
s for s in signatures if s["type"] in self.relevant_signatures_types
443+
]
444+
445+
# Matching logics
446+
signatures_number = len(relevant_signatures)
447+
matching_number = 0
448+
for signature in relevant_signatures:
449+
alert_data_for_signature = alert_data[signature["type"]]
450+
signature_result = False
451+
if alert_data_for_signature["type"] == "fuzzy":
452+
signature_result = self.match_alert_element_fuzzy(
453+
signature["value"],
454+
alert_data_for_signature["data"],
455+
alert_data_for_signature["score"],
456+
)
457+
elif alert_data_for_signature["type"] == "simple":
458+
signature_result = signature["value"] in str(
459+
alert_data_for_signature["data"]
460+
)
461+
462+
if signature_result:
463+
matching_number = matching_number + 1
464+
465+
if signatures_number == matching_number:
466+
return True
467+
return False

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ opentelemetry-api~=1.24.0
1313
opentelemetry-sdk~=1.24.0
1414
# OpenBAS
1515
requests-toolbelt~=1.0.0
16-
dataclasses-json~=0.6.4
16+
dataclasses-json~=0.6.4
17+
thefuzz~=0.22

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ install_requires =
4848
# OpenBAS
4949
requests-toolbelt~=1.0.0
5050
dataclasses-json~=0.6.4
51+
thefuzz~=0.22
5152

5253
[options.extras_require]
5354
dev =

test-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ pytest_randomly~=3.8
99
types-python-dateutil>=2.8
1010
types-pytz>=2021.3.5
1111
wheel~=0.36
12+
thefuzz~=0.22

0 commit comments

Comments
 (0)