Path: blob/main/cyberbattle/simulation/commandcontrol_test.py
960 views
# Copyright (c) Microsoft Corporation.1# Licensed under the MIT License.23"""4Unit tests for commandcontrol.py.56"""7# pylint: disable=missing-function-docstring89from . import model, commandcontrol10from ..samples.toyctf import toy_ctf as ctf111213def test_toyctf() -> None:14# Use the C&C to exploit remote and local vulnerabilities in the toy CTF game15network = model.create_network(ctf.nodes)16env = model.Environment(network=network, vulnerability_library=dict([]), identifiers=ctf.ENV_IDENTIFIERS)17command = commandcontrol.CommandControl(env)18leak_website = command.run_attack("client", "SearchEdgeHistory")19assert leak_website20github = command.run_remote_attack("client", "Website", "ScanPageContent")21leaked_sas_url_outcome = command.run_remote_attack("client", "GitHubProject", "CredScanGitHistory")22leaked_sas_url = commandcontrol.get_outcome_first_credential(leaked_sas_url_outcome)2324blobwithflag = command.connect_and_infect("client", "AzureStorage", "HTTPS", leaked_sas_url)25assert blobwithflag is not False2627browsable_directory = command.run_remote_attack("client", "Website", "ScanPageSource")28assert browsable_directory2930outcome_mysqlleak = command.run_remote_attack("client", "Website.Directory", "NavigateWebDirectoryFurther")31mysql_credential = commandcontrol.get_outcome_first_credential(outcome_mysqlleak)32sharepoint_url = command.run_remote_attack("client", "Website.Directory", "NavigateWebDirectory")33assert sharepoint_url3435outcome_azure_ad = command.run_remote_attack("client", "Sharepoint", "ScanSharepointParentDirectory")36azure_ad_credentials = commandcontrol.get_outcome_first_credential(outcome_azure_ad)3738azure_vm_info = command.connect_and_infect("client", "AzureResourceManager", "HTTPS", azure_ad_credentials)39assert azure_vm_info is not False4041azure_resources = command.run_remote_attack("client", "AzureResourceManager", "ListAzureResources")42assert azure_resources4344directly_ssh_connected = command.connect_and_infect("client", "AzureVM", "SSH", mysql_credential)45assert not directly_ssh_connected4647sshd = command.connect_and_infect("client", "Website", "SSH", mysql_credential)48assert sshd is not False4950outcome = command.run_attack("Website", "CredScanBashHistory")51monitor_bash_breds = commandcontrol.get_outcome_first_credential(outcome)5253connected_as_monitor = command.connect_and_infect("Website", "Website[user=monitor]", "sudo", monitor_bash_breds)54assert not connected_as_monitor5556connected_as_monitor_from_client = command.connect_and_infect("client", "Website[user=monitor]", "SSH", monitor_bash_breds)57assert not connected_as_monitor_from_client5859flag = command.connect_and_infect("Website", "Website[user=monitor]", "su", monitor_bash_breds)60assert flag is not False6162outcome_azuread = command.run_attack("Website[user=monitor]", "CredScan-HomeDirectory")63azure_ad_user_credential = commandcontrol.get_outcome_first_credential(outcome_azuread)6465secrets = command.connect_and_infect("client", "AzureResourceManager", "HTTPS", azure_ad_user_credential)66assert secrets is not False6768reward = command.total_reward()69print("Total reward " + str(reward))70assert reward == 389.071assert github is not None72pass737475