mirror of https://github.com/dirtbags/moth.git
135 lines
3.1 KiB
Bash
Executable File
135 lines
3.1 KiB
Bash
Executable File
#! /bin/sh
|
||
|
||
: ${CTF_BASE:=/var/lib/ctf}
|
||
BASE=$CTF_BASE/teams
|
||
|
||
esc () {
|
||
printf '%s' "$*" | sed 's/[^-0-9A-Za-z ]/_/g; s/ /+/g'
|
||
}
|
||
|
||
newteam () {
|
||
echo '== Team Creation =='
|
||
echo
|
||
echo -n 'What would you like your team to be called (3-12 chars)? '
|
||
read -r name
|
||
echo
|
||
namelen=$(printf "%s" "$name" | wc -c)
|
||
if [ $namelen -lt 3 ] || [ $namelen -gt 12 ]; then
|
||
echo 'Invalid name length'
|
||
return
|
||
fi
|
||
hash=$(printf '%s %s' "$salt" "$name" | md5sum | cut -b 1-8)
|
||
|
||
if [ -f $BASE/names/$hash ]; then
|
||
echo "That name is already in use. Try another one."
|
||
return
|
||
fi
|
||
|
||
printf '%s' "$name" > $BASE/names/$hash
|
||
|
||
cat <<EOD
|
||
Your team hash is [1m$hash[0m. Write that down somewhere and don't lose it.
|
||
If you forget your hash, you'll have to start over from the beginning
|
||
with a new team and everybody will laugh at you.
|
||
EOD
|
||
}
|
||
|
||
fini () {
|
||
echo
|
||
echo "Press [Enter] to clear the screen."
|
||
read
|
||
exit 0
|
||
}
|
||
|
||
log () {
|
||
awk -v H=$1 '($2 == H) { print($3, $4); }' $CTF_BASE/points.log
|
||
}
|
||
|
||
|
||
clear
|
||
read salt < $BASE/salt
|
||
|
||
printf '\017Team hash ("new" to create a new team): '
|
||
read -r hash
|
||
echo
|
||
if [ -z "$hash" ]; then
|
||
exit 0
|
||
elif [ "$hash" = "new" ]; then
|
||
newteam
|
||
fini
|
||
elif [ "$hash" = 58 ]; then
|
||
name='Thumper Bumper'
|
||
elif ! [ -f $BASE/names/$hash ]; then
|
||
echo "No such team, fool."
|
||
echo "Is this when everybody laughs at you for forgetting your hash?"
|
||
fini
|
||
else
|
||
read -r name < $BASE/names/$hash
|
||
fi
|
||
|
||
clear
|
||
|
||
|
||
printf 'Welcome back, [1m%s[0m.\n' "$name"
|
||
|
||
while true; do
|
||
printf "[7mp2>[0m "
|
||
read -r answer
|
||
|
||
case "$answer" in
|
||
\?|help)
|
||
cat <<EOD
|
||
[1m Help
|
||
----------------------------------------------------------[0m
|
||
|
||
Type [1mquit[0m to leave the p2 shell.
|
||
Type [1mlog[0m to show answered puzzles.
|
||
|
||
Any other string is checked as an answer. If the answer
|
||
is correct, you are awarded points and the scoreboard will
|
||
update within 10 seconds. Check the puzzles overview to
|
||
see if your answer unlocked a new puzzle.
|
||
EOD
|
||
continue
|
||
;;
|
||
log)
|
||
cat <<EOD
|
||
[1mPuzzles Answered By $name
|
||
---------------------------------------[0m
|
||
EOD
|
||
log $hash
|
||
continue
|
||
;;
|
||
quit)
|
||
break
|
||
;;
|
||
esac
|
||
|
||
|
||
clear
|
||
matches=0
|
||
for fn in /opt/*/answers.txt; do
|
||
cat=$(basename ${fn%/answers.txt})
|
||
while read points ans; do
|
||
if [ "$ans" = "$answer" ]; then
|
||
if log $hash | grep -wq "$cat $points"; then
|
||
echo "You've already received points for this answer."
|
||
elif /opt/p2/bin/pointscli $hash $cat $points p2console; then
|
||
echo "You get [1m$points[0m more points in the [1m$cat[0m category."
|
||
matches=$(expr $matches + 1)
|
||
else
|
||
echo "[1mError recording points. Tell the officials![0m"
|
||
fi
|
||
fi
|
||
done < $fn
|
||
done
|
||
|
||
if [ "$matches" -eq 0 ]; then
|
||
echo 'That is not a correct answer. Type "help" for help.'
|
||
fi
|
||
if [ "$matches" -gt 1 ]; then
|
||
echo "Holy shit! Double word score!"
|
||
fi
|
||
done
|
||
|