Skip to content

Commit a6e6434

Browse files
committed
docs: add chapter Fail2ban
1 parent 2701e8e commit a6e6434

2 files changed

Lines changed: 275 additions & 0 deletions

File tree

chapters.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
- scp.md: scp 命令
99
- rsync.md: rsync 命令
1010
- sftp.md: sftp 命令
11+
- fail2ban.md: Fail2ban 教程

docs/fail2ban.md

Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
# Fail2ban 教程
2+
3+
## 简介
4+
5+
Fail2ban 是一个 Linux 系统的应用软件,用来防止系统入侵,主要是防止暴力破解系统密码。它是用 Python 开发的。
6+
7+
它主要通过监控日志文件(比如`/var/log/auth.log``/var/log/apache/access.log`等)来生效。一旦发现恶意攻击的登录请求,它会封锁对方的 IP 地址,使得对方无法再发起请求。
8+
9+
Fail2ban 可以防止有人反复尝试 SSH 密码登录,但是如果 SSH 采用的是密钥登录,禁止了密码登录,就不需要 Fail2ban 来保护。
10+
11+
Fail2ban 的安装命令如下。
12+
13+
```bash
14+
# ubuntu & Debian
15+
$ sudo apt install fail2ban
16+
17+
# Fedora
18+
$ sudo dnf install epel-release
19+
$ sudo dnf install fail2ban
20+
21+
# Centos & Red hat
22+
$ yum install fail2ban
23+
```
24+
25+
安装后,使用下面的命令查看 Fail2ban 的状态。
26+
27+
```bash
28+
$ systemctl status fail2ban.service
29+
```
30+
31+
如果没有启动,就启动 Fail2ban。
32+
33+
```bash
34+
$ sudo systemctl start fail2ban
35+
```
36+
37+
重新启动 Fail2ban。
38+
39+
```bash
40+
$ sudo systemctl restart fail2ban
41+
```
42+
43+
设置 Fail2ban 重启后自动运行。
44+
45+
```bash
46+
$ sudo systemctl enable fail2ban
47+
```
48+
49+
## fail2ban-client
50+
51+
Fail2ban 自带一个客户端 fail2ban-client,用来操作 Fail2ban。
52+
53+
```bash
54+
$ fail2ban-client
55+
```
56+
57+
上面的命令会输出 fail2ban-client 所有的用法。
58+
59+
下面的命令查看激活的监控目标。
60+
61+
```bash
62+
$ fail2ban-client status
63+
64+
Status
65+
|- Number of jail: 1
66+
`- Jail list: sshd
67+
```
68+
69+
下面的命令查看某个监控目标(这里是 sshd)的运行情况。
70+
71+
```bash
72+
$ sudo fail2ban-client status sshd
73+
74+
Status for the jail: sshd
75+
|- Filter
76+
| |- Currently failed: 1
77+
| |- Total failed: 9
78+
| `- Journal matches: _SYSTEMD_UNIT=sshd.service + _COMM=sshd
79+
`- Actions
80+
|- Currently banned: 1
81+
|- Total banned: 1
82+
`- Banned IP list: 0.0.0.0
83+
```
84+
85+
下面的命令输出一个简要的版本,包括所有监控目标被封的 IP 地址。
86+
87+
```bash
88+
$ sudo fail2ban-client banned
89+
[{'sshd': ['192.168.100.50']}, {'apache-auth': []}]
90+
```
91+
92+
下面的命令可以解封某个 IP 地址。
93+
94+
```bash
95+
$ sudo fail2ban-client set sshd unbanip 192.168.1.69
96+
```
97+
98+
## 配置
99+
100+
### 主配置文件
101+
102+
Fail2ban 主配置文件是在`/etc/fail2ban/fail2ban.conf`,可以新建一份副本`/etc/fail2ban/fail2ban.local`,修改都针对副本。
103+
104+
```bash
105+
$ sudo cp /etc/fail2ban/fail2ban.conf /etc/fail2ban/fail2ban.local
106+
```
107+
108+
下面是设置 Fail2ban 的日志位置。
109+
110+
```bash
111+
[Definition]
112+
logtarget = /var/log/fail2ban/fail2ban.log
113+
```
114+
115+
修改配置以后,需要重新启动`fail2ban.service`,让其生效。
116+
117+
### 封禁配置
118+
119+
Fail2ban 封禁行为的配置文件是`/etc/fail2ban/jail.conf`。为了便于修改,可以把它复制一份`/etc/fail2ban/jail.local`,后面的修改都针对`jail.local`这个文件。
120+
121+
```bash
122+
$ sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
123+
```
124+
125+
你也可以在目录`/etc/fail2ban/jail.d`里面,新建单独的子配置文件,比如`/etc/fail2ban/jail.d/sshd.local`
126+
127+
同样地,修改配置以后,需要重新启动`fail2ban.service`,让其生效。
128+
129+
配置文件里面,`[DEFAULT]`标题行表示对于所有封禁目标生效。举例来说,如果封禁时间修改为1天,`/etc/fail2ban/jail.local`里面可以写成:
130+
131+
```bash
132+
[DEFAULT]
133+
bantime = 1d
134+
```
135+
136+
如果某人被封时,对站长发送邮件通知,可以如下设置。
137+
138+
```bash
139+
[DEFAULT]
140+
destemail = yourname@example.com
141+
sender = yourname@example.com
142+
143+
# to ban & send an e-mail with whois report to the destemail.
144+
action = %(action_mw)s
145+
146+
# same as action_mw but also send relevant log lines
147+
#action = %(action_mwl)s
148+
```
149+
150+
如果配置写在其他标题行下,就表示只对该封禁目标生效,比如写在`[sshd]`下面,就表示只对 sshd 生效。
151+
152+
默认情况下,Fail2ban 对各种服务都是关闭的,如果要针对某一项服务开启,需要在配置文件里面声明。
153+
154+
```bash
155+
[sshd]
156+
enabled = true
157+
```
158+
159+
上面声明表示,Fail2ban 对 sshd 开启。
160+
161+
### 配置项
162+
163+
下面是配置文件`jail.local`的配置项含义,所有配置项的格式都是`key=value`
164+
165+
(1)bantime
166+
167+
封禁的时间长度,单位`m`表示分钟,`d`表示天,如果不写单位,则表示秒。Fail2ban 默认封禁10分钟(10m 或 600)。
168+
169+
```bash
170+
[DEFAULT]
171+
bantime = 10m
172+
```
173+
174+
(2)findtime
175+
176+
登录失败计算的时间长度,单位`m`表示分钟,`d`表示天,如果不写单位,则表示秒。Fail2ban 默认封禁 10 分钟内登录 5 次失败的客户端。
177+
178+
```bash
179+
[DEFAULT]
180+
181+
findtime = 10m
182+
maxretry = 5
183+
```
184+
185+
(3)maxretry
186+
187+
尝试登录的最大失败次数。
188+
189+
(4)destemail
190+
191+
接受通知的邮件地址。
192+
193+
```bash
194+
[DEFAULT]
195+
destemail = root@localhost
196+
sender = root@<fq-hostname>
197+
mta = sendmail
198+
```
199+
200+
(5)sendername
201+
202+
通知邮件的“发件人”字段的值。
203+
204+
(6)mta
205+
206+
发送邮件的邮件服务,默认是`sendmail`
207+
208+
(7)action
209+
210+
封禁时采取的动作。
211+
212+
```bash
213+
[DEFAULT]
214+
action = $(action_)s
215+
```
216+
217+
上面的`action_`是默认动作,表示拒绝封禁对象的流量,直到封禁期结束。
218+
219+
下面是 Fail2ban 提供的一些其他动作。
220+
221+
```bash
222+
# ban & send an e-mail with whois report to the destemail.
223+
action_mw = %(action_)s
224+
%(mta)s-whois[sender="%(sender)s", dest="%(destemail)s", protocol="%(protocol)s", chain="%(chain)s"]
225+
226+
# ban & send an e-mail with whois report and relevant log lines
227+
# to the destemail.
228+
action_mwl = %(action_)s
229+
%(mta)s-whois-lines[sender="%(sender)s", dest="%(destemail)s", logpath="%(logpath)s", chain="%(chain)s"]
230+
231+
# See the IMPORTANT note in action.d/xarf-login-attack for when to use this action
232+
#
233+
# ban & send a xarf e-mail to abuse contact of IP address and include relevant log lines
234+
# to the destemail.
235+
action_xarf = %(action_)s
236+
xarf-login-attack[service=%(__name__)s, sender="%(sender)s", logpath="%(logpath)s", port="%(port)s"]
237+
238+
# ban IP on CloudFlare & send an e-mail with whois report and relevant log lines
239+
# to the destemail.
240+
action_cf_mwl = cloudflare[cfuser="%(cfemail)s", cftoken="%(cfapikey)s"]
241+
%(mta)s-whois-lines[sender="%(sender)s", dest="%(destemail)s", logpath="%(logpath)s", chain="%(chain)s"]
242+
```
243+
244+
(8)ignoreip
245+
246+
Fail2ban 可以忽视的可信 IP 地址。多个 IP 地址之间使用空格分隔。
247+
248+
```bash
249+
ignoreip = 127.0.0.1/8 192.168.1.10 192.168.1.20
250+
```
251+
252+
(9)port
253+
254+
指定要监控的端口。可以设为任何端口号或服务名称,比如`ssh``22``2200`等。
255+
256+
### ssh 配置
257+
258+
下面是 sshd 的设置范例。
259+
260+
```bash
261+
[sshd]
262+
enabled = true
263+
port = ssh
264+
filter = sshd
265+
banaction = iptables
266+
backend = systemd
267+
maxretry = 5
268+
findtime = 1d
269+
bantime = 2w
270+
ignoreip = 127.0.0.1/8
271+
```
272+
273+
首先需要注意,为了让 Fail2ban 能够完整发挥作用,最好在`/etc/ssh/sshd_config`里面设置`LogLevel VERBOSE`,保证日志有足够的信息。
274+

0 commit comments

Comments
 (0)