En este manual veremos como programar un sencillo script en Bash para monitorizar la resolución DNS de un dominio en concreto, se trata de un script que debe dejarse en una sesión de Screen durante un prolongado período de tiempo.
Una de las características mas interesantes de dicho script es que solo mostrará el resultado de las pruebas cuando se lo pidamos ya sea presionando la tecla “x” o enviándole la señal SIGUSR1 al proceso o mostrando los logs de error al presionar la tecla “c” o enviándole la señal SIGUSR2 al proceso.
vi dnsChecker.sh
#!/usr/bin/env bash
clear
if [ $# -ne 1 ]; then
echo ">> ERROR: Illegal number of parameters"
exit
else
DOMAIN=$1
fi
uname -a|grep FreeBSD 1>/dev/null
if [ $? -eq 0 ]; then
OS=FREEBSD
else
OS=LINUX
fi
ERROR_LOG_FILE=/tmp/dnsChecker
if [ $OS == "FREEBSD" ]; then
RANDOM_STRING=$(date | md5)
else
RANDOM_STRING=$(date | md5sum | awk '{print$1}')
fi
ERROR_LOG_FILE=$ERROR_LOG_FILE'_'$RANDOM_STRING'.log'
> $ERROR_LOG_FILE
START_DATE=$(date)
echo "Collecting DNS stats: $DOMAIN -- $START_DATE" >> $ERROR_LOG_FILE
echo "----------------------------------"
echo "| Bash-DNS checker by kr0m v0.3b |"
echo "----------------------------------"
CORRECT=0
ERROR=0
TOTAL=0
show_dns_stats=0
show_dns_stats(){
echo ""
echo "----------------------------------"
echo ">> Stats collection started: $START_DATE"
echo ">> Domain: $DOMAIN"
echo ">> Total requests: $TOTAL"
echo ">> Success: $CORRECT req -> $CORRECT_PERCENTAGE%"
echo ">> Errors: $ERROR req -> $ERROR_PERCENTAGE%"
echo "----------------------------------"
}
show_dns_errors(){
echo ""
N=$(wc -l $ERROR_LOG_FILE|awk '{print$1}')
#echo "N: $N"
echo "----------------------------------"
echo ">> Error log file: $ERROR_LOG_FILE"
if [ $N -gt 1 ]; then
IFS=$'\n'
for ERROR_LINE in $(<$ERROR_LOG_FILE); do
echo ">> $ERROR_LINE"
done
echo "----------------------------------"
else
echo ">> No errors registered"
echo "----------------------------------"
fi
}
# man signal
# stty -a
# trap [action] [signal]
trap "show_dns_stats" SIGUSR1
trap "show_dns_errors" SIGUSR2
# Save current tty settings:
if [ $OS == "FREEBSD" ]; then
old_tty=$(stty -g)
else
old_tty=$(stty --save)
fi
# Disable tty echo and canonical input (ERASE and KILL processing)
stty -echo -icanon min 0;
echo ">> Collecting DNS stats: $DOMAIN"
echo ">> Error log file: $ERROR_LOG_FILE"
echo ""
echo "To check collected data: Press x or send SIGUSR1 to process PID(kill -30 $$)"
echo "To check error log: Press c or send SIGUSR2 to process PID(kill -31 $$)"
while true; do
if read -t 0; then # Input ready
read -n 1 char
if [ ! -z ${char} ] && [ ${char} == "x" ]; then
#echo -e "\nRead: ${char}\n"
show_dns_stats
fi
if [ ! -z ${char} ] && [ ${char} == "c" ]; then
#echo -e "\nRead: ${char}\n"
show_dns_errors
fi
else # No input
ip=$(dig +short $DOMAIN)
if [ -n "$ip" ]; then
#echo "IP: $ip"
let CORRECT=$CORRECT+1
else
#echo ""
#echo "-- $(date) --"
#echo "++ ERROR: Could not resolve hostname: $DOMAIN"
echo "$(date) -> $DOMAIN" >> $ERROR_LOG_FILE
let ERROR=$ERROR+1
fi
let TOTAL=$TOTAL+1
#echo "CORRECT: $CORRECT"
#echo "ERROR: $ERROR"
#echo "TOTAL: $TOTAL"
CORRECT_PERCENTAGE=$(bc -l <<<"$CORRECT/$TOTAL*100" | awk -F. '{print $1"."substr($2,1,2)}')
ERROR_PERCENTAGE=$(bc -l <<<"$ERROR/$TOTAL*100" | awk -F. '{print $1"."substr($2,1,2)}')
# Fix CORRECT_PERCENTAGE/ERROR_PERCENTAGE output quirks
FIRST_DIGIT_CORRECT_PERCENTAGE=$(echo $CORRECT_PERCENTAGE | head -c1)
if [ $FIRST_DIGIT_CORRECT_PERCENTAGE == "." ]; then
CORRECT_PERCENTAGE="0"$CORRECT_PERCENTAGE
fi
FIRST_DIGIT_ERROR_PERCENTAGE=$(echo $ERROR_PERCENTAGE | head -c1)
if [ $FIRST_DIGIT_ERROR_PERCENTAGE == "." ]; then
ERROR_PERCENTAGE="0"$ERROR_PERCENTAGE
fi
if [ $CORRECT_PERCENTAGE == "0." ]; then
CORRECT_PERCENTAGE=0
fi
if [ $ERROR_PERCENTAGE == "0." ]; then
ERROR_PERCENTAGE=0
fi
sleep 0.2
fi
done
# Restore original tty settings:
stty $old_tty
Asginamos los permisos necesarios al script:
chmod 700 dnsChecker.sh
La salida del script es la siguiente:
./dnsChecker.sh alfaexploit.com
----------------------------------
| Bash-DNS checker by kr0m v0.3b |
----------------------------------
>> Collecting DNS stats: alfaexploit.com
>> Error log file: /tmp/dnsChecker_0a347a873d1da30bb77c03a6d55ef9f7.log
To check collected data: Press x or send SIGUSR1 to process PID(kill -30 56211)
To check error log: Press c or send SIGUSR2 to process PID(kill -31 56211)
----------------------------------
>> Stats collection started: viernes, 8 de abril de 2022, 09:58:56 CEST
>> Domain: alfaexploit.com
>> Total requests: 7
>> Success: 7 req -> 100.00%
>> Errors: 0 req -> 0%
----------------------------------
----------------------------------
>> Error log file: /tmp/dnsChecker_0a347a873d1da30bb77c03a6d55ef9f7.log
>> No errors registered
----------------------------------