New captcha shapes and a bit refactoring

This commit is contained in:
Echolib 2021-08-31 12:30:39 +02:00 committed by Adrien Bourmault
parent 4d6fd9ef5a
commit 69ac7a9537
Signed by: neox
GPG Key ID: 6EB408FE0ACEC664

View File

@ -1,5 +1,20 @@
#!/bin/bash #!/bin/bash
# Copyright © 2021 Adrien Bourmault (neox@a-lec.org)
# Copyright © 2021 Echolib (echolib@dismail.de)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# This script is an example captcha script. # This script is an example captcha script.
# It takes the text to recognize in the captcha image as a parameter. # 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. # It return the image binary as a result. ejabberd support PNG, JPEG and GIF.
@ -14,121 +29,140 @@
# It is NOT compliant with ImageMagick forks like GraphicsMagick. # It is NOT compliant with ImageMagick forks like GraphicsMagick.
INPUT=$1 INPUT=$1
LENINPUT=${#INPUT}
TRANSFORMATIONS=(INTRUS SOMME)
DIGIT=(zéros uns deux trois quatres cinqs six septs huits neufs dix) DIGIT=(zéros uns deux trois quatres cinqs six septs huits neufs dix)
if test -n ${BASH_VERSION:-''} ; then # Loop 10 times (should be enough) to check if a TARGET number is in INPUT
get_random () GENERATE__NOTIN() {
{ for i in {1..10}
R=$RANDOM do
} FALSENBR=`seq 1 9 | sort -R | head -n 1`
! [[ $INPUT =~ $FALSENBR ]] \
&& break
done
}
#=====
# Modules
#=====
# REPLACE a random number in INPUT by another one Randomized
REPLACE() {
NAME="REPLACE"
PLACE=`seq 1 6 | sort -R | head -n 1`
TARGET=`echo "$INPUT" | head -c $PLACE | tail -c 1`
GENERATE__NOTIN
NAME+="-$FALSENBR-$TARGET"
NEWINPUT=${INPUT//$TARGET/$FALSENBR}
INSTRUCTIONS1="Remplacez les ${DIGIT[FALSENBR]} par les ${DIGIT[TARGET]}"
}
# REVerse INPUT. Ask to Add first or last number in random.
REVERSE() {
NAME="REVERSE"
RFL=`seq 1 2 | sort -R | head -n 1` # Rand First (1) or Last (2)
ADDNBR=${DIGIT[ADDNBR]}
for ((i=$LENINPUT-1;i>=0;i--))
do
NEWINPUT="$NEWINPUT${INPUT:$i:1}"
done
if (( $RFL == 1 ));then
ADDNBR="${INPUT:0:1}"
NAME+="_FIRST"
NEWINPUT=${NEWINPUT:0:$LENINPUT-1} # Do not show first INPUT NBR
INSTRUCTIONS1="Écrivez le chiffre ${DIGIT[$ADDNBR]} puis"
INSTRUCTIONS2="Recopiez de DROITE à GAUCHE ←"
else else
for n in `od -A n -t u2 -N 64 /dev/urandom`; do RL="$RL$n "; done ADDNBR="${INPUT:$LENINPUT-1:1}"
get_random () NAME+="_LAST"
{ NEWINPUT=${NEWINPUT:1:$LENINPUT-1} # Do not show last INPUT NBR
R=${RL%% *} INSTRUCTIONS1="Recopiez de DROITE à GAUCHE ←"
RL=${RL#* } INSTRUCTIONS2="Terminez par le chiffre ${DIGIT[$ADDNBR]}"
}
fi fi
NAME+="-$ADDNBR"
INTRUS()
{
LENGTH=${#INPUT}
NUMBERS=$(echo $INPUT | grep -o . | tr '\n' ' ')
SORTED_UNIQ_NUM=$(echo "${NUMBERS[@]}" | sort -u | tr '\n' ' ')
INTRUS=-1
for i in 1 2 3 4 5 6 7 8 9
do
if [[ ! " ${SORTED_UNIQ_NUM[@]} " =~ " ${i} " ]]; then
INTRUS=$i
break
fi
done
# Worst case
if [[ $INTRUS -eq "-1" ]]
then
echo Tapez "$INPUT |sans changement"
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
} }
SOMME() # Add FALSE NBR to INPUT at three randomized place
{ NOTIN() {
get_random NAME="NOTIN"
RB=$(($R % 5)) GENERATE__NOTIN
get_random PLACE=(`seq 0 $((LENINPUT-1)) | sort -R | head -n 3 | sort -n`)
RA=$(($R % 10 * (10 ** $RB))) for i in `seq 0 $((LENINPUT-1))`
do
if [[ $(($INPUT % 2)) -eq 0 ]]; then (( $i == ${PLACE[0]} )) \
A=$(($INPUT - $RA)) && NEWINPUT+="$FALSENBR"
B=$RA (( $i == ${PLACE[1]} )) \
else && NEWINPUT+="$FALSENBR"
B=$(($INPUT - $RA)) (( $i == ${PLACE[2]} )) \
A=$RA && NEWINPUT+="$FALSENBR"
fi NEWINPUT="$NEWINPUT${INPUT:$i:1}"
done
get_random NAME+="-$FALSENBR"
R=$(($R % 100)) INSTRUCTIONS1="Recopiez sans les ${DIGIT[FALSENBR]}"
if [[ $R -lt 25 ]]; then
echo "Tapez le résultat de | $A + $B"
elif [[ $R -lt 50 ]]; then
echo "Sommez | $A et $B"
elif [[ $R -lt 75 ]]; then
echo "Additionnez | $A et $B"
else
echo "Saisissez le résultat de | $A + $B"
fi
} }
get_random # Take a random NBR from INPUT. Calculate X
RAND_INDEX=$(($R % ${#TRANSFORMATIONS[@]})) CALCUL() {
PLACE=`seq 1 $((LENINPUT)) | sort -R | head -n 1`
TARGET=`echo "$INPUT" | head -c $PLACE | tail -c 1`
NEWINPUT=${INPUT//$TARGET/X}
INSTRUCTIONS=$(echo "$(${TRANSFORMATIONS[$RAND_INDEX]})" | cut -d "|" -f 1) RC=`seq 1 2 | sort -R | head -n 1` # 1 = - | 2 = +
INSTRUCTIONS2=$(echo "$(${TRANSFORMATIONS[$RAND_INDEX]})" | cut -d "|" -f 3) if (( $RC == 1 ));then
TEXT=$(echo "$(${TRANSFORMATIONS[$RAND_INDEX]})" | cut -d "|" -f 2) FALSENBR=`seq $TARGET 10 | sort -R | head -n 1`
RESUME=$(( FALSENBR - TARGET ))
INSTRUCTIONS1="Calculez: $FALSENBR moins $RESUME. Copiez..."
NAME="CALCULM-$FALSENBR-$RESUME"
else
FALSENBR=`seq 1 4 | sort -R | head -n 1`
if (( $FALSENBR <= $TARGET ));then
RESULT=$(( TARGET + FALSENBR ))
RESUME=$(( TARGET - FALSENBR ))
INSTRUCTIONS1="Calculez: ${DIGIT[FALSENBR]} + ${DIGIT[RESUME]}. Copiez..."
NAME="CALCULP-$FALSENBR-$RESUME"
else
RESULT=$(( FALSENBR - TARGET ))
RESUME=$(( TARGET - RESULT ))
INSTRUCTIONS1="Calculez: ${DIGIT[RESULT]} + ${DIGIT[RESUME]}. Copiez..."
NAME="CALCULP-$RESULT-$RESUME"
fi
fi
INSTRUCTIONS2="...en remplaçant chaque X par la somme"
}
#=====
# Choose a random module
#=====
MODULES=(CALCUL NOTIN REPLACE REVERSE)
NBRMODULES=${#MODULES[@]}
RMODULE=`seq 0 $((NBRMODULES-1)) | sort -R | head -n 1`
eval "${MODULES[RMODULE]}"
TEXT="$NEWINPUT"
CHAPRILURI="$HOME/Images/Captcha/${NAME}_${NEWINPUT}_${INPUT}.png"
convert -size 300x70 xc:none -pointsize 20 \ convert -size 300x70 xc:none -pointsize 20 \
\( -clone 0 -fill black \ \( -clone 0 -fill black \
-stroke black -strokewidth 1 \ -stroke black -strokewidth 1 \
-font Helvetica-Narrow -annotate "0x0+0+0" "\n $INSTRUCTIONS" \ -font Helvetica-Narrow -annotate "0x0+0+0" "\n $INSTRUCTIONS1" \
-font Helvetica-Bold -annotate "0x0+0+22" "\n $TEXT" \ -font Helvetica-Bold -annotate "0x0+0+22" "\n $TEXT" \
-font Helvetica-Narrow -annotate "0x0+0+44" "\n $INSTRUCTIONS2" \ -font Helvetica-Narrow -annotate "0x0+0+44" "\n $INSTRUCTIONS2" \
-roll +$ROLL_X+0 \ -roll +$ROLL_X+0 \
-wave "$WAVE1_AMPLITUDE"x"$WAVE1_LENGTH" \ -wave "$WAVE1_AMPLITUDE"x"$WAVE1_LENGTH" \
-roll -$ROLL_X+0 \) \ -roll -$ROLL_X+0 \) \
-flatten -crop 300x70 +repage -quality 500 -depth 11 png:"/var/lib/ejabberd/chapril_captchas/EXPERIMENTAL_$(date "+%Y-%m-%d-%H%M%S")_${INPUT}.png" -flatten -crop 300x70 +repage -quality 500 \
-depth 11 png:"$CHAPRILURI"
convert -size 300x70 xc:none -pointsize 20 \ convert -size 300x70 xc:none -pointsize 20 \
\( -clone 0 -fill black \ \( -clone 0 -fill black \
-stroke black -strokewidth 1 \ -stroke black -strokewidth 1 \
-font Helvetica-Narrow -annotate "0x0+0+0" "\n $INSTRUCTIONS" \ -font Helvetica-Narrow -annotate "0x0+0+0" "\n $INSTRUCTIONS1" \
-font Helvetica-Bold -annotate "0x0+0+22" "\n $TEXT" \ -font Helvetica-Bold -annotate "0x0+0+22" "\n $TEXT" \
-font Helvetica-Narrow -annotate "0x0+0+44" "\n $INSTRUCTIONS2" \ -font Helvetica-Narrow -annotate "0x0+0+44" "\n $INSTRUCTIONS2" \
-roll +$ROLL_X+0 \ -roll +$ROLL_X+0 \
-wave "$WAVE1_AMPLITUDE"x"$WAVE1_LENGTH" \ -wave "$WAVE1_AMPLITUDE"x"$WAVE1_LENGTH" \
-roll -$ROLL_X+0 \) \ -roll -$ROLL_X+0 \) \