服务器安全基础指南

首先,VPS 服务器被黑怎么办?php

第一, 10秒以内登陆你的 VPS 提供商帐号,关机。
第二,切断被黑机器的外网。
第三,在相同的 VPS 提供商重开一台机器,内网登陆,备份重要的信息
第四,迁移数据
第五,报废以前被黑的 VPS nginx

最近正好接触到一次服务器因为开启redis但没注意安全设置从而被提权的入侵的事件,如下是本身作的一些小总结。web

此文章适用于系统版本为:Ubuntu 14.04 64redis

Nginx 配置:

升级到稳定版shell

sudo apt-get update && sudo apt-get upgrade

sudo apt-get upgrade nginx

Nginx 目前在 Ubuntu 14.04 中稳定版本号是 1.4.6 ubuntu

能够经过在 VPS 上执行如下 curl 命令来查看信息:安全

curl -I http://localhost

返回大概是:服务器

HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)

这里直接显示了 Nginx 的版本号,能够经过配置隐藏:框架

sudo vi /etc/nginx/nginx.conf

http 部分添加:less

http {
        ##
        # Basic Settings
        ##
        server_tokens off;
}

重启 Nginx

sudo service nginx reload

这样再次执行 curl 就能够看到隐藏了 Nginx 的版本号了:

curl -I http://localhost
HTTP/1.1 200 OK
Server: nginx
...
X-Powered-By: PHP/5.5.9-1ubuntu4.14
...

注意到上面 X-Powered-By: PHP/5.5.9-1ubuntu4.14 这一行暴露了 PHP 的版本号,因此咱们来配置 php.ini 来隐藏之,即在 php.ini 文件中设置:

expose_php = Off

若是你的服务器的 php.ini 默认就隐藏了版本,那么恭喜你。

SSH 配置:

配置 ssh ,好比关掉密码认证式登陆等:

sudo vi /etc/ssh/sshd_config

打开以后配置:

# 不容许空密码
PermitEmptyPasswords no
# 关闭密码验证登陆,前提是你已经使用了 ssh 密钥验证方式登陆
PasswordAuthentication no
# 若是你在服务器上手动添加了用户并将用户分配到 root 用户组,能够考虑禁止root用户登陆
PermitRootLogin no

重启 ssh

sudo service ssh restart

iptables 配置

iptables 提供了一种可灵活配置安全策略防火墙框架,具体的原理能够看这篇文章:http://seanlook.com/2014/02/23/iptables-understand/

博主写文章很认真,推荐一下

开始以前,能够经过如下命令全新配置 iptables

iptables -F

注意上述命令会将你以前本身设置的过滤规则清空。

你能够查看 iptables 的相关资料,一条一条添加安全规则,也能够这样:

sudo vi /etc/iptables.up.rules

添加内容:

*filter

# Accepts all established inbound connections
 -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# Allows all outbound traffic
# You could modify this to only allow certain traffic
 -A OUTPUT -j ACCEPT

# Allows HTTP and HTTPS connections from anywhere (the normal ports for websites)
 -A INPUT -p tcp --dport 443 -j ACCEPT
 -A INPUT -p tcp --dport 80 -j ACCEPT
# Allows SSH connections for script kiddies
# THE -dport NUMBER IS THE SAME ONE YOU SET UP IN THE SSHD_CONFIG FILE
 -A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT

# Now you should read up on iptables rules and consider whether ssh access
# for everyone is really desired. Most likely you will only allow access from certain IPs.

# Allow ping
 -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT

# log iptables denied calls (access via 'dmesg' command)
 -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7

# Reject all other inbound - default deny unless explicitly allowed policy:
 -A INPUT -j REJECT
 -A FORWARD -j REJECT

COMMIT

其中很是值得注意的是这三条:

# 443 一般是 ssl 的端口,若是 VPS 是做为一台web应用的服务器而且启用 https,你须要这个
 -A INPUT -p tcp --dport 443 -j ACCEPT
 # 80 端口就很少说了
 -A INPUT -p tcp --dport 80 -j ACCEPT
 # 这是 ssh 默认的 22 端口,开放给本身登陆使用
 -A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT

注意这里的 22 端口,你能够修改成其余的端口,而且关闭 22 端口。更有一种解决方案是:将生产机器 ssh 端口彻底关闭,用一台同一子网下的跳板机,经过来登陆目标生产机器。

其余的配置的话,你须要根据你的业务来具体配置相对应的安全策略。

写好这些配置项以后,告诉 iptables 你的配置在哪:

sudo iptables-restore < /etc/iptables.up.rules

建立 shell 脚原本开机自启动:

sudo vi /etc/network/if-up.d/iptables

添加内容:

#!/bin/sh
iptables-restore /etc/iptables.up.rules

给以可执行的权限:

chmod +x /etc/network/if-up.d/iptables

到这里,iptables 的基础设置就OK了。

配置 Fail2Ban

Fail2Ban 的工做原理是:经过监控系统的日志文件,并根据检测到的任何可疑的行为自动触发不一样的防护动做,如将产生可疑行为的目标 ip 锁定等。

安装:

sudo apt-get install fail2ban

打开配置文件,进行配置:

sudo vi /etc/fail2ban/jail.conf

写入:

[DEFAULT]
ignoreip = 127.0.0.1/8
bantime  = 3600
maxretry = 3
destemail = email@yourdomain.com
action = %(action_mw)s

[ssh]

enabled  = true
port     = ssh
filter   = sshd
logpath  = /var/log/auth.log
maxretry = 3

到这里,就是我我的从上次被黑当中总结的一些基本的服务器安全,而至于群里 @little 大神说的跳板机的方案,容我再消化消化再尝试部署起来。

总结

1.服务一些基本的安全配置得跟上。
2.关注安全圈的新闻和动态,及时升级软件和修复相关安全漏洞。
3.服务器上的服务,用哪一个就看哪一个,不用的坚定关掉。
4.服务器一旦被入侵,不急。关机,断网,备份,迁移,舍弃。

记住:The less information you provide, the more secure you are .

相关文章
相关标签/搜索