Home 关于sshd 安全的一些配置
Post
Cancel

关于sshd 安全的一些配置

云服务器系统安装完后,默认开启sshd服务。记一些加强公网服务器sshd安全的事项。

查看登录失败的系统日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
$ sudo lastb
wxy      ssh:notty    170.64.174.189   Sun Feb 12 01:53 - 01:53  (00:00)
chengh   ssh:notty    170.64.174.189   Sun Feb 12 01:53 - 01:53  (00:00)
dockery  ssh:notty    170.64.174.189   Sun Feb 12 01:53 - 01:53  (00:00)
test31   ssh:notty    170.64.174.189   Sun Feb 12 01:52 - 01:52  (00:00)
hcode    ssh:notty    170.64.174.189   Sun Feb 12 01:52 - 01:52  (00:00)
test30   ssh:notty    170.64.174.189   Sun Feb 12 01:52 - 01:52  (00:00)
WKY      ssh:notty    170.64.174.189   Sun Feb 12 01:52 - 01:52  (00:00)
liuyu    ssh:notty    170.64.174.189   Sun Feb 12 01:52 - 01:52  (00:00)
boao     ssh:notty    170.64.174.189   Sun Feb 12 01:52 - 01:52  (00:00)
James    ssh:notty    170.64.174.189   Sun Feb 12 01:52 - 01:52  (00:00)
...
...
ubuntu   ssh:notty    206.189.138.40   Mon Feb  6 23:14 - 23:14  (00:00)
ubuntu   ssh:notty    206.189.138.40   Mon Feb  6 23:14 - 23:14  (00:00)
ubuntu   ssh:notty    206.189.138.40   Mon Feb  6 23:12 - 23:12  (00:00)
ubuntu   ssh:notty    206.189.138.40   Mon Feb  6 23:12 - 23:12  (00:00)
gitlab-r ssh:notty    112.168.126.45   Mon Feb  6 23:10 - 23:10  (00:00)
gitlab-r ssh:notty    112.168.126.45   Mon Feb  6 23:10 - 23:10  (00:00)
gitlab-r ssh:notty    112.168.126.45   Mon Feb  6 23:10 - 23:10  (00:00)
gitlab-r ssh:notty    112.168.126.45   Mon Feb  6 23:10 - 23:10  (00:00)
...
...
root     ssh:notty    206.189.138.40   Mon Feb  6 22:29 - 22:29  (00:00)
root     ssh:notty    206.189.138.40   Mon Feb  6 22:28 - 22:28  (00:00)
telnet   ssh:notty    36.38.21.216     Mon Feb  6 22:27 - 22:27  (00:00)
telnet   ssh:notty    36.38.21.216     Mon Feb  6 22:27 - 22:27  (00:00)
root     ssh:notty    206.189.138.40   Mon Feb  6 22:26 - 22:26  (00:00)
root     ssh:notty    206.189.138.40   Mon Feb  6 22:25 - 22:25  (00:00)
root     ssh:notty    206.189.138.40   Mon Feb  6 22:23 - 22:23  (00:00)
root     ssh:notty    206.189.138.40   Mon Feb  6 22:22 - 22:22  (00:00)

有非常多的登录失败日志,需要对云服务器的ssh安全完善一下。

完善方式

以root权限,编辑 /etc/ssh/sshd_config 文件

(1)禁止root用户登录

root用户是linux系统都会有的一个账户,等于一个已知用户名

1
2
3
4
# 直接禁用 
PermitRootLogin no
# 或仅允许密钥
# PermitRootLogin prohibit-password

同时用户名尽量不要使用有特殊意义的名称,如 ubuntu 。

(2)禁用密码,仅允许密钥认证登录

关闭密码登录方式,仅开启密钥认证登录,可以极大提高安全

1
2
3
PubkeyAuthentication yes
PasswordAuthentication no
PermitEmptyPasswords no

(3)修改sshd默认端口

ssh默认端口22,这个端口也是一般的默认攻击端口,直接关闭,改用其他端口, 同时配合云服务器的外部防火墙,可以直接阻断22端口的相关流量。example:

1
Port 12345

同时,需要打开云服务器厂商的防火墙对应的端口,否则流量会被挡。

(4)IP 黑名单

使用以上3步,基本安全了。如果发现lastb 中仍出现大量失败请求,可以进一步使用IP很名单。 该步骤通过编辑 /etc/hosts.deny ,将登录多次失败的IP加入黑名单。

实现策略:编写脚本,获取lastb的输出,分析内容,如果同一个IP失败超过5次,就将其加入黑名单,防止爆破。 脚本交给crontab或systemd.timer 周期运行,如1小时运行一次。lastb则每周或每天清理一次。 或者使用linux的实时文件事件监听系统 inotify ,检测 /var/log/btmp 的变化,有修改时执行检测。

参考实现,on debian11

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash

# run as root
# 功能定义:一周内登录失败达到5次的IP为攻击者IP,将其加入blacklist
# 实现,1.遍历lastb输出,满5计入 buffarray,
#      2.for each in buffarray ,add to hosts.deny file if checked not in 

lastIP="127.0.0.1"
# failip 关联数组,全局变量
declare -A failip

function check_if_in2(){
    if [ ! "${failip[${1}]}" ]; then
        echo "not in" 
    else 
        echo "be in" 
    fi
}

function isValidIp() { 
    local ip=$1 
    local ret=1 
    
    if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then 
        ip=(${ip//\./ }) # 按.分割,转成数组,方便下面的判断
        [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]] 
        ret=$? 
    fi 
    return $ret 
}

while read LINE
do
    if [[ $LINE == "" ]];then
        continue
    fi

    thisIP=`echo "$LINE" | awk -F' ' '{print $3;}'`

    if [[ $thisIP == $lastIP ]];then
        failip[${lastIP}]=$((failip[${lastIP}]+1))
        continue
    else
        lastIP=$thisIP
    fi

    rv=`check_if_in2 $thisIP`
    if [[ $rv == "be in" ]];then
        failip[${thisIP}]=$((failip[${thisIP}]+1))
    else
        failip[${thisIP}]=1
    fi
done <<< `lastb -i | grep -v tty[0-9]`

for u in ${!failip[*]}
do
    if ! isValidIp ${u};then
        continue
    fi
    # echo "failip ${u} count : ${failip[$u]}"
    if (( ${failip[$u]} >= 5 ))  ;then
        echo "ALL:${u}" >> /etc/hosts.deny
    fi
done

hosts.allow和hosts.deny文件可以参考:
https://cloud.tencent.com/developer/article/1533595

(5)IP 白名单

仅允许指定的IP,或IP段登录,有固定公网IP直接填入,没有固定IP可以通过搭建一个 虚拟的私有局域网, 使用内部的虚拟私网IP。


最后重启ssh服务

1
sudo systemctl restart ssh

sshd_config 参考

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Include /etc/ssh/sshd_config.d/*.conf
Port 12345

LoginGraceTime 30
PermitRootLogin no
MaxAuthTries 6

PubkeyAuthentication yes

PermitEmptyPasswords no

ChallengeResponseAuthentication no

UsePAM yes

PrintMotd no

AcceptEnv LANG LC_*

Subsystem sftp  /usr/lib/openssh/sftp-server

X11Forwarding no   # 有图形转发需要就打开
PasswordAuthentication no
This post is licensed under CC BY 4.0 by the author.