From 2b63ec3527de87fb6b1dd657d63d9622198586e1 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Tue, 28 Feb 2023 11:04:30 +0100 Subject: [PATCH] remove if --- roles/deploy-web/files/blacklist.sh | 66 ++++++++++++++--------------- 1 file changed, 32 insertions(+), 34 deletions(-) diff --git a/roles/deploy-web/files/blacklist.sh b/roles/deploy-web/files/blacklist.sh index fe8be21..25144b2 100755 --- a/roles/deploy-web/files/blacklist.sh +++ b/roles/deploy-web/files/blacklist.sh @@ -3,40 +3,38 @@ MAIL=/tmp/mail SERVER_LOG=/var/log/nginx HOST=($(cat /etc/sentinel/virtualhost)) BLACKLIST=/etc/sentinel/blacklist -currently_blacklist=$(ps -ef | grep blacklist | grep bash |grep ${USER} | wc -l) -if [ ${currently_blacklist} -eq 2 ]; then - chain_count=$(iptables -L BLACKLIST -n | wc -l) - if [ ${chain_count} -eq 0 ]; then - bash /usr/local/bin/sentinel/refill_blacklist.sh - fi - - for i in ${HOST[@]} - do - log_access=${SERVER_LOG}/${i}_access.log - tail -n 50 $log_access | awk -F "|" '{ if($2 == "400" || $2 == "404") print $0}' > /tmp/error_$i - cat /tmp/error_$i | awk -F "|" '{ if($2 == "404") print $1}' > /tmp/404_$i - cat /tmp/error_$i | awk -F "|" '{ if($2 == "400") print $1}' > /tmp/400_$i - cat /tmp/404_$i | sort | uniq -c | awk '{ if($1 >= 5) print $2}' > /tmp/blacklist_404 - cat /tmp/400_$i |sort | uniq -c |awk '{ if($1 >= 5) print $2}' > /tmp/blacklist_400 - count=$(cat /tmp/blacklist_404 /tmp/blacklist_400 |grep -f ${BLACKLIST} -v |sort |uniq |wc -l) - if [ ${count} -ne 0 ]; then - echo "Nouvelle IP blacklisté" > ${MAIL} - list_ip=($(cat /tmp/blacklist_400 /tmp/blacklist_404 |grep -f ${BLACKLIST} -v |sort |uniq)) - for j in ${list_ip[@]} - do - echo ${j} >> ${MAIL} - curl http://ipinfo.io/${j} >> ${MAIL} - echo "" >> ${MAIL} - cat /tmp/error_$i | grep ${j} >> ${MAIL} - echo "" >> ${MAIL} - echo ${j} >> ${BLACKLIST} - iptables -A BLACKLIST -s ${j} -j DROP - done - echo "IP dejà blacklisté : " >> ${MAIL} - cat ${BLACKLIST} >> ${MAIL} - cat ${MAIL} |mail -s "Blacklist IP ${i}" valczebackup@gmail.com - fi - done +chain_count=$(iptables -L BLACKLIST -n | wc -l) +if [ ${chain_count} -eq 0 ]; then + bash /usr/local/bin/sentinel/refill_blacklist.sh fi + +for i in ${HOST[@]} +do + log_access=${SERVER_LOG}/${i}_access.log + tail -n 50 $log_access | awk -F "|" '{ if($2 == "400" || $2 == "404") print $0}' > /tmp/error_$i + cat /tmp/error_$i | awk -F "|" '{ if($2 == "404") print $1}' > /tmp/404_$i + cat /tmp/error_$i | awk -F "|" '{ if($2 == "400") print $1}' > /tmp/400_$i + cat /tmp/404_$i | sort | uniq -c | awk '{ if($1 >= 5) print $2}' > /tmp/blacklist_404 + cat /tmp/400_$i |sort | uniq -c |awk '{ if($1 >= 5) print $2}' > /tmp/blacklist_400 + count=$(cat /tmp/blacklist_404 /tmp/blacklist_400 |grep -f ${BLACKLIST} -v |sort |uniq |wc -l) + if [ ${count} -ne 0 ]; then + echo "Nouvelle IP blacklisté" > ${MAIL} + list_ip=($(cat /tmp/blacklist_400 /tmp/blacklist_404 |grep -f ${BLACKLIST} -v |sort |uniq)) + for j in ${list_ip[@]} + do + echo ${j} >> ${MAIL} + curl http://ipinfo.io/${j} >> ${MAIL} + echo "" >> ${MAIL} + cat /tmp/error_$i | grep ${j} >> ${MAIL} + echo "" >> ${MAIL} + echo ${j} >> ${BLACKLIST} + iptables -A BLACKLIST -s ${j} -j DROP + done + echo "IP dejà blacklisté : " >> ${MAIL} + cat ${BLACKLIST} >> ${MAIL} + cat ${MAIL} |mail -s "Blacklist IP ${i}" valczebackup@gmail.com + fi +done +