windows密钥链接linux
前言
- 本文目标: 在公网服务器的linux系统面临黑客的恶意ssh破解,破解密码可能不在密码本中但是会导致ssh创建资源影响系统运行,影响整体的安全性.本文旨在通过windows生成密钥,关闭密码登录ssh来避免爆破产生的ssh内存问题
背景介绍
在一个资源有限的服务器,进程被OOM杀死了,查询系统日志发现有大量的ssh进程创建后OOM
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
38root@htnice:/data# journalctl -k | grep -B 30 -A 5 "Killed process 1149 (python)"
Oct 17 06:53:24 htnice kernel: [ 140597] 106 140597 3787 489 73728 0 0 sshd
Oct 17 06:53:24 htnice kernel: [ 140598] 106 140598 3787 486 69632 0 0 sshd
Oct 17 06:53:24 htnice kernel: [ 140599] 0 140599 4166 704 77824 0 0 sshd
Oct 17 06:53:24 htnice kernel: [ 140600] 106 140600 3787 488 69632 0 0 sshd
Oct 17 06:53:24 htnice kernel: [ 140601] 0 140601 3787 487 73728 0 0 sshd
Oct 17 06:53:24 htnice kernel: [ 140602] 106 140602 3787 489 69632 0 0 sshd
Oct 17 06:53:24 htnice kernel: [ 140603] 106 140603 3787 488 73728 0 0 sshd
Oct 17 06:53:25 htnice kernel: [ 140604] 106 140604 3787 488 65536 0 0 sshd
Oct 17 06:53:25 htnice kernel: [ 140605] 0 140605 3787 505 65536 0 0 sshd
Oct 17 06:53:25 htnice kernel: [ 140608] 0 140608 3787 506 69632 0 0 sshd
Oct 17 06:53:25 htnice kernel: [ 140610] 106 140610 3787 489 77824 0 0 sshd
Oct 17 06:53:25 htnice kernel: [ 140612] 106 140612 3787 489 73728 0 0 sshd
Oct 17 06:53:25 htnice kernel: [ 140614] 106 140614 3787 488 73728 0 0 sshd
Oct 17 06:53:25 htnice kernel: [ 140616] 0 140616 3755 460 77824 0 0 sshd
Oct 17 06:53:25 htnice kernel: [ 140617] 106 140617 3787 489 73728 0 0 sshd
Oct 17 06:53:25 htnice kernel: [ 140619] 0 140619 3755 490 77824 0 0 sshd
Oct 17 06:53:25 htnice kernel: [ 140624] 0 140624 3614 325 65536 0 0 sshd
Oct 17 06:53:25 htnice kernel: [ 140625] 106 140625 3787 517 69632 0 0 sshd
Oct 17 06:53:25 htnice kernel: [ 140627] 106 140627 3787 516 65536 0 0 sshd
Oct 17 06:53:25 htnice kernel: [ 140629] 0 140629 1808 490 45056 0 0 modprobe
Oct 17 06:53:25 htnice kernel: [ 140631] 0 140631 3585 277 61440 0 0 sshd
Oct 17 06:53:25 htnice kernel: [ 140634] 0 140634 1812 211 40960 0 0 modprobe
Oct 17 06:53:25 htnice kernel: [ 140635] 0 140635 1812 106 45056 0 0 modprobe
Oct 17 06:53:25 htnice kernel: [ 140636] 0 140636 1808 569 45056 0 0 modprobe
Oct 17 06:53:25 htnice kernel: [ 140637] 0 140637 1812 426 45056 0 0 modprobe
Oct 17 06:53:25 htnice kernel: [ 140638] 0 140638 1812 234 45056 0 0 modprobe
Oct 17 06:53:25 htnice kernel: [ 140639] 0 140639 1812 235 40960 0 0 modprobe
Oct 17 06:53:25 htnice kernel: [ 140640] 0 140640 120 2 24576 0 0 modprobe
Oct 17 06:53:25 htnice kernel: [ 140641] 0 140641 73 1 20480 0 0 modprobe
Oct 17 06:53:25 htnice kernel: oom-kill:constraint=CONSTRAINT_NONE,nodemask=(null),cpuset=/,mems_allowed=0,global_oom,task_memcg=/system.slice/cron.service,task=python,pid=1149,uid=0
Oct 17 06:53:25 htnice kernel: Out of memory: Killed process 1149 (python) total-vm:179976kB, anon-rss:17144kB, file-rss:1508kB, shmem-rss:0kB, UID:0 pgtables:112kB oom_score_adj:0
Oct 17 06:54:02 htnice kernel: sshd invoked oom-killer: gfp_mask=0x100dca(GFP_HIGHUSER_MOVABLE|__GFP_ZERO), order=0, oom_score_adj=0
Oct 17 06:54:02 htnice kernel: CPU: 0 PID: 140907 Comm: sshd Not tainted 5.10.140 #19
Oct 17 06:54:02 htnice kernel: Hardware name: Nuvoton MA35D1-IoT (DT)
Oct 17 06:54:02 htnice kernel: Call trace:
Oct 17 06:54:02 htnice kernel: dump_backtrace+0x0/0x19c
root@htnice:/data#使用命令监听ssh链接发现一直有人在爆破
1
tail -f /var/log/auth.log | grep --line-buffered "Failed" --color=auto
SSH密钥
ssh-keygen可以生成非对称加密的密钥
1
ssh-keygen -t ed25519 -f C:\Users\用户名\.ssh\文件名 -C "备注"
- 随后会在.ssh目录下生成
文件名和文件名.pub,前者私钥后者公钥
- 随后会在.ssh目录下生成
查看公钥内容
1
type C:\Users\用户名\.ssh\文件名.pub
- 复制出完整的一行
登录到linux服务器
1
2
3
4
5
6
7
8
9
10
11
12创建 .ssh 目录(如果不存在)
mkdir -p ~/.ssh
chmod 700 ~/.ssh
编辑 authorized_keys 文件
nano ~/.ssh/authorized_keys
粘贴刚才复制的公钥(新的一行)
保存: Ctrl+X, 然后 Y, 然后 Enter
设置权限
chmod 600 ~/.ssh/authorized_keys- 如果第N台电脑也要这样链接,则是把公钥追加到
~/.ssh/authorized_keys
- 如果第N台电脑也要这样链接,则是把公钥追加到
此时则是可以在windows下进行ssh登录了
1
ssh -i C:\Users\用户名\.ssh\文件名 -p 端口 用户名@ip
禁止密码登录(关键安全步骤)
- 在确认密钥登录可以成功之后,我们就可以安全地禁用密码登录了,从而彻底杜绝黑客的密码爆破尝试.
重要提示: 在执行以下步骤之前,请务必保持当前已通过密钥登录的SSH窗口处于连接状态.同时,最好打开一个新的终端窗口,再次测试密钥登录是否正常.这样可以防止因为配置失误而导致自己被锁在服务器之外.
编辑SSH配置文件:使用 nano 或 vim 编辑器打开 SSH 服务的配置文件 /etc/ssh/sshd_config.这通常需要 sudo 权限
1
sudo nano /etc/ssh/sshd_config
修改配置项:在文件中找到以下几项,并确保它们的值如下所示.如果某一项被 # 注释掉了,请务必删除行首的 # 使其生效.
禁止密码验证:这是最核心的设置
1
PasswordAuthentication no
确保公钥验证是开启的 (通常默认就是开启的).
1
PubkeyAuthentication yes
(可选但推荐) 禁止质询-响应(challenge-response)认证:这种方式也可能被用来尝试密码登录.
1
ChallengeResponseAuthentication no
修改完成后,保存文件并退出 (Ctrl+X, 然后 Y, 然后 Enter).
重启SSH服务:为了让新的配置生效,你需要重启SSH服务
1
sudo systemctl restart sshd
验证:现在,打开一个新的终端窗口,尝试使用密码登录你的服务器
- 如果配置成功,服务器会拒绝你的密码登录请求,并可能会提示 Permission denied (publickey) 之类的信息.这表明服务器现在只接受密钥方式的登录