一般暴力破解步骤
  1. 扫描端口号: 22   使用nmap工具扫描
  2. 找到对应的linux服务器
  3. 开始暴力破解,需要知道用户名,IP。

防止暴力破解方式

方法1:

密码足够复杂

方法2:

修改默认的端口号,避免端口号被扫描到。  sshd 默认端口号:22

# cat /etc/ssh/sshd_config
// 修改端口号
Port 28377

方法3

不使用 root 用户名登陆

方法4

不使用密码登陆,只使用ssh证书登陆

一般情况以上可以防止暴力破解

使用 fail2ban 实现锁 IP 防止暴力破解

比如:设置ssh远程登陆5分钟内3次密码验证失败,禁止用户 IP 访问主机1小时,1小时后该限制自动解除。

安装

下载 安装包(python 包)
访问: http://www.fail2ban.org/wiki/index.php/Downloads

# wget https://github.com/fail2ban/fail2ban/archive/0.9.4.tar.gz
# mv 0.9.4.tar.gz fail2ban-0.9.4.tar.gz

安装说明

// 查看 fail2ban 文档
# cat README.md 

Required:
- [Python2 >= 2.6 or Python >= 3.2](http://www.python.org) or [PyPy](http://pypy.org)

....

To install, just do:

    tar xvfj fail2ban-0.9.4.tar.bz2
    cd fail2ban-0.9.4
    python setup.py install

以上文档说明

  1. fail2ban 需要的Python版本: [Python2 >= 2.6 or Python >= 3.2] 的版本
  2. 安装方法

解压,安装

tar xvfj fail2ban-0.9.4.tar.bz2
cd fail2ban-0.9.4
python setup.py install

生成启动脚本

[fail2ban-0.9.4]# cp files/redhat-initd /etc/init.d/fail2ban

如何知道 files/redhat-initd 就是启动脚本

[fail2ban-0.9.4]# cd /etc/init.d/
[init.d]# ls
fail2ban  functions  netconsole  network  vzquota

/etc/init.d/ 下的脚本有个共同特点,都有 chkconfig: xx xx xx

network

#! /bin/bash
#
# network       Bring up/down networking
#
# chkconfig: xx xx xx

fail2ban

#!/bin/bash
#
# chkconfig: - 92 08

所以规律就是:在fail2ban源码包过滤关键字:chkconfig
只要脚本中包含 chkconfig ,就是一个 Redhat 的开机启动脚本

回到fail2ban解压目录,过滤 chkconfig

[fail2ban-0.9.4]# grep chkconfig ./* -R --color
./files/redhat-initd:# chkconfig: - 92 08

chkconfig 配置解释:

# chkconfig: 2345         10          90
            2345系统级   10 启动顺序   关机时,关闭服务的顺序

修改 fail2ban 配置文件, 实现 fail2ban 锁 IP 防止暴力破解

[fail2ban-0.9.4]# vim /etc/fail2ban/jail.conf 

添加一下配置

[ssh-iptables]
enabled = true
filter   = sshd
action   = iptables[name=SSH, port=ssh, protocol=tcp]
           sendmail-whois[name=SSH, dest=you@example.com, sender=fail2ban@exmple.com, sendername="Fail2Ban"]
logpath = /var/log/secure
maxretry = 3
findtime = 300
bantime = 60

启动 fail2ban 服务

[fail2ban-0.9.4]# service fail2ban start
Reloading systemd:  [  OK  ]
Starting fail2ban (via systemctl):  [  OK  ]

测试 错误登陆

[xxx]#  ssh -p xxx  xxx.xxx.xxx.xxx
root@xxx.xx.x.xxx's password: 

查看监控的 信息

[~]# fail2ban-client status ssh-iptables
Status for the jail: ssh-iptables
|- Filter
|  |- Currently failed: 0
|  |- Total failed:     0
|  `- File list:        /var/log/secure
`- Actions
   |- Currently banned: 0
   |- Total banned:     0
   `- Banned IP list:

查看日志

[xx]# cat /var/log/secure