-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctftool.sh
283 lines (243 loc) · 7.61 KB
/
ctftool.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
275
276
277
278
279
280
281
282
283
#!/usr/bin/env bash
RED="\033[0;31m"
GREEN="\033[0;32m"
YELLOW="\033[0;33m"
BLUE="\033[0;34m"
CYAN="\033[0;36m"
RESET="\033[0m"
BASE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/challenges"
mkdir -p "$BASE"
CTF_FILE="/tmp/.ctftool"
touch "$CTF_FILE"
help() {
echo -e "${CYAN}Usage Instructions:${RESET}"
echo -e "${CYAN} ctf new <ctfname>${RESET} - Create a new CTF with the specified name."
echo -e "${CYAN} ctf delete <ctfname>${RESET} - Delete an existing CTF."
echo -e "${CYAN} ctf set <ctfname>${RESET} - Set the current CTF directory."
echo -e "${CYAN} ctf unset${RESET} - Unset the current CTF and challenge."
echo -e "${CYAN} ctf list${RESET} - List all CTFs available."
echo -e "${CYAN} ctf chal new <challenge name>${RESET} - Create a new challenge in the current CTF."
echo -e "${CYAN} ctf chal delete <challenge name>${RESET} - Delete a challenge in the current CTF."
echo -e "${CYAN} ctf chal set <challenge name>${RESET} - Set the current challenge in the CTF."
echo -e "${CYAN} ctf chal unset${RESET} - Unset the current challenge in the current CTF."
echo -e "${CYAN} ctf chal list${RESET} - List all challenges in the current CTF."
}
load_current_ctf() {
if [ -f "$CTF_FILE" ]; then
CURRENT_CTF="$(sed -n '1p' $CTF_FILE)"
CURRENT_CHAL="$(sed -n '2p' $CTF_FILE)"
else
CURRENT_CTF=""
CURRENT_CHAL=""
fi
}
save_current_ctf() {
echo "$CURRENT_CTF" > $CTF_FILE
echo "$CURRENT_CHAL" >> $CTF_FILE
}
ctf_rc() {
if [ -d "$CURRENT_CHAL" ]; then
cd $CURRENT_CHAL || return
echo -e "${BLUE}Navigating to challenge: $CURRENT_CHAL${RESET}"
return 0
fi
if [ -d "$CURRENT_CTF" ]; then
cd $CURRENT_CTF || return
echo -e "${BLUE}Navigating to CTF: $CURRENT_CTF${RESET}"
fi
}
ctf_new() {
if [ -z "$1" ]; then
echo -e "${RED}Error: Please provide a name for the new CTF.${RESET}"
return 1
fi
mkdir -p "$BASE/$1"
echo -e "${GREEN}Success: Created new CTF directory for '$1'.${RESET}"
}
ctf_delete() {
if [ -z "$1" ]; then
echo -e "${RED}Error: Please provide the name of the CTF you want to delete.${RESET}"
return 1
fi
if [ -d "$BASE/$1" ]; then
rm -rfi "$BASE/$1" || return 1
if [ "$BASE/$1" == "$CURRENT_CTF" ]; then
CURRENT_CTF=""
CURRENT_CHAL=""
save_current_ctf
fi
echo -e "${GREEN}Success: CTF '$1' and its contents have been deleted.${RESET}"
else
echo -e "${RED}Error: CTF '$1' does not exist. Please check the name and try again.${RESET}"
fi
}
ctf_set() {
if [ -z "$1" ]; then
echo -e "${RED}Error: Please specify a CTF to set as the current CTF.${RESET}"
return 1
fi
if [ ! -d "$BASE/$1" ]; then
echo -e "${RED}Error: CTF '$1' does not exist. Please ensure the directory exists.${RESET}"
return 1
fi
CURRENT_CTF="$BASE/$1"
CURRENT_CHAL=""
cd $CURRENT_CTF || return 1
save_current_ctf
echo -e "${GREEN}Success: Current CTF has been set to '$1'.${RESET}"
}
ctf_unset() {
if [ -z "$CURRENT_CTF" ] && [ -z "$CURRENT_CHAL" ]; then
echo -e "${YELLOW}No CTF or challenge is currently set. Nothing to unset.${RESET}"
return 0
fi
CURRENT_CTF=""
CURRENT_CHAL=""
save_current_ctf
echo -e "${GREEN}Success: Current CTF and challenge have been unset.${RESET}"
}
ctf_list() {
echo -e "${CYAN}Listing all available CTFs:${RESET}"
if [ "$(ls -A "$BASE")" ]; then
for ctf in "$BASE"/*; do
if [ -d "$ctf" ]; then
echo -e "${GREEN}- $(basename "$ctf")${RESET}"
fi
done
else
echo -e "${YELLOW}No CTFs found in this directory.${RESET}"
fi
}
ctf_chal_new() {
if [ ! -d "$CURRENT_CTF" ]; then
echo -e "${RED}Error: No CTF set. Please set a CTF first using 'ctf set <ctfname>'.${RESET}"
return 1
fi
if [ -z "$1" ]; then
echo -e "${RED}Error: Please provide a name for the new challenge.${RESET}"
return 1
fi
mkdir -p "$CURRENT_CTF/$1" || return 1
echo -e "${GREEN}Success: Created new challenge '$1' in CTF '$CURRENT_CTF'.${RESET}"
}
ctf_chal_delete() {
if [ ! -d "$CURRENT_CTF" ]; then
echo -e "${RED}Error: No CTF set. Please set a CTF first using 'ctf set <ctfname>'.${RESET}"
return 1
fi
if [ -z "$1" ]; then
echo -e "${RED}Error: Please provide the name of the challenge you want to delete.${RESET}"
return 1
fi
if [ -d "$CURRENT_CTF/$1" ]; then
rm -rfi "$CURRENT_CTF/$1" || return 1
if [ "$CURRENT_CTF/$1" == "$CURRENT_CHAL" ]; then
CURRENT_CHAL=""
save_current_ctf
fi
echo -e "${GREEN}Success: Challenge '$1' has been deleted from CTF '$CURRENT_CTF'.${RESET}"
else
echo -e "${RED}Error: Challenge '$1' does not exist in the current CTF. Please check the name and try again.${RESET}"
fi
}
ctf_chal_set() {
if [ ! -d "$CURRENT_CTF" ]; then
echo -e "${RED}Error: No CTF set. Please set a CTF first using 'ctf set <ctfname>'.${RESET}"
return 1
fi
if [ -z "$1" ]; then
echo -e "${RED}Error: Please provide the name of the challenge you want to set as the current challenge.${RESET}"
return 1
fi
if [ -d "$CURRENT_CTF/$1" ]; then
CURRENT_CHAL="$CURRENT_CTF/$1"
cd $CURRENT_CHAL || return 1
save_current_ctf
echo -e "${GREEN}Success: Challenge '$1' has been set as the current challenge in CTF '$CURRENT_CTF'.${RESET}"
else
echo -e "${RED}Error: Challenge '$1' does not exist in the current CTF. Please check the name and try again.${RESET}"
fi
}
ctf_chal_unset() {
if [ -z "$CURRENT_CHAL" ]; then
echo -e "${YELLOW}No challenge is currently set. Nothing to unset.${RESET}"
return 0
fi
CURRENT_CHAL=""
save_current_ctf
echo -e "${GREEN}Success: Current challenge has been unset.${RESET}"
}
ctf_chal_list() {
if [ ! -d "$CURRENT_CTF" ]; then
echo -e "${RED}Error: No CTF set. Please set a CTF first using 'ctf set <ctfname>'.${RESET}"
return 1
fi
echo -e "${CYAN}Listing challenges in CTF '$CURRENT_CTF':${RESET}"
if [ "$(ls -A "$CURRENT_CTF")" ]; then
for chal in "$CURRENT_CTF"/*; do
if [ -d "$chal" ]; then
echo -e "${GREEN}- $(basename "$chal")${RESET}"
fi
done
else
echo -e "${YELLOW}No challenges found in this CTF.${RESET}"
fi
}
load_current_ctf
case "$1" in
rc)
shift
ctf_rc
;;
new)
shift
ctf_new "$@"
;;
delete)
shift
ctf_delete "$@"
;;
set)
shift
ctf_set "$@"
;;
unset)
shift
ctf_unset
;;
list)
shift
ctf_list
;;
chal)
shift
case "$1" in
new)
shift
ctf_chal_new "$@"
;;
delete)
shift
ctf_chal_delete "$@"
;;
set)
shift
ctf_chal_set "$@"
;;
unset)
shift
ctf_chal_unset "$@"
;;
list)
shift
ctf_chal_list
;;
*)
help
;;
esac
;;
*)
help
;;
esac