Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/CyberBattleSim
Path: blob/main/notebooks/toyctf-solved.ipynb
597 views
Kernel: cybersim

Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License.

Capture The Flag Toy Example - Solved manually

This notebook demonstrates how to model a toy Capture The Flag security game as a CyberBattle environment.

import sys, logging import cyberbattle.simulation.model as model import cyberbattle.simulation.commandcontrol as commandcontrol import cyberbattle.samples.toyctf.toy_ctf as ctf logging.basicConfig(stream=sys.stdout, level=logging.INFO, format="%(levelname)s: %(message)s") import plotly.offline as plo plo.init_notebook_mode(connected=True) %matplotlib inline
network = model.create_network(ctf.nodes) env = model.Environment(network=network, vulnerability_library=dict([]), identifiers=ctf.ENV_IDENTIFIERS) env.plot_environment_graph()
Image in a Jupyter notebook

Solution to the CTF

This is the list of actions taken to capture 7 of the 8 flags from the CTF game.

SourceActionResult
WEBSITEpage content has a link to githubDiscover Github project
GITHUBnavigate github historyFLAG Some secure access token (SAS) leaked in a reverted git commit (CredScan)
AZURESTORAGEaccess blob using SAS token
WEBSITEview source HTMLFind URL to hidden .txt file on the website, extract directory path from it
navigate to parent URL and find 3 filesFLAG Discover browseable web directory
- readme.txt fileDiscover secret data (the flag)
- getting-started.txtDiscover MYSQL credentials
- deprecation-checklist.txtDiscover URL to external sharepoint website
SHAREPOINTNavigate to sharepoint siteFLAG Finding AD Service Principal Credentials on Sharepoint
CLIENT-AZUREaz resource with creds from sharepointObtain secrets hidden in azure managed resources
Get AzureVM info, including public IP address
CLIENTssh IPFailed attempt: internet incoming traffic blocked on the VM by NSG
CLIENTSSH into WEBSITE with mysql credsFLAG Shared credentials with database user
FLAG Login using insecure SSH user/password
WEBSITE/SSHhistoryFLAG Stealing credentials for the monitoring user
sudo -u monitorFailed! monitor not sudoable. message about being reported!
CLIENTSSH into WEBSITE with 'monitor credsFailed! password authentication disabled! looking for private key
CLIENTSSH into WEBSITE as 'web'
su -u monitor using passwordFLAG User escalation by stealing credentials from bash history
cat ~/azurecreds.txtGet user credentials to Azure
CLIENTaz resource with monitor's credsSteal more secrets
c2 = commandcontrol.CommandControl(env) dbg = commandcontrol.EnvironmentDebugging(c2) # 1 - Start from client dbg.plot_discovered_network()
c2.print_all_attacks()
outcome = c2.run_attack("client", "SearchEdgeHistory") dbg.plot_discovered_network()
INFO: discovered node: Website INFO: GOT REWARD: Web browser history revealed website URL of interest
c2.print_all_attacks()
# 2 github = c2.run_remote_attack("client", "Website", "ScanPageContent") dbg.plot_discovered_network()
INFO: discovered node: GitHubProject INFO: GOT REWARD: WEBSITE page content has a link to github -> Github project discovered!
# 3 leakedSasUrl = c2.run_remote_attack("client", "GitHubProject", "CredScanGitHistory") dbg.plot_discovered_network()
INFO: discovered node: AzureStorage INFO: discovered credential: CachedCredential(node='AzureStorage', port='HTTPS', credential='SASTOKEN1') INFO: GOT REWARD: CredScan success: Some secure access token (SAS) was leaked in a reverted git commit
# 4 blobwithflag = c2.connect_and_infect("client", "AzureStorage", "HTTPS", "SASTOKEN1") dbg.plot_discovered_network() blobwithflag
INFO: Infected node 'AzureStorage' from 'client' via HTTPS with credential 'SASTOKEN1'
True
# 5 browsableDirectory = c2.run_remote_attack("client", "Website", "ScanPageSource") dbg.plot_discovered_network()
INFO: discovered node: Website.Directory INFO: GOT REWARD: Viewing the web page source reveals a URL to a .txt file and directory on the website
# 6 outcome_mysqlleak = c2.run_remote_attack("client", "Website.Directory", "NavigateWebDirectoryFurther") sharepoint_url = c2.run_remote_attack("client", "Website.Directory", "NavigateWebDirectory") dbg.plot_discovered_network()
INFO: discovered node: Website INFO: discovered credential: CachedCredential(node='Website', port='MySQL', credential='ReusedMySqlCred-web') INFO: GOT REWARD: Discover browseable web directory: Navigating to parent URL revealed file `readme.txt`with secret data (aflag); and `getting-started.txt` with MYSQL credentials INFO: discovered node: Sharepoint INFO: GOT REWARD: Navigating to parent URL revealed file `deprecation-checklist.txt` containinga URL to an external sharepoint website
# 7 outcome_azure_ad = c2.run_remote_attack("client", "Sharepoint", "ScanSharepointParentDirectory") dbg.plot_discovered_network()
INFO: discovered node: AzureResourceManager INFO: discovered credential: CachedCredential(node='AzureResourceManager', port='HTTPS', credential='ADPrincipalCreds') INFO: GOT REWARD: Navigating to the Sharepoint site revealed AD Service Principal Credentials
# 8 azureVmInfo = c2.connect_and_infect("client", "AzureResourceManager", "HTTPS", "ADPrincipalCreds") dbg.plot_discovered_network()
INFO: Infected node 'AzureResourceManager' from 'client' via HTTPS with credential 'ADPrincipalCreds' INFO: Owned message: FLAG: Shared credentials with database user - Obtained secrets hidden in Azure Managed Resources
c2.run_remote_attack("client", "AzureResourceManager", "ListAzureResources") dbg.plot_discovered_network()
INFO: discovered node: AzureVM INFO: GOT REWARD: Obtained Azure VM and public IP information
# 9 - CLIENT: Attempt to SSH into AzureVM from IP retrieved from Azure Resource Manager should_fail = c2.connect_and_infect("client", "AzureVM", "SSH", "ReusedMySqlCred-web") print("Success=" + str(should_fail)) dbg.plot_discovered_network()
INFO: BLOCKED TRAFFIC: target node 'AzureVM' is blocking outgoing traffic on port 'SSH' Success=False
# 10 owned = c2.connect_and_infect("client", "Website", "SSH", "ReusedMySqlCred-web") dbg.plot_discovered_network()
INFO: Infected node 'Website' from 'client' via SSH with credential 'ReusedMySqlCred-web' INFO: Owned message: FLAG: Login using insecure SSH user/password
# 11 outcome = c2.run_attack("Website", "CredScanBashHistory") dbg.plot_discovered_network()
INFO: discovered node: Website[user=monitor] INFO: discovered credential: CachedCredential(node='Website[user=monitor]', port='SSH', credential='monitorBashCreds') INFO: GOT REWARD: FLAG: SSH history revealed credentials for the monitoring user (monitor)
c2.print_all_attacks()
# 12 should_fail = c2.connect_and_infect("Website", "Website[user=monitor]", "sudo", "monitorBashCreds") dbg.plot_discovered_network()
INFO: BLOCKED TRAFFIC: target node 'Website[user=monitor]' is blocking outgoing traffic on port 'sudo'
# 13 should_fail = c2.connect_and_infect("client", "Website[user=monitor]", "SSH", "monitorBashCreds") dbg.plot_discovered_network() should_fail
INFO: BLOCKED TRAFFIC: target node 'Website[user=monitor]' is blocking outgoing traffic on port 'SSH'
False
# 14 flag = c2.connect_and_infect("Website", "Website[user=monitor]", "su", "monitorBashCreds") dbg.plot_discovered_network()
INFO: Infected node 'Website[user=monitor]' from 'Website' via su with credential 'monitorBashCreds' INFO: Owned message: FLAG User escalation by stealing credentials from bash history
# 15 outcome = c2.run_attack("Website[user=monitor]", "CredScan-HomeDirectory") dbg.plot_discovered_network()
INFO: discovered node: AzureResourceManager[user=monitor] INFO: discovered credential: CachedCredential(node='AzureResourceManager[user=monitor]', port='HTTPS', credential='azuread_user_credentials') INFO: GOT REWARD: SSH: cat ~/azurecreds.txt (running as monitor) revealed Azure user credential!
# 16 secrets = c2.connect_and_infect("client", "AzureResourceManager[user=monitor]", "HTTPS", "azuread_user_credentials") dbg.plot_discovered_network()
INFO: Infected node 'AzureResourceManager[user=monitor]' from 'client' via HTTPS with credential 'azuread_user_credentials' INFO: Owned message: More secrets stolen when logged as interactive `monitor` user in Azure with `az`
c2.print_all_attacks()