Hello good people, I have scan detection running with psad and it works by logging events to /var/log/psad.conf
I added another chain in the /usr/local/hestia/bin/v-add-firewall-chain file and it now looks like this:
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Self heal iptables links
heal_iptables_links
# Checking known chains
case $chain in
SSH) # Get ssh port by reading ssh config file.
sshport=$(grep '^Port ' /etc/ssh/sshd_config | head -1 | cut -d ' ' -f 2)
if [ -z "$sshport" ]; then
sshport=22
fi
port=$sshport;
protocol=TCP ;;
FTP) port=21; protocol=TCP ;;
MAIL) port='25,465,995,993'; protocol=TCP ;;
DNS) port=53; protocol=UDP ;;
WEB) port='80,443'; protocol=TCP ;;
DB) port='3306,5432'; protocol=TCP ;;
HESTIA) port=$hestiaport; protocol=TCP ;;
PSAD) port=''; protocol=ALL ;;
RECIDIVE) port=''; protocol=ALL ;;
*) check_args '2' "$#" 'CHAIN PORT' ;;
esac
I created a very simple filter in /etc/fail2ban/filter.d/psad.conf:
[Definition]
failregex = .* psad: scan detected <HOST> -\> .*
.* psad: scan detected .* scan\): <HOST> -\> .*
ignoreregex = .* icmp .*
and of course section in jail.local
[scan-psad]
enabled = true
filter = psad
action = hestia[name=PSAD]
logpath = /var/log/psad.log
findtime = 2419200
maxretry = 3
So after several times of scanning from “hostile” hosts, the IP is added to the fail2ban-recidive chain on both UDP, TCP and ICMP (I created a separate rule for adding icmp in /usr/local/hestia/bin/v-add-firewall-ban):
# Adding ip to banlist
if [ $chain == "RECIDIVE" ];then
$iptables -w 15 -I fail2ban-$chain 1 -s $ip -j DROP 2>/dev/null
$iptables -w 15 -A INPUT -s $ip -p ICMP -j DROP 2>/dev/null
else
$iptables -w 15 -I fail2ban-$chain 1 -s $ip -j DROP 2>/dev/null
fi
My problem is that even though the IP address is sitting correctly in the iptables table, psad still detects scans from this address and fail2ban tries to add it to the recidive table again. It looks like:
==> /var/log/psad.log <==
2024-06-06 08:15:17.3279 info psad: scan detected (Nmap -sT or -sS scan): 3.142.211.224 -> 81.2.xxx.xxx tcp: [8702] flags: SYN tcp pkts: 1 DL: 2
2024-06-06 08:15:17.3279 info psad: scan detected (Nmap -sT or -sS scan): 3.142.211.224 -> 81.2.xxx.xxx tcp: [8702] flags: SYN tcp pkts: 1 DL: 2
==> /var/log/fail2ban.log <==
2024-06-06 08:15:17,577 fail2ban.actions [2042648]: NOTICE [scan-psad] Ban 3.142.211.224
2024-06-06 08:15:18,275 fail2ban.actions [2042648]: WARNING [recidive] 3.142.211.224 already banned
# iptables -nL |grep -w 185.169.4.105
DROP icmp -- 185.169.4.105 0.0.0.0/0 <- chain INPUT
DROP all -- 185.169.4.105 0.0.0.0/0 <- chain fail2ban-psad
DROP all -- 185.169.4.105 0.0.0.0/0 <- chain fail2ban-recidive
What am I doing wrong, or what is iptables doing wrong, or what is psad doing wrong that I have a very large number of “already banned” in the logs?
Please give me some tips.