Island, Agent, Common: rename CredentialsType to CredentialComponentType

This commit is contained in:
vakarisz 2022-02-22 16:12:02 +02:00
parent c87297eb2a
commit 719d8dd2ad
9 changed files with 36 additions and 41 deletions

View File

@ -1,7 +1,7 @@
from enum import Enum from enum import Enum
class CredentialsType(Enum): class CredentialComponentType(Enum):
USERNAME = "username" USERNAME = "username"
PASSWORD = "password" PASSWORD = "password"
NT_HASH = "nt_hash" NT_HASH = "nt_hash"

View File

@ -1,10 +1,12 @@
from dataclasses import dataclass, field from dataclasses import dataclass, field
from common.common_consts.credentials_type import CredentialsType from common.common_consts.credential_component_type import CredentialComponentType
from infection_monkey.i_puppet import ICredentialComponent from infection_monkey.i_puppet import ICredentialComponent
@dataclass(frozen=True) @dataclass(frozen=True)
class LMHash(ICredentialComponent): class LMHash(ICredentialComponent):
credential_type: CredentialsType = field(default=CredentialsType.LM_HASH.value, init=False) credential_type: CredentialComponentType = field(
default=CredentialComponentType.LM_HASH.value, init=False
)
lm_hash: str lm_hash: str

View File

@ -1,10 +1,12 @@
from dataclasses import dataclass, field from dataclasses import dataclass, field
from common.common_consts.credentials_type import CredentialsType from common.common_consts.credential_component_type import CredentialComponentType
from infection_monkey.i_puppet import ICredentialComponent from infection_monkey.i_puppet import ICredentialComponent
@dataclass(frozen=True) @dataclass(frozen=True)
class NTHash(ICredentialComponent): class NTHash(ICredentialComponent):
credential_type: CredentialsType = field(default=CredentialsType.NT_HASH.value, init=False) credential_type: CredentialComponentType = field(
default=CredentialComponentType.NT_HASH.value, init=False
)
nt_hash: str nt_hash: str

View File

@ -1,10 +1,12 @@
from dataclasses import dataclass, field from dataclasses import dataclass, field
from common.common_consts.credentials_type import CredentialsType from common.common_consts.credential_component_type import CredentialComponentType
from infection_monkey.i_puppet import ICredentialComponent from infection_monkey.i_puppet import ICredentialComponent
@dataclass(frozen=True) @dataclass(frozen=True)
class Password(ICredentialComponent): class Password(ICredentialComponent):
credential_type: CredentialsType = field(default=CredentialsType.PASSWORD.value, init=False) credential_type: CredentialComponentType = field(
default=CredentialComponentType.PASSWORD.value, init=False
)
password: str password: str

View File

@ -1,11 +1,13 @@
from dataclasses import dataclass, field from dataclasses import dataclass, field
from common.common_consts.credentials_type import CredentialsType from common.common_consts.credential_component_type import CredentialComponentType
from infection_monkey.i_puppet import ICredentialComponent from infection_monkey.i_puppet import ICredentialComponent
@dataclass(frozen=True) @dataclass(frozen=True)
class SSHKeypair(ICredentialComponent): class SSHKeypair(ICredentialComponent):
credential_type: CredentialsType = field(default=CredentialsType.SSH_KEYPAIR.value, init=False) credential_type: CredentialComponentType = field(
default=CredentialComponentType.SSH_KEYPAIR.value, init=False
)
private_key: str private_key: str
public_key: str public_key: str

View File

@ -1,10 +1,12 @@
from dataclasses import dataclass, field from dataclasses import dataclass, field
from common.common_consts.credentials_type import CredentialsType from common.common_consts.credential_component_type import CredentialComponentType
from infection_monkey.i_puppet import ICredentialComponent from infection_monkey.i_puppet import ICredentialComponent
@dataclass(frozen=True) @dataclass(frozen=True)
class Username(ICredentialComponent): class Username(ICredentialComponent):
credential_type: CredentialsType = field(default=CredentialsType.USERNAME.value, init=False) credential_type: CredentialComponentType = field(
default=CredentialComponentType.USERNAME.value, init=False
)
username: str username: str

View File

@ -1,10 +1,10 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from common.common_consts.credentials_type import CredentialsType from common.common_consts.credential_component_type import CredentialComponentType
class ICredentialComponent(ABC): class ICredentialComponent(ABC):
@property @property
@abstractmethod @abstractmethod
def credential_type(self) -> CredentialsType: def credential_type(self) -> CredentialComponentType:
pass pass

View File

@ -1,44 +1,29 @@
import logging import logging
from typing import Mapping
from common.common_consts.credentials_type import CredentialsType from common.common_consts.credential_component_type import CredentialComponentType
from .identities.username_processor import process_username from .identities.username_processor import process_username
from .secrets.lm_hash_processor import process_lm_hash from .secrets.lm_hash_processor import process_lm_hash
from .secrets.nt_hash_processor import process_nt_hash from .secrets.nt_hash_processor import process_nt_hash
from .secrets.password_processor import process_password from .secrets.password_processor import process_password
from .secrets.ssh_key_processor import process_ssh_key
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
SECRET_PROCESSORS = { SECRET_PROCESSORS = {
CredentialsType.PASSWORD.value: process_password, CredentialComponentType.PASSWORD.value: process_password,
CredentialsType.NT_HASH.value: process_nt_hash, CredentialComponentType.NT_HASH.value: process_nt_hash,
CredentialsType.LM_HASH.value: process_lm_hash, CredentialComponentType.LM_HASH.value: process_lm_hash,
CredentialsType.SSH_KEYPAIR.value: process_ssh_key,
} }
IDENTITY_PROCESSORS = { IDENTITY_PROCESSORS = {
CredentialsType.USERNAME.value: process_username, CredentialComponentType.USERNAME.value: process_username,
} }
def parse_credentials(credentials: dict): def parse_credentials(credentials: Mapping):
for credential in credentials["data"]: for credential in credentials["data"]:
if is_ssh_keypair(credential): for identity in credential["identities"]:
SECRET_PROCESSORS[CredentialsType.SSH_KEYPAIR.value](credential, credentials["monkey_guid"]) IDENTITY_PROCESSORS[identity["credential_type"]](identity)
else: for secret in credential["secrets"]:
for identity in credential["identities"]: SECRET_PROCESSORS[secret["credential_type"]](secret)
IDENTITY_PROCESSORS[identity["credential_type"]](identity)
for secret in credential["secrets"]:
SECRET_PROCESSORS[secret["credential_type"]](secret)
def is_ssh_keypair(credential: dict) -> bool:
return bool(
[
secret
for secret in credential["secrets"]
if secret["credential_type"] == CredentialsType.SSH_KEYPAIR.value
]
)

View File

@ -1,4 +1,4 @@
from common.common_consts.credentials_type import CredentialsType from common.common_consts.credentials_type import CredentialComponentType
from monkey_island.cc.models import Monkey from monkey_island.cc.models import Monkey
from monkey_island.cc.server_utils.encryption import get_datastore_encryptor from monkey_island.cc.server_utils.encryption import get_datastore_encryptor
from monkey_island.cc.services.config import ConfigService from monkey_island.cc.services.config import ConfigService
@ -17,7 +17,7 @@ def process_ssh_key(credentials: dict, monkey_guid: str):
) )
for ssh_key in credentials["secrets"]: for ssh_key in credentials["secrets"]:
if not ssh_key["credential_type"] == CredentialsType.SSH_KEYPAIR.value: if not ssh_key["credential_type"] == CredentialComponentType.SSH_KEYPAIR.value:
raise SSHKeyProcessingError("SSH credentials contain secrets that are not keypairs") raise SSHKeyProcessingError("SSH credentials contain secrets that are not keypairs")
if not ssh_key["public_key"] or not ssh_key["private_key"]: if not ssh_key["public_key"] or not ssh_key["private_key"]: