-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbashcrawl
executable file
·291 lines (268 loc) · 8.42 KB
/
bashcrawl
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
284
285
286
287
288
289
290
#!/usr/bin/env bash
# If you are reading this, you have wandered out of bounds
# and are reading the code that drives the game.
#
# Congratulations!
#
# Learning Linux is all about curiosity, so read this code and see
# if you can figure out what it does. If you're feeling a little
# overwhelmed by the scope try reading some of the smaller scripts
# inside the game.
# Force this to be run as sudo
if [[ $(/usr/bin/id -u) -ne 0 ]]; then
echo "This game must be run with sudo"
exit
fi
if [ -z "${ID}" ]; then
export ID=99
fi
usage() {
cat << EOF
Usage: bashcrawl COMMAND
start GAME-TYPE
Begins or resumes a new game session of the specified type.
Valid type are Basic, Advanced, or Net
remove GAME-TYPE
Removes all the game data files for the specified type.
help
Prints this help message
EOF
}
TYPE=0
JAIL=".jail"
# Determine the number of arguments passed to the script
# If a number was passed update the user id for a (possibly)
# new game
if [ $# -ne 0 ]; then
case $1 in
start)
if [ $# -eq 2 ]; then
case $2 in
Basic)
TYPE=0
JAIL=".jail"
;;
Advanced)
TYPE=1
JAIL=".adv_jail"
;;
Net)
TYPE=2
JAIL=".net_jail"
;;
*)
echo "Unknown game type: $2"
usage
exit 1
;;
esac
printf ""
else
printf "You need to specify the game type (Basic, Advanced, Net)\n\n"
usage
exit 0
fi
;;
remove)
if [ $# -eq 2 ]; then
case $2 in
Basic)
printf "This will delete all of your current progress\nWould you like to continue? y / n "
read RESP
if [ "$RESP" = "y" -o "$RESP" = "Y" ]; then
rm -rf .jail
printf "Your previous progress has been removed\n"
exit 0
else
printf "We are not deleting anything\n"
exit 0
fi
;;
Advanced)
printf "This will delete all of your current progress\nWould you like to continue? y / n "
read RESP
if [ "$RESP" = "y" -o "$RESP" = "Y" ]; then
rm -rf .adv_jail
printf "Your previous progress has been removed\n"
exit 0
else
printf "We are not deleting anything\n"
exit 0
fi
;;
Net)
printf "This will delete all of your current progress\nWould you like to continue? y / n "
read RESP
if [ "$RESP" = "y" -o "$RESP" = "Y" ]; then
rm -rf .net_jail
printf "Your previous progress has been removed\n"
exit 0
else
printf "We are not deleting anything\n"
exit 0
fi
;;
*)
echo "Unknown game type: $2"
usage
exit 1
;;
esac
else
printf "\nYou need to specify the game type (Basic, Advanced, Net)\n\n"
usage
exit 0
fi
;;
help)
usage
exit 0
;;
*)
echo "Unknown option: $1"
usage
exit 1
;;
esac
else
usage
exit 0
fi
# This will catch any errors that are thrown by the script
# as well as catch an exit condition to restore the terminal
# to normal
trap cleanup SIGINT SIGTERM ERR EXIT
cleanup() {
trap - SIGINT SIGTERM ERR EXIT
# script cleanup
# restore the old HOME variable
export HOME=$OLD_HOME
if test $TYPE = 0; then
# destroy all the folders for the game
sudo rm -rf ./.jail/{bin,lib64,lib,dev,etc,usr,proc,sys,dev,var}
elif test $TYPE = 1; then
#cp .adv_jail/ 2>/dev/null
# destroy all the folders for the game
# umount .jail/{dev,proc,sys}/
umount $JAIL/dev/random
umount $JAIL/dev/urandom
sudo rm -rf ./.adv_jail/{bin,lib64,lib,dev,etc,usr,proc,sys,dev,var}
elif test $TYPE = 2; then
#cp .net
# destroy all the folders for the game
# umount .jail/{dev,proc,sys}/
for i in $(ls /run/netns); do
umount $JAIL/run/netns/$i
done
umount $JAIL/run
umount $JAIL/proc
umount $JAIL
umount $JAIL/dev/null
sudo rm -rf ./.net_jail/{bin,lib64,lib,dev,etc,usr,proc,sys,dev,var,run}
sudo ./.utilities/stop.sh
fi
echo Thanks for playing, come back soon
}
# Save off the original value of the HOME variable so we can
# restore it once we're done.
export OLD_HOME=$HOME
# create the directories necessary for a chroot jail
# apropos(1), groff(1), less(1), manpath(1), nroff(1), troff(1), whatis(1)
mkdir -p $JAIL/{bin,lib64,lib,dev,etc,usr,proc,sys,dev,run,var,mnt}
# cp $(which bash) .jail/bin
cp ./.utilities/busybox $JAIL/bin/busybox
for i in $($JAIL/bin/busybox --list)
do
ln -s /bin/busybox $JAIL/bin/$i
done
#cp /bin/{awk,wc,realpath,basename,cut,col,manpath,whatis,less,grotty,nroff,tbl,sh,perl,groff,troff,grops,man,mandb,apropos,bash,pwd,ls,cat,mv,cp,chmod,file,mkdir,sed,grep,rm,echo,touch,clear,env} .jail/bin/
# for i in $(ldd /bin/{awk,wc,realpath,basename,cut,col,manpath,whatis,less,grotty,nroff,tbl,sh,perl,groff,troff,grops,man,mandb,apropos,bash,pwd,ls,cat,mv,cp,chmod,file,mkdir,sed,grep,rm,echo,touch,clear,env} | sed "s/(.*)//; s/^ *//; s/ *$//; s/\/bin\/.*://; s/ => /=>/" | sort | uniq)
cp $(which file) $JAIL/bin/
cp $(which bash) $JAIL/bin/
cp $(which ip) $JAIL/bin/ # need ip since busybox version doesn't have netns
cp $(which ping) $JAIL/bin/
cp $(which nc) $JAIL/bin/
cp $(which ss) $JAIL/bin/
# added strace for debug purposes, removed in production
# cp $(which strace) $JAIL/bin/
for i in $(ldd $JAIL/bin/{file,bash,ip,ping,nc,ss} | sed "s/(.*)//; s/^ *//; s/ *$//; s/\/bin\/.*://; s/ => /=>/" | sort | uniq)
do
i=$(echo $i | sed "s/.*=>//")
if test -f "$i"; then
cp --parents $i $JAIL
fi
done
case $TYPE in
0)
# Set a new HOME environment variable. When we open the new
# shell for the game it will pull from the bashrc in the new
# HOME directory.
export HOME=/room/tower/mainhall
if test -d $JAIL/room; then
# room already exists so do nothing
echo "Resuming your previous game, good luck!"
else
echo "Starting a new game"
cp -r .room $JAIL/room
cp ./.utilities/welcome_message .jail
cp ./.utilities/.secrets.txt ${JAIL}${HOME}
fi
;;
1)
export HOME=/gate
touch $JAIL/dev/random
touch $JAIL/dev/urandom
mount --rbind /dev/random $JAIL/dev/random
mount --rbind /dev/urandom $JAIL/dev/urandom
if test -d $JAIL/gate; then
# gate already exists so do nothing
echo "Resuming your previous game, good luck!"
else
echo "Starting a new game"
cp -r .gate $JAIL/gate
cp ./.utilities/advanced_welcome_message $JAIL
cp ./.utilities/.secrets.txt ${JAIL}${HOME}
fi
;;
2)
export HOME="/crossroad/City/The_Royal_Byte/"
if test -d $JAIL/crossroad; then
echo "Resuming your previous game, good luck!"
else
echo "Starting a new game"
cp -r .netroom $JAIL/crossroad
cp ./.utilities/net_welcome_message $JAIL
cp ./.utilities/start.sh $JAIL/.start.sh
cp ./.utilities/.secrets.txt ${JAIL}${HOME}
fi
chmod +x $JAIL/.start.sh
sudo $JAIL/.start.sh
touch $JAIL/dev/null
mount --rbind /dev/null $JAIL/dev/null
mount --bind --make-private $JAIL $JAIL
mount --bind /proc $JAIL/proc/
mount --bind /run $JAIL/run/
for i in $(ls /run/netns); do
mount --rbind /run/netns/$i $JAIL/run/netns/$i
done
;;
esac
# copy over stuff so man works
mkdir -p $JAIL/usr/share/man
tar -C $JAIL/usr/share/man -xvzf ./.utilities/catpages.tar.gz &>/dev/null
# copy over stuff so file works
cp -r --parents /etc/magic $JAIL
cp -r --parents /usr/share/misc/magic $JAIL
cp -r --parents /lib/terminfo $JAIL
cp --parents /usr/share/misc/magic.mgc $JAIL
# copy over stuff so ip works
cp -r --parents /etc/iproute2 $JAIL
#mount --bind /var $JAIL/var/
#mount --bind /mnt $JAIL/mnt/
#mount --bind /dev $JAIL/dev/
#mount --bind /proc $JAIL/proc/
#mount --bind /run $JAIL/run/
#mount --make-shared $JAIL/run/
#mount --bind /sys $JAIL/sys
#cp -r /usr/libexec/man-db .jail/usr/libexec
sudo chroot $JAIL /bin/env -i ID="$ID" HOME="$HOME" TERM="$TERM" PS1='\$ ' ENV="$HOME/.bashrc" PATH=/bin /bin/bash