如何配置VPS安全加固

  1. 配置sshd_config文件
  2. 使用SSH Key登录且仅允许使用key登录
  3. 安装防火墙,只允许限定端口可以访问
  4. 启用Fail2ban
  5. 启用两步验证google-authenticator
  6. 限定允许访问的固定ip和动态IP

一、配置sshd_config文件

a. 修改默认端口

nano /etc/ssh/sshd_config

Port 22 修改为其他端口30682

我是用了random number generator,输入最小10000,最大65000。谷歌生成随机端口用来作为SSH链接的访问端口,可以减少大部分默认的攻击

如何配置VPS安全加固

b. 禁止Root用户远程登录或者使用密码登录

二选一

## 不允许Root用户登录
PermitRootLogin no
## 不允许Root用户使用密码登录,但可以SSH Key登录
PermitRootLogin prohibit-password

c. 启用public key登录SSH Key

## 默认文件里面是被注释掉了,去除注释就好了
PubkeyAuthentication yes
AuthorizedKeysFile      .ssh/authorized_keys

d. 不允许密码登录,不允许空密码

## 不允许密码登录,取消掉注释,设置为no。允许空密码,取消注释,设置为no
PasswordAuthentication no
PermitEmptyPasswords no

二、生成SSH Key仅允许key登录

## 生成4096位rsa加密ssh key 密码phrase明文DonSullivan
ssh-keygen -m pem -t rsa -b 4096 -f ~/.ssh/DonSullivan.key -C "DonSullivan"
## 进入隐藏目录.ssh
cd ~/.ssh/
## 把pub公钥重命名为authorized_keys
mv DonSullivan.key.pub authorized_keys
## 修改~/.ssh/目录权限为700 所有者可读写执行,其他人权限0
chmod -R 700 ./
## 修改authorized_keys文件权限为600,所有者可读写,其他人只读。
## 如果不配置600的权限,系统会提示权限过高,配置失败。
chmod 600 authorized_keys

三、安装防火墙只允许限定端口可以访问

Debian系统9 10版本都用iptables配置,centos那个firewall-cmd配置太麻烦了一般情况下。

https://www.cnblogs.com/pipci/p/9714533.html

一开始我是按照这个来配置的了。后面找了一篇官网英文的建议安全设置

https://wiki.debian.org/iptables

nano /etc/iptables.test.rules

建议默认配置规则代码

*filter

# Allows all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
# 允许链路循环,本机ip的流量127.0.0.1都允许,其余的都拒绝。
-A INPUT -i lo -j ACCEPT
-A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT

# 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)
# 开放80 443端口给http和https
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT

# Allows SSH connections 
# 允许SSH链接
# The --dport number is the same as in /etc/ssh/sshd_config
# 因为我前面在sshd_config文件把22端口修改为了30682
# 下面那个22要跟着一起自行修改为30682
-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
#  note that blocking other types of icmp packets is considered a bad idea by some
#  remove -m icmp --icmp-type 8 from this line to allow all kinds of icmp:
#  https://security.stackexchange.com/questions/22711
#  开放icmp 允许用户ping,如果禁ping就ACCEPT修改为REJECT
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT

# log iptables denied calls (access via 'dmesg' command)
# 记录日志,通过dmesg命令把拒绝了信息记录到日志里面去
-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

一般情况下面VPS开放ssh端口,80,443端口够用了。如果是需要使用mysql那么就需要开放3306端口。

因为我有一次Raksmart主机被攻破了,然后发了很多垃圾邮件,被举报SPAM。机房停机到过期。气死我了。所以后面我要把常见的发邮件端口给禁用了。入站出站全部禁用掉。25,2525,2526,587,465,993,995

还有一次经历是Openwrt好像被人攻破了,然后偷跑v2ray的流量一下子就跑光了。一般没病的情况下面,不要把路由公开到网上去,允许外网访问。真TMD的很危险。一天不知道被扫荡多少次。

四、启用Fail2ban

网上教程一大堆,安装两行就够了,配置就是用默认的就好足够用了

sudo apt-get update
sudo apt-get install fail2ban

https://www.digitalocean.com/community/tutorials/how-to-protect-ssh-with-fail2ban-on-ubuntu-14-04

这个东西更新没有那么快的,经久不衰。

五、启用两步验证google-authenticator

参考这个配置登录VPS启用两步验证码,安全性绝对一级棒,但是每次验证都很麻烦。在youtube上面看到一个硬件指纹的YubiKey,可惜太贵了55美金,359元人民币,不然用来代替两步验证码是一个很好的选择了。 https://www.yubico.com/

https://www.vultr.com/docs/how-to-setup-two-factor-authentication-2fa-for-ssh-on-debian-9-using-google-authenticator

# 更新系统
sudo apt update
# 安装谷歌验证器插件
sudo apt install libpam-google-authenticator -y
# 运行配置二维码和应急码
google-authenticator
# y y y n y 配置安3个y,1个n,1个y。以下为演示
# Do you want authentication tokens to be time-based (y/n) y
# Do you want me to update your #"/home/DonSullivan/.google_authenticator" file (y/n) y
#Do you want to disallow multiple uses of the same #authentication
#token? This restricts you to one login about every 30s, but it #increases
#your chances to notice or even prevent man-in-the-middle #attacks (y/n) y
#you can increase the window from its default
#size of +-1min (window size of 3) to about +-4min (window size #of
#17 acceptable tokens).
#Do you want to do so? (y/n) n
#If the computer that you are logging into isn't hardened #against brute-force
#login attempts, you can enable rate-limiting for the #authentication module.
#By default, this limits attackers to no more than 3 login #attempts every 30s.
#Do you want to enable rate-limiting (y/n) y

配置pam.d模块,也就是密码验证模块

nano /etc/pam.d/sshd
#找到@include common-auth并取消注释
@include common-auth
auth required pam_google_authenticator.so
#ctrl + x 退出保存
sudo nano /etc/ssh/sshd_config
# 禁用密码登录
PasswordAuthentication no
# 授权登录方式先是验证ssh key,然后是键盘输入二次验证码。
AuthenticationMethods publickey,keyboard-interactive
# CentOS 重启ssh服务
sudo service ssh restart
# Debian 重启sshd服务
systemctl restart sshd

如果看到下面这个提示那就是配置成功正确的了

ssh user@serverip

Authenticated with partial success.
Verification code:

六、限定允许访问的固定ip和动态IP

谷歌搜索“只允许动态ip登录ssh”

参考两篇文章

https://www.huaweicloud.com/articles/6219f796ba98c93ca0d4e8873b976b85.html

https://blog.51cto.com/xiangcun168/1699264

#允许访问的固定ip
nano /etc/hosts.allow
sshd:175.10.147.202:allow
#禁止所有IP访问(除了允许访问ip以外,全部禁止访问sshd)
nano /etc/hosts.deny
sshd:ALL:deny

然后使用ddns解析自动脚本获取动态办公室网络的动态ip

# 编辑getip.sh脚本
nano /var/local/getip.sh
# 复制一下内容
#!/bin/bash
#get new ip from ddns
getip=`dig +short 你的DDNS.asuscomm.com`
oldip=`cat /etc/hosts.allow|grep sshd |awk -F':' '{print $2}'|head -n1`
if [ -z "$getip" ]
  then
    getip=`dig +short 你的DDNS.asuscomm.com @8.8.8.8`
fi
if [ -z "$getip" ]
  then
    getip=`dig +short 你的DDNS.asuscomm.com @1.1.1.1`
fi
if [ -z "$getip" ]
  then
    exit
  else
    if [ $getip!=$oldip ]
      then
      sed -i "s/$oldip/$getip/g" /etc/hosts.allow
    else
      exit
    fi
fi
#赋予777的权限,之前给755的权限好像运行会出错。
chmod 777 /var/local/getip.sh
#添加定时任务,每分钟检查ip更新一下ddns动态ip。
crontab -e
*/1 * * * * /var/local/getip.sh  > /dev/null 2>&1

总结

  1. 修改端口,禁用密码,不允许空密码
  2. 公钥登录,启用ssh key登录
  3. 配置防火墙,开放有限端口80 443 ssh port 3306
  4. Fail2ban,防止暴力登陆
  5. 两步验证
  6. 限定IP

一般情况下面能够做到这六级防护安全性非常高了,不过也要小心不要把自己锁在门外了。到时候只能重装系统或者noVNC那就比较尴尬了。

原创文章,作者:jessegold,如若转载,请注明出处:https://www.hero4u.cn/blog/2021/02/how-to-configure-vps-and-securing-ssh/

(0)
jessegoldjessegold
上一篇 2021-01-29
下一篇 2021-02-03

相关推荐