Finally I managed to get both the CSF regex & fail2ban regex to work and the spam being minimized a lot. Hope this will help someone.
For CSF
========
1. Set the value of CUSTOM2_LOG in CSF to /var/log/maillog.
2. Set the following in /etc/csf/regex.custom.pm. You may also test your regex pattern at
https://regex101.com.
if (($lgfile eq $config{CUSTOM2_LOG}) and ($line =~ /^.*postfix\/smtpd\[.*\]: NOQUEUE: reject: RCPT from (\S+)\[(\S+)\]: 450 4.1.1 <(\S+)>.*$/)) {
#It will block permanently anyone with more than 1 matches.
return ("450 4.1.1 spammer do not delete - $3",$2,"SMTP","1","25,587,465","1");
}
3. Restart CSF & LFD.
service lfd restart
csf -r
4. Check the CSF log file /etc/csf/csf.deny.
For fail2ban
========
1. In /etc/fail2ban/jail.conf, [postfix-tcpwrapper] section, set the following.
enabled = true
filter = postfix
action = hostsdeny[file=/etc/hosts.deny]
logpath = /var/log/maillog
bantime = 604800
ignoreip = 127.0.0.1/8
findtime = 300
maxretry = 3
You may adjust the value of bantime & maxretry with your own desire value.
2. Set the regex in /etc/fail2ban/filter.d/postfix.conf.
failregex = reject: RCPT from .*\[<HOST>\]: 450 4.1.1
reject: RCPT from .*\[<HOST>\]: 454 4.7.1
reject: RCPT from (.*)\[<HOST>\]: 554
3. You may test run the regex checking with the command as below.
fail2ban-regex /var/log/maillog /etc/fail2ban/filter.d/postfix.conf
4. Restart fail2ban service. (service fail2ban restart)
5. Check the file /etc/hosts.deny see if any IP being banned.