61 lines
1.8 KiB
Bash
Raw Normal View History

2023-01-25 20:09:19 +01:00
#!/bin/bash
MAIL=/tmp/mail
SERVER_LOG=/var/log/nginx
HOST=($(cat /etc/sentinel/virtualhost))
2023-02-02 23:35:06 +01:00
BLACKLIST=/etc/sentinel/blacklist
2023-02-04 18:38:19 +01:00
chain_count=$(iptables -L BLACKLIST | wc -l)
if [ ${chain_count} -eq 0 ]; then
iptables -N BLACKLIST
2023-02-05 17:58:15 +01:00
iptables -A INPUT -p tcp -m tcp --dport 80 -j BLACKLIST
iptables -A INPUT -p tcp -m tcp --dport 443 -j BLACKLIST
2023-02-04 18:38:19 +01:00
fi
2023-02-02 23:35:06 +01:00
if [ ! -f ${BLACKLIST} ]; then
touch ${BLACKLIST}
2023-01-25 20:09:19 +01:00
fi
2023-02-04 18:38:19 +01:00
if [ ${chain_count} -gt 2 ]; then
chain_count=$(echo ${chain_count}-2 |bc)
iptables_ip=($(iptables -nvL BLACKLIST | tail -n ${chain_count} | awk '{print $8}'))
for i in $(cat ${BLACKLIST})
do
block_ip=1
for j in ${iptables_ip[@]}
do
if [ "${i}" == "${j}" ]; then
block_ip=0
fi
done
if [ ${block_ip} -eq 1 ]; then
iptables -A BLACKLIST -s ${i} -j DROP
fi
done
else
for i in $(cat ${BLACKLIST})
do
iptables -A BLACKLIST -s ${i} -j DROP
done
fi
2023-01-25 20:09:19 +01:00
for i in ${HOST[@]}
do
log_access=${SERVER_LOG}/${i}_access.log
tail -n 50 $log_access | awk -F "|" '{ if($2 == "404") print $1}' > /tmp/404_$i
tail -n 50 $log_access | 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
2023-02-02 23:35:06 +01:00
count=$(cat /tmp/blacklist_404 /tmp/blacklist_400 |grep -f ${BLACKLIST} -v |sort |uniq |wc -l)
2023-01-25 20:09:19 +01:00
if [ ${count} -ne 0 ]; then
2023-02-02 23:35:06 +01:00
echo "Nouvelle IP blacklisté" > ${MAIL}
2023-02-05 20:58:28 +01:00
list_ip=($(cat /tmp/blacklist_400 /tmp/blacklist_404 |grep -f ${BLACKLIST} -v |sort |uniq))
for i in ${list_ip[@]}
do
echo ${i} >> ${MAIL}
curl http://ipinfo.ip/${i} >> ${MAIL}
echo ${i} >> ${BLACKLIST}
done
2023-02-02 23:35:06 +01:00
echo "IP dejà blacklisté : " >> ${MAIL}
cat ${BLACKLIST} >> ${MAIL}
2023-01-25 20:09:19 +01:00
cat ${MAIL} |mail -s "Blacklist IP ${i}" valczebackup@gmail.com
fi
done