xmpp.chapril.org-tools/captcha/captcha.sh

137 lines
3.6 KiB
Bash
Raw Normal View History

2021-01-08 20:30:08 +01:00
#!/bin/bash
2020-12-27 16:55:35 +01:00
# This script is an example captcha script.
# It takes the text to recognize in the captcha image as a parameter.
# It return the image binary as a result. ejabberd support PNG, JPEG and GIF.
# The whole idea of the captcha script is to let server admins adapt it to
# their own needs. The goal is to be able to make the captcha generation as
# unique as possible, to make the captcha challenge difficult to bypass by
# a bot.
# Server admins are thus supposed to write and use their own captcha generators.
# This script relies on ImageMagick.
# It is NOT compliant with ImageMagick forks like GraphicsMagick.
INPUT=$1
TRANSFORMATIONS=(INTRUS SOMME)
DIGIT=(zéros uns deux trois quatres cinqs six septs huits neufs dix)
2020-12-27 16:55:35 +01:00
if test -n ${BASH_VERSION:-''} ; then
get_random ()
{
R=$RANDOM
}
else
2021-01-08 20:30:08 +01:00
for n in `od -A n -t u2 -N 64 /dev/urandom`; do RL="$RL$n "; done
2020-12-27 16:55:35 +01:00
get_random ()
{
R=${RL%% *}
RL=${RL#* }
}
fi
INTRUS()
2021-01-08 20:30:08 +01:00
{
LENGTH=${#INPUT}
NUMBERS=$(echo $INPUT | grep -o . | tr '\n' ' ')
SORTED_UNIQ_NUM=$(echo "${NUMBERS[@]}" | sort -u | tr '\n' ' ')
INTRUS=-1
2021-01-14 18:14:39 +01:00
for i in 1 2 3 4 5 6 7 8 9
2021-01-08 20:30:08 +01:00
do
if [[ ! " ${SORTED_UNIQ_NUM[@]} " =~ " ${i} " ]]; then
INTRUS=$i
break
fi
done
# Worst case
if [[ $INTRUS -eq "-1" ]]
then
echo Tapez "$INPUT |sans changement"
2021-01-08 20:30:08 +01:00
return
fi
for num in ${NUMBERS[@]}
do
get_random
R=$(($R % 100))
if [[ $R -lt 60 ]]; then
NEWINPUT=${NEWINPUT}${num}${INTRUS}
else
NEWINPUT=${NEWINPUT}${num}
fi
done
get_random
R=$(($R % 100))
if [[ $R -lt 50 ]]; then
echo "Tapez ce nombre en enlevant les ${DIGIT[$INTRUS]} | $NEWINPUT"
else
echo "Saisissez ce nombre sans les ${DIGIT[$INTRUS]} | $NEWINPUT"
fi
2021-01-08 20:30:08 +01:00
}
SOMME()
2021-01-08 20:30:08 +01:00
{
get_random
2021-01-14 12:25:00 +01:00
RB=$(($R % 5))
get_random
RA=$(($R % 10 * (10 ** $RB)))
2021-01-08 20:30:08 +01:00
if [[ $(($INPUT % 2)) -eq 0 ]]; then
A=$(($INPUT - $RA))
B=$RA
2021-01-08 20:30:08 +01:00
else
2021-01-08 22:20:07 +01:00
B=$(($INPUT - $RA))
A=$RA
2021-01-08 20:30:08 +01:00
fi
get_random
R=$(($R % 100))
if [[ $R -lt 25 ]]; then
2021-01-14 18:14:39 +01:00
echo "Tapez le résultat de | $A + $B"
elif [[ $R -lt 50 ]]; then
2021-01-14 18:14:39 +01:00
echo "Sommez | $A et $B"
elif [[ $R -lt 75 ]]; then
2021-01-14 18:14:39 +01:00
echo "Additionnez | $A et $B"
else
2021-01-14 18:14:39 +01:00
echo "Saisissez le résultat de | $A + $B"
fi
2021-01-08 20:30:08 +01:00
}
2020-12-27 16:55:35 +01:00
2020-12-30 20:01:50 +01:00
get_random
2021-01-08 20:30:08 +01:00
RAND_INDEX=$(($R % ${#TRANSFORMATIONS[@]}))
2020-12-27 16:55:35 +01:00
INSTRUCTIONS0=$(echo "$(${TRANSFORMATIONS[$RAND_INDEX]})")
INSTRUCTIONS1=$(echo $INSTRUCTIONS0 | cut -d "|" -f 1)
INSTRUCTIONS2=$(echo $INSTRUCTIONS0 | cut -d "|" -f 3)
TEXT=$(echo $INSTRUCTIONS0 | cut -d "|" -f 2)
2021-01-14 18:14:39 +01:00
convert -size 300x70 xc:none -pointsize 20 \
\( -clone 0 -fill black \
-stroke black -strokewidth 1 \
-font Helvetica-Narrow -annotate "0x0+0+0" "\n $INSTRUCTIONS1" \
2021-01-14 18:56:33 +01:00
-font Helvetica-Bold -annotate "0x0+0+22" "\n $TEXT" \
-font Helvetica-Narrow -annotate "0x0+0+44" "\n $INSTRUCTIONS2" \
-roll +$ROLL_X+0 \
-wave "$WAVE1_AMPLITUDE"x"$WAVE1_LENGTH" \
-roll -$ROLL_X+0 \) \
-flatten -crop 300x70 +repage -quality 500 -depth 11 png:"/var/lib/ejabberd/chapril_captchas/$(date "+%Y-%m-%d-%H%M%S")_${INPUT}.png"
convert -size 300x70 xc:none -pointsize 20 \
2021-01-08 20:30:08 +01:00
\( -clone 0 -fill black \
-stroke black -strokewidth 1 \
-font Helvetica-Narrow -annotate "0x0+0+0" "\n $INSTRUCTIONS1" \
2021-01-14 18:56:33 +01:00
-font Helvetica-Bold -annotate "0x0+0+22" "\n $TEXT" \
-font Helvetica-Narrow -annotate "0x0+0+44" "\n $INSTRUCTIONS2" \
2020-12-31 13:36:06 +01:00
-roll +$ROLL_X+0 \
-wave "$WAVE1_AMPLITUDE"x"$WAVE1_LENGTH" \
2021-01-08 20:30:08 +01:00
-roll -$ROLL_X+0 \) \
-flatten -crop 300x70 +repage -quality 500 -depth 11 png:-