From e5702032d9a9aafd8b1be04687307c3e309b6a3a Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Mon, 11 Jul 2022 17:21:39 +0200 Subject: [PATCH] Island: Use ICredentialsRepository in credentials telemetry processing --- .../credentials/credentials_parser.py | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/monkey/monkey_island/cc/services/telemetry/processing/credentials/credentials_parser.py b/monkey/monkey_island/cc/services/telemetry/processing/credentials/credentials_parser.py index 8ab905ce6..aba21a1e1 100644 --- a/monkey/monkey_island/cc/services/telemetry/processing/credentials/credentials_parser.py +++ b/monkey/monkey_island/cc/services/telemetry/processing/credentials/credentials_parser.py @@ -2,10 +2,9 @@ import logging from itertools import chain from typing import Mapping -from common.credentials import CredentialComponentType -from monkey_island.cc.models import StolenCredentials +from common.credentials import CredentialComponentType, Credentials +from monkey_island.cc.repository import ICredentialsRepository -from .credentials import Credentials from .identities.username_processor import process_username from .secrets.lm_hash_processor import process_lm_hash from .secrets.nt_hash_processor import process_nt_hash @@ -23,19 +22,24 @@ CREDENTIAL_COMPONENT_PROCESSORS = { } -def parse_credentials(telemetry_dict: Mapping): - credentials = [ - Credentials.from_mapping(credential, telemetry_dict["monkey_guid"]) - for credential in telemetry_dict["data"] - ] +class CredentialsParser: + """ + This class parses and stores telemetry credentials. + """ - for credential in credentials: - _store_in_db(credential) - for cred_comp in chain(credential.identities, credential.secrets): - credential_type = CredentialComponentType[cred_comp["credential_type"]] - CREDENTIAL_COMPONENT_PROCESSORS[credential_type](cred_comp, credential) + def __init__(self, credentials_repository: ICredentialsRepository): + self._credentials_repository = credentials_repository + def __call__(self, telemetry_dict): + self._parse_credentials(telemetry_dict) -def _store_in_db(credentials: Credentials): - stolen_cred_doc = StolenCredentials.from_credentials(credentials) - stolen_cred_doc.save() + def _parse_credentials(self, telemetry_dict: Mapping): + credentials = [ + Credentials.from_mapping(credential) for credential in telemetry_dict["data"] + ] + self._credentials_repository.save_stolen_credentials(credentials) + + for credential in credentials: + for cred_comp in chain(credential.identities, credential.secrets): + credential_type = CredentialComponentType[cred_comp["credential_type"]] + CREDENTIAL_COMPONENT_PROCESSORS[credential_type](cred_comp, credential)