实战sshd服务fail2ban阻止暴力破解
一、 系统配置介绍
1.0.1 纯环境镜像软件包列表
HOSTNAME | work231 |
IP | 192.168.2.231 |
OS | Centos 6.8 |
核心数量 | 2 |
内存大小 | 2G |
CPU | i3-4130 |
1.0.2 纯环境镜像软件包列表
软件 | 版本 | 安装目录 |
Fail2ban | 0.9.6.6 | /etc/fail2ban |
Python | 2.7.13 | /webserver/python2 |
pip | 9.0.1 | |
setuptools | 32.0.0 |
二、编译之前准备工作
2.0.1 关闭 Selinux 服务
把 SELINUX=enforcing 改为 SELINUX=disabled
vim /etc/selinux/config
2.0.2 下载软件包
Fail2ban官网: http://www.fail2ban.org/wiki/index.php/Downloads
wget https://github.com/fail2ban/fail2ban/archive/0.9.6.tar.gz wget https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgz wget https://pypi.python.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.tar.gz wget https://pypi.python.org/packages/88/13/7d560b75334a8e4b4903f537b7e5a1ad9f1a2f1216e2587aaaf91b38c991/setuptools-35.0.2.zip
2.0.3 更新 Python Yum 源
# Centos 6 python yum 源
[root@work231 ~]# /etc/yum.repos.d/epel.repo [epel] name=Extra Packages for Enterprise Linux 6 - \$basearch #baseurl=http://download.fedoraproject.org/pub/epel/6/\$basearch mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=\$basearch failovermethod=priority enabled=1 gpgcheck=0
# Centos 7 python yum 源
[root@work231 ~]# /etc/yum.repos.d/epel.repo [epel] name=Extra Packages for Enterprise Linux 7 - \$basearch #baseurl=http://download.fedoraproject.org/pub/epel/7/\$basearch mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=\$basearch failovermethod=priority enabled=1 gpgcheck=0
三、开始安装 Python
3.0.1 Python 2.7.13 安装
[root@work231 ~]# mkdir -p /webserver/python2 #新建python2.7.13 安装目录 [root@work231 ~]# tar zxf Python-2.7.13.tgz #解压 Python-2.7.13.tgz [root@work231 ~]# pushd Python-2.7.13 [root@work231 Python-2.7.13]# ./configure --prefix=/webserver/python2 [root@work231 Python-2.7.13]# make && make install [root@work231 Python-2.7.13]# ln -s /webserver/python2/bin/python /usr/bin/python2 [root@work231 Python-2.7.13]# popd
3.0.2 setuptools-32.0.0.zip 安装
[root@work231 ~]# unzip -q setuptools-32.0.0.zip [root@work231 ~]# pushd setuptools-32.0.0 [root@work231 setuptools-32.0.0]# /webserver/python2/bin/python setup.py install [root@work231 setuptools-32.0.0]# popd
3.0.3 pip-9.0.1.tar.gz 安装
[root@work231 ~]# tar zxf pip-9.0.1.tar.gz [root@work231 ~]# pushd pip-9.0.1 [root@work231 pip-9.0.1]# /webserver/python2/bin/python setup.py install [root@work231 ~]# ln -s /webserver/python2/bin/pip /usr/bin/pip2 [root@work231 pip-9.0.1]# popd
3.0.4 配置 pip 源
[root@work231 ~]# [ ! -d "/root/.pip" ] && mkdir /root/.pip [root@work231 ~]# vim /root/.pip/pip.conf
配置阿里云 pip 源
[global] index-url=http://mirrors.aliyun.com/pypi/simple/ [install] trusted-host=mirrors.aliyun.com [list] format=columns
或者选择清华大学 pip 源
[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple [install] trusted-host = mirrors.aliyun.com [list] format = columns
四、开始安装 fail2ban
[root@work231 ~]# tar zxf 0.9.6.tar.gz [root@work231 ~]# cd fail2ban-0.9.6/ [root@work231 fail2ban-0.9.6]# /webserver/python2/bin/python setup.py install
设置fail2ban服务启动脚本:
[root@work231 fail2ban-0.9.6]# /bin/cp files/redhat-initd /etc/init.d/fail2ban
修改第27行 /etc/init.d/fail2ban 文件里的 FAIL2BAN="/usr/bin/fail2ban-client" 路径为
FAIL2BAN=/webserver/python2/bin/fail2ban-client
在第35行下面添加一行内容为:
[ ! -e "/var/run/fail2ban" ] mkdir /var/run/fail2ban
添加开机自动启动
[root@work231 fail2ban-0.9.6]# chmod +x /etc/init.d/fail2ban # 设置开机启动文件权限 [root@work231 fail2ban-0.9.6]# chkconfig --add fail2ban # 添加开机启动服务 [root@work231 fail2ban-0.9.6]# chkconfig fail2ban on # 设置开机启动
小技巧: 你怎么知道要复制这个文件? 一个新的软件包,后期怎么可以知道哪个文件是启动脚本文件?这就要找服务器启动脚本文件中有什么特点,然后过滤出来对应的文件名。
[root@work231 fail2ban-0.9.6]## grep chkconfig ./* -R --color
./files/redhat-initd:# chkconfig: - 92 08
相关主要文件说明:
/etc/fail2ban/action.d #动作文件夹,内含默认文件。iptables以及mail等动作配置
/etc/fail2ban/fail2ban.conf #定义了fai2ban日志级别、日志位置及sock文件位置
/etc/fail2ban/filter.d #条件文件夹,内含默认文件。过滤日志关键内容设置
/etc/fail2ban/jail.conf #主要配置文件,模块化。主要设置启用ban动作的服务及动作阀值
# jail [dʒeɪl] 监狱
/etc/rc.d/init.d/fail2ban #启动脚本文件
设置 fail2ban 日志分割
[root@work231 fail2ban-0.9.6]# mkdir /data/logs/fail2banlogs [root@work231 fail2ban-0.9.6]# cat > /etc/logrotate.d/fail2ban << EOF /data/logs/fail2banlogs/fail2ban.log { missingok notifempty postrotate /webserver/python2/bin/fail2ban-client flushlogs >/dev/null || true endscript } EOF
设置防火墙安全策略,修改文件 /etc/fail2ban/action.d/iptables-common.conf 第64行,最后一行
iptables = iptables <lockingopt> 修改为 iptables = iptables
配置fail2ban 配置文件 jail.local
[root@work231 fail2ban-0.9.6]# echo >/var/log/auth.log [root@work231 fail2ban-0.9.6]# echo >/var/log/secure [root@work231 fail2ban-0.9.6]# cat > /etc/fail2ban/jail.local << EOF #全局设置 [DEFAULT] #忽略的IP列表,不受设置限制(白名单) ,多个IP用空格分开 ignoreip = 127.0.0.1/8 #客户端主机用户IP被禁止访问主机 的时间(秒),单位:秒,3600=1小时,86400=24小时 bantime = 3600 #检测时间,在此时间内超过5分钟规定次数就会激活fail2ban findtime = 300 #客户端主机 在 findtime 时间段内超过规定5次密码尝试失败的次数会被禁止掉 maxretry = 5 [sshd] #是否激活此项(true/false) enabled = true filter = sshd #动作的相关参数,对应action.d/iptables.conf文件 action = iptables[name=SSH, port=ssh, protocol=tcp] backend = %(sshd_backend)s #检测的系统的登陆日志文件。这里要写sshd服务日志文件。 默认为logpath = i logpath = /var/log/secure #触发报警的收件人 sendmail-whois[name=SSH, dest=serice@fooher.com, sender=fail2ban@example.com, sendername="运维天涯"] EOF
启动 fail2ban 服务
[root@work231 fail2ban-0.9.6]# /etc/init.d/fail2ban start [root@work231 fail2ban-0.9.6]# iptables -nL
会多生成一个规则链
配置好之后我们检测下fail2ban工作状态
[root@work231 log]# /webserver/python2/bin/fail2ban-client status Status |- Number of jail: 2 `- Jail list: sshd, sshd-ddos
具体看某一项的状态也可以看,如果显示被ban的ip和数目就表示成功了,如果都是0,说明没有成功。
[root@work231 bin]# /usr/local/python/bin//fail2ban-client status sshd
测试:故意输入错误密码3次,再进行登录时,会拒绝登录,在其他的机器上ssh 192.168.99.232 连续三次输错密码,使用命令,查看阻止状态
测试结果:
我们在来看看fail2ban的日志记录 记录了被阻止的IP 成功阻止了ssh 密码猜测
fail2ban解锁IP地址命令
[root@work231 fail2ban-0.9.6]# /webserver/python2/bin/fail2ban-client set sshd unbanip 192.168.99.232
五、卸载 fail2ban
[root@work231 fail2ban]# /etc/init.d/fail2ban stop [root@work231 fail2ban]# /webserver/python2/bin/pip uninstall -y fail2ban [root@work231 fail2ban]# rm -rf /etc/fail2ban /etc/logrotate.d/fail2ban /var/log/fail2ban.* /var/run/fail2ban /var/log/auth.log