Path: blob/main/cyberbattle/samples/toyctf/toy_ctf.py
597 views
# Copyright (c) Microsoft Corporation.1# Licensed under the MIT License.23"""Model a toy Capture the flag exercise45See Jupyter notebook toyctf-simulation.ipynb for an example of6game played on this simulation.7"""89from cyberbattle.simulation import model as m10from cyberbattle.simulation.model import NodeID, NodeInfo, VulnerabilityID, VulnerabilityInfo11from typing import Dict, Iterator, cast, Tuple1213default_allow_rules = [14m.FirewallRule("RDP", m.RulePermission.ALLOW),15m.FirewallRule("SSH", m.RulePermission.ALLOW),16m.FirewallRule("HTTPS", m.RulePermission.ALLOW),17m.FirewallRule("HTTP", m.RulePermission.ALLOW),18]1920# Network nodes involved in the Capture the flag game21nodes = {22"Website": m.NodeInfo(23services=[m.ListeningService("HTTPS"), m.ListeningService("SSH", allowedCredentials=["ReusedMySqlCred-web"])],24firewall=m.FirewallConfiguration(incoming=default_allow_rules, outgoing=default_allow_rules + [m.FirewallRule("su", m.RulePermission.ALLOW), m.FirewallRule("sudo", m.RulePermission.ALLOW)]),25value=100,26# If can SSH into server then gets FLAG "Shared credentials with27# database user"28properties=["MySql", "Ubuntu", "nginx/1.10.3"],29owned_string="FLAG: Login using insecure SSH user/password",30vulnerabilities=dict(31ScanPageContent=m.VulnerabilityInfo(32description="LeakedGitHubProjectUrl: Website page content shows a link to GitHub " "repo",33type=m.VulnerabilityType.REMOTE,34outcome=m.LeakedNodesId(["GitHubProject"]),35reward_string="WEBSITE page content has a link to github -> Github project discovered!",36cost=1.0,37),38ScanPageSource=m.VulnerabilityInfo(39description="Website page source contains refrence to browseable " "relative web directory",40type=m.VulnerabilityType.REMOTE,41outcome=m.LeakedNodesId(["Website.Directory"]),42reward_string="Viewing the web page source reveals a URL to a .txt file and directory on the website",43cost=1.0,44),45CredScanBashHistory=m.VulnerabilityInfo(46description="bash history leaking creds - FLAG Stealing " "credentials for the monitoring user",47type=m.VulnerabilityType.LOCAL,48outcome=m.LeakedCredentials(credentials=[m.CachedCredential(node="Website[user=monitor]", port="SSH", credential="monitorBashCreds")]),49reward_string="FLAG: SSH history revealed credentials for the monitoring user (monitor)",50cost=1.0,51),52),53),54"Website.Directory": m.NodeInfo(55services=[m.ListeningService("HTTPS")],56value=50,57properties=["Ubuntu", "nginx/1.10.3", "CTFFLAG:Readme.txt-Discover secret data"],58vulnerabilities=dict(59NavigateWebDirectoryFurther=m.VulnerabilityInfo(60description="Discover MYSQL credentials MySql for user " "'web' in (getting-started.txt)",61type=m.VulnerabilityType.REMOTE,62outcome=m.LeakedCredentials(credentials=[m.CachedCredential(node="Website", port="MySQL", credential="ReusedMySqlCred-web")]),63reward_string="Discover browseable web directory: Navigating to parent URL revealed file `readme.txt`" "with secret data (aflag); and `getting-started.txt` with MYSQL credentials",64cost=1.0,65),66NavigateWebDirectory=m.VulnerabilityInfo(67description="Discover URL to external sharepoint website " "(in deprecation-checklist.txt)",68type=m.VulnerabilityType.REMOTE,69outcome=m.LeakedNodesId(["Sharepoint"]),70reward_string="Navigating to parent URL revealed file `deprecation-checklist.txt` containing" "a URL to an external sharepoint website",71cost=1.0,72),73),74),75"Website[user=monitor]": m.NodeInfo(76services=[m.ListeningService("SSH", allowedCredentials=[]), m.ListeningService("SSH-key", allowedCredentials=["unkownkey"]), m.ListeningService("su", allowedCredentials=["monitorBashCreds"])],77value=100,78properties=["MySql", "Ubuntu", "nginx/1.10.3"],79owned_string="FLAG User escalation by stealing credentials from bash history",80firewall=m.FirewallConfiguration(81outgoing=default_allow_rules,82incoming=[83m.FirewallRule("SSH", m.RulePermission.BLOCK, reason="password authentication disabled! SSH needs private key to authenticate."),84m.FirewallRule("sudo", m.RulePermission.BLOCK, reason="`sudo -u monitor` failed. User 'monitor' not sudoable." "This warning will be reported!"),85m.FirewallRule("su", m.RulePermission.ALLOW),86]87+ default_allow_rules,88),89vulnerabilities={90"CredScan-HomeDirectory": m.VulnerabilityInfo(91description="azurecredential.txt file in home directory",92type=m.VulnerabilityType.LOCAL,93outcome=m.LeakedCredentials(credentials=[m.CachedCredential(node="AzureResourceManager[user=monitor]", port="HTTPS", credential="azuread_user_credentials")]),94reward_string="SSH: cat ~/azurecreds.txt (running as monitor) revealed Azure user credential!",95cost=1.0,96),97},98),99"GitHubProject": m.NodeInfo(100services=[m.ListeningService("GIT")],101value=10,102properties=["GitHub", "SasUrlInCommit"],103vulnerabilities=dict(104CredScanGitHistory=m.VulnerabilityInfo(105description="Some secure access token (SAS) leaked in a " "reverted git commit",106type=m.VulnerabilityType.REMOTE,107precondition=m.Precondition("SasUrlInCommit&GitHub"),108outcome=m.LeakedCredentials(credentials=[m.CachedCredential(node="AzureStorage", port="HTTPS", credential="SASTOKEN1")]),109rates=m.Rates(probingDetectionRate=0.0, exploitDetectionRate=0.0, successRate=1.0),110reward_string="CredScan success: Some secure access token (SAS) was leaked in a reverted git commit",111cost=1.0,112)113),114),115"AzureStorage": m.NodeInfo(116services=[m.ListeningService("HTTPS", allowedCredentials=["SASTOKEN1"])],117value=50,118properties=["CTFFLAG:LeakedCustomerData"],119vulnerabilities=dict(120AccessDataWithSASToken=m.VulnerabilityInfo(121description="Stealing secrets using a publicly shared " "SAS token",122type=m.VulnerabilityType.REMOTE,123outcome=m.CustomerData(),124rates=m.Rates(successRate=1.0),125reward_string="Stole data using a publicly shared SAS token",126cost=1.0,127)128),129),130"Sharepoint": m.NodeInfo(131services=[m.ListeningService("HTTPS")],132value=100,133properties=["SharepointLeakingPassword"],134firewall=m.FirewallConfiguration(135incoming=[m.FirewallRule("SSH", m.RulePermission.ALLOW), m.FirewallRule("HTTP", m.RulePermission.ALLOW), m.FirewallRule("HTTPS", m.RulePermission.ALLOW)], outgoing=[]136),137vulnerabilities=dict(138ScanSharepointParentDirectory=m.VulnerabilityInfo(139description="Navigate to SharePoint site, browse parent " "directory",140type=m.VulnerabilityType.REMOTE,141outcome=m.LeakedCredentials(credentials=[m.CachedCredential(node="AzureResourceManager", port="HTTPS", credential="ADPrincipalCreds")]),142rates=m.Rates(successRate=1.0),143reward_string="Navigating to the Sharepoint site revealed AD Service Principal Credentials",144cost=1.0,145)146),147),148"AzureResourceManager": m.NodeInfo(149services=[m.ListeningService("HTTPS", allowedCredentials=["ADPrincipalCreds", "azuread_user_credentials"])],150owned_string="FLAG: Shared credentials with database user - Obtained secrets hidden in Azure Managed Resources",151value=50,152properties=["CTFFLAG:LeakedCustomerData2"],153vulnerabilities=dict(154ListAzureResources=m.VulnerabilityInfo(155description="AzureVM info, including public IP address",156type=m.VulnerabilityType.REMOTE,157outcome=m.LeakedNodesId(["AzureVM"]),158reward_string="Obtained Azure VM and public IP information",159cost=1.0,160)161),162),163"AzureResourceManager[user=monitor]": m.NodeInfo(164services=[m.ListeningService("HTTPS", allowedCredentials=["azuread_user_credentials"])],165owned_string="More secrets stolen when logged as interactive `monitor` user in Azure with `az`",166value=50,167properties=[],168),169"AzureVM": m.NodeInfo(170services=[m.ListeningService("PING"), m.ListeningService("SSH")],171value=100,172properties=["CTFFLAG:VMPRIVATEINFO"],173firewall=m.FirewallConfiguration(incoming=[m.FirewallRule("SSH", m.RulePermission.BLOCK, reason="internet incoming traffic blocked on the VM by NSG firewall")], outgoing=[]),174),175"client": m.NodeInfo(176services=[],177value=0,178vulnerabilities=dict(179SearchEdgeHistory=m.VulnerabilityInfo(180description="Search web history for list of accessed websites",181type=m.VulnerabilityType.LOCAL,182outcome=m.LeakedNodesId(["Website"]),183reward_string="Web browser history revealed website URL of interest",184cost=1.0,185)186),187agent_installed=True,188reimagable=False,189),190}191192global_vulnerability_library: Dict[VulnerabilityID, VulnerabilityInfo] = dict([])193194# Environment constants195ENV_IDENTIFIERS = m.infer_constants_from_nodes(cast(Iterator[Tuple[NodeID, NodeInfo]], list(nodes.items())), global_vulnerability_library)196197198def new_environment() -> m.Environment:199return m.Environment(network=m.create_network(nodes), vulnerability_library=global_vulnerability_library, identifiers=ENV_IDENTIFIERS)200201202