System environment: Centos5.5: configure Fail2ban ##################################### on Centos ################################### ###################### 1. install the Fail2ban service and download rpmforge, there are a large number of latest rpm packages. wgethttp: // dag.wieers.com/rpm/packages/rpmforge-release/rpmfo
System environment: Centos 5.5
Note: Configure Fail2ban on Centos
######################################## ##################
1. install the Fail2ban service
Download rpmforge, which contains a large number of the latest rpm packages.
Wget http://dag.wieers.com/rpm/packages/rpmforge-release/rpmforge-release-0.3.6-1.el5.rf.i386.rpm
Rpm-ivh rpmforge-release-0.3.6-1.el5.rf.i386.rpm
Start yum ~~!
Yum install fail2ban
Fail2ban noarch 0.8.2-3. el5.rf rpmforge 125 k
Installing for dependencies:
Gamin
Gamin-python
Installation is very simple.
Fail2ban principle
Fail2ban calls iptables to block external attacks in real time, find the logs that meet the conditions within a period of time according to your requirements, and then act.
2. set fail2ban service
The fail2ban settings are listed here.
/Etc/fail2ban
Fail2ban. conf log setting document
Jail. conf
/Etc/fail2ban/filter. d. specify the directory for blocking content.
3. Why do I need to install fail2ban?
Extmail itself is a solution without the mail system being attacked. hackers are constantly trying to get a password or email address for pop3 smtp Authentication. you may ask what is the function of obtaining a mail address? The email address can be sold! It is also very valuable. if spammers purchase these addresses, your email server will be busy.
Do you need to install fail2ban protection?
IV. implement protection
1. protect SSH interception
First, let's see how fail2ban intercepts it?
Cat/etc/fail2ban/filter. d/sshd. conf
Failregex = ^ % (_ prefix_line) s (? : Error: PAM :)? Authentication failure for. * from \ S * $
^ % (_ Prefix_line) sFailed [-/\ w] + for. * from (? : Port \ d *)? (? : Ssh \ d *)? $
^ % (_ Prefix_line) sROOT login refused. * FROM \ S * $
^ % (_ Prefix_line) s [iI] (? : Llegal | nvalid) user. * from \ S * $
^ % (_ Prefix_line) sUser \ S + from Not allowed because not listed in AllowUsers $
^ % (_ Prefix_line) sauthentication failure; logname = \ S * uid = \ S * euid = \ S * tty = \ S * ruser = \ S * rhost = (? : \ S + user = .*)? \ S * $
^ % (_ Prefix_line) srefused connect from \ S + \( \) \ S * $
^ % (_ Prefix_line) sAddress . * Possible break-in attempt \ s * $
Fail2ban uses a regular expression to find out:
Authentication failure/authentication failure
ROOT LOGIN REFUSED
Refused connect from
Possible break-IN ATTEMPT
Not allowed because not listed in AllowUsers
You can delete the preceding information based on your actual requirements. The IP address used to establish the connection
Enable SSH interception
Vi/etc/fail2ban/jail. conf
[Ssh-iptables]
Enabled = true
Filter = sshd
Action = iptables [name = SSH, port = ssh, protocol = tcp]
Sendmail-whois [name = SSH, dest = recipient's email address, sender = sender's email address]
Logpath =/var/log/secure
Maxretry = 3
Findtime= 300
Banktime = 86400
Fail2ban will view the/var/log/secure log file as required, and then meet the conditions within findtime = 300 "5 Minutes"
If maxretry = 3 "3 Times meet the condition" is reached, the IP address will be blocked from connecting to port 22 bantime = 86400 "one day ".
2. POP3 protection interception
Cat/etc/fail2ban/filter. d/courierlogin. conf
Failregex = login failed,. *, ip = \[ \] $
This indicates that the pop3 connection with the user name/password is incorrectly entered.
Enable pop3 protection
Vi/etc/fail2ban/jail. conf
[POP3]
Enabled = true
Filter = courierlogin
Action = iptables [name = pop3, port = 110, protocol = tcp]
Logpath =/var/log/maillog
Banktime = 1800
Findtime= 300
Maxretry = 15
Do you know how to read it? I don't want to write! Tired. You can modify the parameters as required ~!
3. POP3-SSL protection interception
Cat/etc/fail2ban/filter. d/courierlogin-ssl.conf
Failregex = login failed,. *, ip = \[ \] $
This indicates incorrectly entering the pop3-ssl connection for the user name/password.
Vi/etc/fail2ban/jail. conf
POP3-SSL
Enabled = true
Filter = courierlogin-ssl
Action = iptables [name = pop3-ssl, port = 995, protocol = tcp]
Logpath =/var/log/maillog
Banktime = 1800
Findtime= 300
Maxretry = 15
4 SMTP protection interception
Cat/etc/fail2ban/filter. d/couriersmtp. conf
Failregex = postfix/smtpd. * warning: unknown \[ \]: Sasl login authentication failed: authentication failure
This indicates that the smtp connection with the user name/password is incorrectly entered.
Vi/etc/fail2ban/jail. conf
[SMTP]
Enabled = true
Filter = couriersmtp
Action = iptables [name = smtp, port = 25: 366, protocol = tcp]
Logpath =/var/log/maillog
Banktime = 1800
Findtime= 300
Maxretry = 15
The parameters can be modified according to your actual requirements.
5. Extmail logon protection
Because Extmail web mail login is not protected, reinforce it now!
Vi/etc/fail2ban/filter. d/extmail. conf
Failregex = extmail. *: user =. *, client = , Module = login, status = badlogin
In this example, the user name and password are incorrectly entered for web mail login.
Vi/etc/fail2ban/jail. conf
[Extmail]
Enabled = true
Filter = extmail
Action = iptables [name = httpd, port = http, protocol = tcp]
Logpath =/var/log/maillog
Banktime = 300
Findtime= 300
Maxretry = 6
6. POSTFIX protects User unknow.
I don't know the meaning of this action. let's take a look at this article.
Http://hi.baidu.com/enjoyunix/blog/item/e8506058fd3c3189810a183a.html
Vi/etc/fail2ban/filter. d/postfix. conf
Failregex = reject: RCPT from (.*)\[ \]: 450
Vi/etc/fail2ban/jail. conf
[POSTFIX]
Enabled = true
Filter = postfix
Action = iptables [name = postfix, port = 25, protocol = tcp]
Logpath =/var/log/maillog
Banktime = 43200
Findtime= 1200
Maxretry = 5
This protects User unknow testing and spam bounce attacks.
5. check iptables.
Iptables-L-nv
Pkts bytes target prot opt in out source destination
0 0 fail2ban-pop3-ssl tcp -- ** 0.0.0.0/0 0.0.0.0/0 tcp dpt: 995
773 83329 fail2ban-postfix tcp -- ** 0.0.0.0/0 0.0.0.0/0 tcp dpt: 25
299 12660 fail2ban-pop3 tcp -- ** 0.0.0.0/0 0.0.0.0/0 tcp dpt: 110
301 12740 fail2ban-ftp tcp -- ** 0.0.0.0/0 0.0.0.0/0 tcp dpt: 21
3354 253 K fail2ban-SSH tcp -- ** 0.0.0.0/0 0.0.0.0/0 tcp dpt: 22
438 33979 fail2ban-httpd tcp -- ** 0.0.0.0/0 0.0.0.0/0 tcp dpt: 80
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
Pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 5703 packets, 829 K bytes)
Pkts bytes target prot opt in out source destination
Chain fail2ban-SSH (1 references)
Pkts bytes target prot opt in out source destination
3354 253 k return all -- ** 0.0.0.0/0 0.0.0.0/0
Chain fail2ban-ftp (1 references)
Pkts bytes target prot opt in out source destination
301 12740 RETURN all -- ** 0.0.0.0/0 0.0.0.0/0
Chain fail2ban-httpd (1 references)
Pkts bytes target prot opt in out source destination
438 33979 RETURN all -- ** 0.0.0.0/0 0.0.0.0/0
Chain fail2ban-pop3 (1 references)
Pkts bytes target prot opt in out source destination
299 12660 RETURN all -- ** 0.0.0.0/0 0.0.0.0/0
Chain fail2ban-pop3-ssl (1 references)
Pkts bytes target prot opt in out source destination
0 0 RETURN all -- ** 0.0.0.0/0 0.0.0.0/0
Chain fail2ban-postfix (1 references)
Pkts bytes target prot opt in out source destination
773 83329 RETURN all -- ** 0.0.0.0/0 0.0.0.0/0
Chain fail2ban-smtp (0 Records)
Pkts bytes target prot opt in out source destination
0 0 RETURN all -- ** 0.0.0.0/0 0.0.0.0/0
How can we monitor it?
# Watch-n 1 fail2ban-client status POSTFIX
Status for the jail: POSTFIX
|-Filter
|-File list:/var/log/maillog
|-Currently failed: 2
| '-Total failed: 22
'-Action
|-Currently banned: 0
| '-IP list:
'-Total banned: 0
It's boring to look at those spam! Constantly test my email server.
6. check fail2ban logs.
# Cat fail2ban. log | grep '] ban'
20:22:44, 575 fail2ban. actions: WARNING [POSTFIX] Ban IP address
In the past, we can see the IP addresses intercepted and the service.
# Cat fail2ban. log | grep '] Unban'
You can see when to solve the problem.