New captcha in prod

This commit is contained in:
Adrien Bourmault 2021-08-31 20:37:06 +02:00
parent b63d626d4f
commit bd635ae2a1
Signed by: neox
GPG Key ID: 6EB408FE0ACEC664
2 changed files with 144 additions and 98 deletions

View File

@ -1,5 +1,13 @@
#!/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 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,104 +22,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}
DIGITS=(zéros uns deux trois quatres cinqs six septs huits neufs dix)
DIGIT=(zéro un deux trois quatre cinq six sept huit neuf dix)
TRANSFORMATIONS=(INTRUS SOMME)
DIGIT=(zéros uns deux trois quatres cinqs six septs huits neufs dix)
if test -n ${BASH_VERSION:-''} ; then # ****************************************************************************
get_random () #
{ # Tools
R=$RANDOM #
} # ****************************************************************************
else
for n in `od -A n -t u2 -N 64 /dev/urandom`; do RL="$RL$n "; done
get_random ()
{
R=${RL%% *}
RL=${RL#* }
}
fi
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 # Loops 10 times (should be enough) to check if a TARGET number is in INPUT
GENERATE__NOTIN() {
for i in {1..10}
do do
if [[ ! " ${SORTED_UNIQ_NUM[@]} " =~ " ${i} " ]]; then FALSENBR=`seq 1 9 | sort -R | head -n 1`
INTRUS=$i ! [[ $INPUT =~ $FALSENBR ]] \
break && break
fi done
}
# ****************************************************************************
#
# Captcha shapes
#
# ****************************************************************************
# REPLACEs 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}
INSTRUCTIONS2="en changeant les ${DIGITS[FALSENBR]} en ${DIGITS[TARGET]}"
INSTRUCTIONS1="Recopiez"
}
# REVERSEs 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=${DIGITS[ADDNBR]}
for ((i=$LENINPUT-1;i>=0;i--))
do
NEWINPUT="$NEWINPUT${INPUT:$i:1}"
done done
# Worst case if (( $RFL == 1 ));then
if [[ $INTRUS -eq "-1" ]] ADDNBR="${INPUT:0:1}"
then NAME+="_FIRST"
echo Tapez "$INPUT |sans changement" NEWINPUT=${NEWINPUT:0:$LENINPUT-1} # Do not show first INPUT NBR
return INSTRUCTIONS1="Écrivez le chiffre ${DIGIT[$ADDNBR]} puis"
INSTRUCTIONS2="Recopiez de DROITE à GAUCHE ←"
else
ADDNBR="${INPUT:$LENINPUT-1:1}"
NAME+="_LAST"
NEWINPUT=${NEWINPUT:1:$LENINPUT-1} # Do not show last INPUT NBR
INSTRUCTIONS1="Recopiez de DROITE à GAUCHE ←"
INSTRUCTIONS2="Terminez par le chiffre ${DIGIT[$ADDNBR]}"
fi fi
NAME+="-$ADDNBR"
}
for num in ${NUMBERS[@]}
# Add FALSE NBR to INPUT at three randomized place
NOTIN() {
NAME="NOTIN"
GENERATE__NOTIN
PLACE=(`seq 0 $((LENINPUT-1)) | sort -R | head -n 3 | sort -n`)
for i in `seq 0 $((LENINPUT-1))`
do do
get_random (( $i == ${PLACE[0]} )) \
R=$(($R % 100)) && NEWINPUT+="$FALSENBR"
(( $i == ${PLACE[1]} )) \
if [[ $R -lt 60 ]]; then && NEWINPUT+="$FALSENBR"
NEWINPUT=${NEWINPUT}${num}${INTRUS} (( $i == ${PLACE[2]} )) \
else && NEWINPUT+="$FALSENBR"
NEWINPUT=${NEWINPUT}${num} NEWINPUT="$NEWINPUT${INPUT:$i:1}"
fi
done done
NAME+="-$FALSENBR"
get_random INSTRUCTIONS1="Recopiez sans les ${DIGITS[FALSENBR]}"
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()
{
get_random
RB=$(($R % 5))
get_random
RA=$(($R % 10 * (10 ** $RB)))
if [[ $(($INPUT % 2)) -eq 0 ]]; then # Take a random NBR from INPUT. Calculate X
A=$(($INPUT - $RA)) CALCUL() {
B=$RA PLACE=`seq 1 $((LENINPUT)) | sort -R | head -n 1`
TARGET=`echo "$INPUT" | head -c $PLACE | tail -c 1`
NEWINPUT=${INPUT//$TARGET/X}
RC=`seq 1 2 | sort -R | head -n 1` # 1 = - | 2 = +
if (( $RC == 1 ));then
FALSENBR=`seq $TARGET 10 | sort -R | head -n 1`
RESUME=$(( FALSENBR - TARGET ))
INSTRUCTIONS1="Calculez: $FALSENBR moins $RESUME. Copiez..."
NAME="CALCULM-$FALSENBR-$RESUME"
else else
B=$(($INPUT - $RA)) FALSENBR=`seq 1 4 | sort -R | head -n 1`
A=$RA if (( $FALSENBR <= $TARGET ));then
fi RESULT=$(( TARGET + FALSENBR ))
RESUME=$(( TARGET - FALSENBR ))
get_random INSTRUCTIONS1="Calculez: ${DIGIT[FALSENBR]} + ${DIGIT[RESUME]}. Copiez..."
R=$(($R % 100)) NAME="CALCULP-$FALSENBR-$RESUME"
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 else
echo "Saisissez le résultat de | $A + $B" RESULT=$(( FALSENBR - TARGET ))
RESUME=$(( TARGET - RESULT ))
INSTRUCTIONS1="Calculez: ${DIGIT[RESULT]} + ${DIGIT[RESUME]}. Copiez..."
NAME="CALCULP-$RESULT-$RESUME"
fi fi
fi
INSTRUCTIONS2="...en remplaçant chaque X par la somme"
} }
get_random
RAND_INDEX=$(($R % ${#TRANSFORMATIONS[@]}))
INSTRUCTIONS0=$(echo "$(${TRANSFORMATIONS[$RAND_INDEX]})") # ****************************************************************************
INSTRUCTIONS1=$(echo $INSTRUCTIONS0 | cut -d "|" -f 1) #
INSTRUCTIONS2=$(echo $INSTRUCTIONS0 | cut -d "|" -f 3) # Random captcha shape choice (aka main)
TEXT=$(echo $INSTRUCTIONS0 | cut -d "|" -f 2) #
# ****************************************************************************
MODULES=(CALCUL NOTIN REPLACE REVERSE)
RMODULE=`seq 0 $(( ${#MODULES[@]} - 1)) | sort -R | head -n 1`
eval "${MODULES[RMODULE]}"
TEXT="$NEWINPUT"
CHAPRILURI="/var/lib/ejabberd/chapril_captchas_experimental/$(date "+%Y-%m-%d-%H%M%S")_${NAME}_${INPUT}.png"
convert -size 300x70 xc:none -pointsize 20 \ convert -size 300x70 xc:none -pointsize 20 \
\( -clone 0 -fill black \ \( -clone 0 -fill black \
@ -122,7 +166,8 @@ convert -size 300x70 xc:none -pointsize 20 \
-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/$(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 \

View File

@ -61,7 +61,8 @@ TARGET=`echo "$INPUT" | head -c $PLACE | tail -c 1`
GENERATE__NOTIN GENERATE__NOTIN
NAME+="-$FALSENBR-$TARGET" NAME+="-$FALSENBR-$TARGET"
NEWINPUT=${INPUT//$TARGET/$FALSENBR} NEWINPUT=${INPUT//$TARGET/$FALSENBR}
INSTRUCTIONS1="Remplacez les ${DIGITS[FALSENBR]} par des ${DIGITS[TARGET]}" INSTRUCTIONS2="en changeant les ${DIGITS[FALSENBR]} en ${DIGITS[TARGET]}"
INSTRUCTIONS1="Recopiez"
} }
@ -154,7 +155,7 @@ RMODULE=`seq 0 $(( ${#MODULES[@]} - 1)) | sort -R | head -n 1`
eval "${MODULES[RMODULE]}" eval "${MODULES[RMODULE]}"
TEXT="$NEWINPUT" TEXT="$NEWINPUT"
CHAPRILURI="/var/lib/ejabberd/chapril_captchas_experimental/${NAME}_${NEWINPUT}_${INPUT}.png" CHAPRILURI="/var/lib/ejabberd/chapril_captchas_experimental/$(date "+%Y-%m-%d-%H%M%S")_${NAME}_${INPUT}.png"
convert -size 300x70 xc:none -pointsize 20 \ convert -size 300x70 xc:none -pointsize 20 \
\( -clone 0 -fill black \ \( -clone 0 -fill black \