Bài viết sau HOSTVN sẽ hướng dẫn các bạn cài đặt Fail2Ban để tự động block những địa chỉ IP đăng nhập sai thông tin SSH nhiều lần.
Các bạn sử dụng lệnh sau để cài đặt Fail2Ban từ EPEL Repository
# yum install epel-release
# yum install fail2ban
Khi cài đặt xong, các bạn mở file cấu hình của Fail2ban lên và chỉnh theo ý muốn
vi /etc/fail2ban/jail.conf
Các thông số trong file sẽ như sau:
# "ignoreip" can be a list of IP addresses, CIDR masks or DNS hosts. Fail2ban
# will not ban a host which matches an address in this list. Several addresses
# can be defined using space (and/or comma) separator.
#ignoreip = 127.0.0.1/8 ::1
# External command that will take an tagged arguments to ignore, e.g. <ip>,
# and return true if the IP is to be ignored. False otherwise.
#
# ignorecommand = /path/to/command <ip>
ignorecommand =
# "bantime" is the number of seconds that a host is banned.
bantime = 10m
# A host is banned if it has generated "maxretry" during the last "findtime"
# seconds.
findtime = 10m
# "maxretry" is the number of failures before a host get banned.
maxretry = 5
Trong đó:
Tạo file cấu hình
# vi /etc/fail2ban/jail.test
Thêm nội dung sau vào trình soạn thảo
[sshd]
enabled = true
filter = sshd
action = iptables[name=SSH, port=ssh, protocol=tcp]
# sendmail-whois[name=SSH, dest=root, sender=kythuat@hostvn.net]
logpath = /var/log/secure
maxretry = 5
bantime = 3600
Trong đó:
Các bạn sử dụng lệnh sau để khởi động Fai2ban
# chkconfig --level 23 fail2ban on
# systemctl start fail2ban
Các bạn có thể kiểm tra nhật kí theo lệnh sau:
# cat /var/log/secure | grep 'Failed password' | sort | uniq -c
[root@host-103-216-114-145 ~]# cat /var/log/secure | grep 'Failed password' | sort | uniq -c
1 Oct 23 21:51:43 host-103-216-114-145 sshd[7617]: Failed password for invalid user caidianqi from 167.71.9.95 port 50976 ssh2
1 Oct 23 21:53:52 host-103-216-114-145 sshd[7621]: Failed password for invalid user caidianqi from 167.71.9.95 port 32958 ssh2
1 Oct 23 21:54:40 host-103-216-114-145 sshd[7623]: Failed password for root from 20.185.34.197 port 55700 ssh2
1 Oct 23 21:56:04 host-103-216-114-145 sshd[7629]: Failed password for invalid user caiyf from 167.71.9.95 port 43196 ssh2
1 Oct 23 21:56:07 host-103-216-114-145 sshd[7627]: Failed password for root from 221.131.165.33 port 63655 ssh2
1 Oct 23 21:56:09 host-103-216-114-145 sshd[7627]: Failed password for root from 221.131.165.33 port 63655 ssh2
1 Oct 23 21:56:12 host-103-216-114-145 sshd[7627]: Failed password for root from 221.131.165.33 port 63655 ssh2
1 Oct 23 21:56:14 host-103-216-114-145 sshd[7634]: Failed password for root from 221.131.165.33 port 23881 ssh2
1 Oct 23 21:56:17 host-103-216-114-145 sshd[7634]: Failed password for root from 221.131.165.33 port 23881 ssh2
1 Oct 23 21:56:20 host-103-216-114-145 sshd[7634]: Failed password for root from 221.131.165.33 port 23881 ssh2
1 Oct 23 21:56:23 host-103-216-114-145 sshd[7639]: Failed password for root from 221.131.165.33 port 14526 ssh2
Kiểm tra các IP bị chặn các bạn sử dụng lệnh sau:
# fail2ban-client status sshd
Status for the jail: sshd
|- Filter
| |- Currently failed: 0
| |- Total failed: 3
| `- File list: /var/log/secure
`- Actions
|- Currently banned: 1
|- Total banned: 1
`- Banned IP list: 24.45.66.78
Để gỡ IP các bạn thực hiện lệnh sau:
# fail2ban-client set sshd unbanip ip-address
Trong đó:
Như vậy HOSTVN đã hướng dẫn các bạn cài đặt thành công Fail2Ban trên CentOS. Hi vọng bài viết này, bạn có thể xây dựng thêm kiến thức cho mình một cách tốt nhất, chúc bạn thành công!