Setup fail2ban » Historie » Version 5
Jeremias Keihsler, 13.04.2019 13:27
1 | 1 | Jeremias Keihsler | h1. Setup fail2ban |
---|---|---|---|
2 | |||
3 | 2 | Jeremias Keihsler | h2. Requirements |
4 | |||
5 | To install fail2ban you will need the following: |
||
6 | * a installed and supported operating system (e.g. CentOS 7.x) |
||
7 | * root-access |
||
8 | * a fast internet connection |
||
9 | * [[repo_epel|EPEL]] |
||
10 | |||
11 | h2. Preliminary Note |
||
12 | |||
13 | This procedure is based on a documentation taken from https://www.howtoforge.com/tutorial/how-to-install-fail2ban-on-centos/ |
||
14 | |||
15 | Most Linux servers offer an SSH login via Port 22 for remote administration purposes. This port is a well-known port, therefore, it is often attacked by brute force attacks. Fail2ban is a software that scans log files for brute force login attempts in real-time and bans the attackers with firewalld or iptables. Fail2ban recognizes unwanted access or security breach efforts to the server within the administrator set time frame and blocks the IP addresses which show signs of brute force attacks or dictionary attacks. This program works in the background and continuously scans the log files for unusual login patterns and security breach attempts. |
||
16 | |||
17 | h2. Install |
||
18 | |||
19 | 3 | Jeremias Keihsler | <pre><code class="bash"> |
20 | 2 | Jeremias Keihsler | yum install fail2ban fail2ban-systemd |
21 | </code></pre> |
||
22 | 4 | Jeremias Keihsler | |
23 | If you have SELinux installed, then update the SELinux policies: |
||
24 | |||
25 | <pre><code class="bash"> |
||
26 | yum update -y selinux-policy* |
||
27 | </code></pre> |
||
28 | |||
29 | h2. Configure settings for Fail2Ban |
||
30 | |||
31 | Once installed, we will have to configure and customize the software with a jail.local configuration file. The jail.local file overrides the jail.conf file and is used to make your custom configuration update safe. |
||
32 | |||
33 | Make a copy of the jail.conf file and save it with the name jail.local: |
||
34 | |||
35 | <pre><code class="bash"> |
||
36 | cp -pf /etc/fail2ban/jail.conf /etc/fail2ban/jail.local |
||
37 | </code></pre> |
||
38 | |||
39 | 5 | Jeremias Keihsler | Open the jail.local file for editing with the following command. |
40 | 4 | Jeremias Keihsler | |
41 | <pre><code class="bash"> |
||
42 | vim /etc/fail2ban/jail.local |
||
43 | </code></pre> |
||
44 | |||
45 | The file code may consist of many lines of codes which execute to prevent a ban on one or many IP addresses, set bantime duration, etc. A typical jail configuration file contains the following lines. |
||
46 | |||
47 | <pre><code class="ini"> |
||
48 | [DEFAULT] |
||
49 | |||
50 | # |
||
51 | # MISCELLANEOUS OPTIONS |
||
52 | # |
||
53 | |||
54 | # "ignoreip" can be an IP address, a CIDR mask or a DNS host. Fail2ban will not |
||
55 | # ban a host which matches an address in this list. Several addresses can be |
||
56 | # defined using space separator. |
||
57 | ignoreip = 127.0.0.1/8 |
||
58 | |||
59 | # External command that will take an tagged arguments to ignore, e.g. <ip>, |
||
60 | # and return true if the IP is to be ignored. False otherwise. |
||
61 | # |
||
62 | # ignorecommand = /path/to/command <ip> |
||
63 | ignorecommand = |
||
64 | |||
65 | # "bantime" is the number of seconds that a host is banned. |
||
66 | bantime = 600 |
||
67 | |||
68 | # A host is banned if it has generated "maxretry" during the last "findtime" |
||
69 | # seconds. |
||
70 | findtime = 600 |
||
71 | |||
72 | # "maxretry" is the number of failures before a host get banned. |
||
73 | 1 | Jeremias Keihsler | maxretry = 5 |
74 | 5 | Jeremias Keihsler | </code></pre> |
75 | |||
76 | Ignoreip is used to set the list of IPs which will not be banned. The list of IP addresses should be given with a space separator. This parameter is used to set your personal IP address (if you access the server from a fixed IP). |
||
77 | |||
78 | Bantime parameter is used to set the duration of seconds for which a host needs to be banned. |
||
79 | |||
80 | Findtime is the parameter which is used to check if a host must be banned or not. When the host generates maxrety in its last findtime, it is banned. |
||
81 | |||
82 | Maxretry is the parameter used to set the limit for the number of retry's by a host, upon exceeding this limit, the host is banned. |
||
83 | |||
84 | h2. Add a jail file to protect SSH. |
||
85 | |||
86 | Create a new file with the editor |
||
87 | |||
88 | <pre><code class="shell"> |
||
89 | vim /etc/fail2ban/jail.d/sshd.local |
||
90 | </code></pre> |
||
91 | |||
92 | To the above file, add the following lines of code. |
||
93 | |||
94 | <pre><code class="ini"> |
||
95 | [sshd] |
||
96 | enabled = true |
||
97 | port = ssh |
||
98 | #action = firewallcmd-ipset |
||
99 | logpath = %(sshd_log)s |
||
100 | maxretry = 5 |
||
101 | bantime = 86400 |
||
102 | </code></pre> |
||
103 | |||
104 | Parameter enabled is set to true, in order to provide protection, to deactivate protection, it is set to false. The filter parameter checks the sshd configuration file, located in the path /etc/fail2ban/filter.d/sshd.conf. |
||
105 | |||
106 | The parameter action is used to derive the IP address which needs to be banned using the filter available from /etc/fail2ban/action.d/firewallcmd-ipset.conf. |
||
107 | |||
108 | Port parameter may be changed to a new value such as port=1212, as is the case. When using port 22, there is no need to change this parameter. |
||
109 | |||
110 | Logpath provides the path where the log file is stored. This log file is scanned by Fail2Ban. |
||
111 | |||
112 | Maxretry is used to set the maximum limit for failed login entries. |
||
113 | |||
114 | Bantime parameter is used to set the duration of seconds for which a host needs to be banned. |
||
115 | |||
116 | h2. Running Fail2Ban service |
||
117 | |||
118 | When you are not running the CentOS Firewall yet, then start it: |
||
119 | |||
120 | <pre><code class="shell"> |
||
121 | systemctl enable firewalld |
||
122 | systemctl start firewalld |
||
123 | </code></pre> |
||
124 | |||
125 | Execute the following lines of command to run the protective Fail2Ban software on the server. |
||
126 | |||
127 | <pre><code class="shell"> |
||
128 | systemctl enable fail2ban |
||
129 | systemctl start fail2ban |
||
130 | </code></pre> |
||
131 | |||
132 | h2. Tracking Failed login entries |
||
133 | |||
134 | The following command is used to check whether there had been failed attempts to login to sever via ssh port. |
||
135 | |||
136 | <pre><code class="shell"> |
||
137 | cat /var/log/secure | grep 'Failed password' |
||
138 | </code></pre> |
||
139 | |||
140 | Executing the above command will get a list of failed root password attempts from different IP addresses. The format of results will be similar to the one showed below: |
||
141 | |||
142 | <pre><code class="shell"> |
||
143 | Apr 4 17:05:12 htf sshd[4287]: Failed password for root from 108.61.157.25 port 23121 ssh2 |
||
144 | Apr 4 17:05:15 htf sshd[3154]: Failed password for root from 108.61.157.25 port 14486 ssh2 |
||
145 | Apr 4 17:05:16 htf sshd[3154]: Failed password for root from 108.61.157.25 port 24157 ssh2 |
||
146 | Apr 4 17:05:18 htf sshd[3154]: Failed password for root from 108.61.157.25 port 24157 ssh2 |
||
147 | </code></pre> |
||
148 | |||
149 | h2. Checking the banned IPs by Fail2Ban |
||
150 | |||
151 | The following command is used to get a list of banned IP addresses which were recognized as brute force threats. |
||
152 | |||
153 | <pre><code class="shell"> |
||
154 | iptables -L -n |
||
155 | </code></pre> |
||
156 | |||
157 | h2. Check the Fail2Ban Status |
||
158 | |||
159 | Use the following command to check the status of the Fail2Ban jails: |
||
160 | |||
161 | <pre><code class="shell"> |
||
162 | fail2ban-client status |
||
163 | </code></pre> |
||
164 | |||
165 | The result should be similar to this: |
||
166 | |||
167 | <pre><code class="shell"> |
||
168 | [root@htf ]# fail2ban-client status |
||
169 | Status |
||
170 | |- Number of jail: 1 |
||
171 | `- Jail list: sshd |
||
172 | </code></pre> |
||
173 | |||
174 | h2. Unbanning an IP address |
||
175 | |||
176 | In order to remove an IP address from the banned list, parameter IPADDRESS is set to appropriate IP which needs unbanning. The name "sshd" is the name of the jail, in this case the "sshd" jail that we configured above. The following command does the job. |
||
177 | |||
178 | <pre><code class="shell"> |
||
179 | fail2ban-client set sshd unbanip IPADDRESS |
||
180 | 4 | Jeremias Keihsler | </code></pre> |