Agent: Move get_credentials() to credentials.py

This commit is contained in:
Mike Salvatore 2021-09-01 13:36:36 -04:00
parent a060313d09
commit e6399de860
4 changed files with 41 additions and 46 deletions

View File

@ -19,8 +19,7 @@ from infection_monkey.exploit.powershell_utils.auth_options import (
AuthOptions,
get_auth_options,
)
from infection_monkey.exploit.powershell_utils.credential_generators import get_credentials
from infection_monkey.exploit.powershell_utils.credentials import Credentials
from infection_monkey.exploit.powershell_utils.credentials import Credentials, get_credentials
from infection_monkey.exploit.powershell_utils.utils import (
IClient,
get_client_based_on_auth_options,

View File

@ -1,41 +0,0 @@
from itertools import product
from typing import List
from infection_monkey.exploit.powershell_utils.credentials import Credentials
def get_credentials(
usernames: List[str], passwords: List[str], is_windows: bool
) -> List[Credentials]:
credentials = []
credentials.extend(_get_empty_credentials(is_windows))
credentials.extend(_get_username_only_credentials(usernames, is_windows))
credentials.extend(_get_username_password_credentials(usernames, passwords))
return credentials
def _get_empty_credentials(is_windows: bool) -> List[Credentials]:
if is_windows:
return [Credentials(username=None, password=None)]
return []
def _get_username_only_credentials(usernames: List[str], is_windows: bool) -> List[Credentials]:
credentials = [Credentials(username=username, password="") for username in usernames]
if is_windows:
credentials.extend(
[Credentials(username=username, password=None) for username in usernames]
)
return credentials
def _get_username_password_credentials(
usernames: List[str], passwords: List[str]
) -> List[Credentials]:
username_password_pairs = product(usernames, passwords)
return [Credentials(credentials[0], credentials[1]) for credentials in username_password_pairs]

View File

@ -1,8 +1,46 @@
from dataclasses import dataclass
from typing import Union
from itertools import product
from typing import List, Union
@dataclass
class Credentials:
username: Union[str, None]
password: Union[str, None]
def get_credentials(
usernames: List[str], passwords: List[str], is_windows: bool
) -> List[Credentials]:
credentials = []
credentials.extend(_get_empty_credentials(is_windows))
credentials.extend(_get_username_only_credentials(usernames, is_windows))
credentials.extend(_get_username_password_credentials(usernames, passwords))
return credentials
def _get_empty_credentials(is_windows: bool) -> List[Credentials]:
if is_windows:
return [Credentials(username=None, password=None)]
return []
def _get_username_only_credentials(usernames: List[str], is_windows: bool) -> List[Credentials]:
credentials = [Credentials(username=username, password="") for username in usernames]
if is_windows:
credentials.extend(
[Credentials(username=username, password=None) for username in usernames]
)
return credentials
def _get_username_password_credentials(
usernames: List[str], passwords: List[str]
) -> List[Credentials]:
username_password_pairs = product(usernames, passwords)
return [Credentials(credentials[0], credentials[1]) for credentials in username_password_pairs]

View File

@ -1,5 +1,4 @@
from infection_monkey.exploit.powershell_utils.credential_generators import get_credentials
from infection_monkey.exploit.powershell_utils.credentials import Credentials
from infection_monkey.exploit.powershell_utils.credentials import Credentials, get_credentials
TEST_USERNAMES = ["user1", "user2"]
TEST_PASSWORDS = ["p1", "p2"]