运维与***的对抗之暴力破解篇

 

一般来讲,***如果要进行暴力破击必须要经过三个步骤

1. 知道外网IP,并扫描端口号

2. 猜测所扫描的端口号属于linux服务器还是windows服务器,并由此得出服务器的常用用户名

3. 暴力破解开始

  

运维VS***

防范措施:

方法1:

1、密码足够复杂:

密码的长度要大于8位最好大于14位密码的复杂度是密码要尽可能有数字、大小写字母和特殊符号混合组成

字典库:密码长度20位

2、修改默认端口号  

 [root@xuegod63 ~]# rpm -ivh /mnt/test/Packages/nmap-5.21-4.el6.x86_64.rpm

[root@xuegod63 ~]# nmap 192.168.1.63  #扫描服务开放了哪些端口

Starting Nmap 5.21 () at 2015-05-18 09:48 CST

Nmap scan report for xuegod63.cn (192.168.1.63)

Host is up (0.000027s latency).

Not shown: 998 closed ports

PORT    STATE SERVICE

22/tcp  open  ssh

111/tcp open  rpcbind

 

Nmap done: 1 IP address (1 host up) scanned in 0.30 seconds

 

将默认端口号22改为:???

[root@xuegod63 ~]# vim /etc/ssh/sshd_config

Port 22

为:Port 81

[root@xuegod63 ~]# /etc/init.d/sshd restart

Stopping sshd:                                             [  OK  ]

Starting sshd:                                             [  OK  ]

 

再次扫描:

[root@xuegod63 ~]# nmap 192.168.1.63

Starting Nmap 5.21 (1) at 2015-05-22 21:13 CST

Nmap scan report for xuegod63.cn (192.168.1.63)

Host is up (0.000019s latency).

Not shown: 998 closed ports

PORT    STATE SERVICE

81/tcp  open  hosts2-ns

111/tcp open  rpcbind

 

 

测试:

[root@xuegod63 ~]# ssh root@192.168.1.63

ssh: connect to host 192.168.1.63 port 22: Connection refused

正确的方法:

[root@xuegod63 ~]# ssh root@192.168.1.63 -p 81

注:改端口号的目的是为了加大***判断服务器是否为linux的难度

 

3、不使用root用户名登录

不使用用户名为root的帐号登录系统,但是要获得root超级管理员权限。

注:判断一个用户是不是超级管理员,看的是用户的ID是否为0。

 

例:创建一个普通帐号

[root@xuegod63 ~]# useradd top

[root@xuegod63 ~]# echo 123456 | passwd --stdin top

Changing password for user top.

passwd: all authentication tokens updated successfully.

[root@xuegod63 ~]# vim /etc/passwd

改:root:x:0:0:root:/root:/bin/bash

为:root:x:0:0:root:/sbin/nologin   #不让root登录系统

 

改:top:x:500:500::/home/top:/bin/bash

为:top:x:0:0::/home/top:/bin/bash  #改ID500为0,top就成root身份

 

测试:

[root@xuegod63 ~]# ssh root@192.168.1.63 -p 81   #root登录不成功

root@192.168.1.63's password: 123456

Last login: Sun May 10 09:43:55 2015 from xuegod63.cn

This account is currently not available.

Connection to 192.168.1.63 closed.

 

[root@xuegod63 ~]# ssh top@192.168.1.63 -p 80

top@192.168.1.63's password:

Last login: Sun May 10 09:49:02 2015 from xuegod63.cn

 

[root@xuegod63 ~]# whoami   #查看当前登录系统的用户名

root

[root@xuegod63 ~]# id top

uid=0(root) gid=0(root) groups=0(root)

以上三种方法:

1、密码足够复杂:

2、修改默认端口号

3、不使用root用户名登录

一般情况这个就可以解决了暴力破解的问题了。

 

但是事实上还是会有些***,只做暴力破解,并没有抱着能够破解成功的打算,持续***是其主要目的

 

我们来看这样一个场景:

最近公网网站一直被别人暴力破解sshd服务密码。虽然没有成功,但会导致系统负载很高,原因是在暴力破解的时候,系统会不断地认证用户,从而增加了系统资源额外开销,导致访问公司网站速度很慢。

fail2ban可以监视你的系统日志,然后匹配日志的错误信息(正则式匹配)执行相应的屏蔽动作(一般情况下是防火墙),而且可以发送e-mail通知系统管理员,很好、很实用、很强大!

 

fail2ban运行机制:简单来说其功能就是防止暴力破解。工作的原理是通过分析一定时间内的相关服务日志,将满足动作的相关IP利用iptables加入到dorp(丢弃)列表一定时间。 

 

应用实例

设置条件:ssh远程登录5分钟内3次密码验证失败,禁止用户IP访问主机1小时,1小时该限制自动解除,此IP可以重新登录。

 

 

下载软件包: fail2ban-0.8.14.tar.gz

还原环境:

[root@xuegod63 ~]# vim /etc/passwd

改:root:x:0:0:root:/root:/sbin/nologin

为:root:x:0:0:root:/root:/bin/bash

 

[root@xuegod63 ~]# vim /etc/ssh/sshd_config

改:Port 81

为:Port 22

 [root@xuegod63 ~]# service sshd restart

Stopping sshd:                                             [  OK  ]

Starting sshd:                                             [  OK  ]

 

 

上传软件包fail2ban到linux服务器上:

安装:fail2ban

[root@xuegod63 ~]# tar  -zxvf fail2ban-0.8.14.tar.gz

[root@xuegod63 fail2ban-0.8.14]# cd fail2ban-0.8.14

[root@xuegod63 fail2ban-0.8.14]# vim README.md  #查看以下内容

 

需要安装python开发环境,并且版本要大于2.4

查看当前系统中python的版本:

[root@xuegod63 fail2ban-0.8.14]# python -V

Python 2.6.6

安装:

[root@xuegod63 ~]# cd fail2ban-0.8.14

[root@xuegod63 fail2ban-0.8.14]#python setup.py install


生成服务启动脚本:

[root@xuegod63 fail2ban-0.8.14]# cp files/redhat-initd /etc/init.d/fail2ban

[root@xuegod63 fail2ban-0.8.14]# chkconfig --add fail2ban

[root@xuegod63 fail2ban-0.8.14]# chkconfig  --list fail2ban

fail2ban       0:off1:off2:off3:on4:on5:on6:off

 

 

企业级实战案例:

设置条件:ssh远程登录5分钟内3次密码验证失败,禁止用户IP访问主机1小时,1小时该限制自动解除,此IP可以重新登录。

 

[root@xuegod63 ~]# 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@example.com, sendername="Fail2Ban"]

logpath  = /var/log/secure

findtime  = 300

maxretry = 3

bantime  = 3600

 

注释:

enabled  = true  #是否激活此项(true/false)修改成 true

logpath  = /var/log/secure #检测的系统的登陆日志文件。这里要写sshd服务日志文件。

#完成:5分钟内3次密码验证失败,禁止用户IP访问主机1小时。 配置如下

findtime  = 300  #在5分钟内内出现规定次数就开始工作,默认时间单位:秒

maxretry = 3    #3次密码验证失败

bantime  = 3600         #禁止用户IP访问主机1小时   

 

启动服务:

[root@xuegod63 fail2ban-0.8.14]#service fail2ban start

Starting fail2ban:                                         [确定]

 

[root@xuegod63 ~]# ssh root@192.168.1.63  #只有密码对, 就可以正常登录

root@192.168.1.63's password:

Last login: Wed May  6 14:58:02 2015 from xuegod63.cn

[root@xuegod63 ~]#

 

测试:故意输入错误密码3次,再进行登录时,会拒绝登录

[root@xuegod64 ~]# ssh 192.168.1.63

root@192.168.1.63's password:

Permission denied, please try again.

root@192.168.1.63's password:

Permission denied, please try again.

root@192.168.1.63's password:

Permission denied (publickey,password).

[root@xuegod64 ~]# ssh 192.168.1.63

ssh: connect to host 192.168.1.63 port 22: Connection refused

 

哪台机器在暴力破解你的服务器?

 

查看fail2ban服务运行状态:

[root@xuegod63 fail2ban]# fail2ban-client status  #配置好之后我们检测下fail2ban是否工作

Status

|- Number of jail:1

`- Jail list:ssh-iptables

 

[root@xuegod63 fail2ban]# fail2ban-client status ssh-iptables  

#具体看某一项的状态也可以看,如果显示被ban的ip和数目就表示成功了,如果都是0,说明没有成功。

Status for the jail: ssh-iptables

|- filter

|  |- File list:/var/log/secure

|  |- Currently failed:0

|  `- Total failed:3

`- action

   |- Currently banned:1

   |  `- IP list:192.168.1.63

   `- Total banned:1

 

总结:

1、密码足够复杂:

2、修改默认端口号

3、不使用root用户名登录

一般情况这个就可以解决了暴力破解的问题了。

4、使用fail2ban防止SSHD服务被暴力破解