2023-02-08 21:17:11 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
2023-03-04 10:47:04 +01:00
|
|
|
IPTABLES=/usr/sbin/iptables
|
2023-02-08 21:17:11 +01:00
|
|
|
BLACKLIST=/etc/sentinel/blacklist
|
2023-03-04 10:47:04 +01:00
|
|
|
chain_count=$(${IPTABLES} -L BLACKLIST -n | wc -l)
|
2023-02-08 21:17:11 +01:00
|
|
|
if [ ${chain_count} -eq 0 ]; then
|
2023-03-04 10:47:04 +01:00
|
|
|
${IPTABLES} -N BLACKLIST
|
|
|
|
${IPTABLES} -I INPUT 1 -p tcp -m tcp --dport 80 -j BLACKLIST
|
|
|
|
${IPTABLES} -I INPUT 1 -p tcp -m tcp --dport 443 -j BLACKLIST
|
2023-02-08 21:17:11 +01:00
|
|
|
|
|
|
|
fi
|
|
|
|
if [ ! -f ${BLACKLIST} ]; then
|
|
|
|
touch ${BLACKLIST}
|
|
|
|
fi
|
|
|
|
if [ ${chain_count} -gt 2 ]; then
|
|
|
|
chain_count=$(echo ${chain_count}-2 |bc)
|
2023-03-04 10:47:04 +01:00
|
|
|
iptables_ip=($(${IPTABLES} -nvL BLACKLIST | tail -n ${chain_count} | awk '{print $8}'))
|
2023-02-08 21:17:11 +01:00
|
|
|
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
|
2023-03-04 10:47:04 +01:00
|
|
|
${IPTABLES} -A BLACKLIST -s ${i} -j DROP
|
2023-02-08 21:17:11 +01:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
else
|
|
|
|
for i in $(cat ${BLACKLIST})
|
|
|
|
do
|
2023-03-04 10:47:04 +01:00
|
|
|
${IPTABLES} -A BLACKLIST -s ${i} -j DROP
|
2023-02-08 21:17:11 +01:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
|