-
Notifications
You must be signed in to change notification settings - Fork 52
/
lab.sh
executable file
·274 lines (252 loc) · 6.65 KB
/
lab.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/usr/bin/env bash
# Initialize command line parsing variables.
print_info=0
print_overview=0
up=0
down=0
prune=0
remove=0
build=0
dep_check=0
lab_level='all'
service_class='all'
red_team_services='none'
blue_team_services='none'
monitoring_services='none'
all_services=0
# Initialize global variables.
# Variable is used in src/labctl.bash.
# shellcheck disable=SC2034
working_dir="$PWD"
key_dir="${working_dir}/etc/keys"
auth_keys="${working_dir}/etc/kali/authorized_keys"
# Include source files.
source src/info.bash
source src/labctl.bash
source src/helper.bash
# Initialize arrays.
lab_levels=(
"all"
"beginner"
"intermediate"
"expert"
)
service_classes=(
"all"
"red_team"
"blue_team"
"victim"
"monitoring"
)
# Variable is used in src/helper.bash.
# shellcheck disable=SC2034
dependencies=(
"docker"
"docker-compose"
"yq"
"bash"
"find"
"sed"
)
# Exit if no flags or arguments are given.
if [ "$#" -eq 0 ]; then
_helper_printUsage
exit 1
fi
# Parse command line arguments.
while [ "$#" -gt 0 ]; do
case "$1" in
-h|--help)
_helper_printUsage
shift
exit 0
;;
-i|--info)
print_info=1
shift
;;
-o|--overview)
if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
service_class=''
for class in $2; do
if _helper_arrayContains "${class}" "${service_classes[*]}"\
; then
service_class="${service_class} ${class}"
else
printf 'Error: Unknown class in: "%s" for %s\n'\
"$2" "$1" >&2
printf 'Possible arguments: %s\n' \
"$(_helper_arrayJoin "${service_classes[@]}")" >&2
exit 1
fi
done
shift 2
else
printf 'Error: Missing argument for %s\n' "$1" >&2
exit 1
fi
print_overview=1
;;
-B|--build)
build=1
shift
;;
-u|--up)
up=1
shift
;;
-d|--down)
down=1
shift
;;
-R|--remove)
remove=1
shift
;;
-p|--prune)
prune=1
shift
;;
-C|--check-dependencies)
dep_check=1
shift
;;
-r|--red-team)
if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
red_team_services="$2"
else
printf 'Error: Missing argument for %s\n' "$1" >&2
exit 1
fi
shift 2
;;
-b|--blue-team)
if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
blue_team_services="$2"
else
printf 'Error: Missing argument for %s\n' "$1" >&2
exit 1
fi
shift 2
;;
-m|--monitoring)
if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
monitoring_services="$2"
else
printf 'Error: Missing argument for %s\n' "$1" >&2
exit 1
fi
shift 2
;;
-l|--level)
if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
lab_level=''
for level in $2; do
if _helper_arrayContains "${level}" "${lab_levels[*]}"; then
lab_level="${lab_level} ${level}"
else
printf 'Error: Unknown argument: %s for %s\n'\
"$2" "$1" >&2
printf 'Possible arguments: %s\n' \
"$(_helper_arrayJoin "${lab_levels[@]}")" >&2
exit 1
fi
done
lab_level="$(echo "${lab_level}" | awk '{$1=$1};1')"
shift 2
else
printf 'Error: Missing argument for %s\n' "$1" >&2
exit 1
fi
;;
-A|--all-services)
all_services=1
red_team_services='all'
blue_team_services='all'
monitoring_services='all'
shift
;;
*)
# Unsupported options.
printf 'Illegal option %s\n' "$1" >&2
_helper_printUsage
exit 1
;;
esac
done
#
# Run lab accourding to parsed cmd line arguments.
#
# Arguments:
# - None
#
# Returns:
# - 0: If none of the later occours.
# - 1: If dependency missing / lab not in desired state.
#
# Prints:
# - stdout: Nothing.
# - stderr: Error messages.
#
# Creates:
# - Nothing.
#
_main() {
# Run dependency check.
if [ "${dep_check}" -eq 0 ]; then
if ! _helper_checkDependencies > /dev/null 2>&1; then
printf 'Error: Missing dependencies.\n' >&2
printf 'Please run "%s -C" to check which dependency is missing.\n'\
"$(basename "$0")" >&2
exit 1
fi
fi
# Print header.
_helper_printHeader
# Execute commads accourding to command line arguments.
if [ "${print_info}" -eq 1 ]; then
_helper_isUp && _info_printInfo
fi
if [ "${print_overview}" -eq 1 ]; then
_helper_isUp && _info_printOverview "${service_class}"
fi
if [ "${up}" -eq 1 ]; then
if _helper_isUp > /dev/null 2>&1; then
printf 'Lab is already running.\n'
exit 1
else
_labctl_up\
"${lab_level}"\
"${red_team_services}"\
"${blue_team_services}"\
"${monitoring_services}"\
"${all_services}"
clear
_helper_printHeader
_info_printOverview "${service_class}"
_info_printInfo
fi
fi
if [ "${down}" -eq 1 ]; then
if ! _helper_isUp > /dev/null 2>&1; then
printf 'Lab is not running.\n'
exit 1
else
_labctl_down
fi
fi
if [ "${prune}" -eq 1 ]; then
_labctl_prune
fi
if [ "${dep_check}" -eq 1 ]; then
_helper_checkDependencies
fi
if [ "${build}" -eq 1 ]; then
_labctl_build
fi
if [ "${remove}" -eq 1 ]; then
_labctl_removeKeys
fi
}
# Run main function.
_main