ssh 和 fail2ban 的安全设置

来源:互联网 发布:淘宝上给差评会怎么样 编辑:程序博客网 时间:2024/05/21 13:58

选择 Debian-8 作为 vps 的操作系统,配置好 ssh 以及安全环境必不可少。选择 fail2ban 软件,通过扫描 log 文件发现恶意攻击者并自动 drop 流量同时通知管理员。

ssh 和 fail2ban 安全配置

转载请注明来自:b0t0w1’blog

搬瓦工 提供了十几种操作系统供选择,我选择了 Debian-8.0-x86_64-minimal 作为操作系统。

Debian 优点

搬瓦工 提供的操作系统有 Ubuntu、Centos、Debian、Fedora 等。

Ubuntu 在个人电脑中使用最多,功能也很全面,网上资料很详尽,各类环境搭建方便不易出错。然而作为 vps 系统耗能太高,内存占用大,对性能影响明显。如果 512MB 的 vps 搭建众多服务,Ubuntu 并不适合。

Centos 是一款服务器操作系统,专为商业服务器定制。相比 Ubuntu 它没有花里花哨的软件与环境,系统性能适中且最为稳定,是商业服务器的首选。但作为个人使用,又显得“力不从心”。不是性能而是环境配置过于繁琐,不兼容。例如默认的 python 为 2.6.6. 导致众多脚本程序无法运行,还得手动下载 python 2.7 和对应的 pip。

Debian 相比 Ubuntu 最大的优点就是性能高,运行相同的程序所占内存远远小于 Ubuntu,而对于 vps 来说内存比什么都贵。相比于 Centos 环境搭建简便很多,各类软件都有配套版本,兼容性、依赖关系容易解决,符合个人使用习惯。缺点就是过于“高精尖”虽然性能很强大但也容易出错。

本人不是 linux 大神,Debian 出错也只能默默叹气。但对于 512MB 的 vps 来说 Debian 是最为合适的选择。也因此选择了 minimal 版本 Debian-8,需要什么环境再装什么环境,最大限度减小系统耗能。


ssh 安全配置

网上众多教程全面但个人局的没有必要那么复杂,做到下列几项足以。

  • 端口
    搬瓦工随机产生 ssh 端口,一般在 27000-30000,如果需要手动修改也要改在这个范围内,建议定时修改端口。

  • 密码
    搬瓦工随机生成的密码包含了大小写、数字,都在 10 位以上,应对 ssh 爆破足以,但也建议定时修改密码。

  • root 登录
    默认允许 root 登录 vps,当然也可以新建普通用户,禁止 root 登录。
vim /etc/ssh/sshd_config# add below linesPermitRootLogin no
  • 密钥登录
    如果觉得还不够安全或者非 root 登录不方便也可以设置密钥登录,然后再禁止密码登录。不过这样导致只能在自己电脑上登录。

  • 用户限制
    可以选择允许登录的用户以及禁止登陆的用户:

vim /etc/ssh/sshd_config# add below linesAllowUsers xxxDenyUsers xxx
  • ip 限制
    ip 也可以作为限制因素,相比 iptables 来说,通过修改配置文件限制 ip ssh 登录要简便的多。
vim /etc/hosts.allow# add below lines# only allow x.x.x.x loginsshd: x.x.x.x# allow all ip loginsshd:all
vim /etc/hosts.deny# add below lines# deny x.x.x.x loginsshd: x.x.x.x

这只是几个最为基本常见的 ssh 防护办法,其他高深技术不再叙述也没有必要。



fail2ban 安装

fail2ban 官方简介

Log files contain interesting information, especially about failed logins. This information can be used to ban an offensive host. This is exactly what Fail2ban does. It scans log files and detects patterns which correspond to possible breakin attempts and then performs actions. Most of the time, it consists of adding a new rule in a firewall chain and sending an e-mail notification to the system administrator.

fail2ban 就是通过扫描所有 log 文件,从中发现恶意的 ssh、ddos、mysql 等行为,然后通过设置 iptables 规则自动化 drop 掉来自这些 ip 的信息,同时发送邮件及时告诉管理员。


fail2ban 安装

apt-get install fail2ban

fail2ban 架构

filter:从 log 文件中按照规则过滤出有用信息。
action:根据规则对恶意行为执行相应 action。如:iptables
jail: 用户配置文件。
client:客户端程序
server:服务端程序


server 简介

server 就是一个监听程序,监听来自 client 的信息,然后根据信息进行相应的 action。

fail2ban-server 命令为:

-b start in background
-f start in foreground
-s socket path
-x force execution of the server
-h, –help display this help message
-V, –version print the version

一般我们使用:

fail2ban-server -b

### client 简介
client 会不停的读取 log 文件从中过滤信息,一旦发现则通过 socks 发送信息至 server。

查看当前状态:

fail2ban-client -d

运行 client

fail2ban-client start

重新导入 jails

fail2ban-client reload

jails 简介

jail 对于用户是最为重要的部分,是各个 sections 的配置信息,大部分为 unable 状态,需要用户修改参数并使其 able。

这里举个例子:

[ssh-iptables]#enabled  = falseenabled  = truefilter   = sshdaction   = iptables[name=SSH, port=ssh, protocol=tcp]#          mail-whois[name=SSH, dest=yourmail@mail.com]#logpath  = /var/log/sshd.loglogpath  = /var/log/auth.logmaxretry = 5

看到模块 ssh-iptables 已经开启,filter 规则为 sshd,action 为使用 iptables drop 改 ip,mail-whois 开启会发送邮件到管理员邮箱及时通知,logpath 时 filter 过滤的 log 文件地址,maxretry 是最多的 ssh 尝试登陆次数。

jails 选项有很多,可以根据实际添加。
2016-09-23_144218.jpg

更多问题请参考:fail2ban 使用手册 和 fail2ban 问题汇总


转载请注明来自:b0t0w1’blog

0 0
原创粉丝点击