Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/CyberBattleSim
Path: blob/main/notebooks/toyctf-solved.py
597 views
1
# ---
2
# jupyter:
3
# jupytext:
4
# formats: py:percent
5
# text_representation:
6
# extension: .py
7
# format_name: percent
8
# format_version: '1.3'
9
# jupytext_version: 1.16.4
10
# kernelspec:
11
# display_name: cybersim
12
# language: python
13
# name: cybersim
14
# ---
15
16
# %% [markdown]
17
# pyright: reportUnusedExpression=false
18
19
# %% [markdown]
20
# Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License.
21
#
22
# # Capture The Flag Toy Example - Solved manually
23
24
# %% [markdown]
25
# This notebook demonstrates how to model a toy `Capture The Flag` security game as a CyberBattle environment.
26
27
# %%
28
import sys, logging
29
import cyberbattle.simulation.model as model
30
import cyberbattle.simulation.commandcontrol as commandcontrol
31
import cyberbattle.samples.toyctf.toy_ctf as ctf
32
33
logging.basicConfig(stream=sys.stdout, level=logging.INFO, format="%(levelname)s: %(message)s")
34
35
import plotly.offline as plo
36
37
plo.init_notebook_mode(connected=True) # type: ignore
38
# %matplotlib inline
39
40
# %%
41
network = model.create_network(ctf.nodes)
42
env = model.Environment(network=network, vulnerability_library=dict([]), identifiers=ctf.ENV_IDENTIFIERS)
43
env.plot_environment_graph()
44
45
# %% [markdown]
46
# ### Solution to the CTF
47
#
48
# This is the list of actions taken to capture 7 of the 8 flags from the CTF game.
49
#
50
# | Source | Action | Result |
51
# |------------ | ------ | ------ |
52
# | WEBSITE | page content has a link to github | Discover Github project |
53
# | GITHUB | navigate github history | **FLAG** Some secure access token (SAS) leaked in a reverted git commit (`CredScan`) |
54
# | AZURESTORAGE| access blob using SAS token | |
55
# | WEBSITE | view source HTML | Find URL to hidden .txt file on the website, extract directory path from it |
56
# | | navigate to parent URL and find 3 files | **FLAG** Discover browseable web directory |
57
# | | - readme.txt file | Discover secret data (the flag) |
58
# | | - getting-started.txt | Discover MYSQL credentials |
59
# | | - deprecation-checklist.txt | Discover URL to external sharepoint website |
60
# | SHAREPOINT | Navigate to sharepoint site | **FLAG** Finding AD Service Principal Credentials on Sharepoint |
61
# | CLIENT-AZURE| `az resource` with creds from sharepoint| Obtain secrets hidden in azure managed resources |
62
# | | | Get AzureVM info, including public IP address |
63
# | CLIENT | `ssh IP` | Failed attempt: internet incoming traffic blocked on the VM by NSG |
64
# | CLIENT | SSH into WEBSITE with mysql creds | **FLAG** Shared credentials with database user|
65
# | | |**FLAG** Login using insecure SSH user/password|
66
# | WEBSITE/SSH | `history` |**FLAG** Stealing credentials for the monitoring user|
67
# | | `sudo -u monitor` | Failed! monitor not sudoable. message about being reported!
68
# | CLIENT | SSH into WEBSITE with 'monitor creds | Failed! password authentication disabled! looking for private key|
69
# | CLIENT | SSH into WEBSITE as 'web' | |
70
# | | `su -u monitor` using password |**FLAG** User escalation by stealing credentials from bash history|
71
# | | `cat ~/azurecreds.txt` | Get user credentials to Azure
72
# | CLIENT | `az resource` with monitor's creds | Steal more secrets
73
#
74
75
# %%
76
c2 = commandcontrol.CommandControl(env)
77
dbg = commandcontrol.EnvironmentDebugging(c2)
78
79
# 1 - Start from client
80
dbg.plot_discovered_network()
81
82
# %%
83
c2.print_all_attacks()
84
85
# %%
86
outcome = c2.run_attack("client", "SearchEdgeHistory")
87
dbg.plot_discovered_network()
88
89
# %%
90
c2.print_all_attacks()
91
92
# %%
93
# 2
94
github = c2.run_remote_attack("client", "Website", "ScanPageContent")
95
dbg.plot_discovered_network()
96
97
# %%
98
# 3
99
leakedSasUrl = c2.run_remote_attack("client", "GitHubProject", "CredScanGitHistory")
100
dbg.plot_discovered_network()
101
102
# %%
103
# 4
104
blobwithflag = c2.connect_and_infect("client", "AzureStorage", "HTTPS", "SASTOKEN1")
105
dbg.plot_discovered_network()
106
blobwithflag
107
108
# %%
109
# 5
110
browsableDirectory = c2.run_remote_attack("client", "Website", "ScanPageSource")
111
dbg.plot_discovered_network()
112
113
# %%
114
# 6
115
outcome_mysqlleak = c2.run_remote_attack("client", "Website.Directory", "NavigateWebDirectoryFurther")
116
sharepoint_url = c2.run_remote_attack("client", "Website.Directory", "NavigateWebDirectory")
117
dbg.plot_discovered_network()
118
119
# %%
120
# 7
121
outcome_azure_ad = c2.run_remote_attack("client", "Sharepoint", "ScanSharepointParentDirectory")
122
dbg.plot_discovered_network()
123
124
# %%
125
# 8
126
azureVmInfo = c2.connect_and_infect("client", "AzureResourceManager", "HTTPS", "ADPrincipalCreds")
127
dbg.plot_discovered_network()
128
129
# %%
130
c2.run_remote_attack("client", "AzureResourceManager", "ListAzureResources")
131
dbg.plot_discovered_network()
132
133
# %%
134
# 9 - CLIENT: Attempt to SSH into AzureVM from IP retrieved from Azure Resource Manager
135
should_fail = c2.connect_and_infect("client", "AzureVM", "SSH", "ReusedMySqlCred-web")
136
print("Success=" + str(should_fail))
137
dbg.plot_discovered_network()
138
139
# %%
140
# 10
141
owned = c2.connect_and_infect("client", "Website", "SSH", "ReusedMySqlCred-web")
142
dbg.plot_discovered_network()
143
144
# %%
145
# 11
146
outcome = c2.run_attack("Website", "CredScanBashHistory")
147
dbg.plot_discovered_network()
148
149
# %%
150
c2.print_all_attacks()
151
152
# %%
153
# 12
154
should_fail = c2.connect_and_infect("Website", "Website[user=monitor]", "sudo", "monitorBashCreds")
155
dbg.plot_discovered_network()
156
157
# %%
158
# 13
159
should_fail = c2.connect_and_infect("client", "Website[user=monitor]", "SSH", "monitorBashCreds")
160
dbg.plot_discovered_network()
161
should_fail
162
163
# %%
164
# 14
165
flag = c2.connect_and_infect("Website", "Website[user=monitor]", "su", "monitorBashCreds")
166
dbg.plot_discovered_network()
167
168
# %%
169
# 15
170
outcome = c2.run_attack("Website[user=monitor]", "CredScan-HomeDirectory")
171
dbg.plot_discovered_network()
172
173
# %%
174
# 16
175
secrets = c2.connect_and_infect("client", "AzureResourceManager[user=monitor]", "HTTPS", "azuread_user_credentials")
176
dbg.plot_discovered_network()
177
178
# %%
179
c2.print_all_attacks()
180
181