Reset blocklist when maximum is reached

This commit is contained in:
q3aql 2024-04-14 21:05:41 +02:00
parent 9164a05729
commit f008726714

View File

@ -17,8 +17,8 @@ else
fi fi
# Check dependencies # Check dependencies
path_check="/usr/bin /bin /usr/local/bin /sbin ${HOME}/.local/bin" path_check="/usr/bin /bin /usr/local/bin /sbin ${HOME}/.local/bin /home/linuxbrew/.linuxbrew/bin"
dependencies="iptables cat grep sort sed rm echo systemctl" dependencies="iptables cat grep sort sed rm echo wc touch systemctl"
dependencies_found="" dependencies_found=""
dependencies_not_found="" dependencies_not_found=""
for checkPath in ${path_check} ; do for checkPath in ${path_check} ; do
@ -118,6 +118,13 @@ while [ ${blockauth} -eq 0 ] ; do
sed -i "s/Failed password for invalid user ${user} from/blockauth\[allowed\]\: invalid pass for ${user} from/g" /var/log/auth.log sed -i "s/Failed password for invalid user ${user} from/blockauth\[allowed\]\: invalid pass for ${user} from/g" /var/log/auth.log
systemctl restart rsyslog &> /dev/null systemctl restart rsyslog &> /dev/null
done done
# Reset blocklist when maximum is reached
num_blocklist=$(cat ${blocklist} | wc -l)
if [ "${num_blocklist}" -ge "${max_ip_blocklist}" ] ; then
rm -rf ${blocklist}
touch ${blocklist}
iptables -F
fi
touch ${blocklist} touch ${blocklist}
cat ${blocklist} > ${blocklist}.temp cat ${blocklist} > ${blocklist}.temp
echo "blockauth: creating blocklist" echo "blockauth: creating blocklist"
@ -126,7 +133,6 @@ while [ ${blockauth} -eq 0 ] ; do
sort -u ${blocklist}.temp > ${blocklist} sort -u ${blocklist}.temp > ${blocklist}
rm -rf ${blocklist}.temp rm -rf ${blocklist}.temp
sed -i 's/Failed password for/blockauth\[blocked\]\: invalid pass for/g' /var/log/auth.log sed -i 's/Failed password for/blockauth\[blocked\]\: invalid pass for/g' /var/log/auth.log
# Exclude allowed IPs # Exclude allowed IPs
if [ -z "${always_ip_allowed}" ] ; then if [ -z "${always_ip_allowed}" ] ; then
echo "blockauth: running exclude allowed ips" echo "blockauth: running exclude allowed ips"
@ -140,7 +146,6 @@ while [ ${blockauth} -eq 0 ] ; do
sed -i "/${allowed_ip}/d" ${blocklist} sed -i "/${allowed_ip}/d" ${blocklist}
done done
fi fi
# Block IPs using iptables # Block IPs using iptables
for block_ip in $(cat ${blocklist}) ; do for block_ip in $(cat ${blocklist}) ; do
read_block_ip=$(iptables -n -L | grep "${block_ip}") read_block_ip=$(iptables -n -L | grep "${block_ip}")
@ -151,7 +156,6 @@ while [ ${blockauth} -eq 0 ] ; do
iptables -A OUTPUT -d ${block_ip} -j DROP iptables -A OUTPUT -d ${block_ip} -j DROP
fi fi
done done
# Reduce log # Reduce log
reduce_log ${filelog} reduce_log ${filelog}
done done