如何给你的服务器SSH加上2FA|TOTP
本文最后更新于 318 天前,其中的信息可能已经有所发展或是发生改变。

1.写在前面

让我们想象。。。算了,今天不想了,我就直说吧。

如果你的服务器的SSH在22端口,那么你应该会在日志看到五湖四海的友好访问者想友好的访问你的服务器。

好吧,简单的办法就是换个端口。但是,如果你正好和我一样懒得打那个-p呢?

密钥登录吗?也行,毕竟比密码难猜。可能你会问如果密钥也被猜到了呢?

OK,注重安全总是好事,你要是觉得没什么的话可以看看本站上面导航栏的黑名单。

那么,还有更好的SSH验证方式吗?

有!你可能听说过2FA,也可能在谷歌和微软那里用过,这玩意也能套进SSH?

当然可以!接下来我将以Google Authenticator为例,给你的SSH再加一层安全保障。

2.配置

我们以Ubuntu为例,首先,先安装google的身份验证器PAM

apt install libpam-google-authenticator

然后,编辑你的/etc/pam.d/sshd

在里面添加

auth required pam_google_authenticator.so

看到那个@include common-account了吗?那里是个配置,如果你只想靠着那6位30秒验证码来登录,就把它注释。不注释的话,登录时除了OTP还要系统密码。

接下来,我们还要在/etc/ssh/sshd_config里加入这个

ChallengeResponseAuthentication yes
KbdInteractiveAuthentication yes #这个配置已经有了,改成yes
AuthenticationMethods "publickey,keyboard-interactive"

第一个是要求验证TOTP,第三个是验证方式

然后,在你的终端执行

google-authenticator

这会配置谷歌验证器,然后,他会问你几个问题,y/n选择。

Do you want authentication tokens to be time-based (y/n)

是否希望身份验证令牌基于时间 (y/n)

然后,他会给你一个二维码和一个密钥。用你手机的google authenticator扫一下或者添加密钥

把手机给的OTP再输入给终端,回车。

Enter code from app (-1 to skip): 113638
Code confirmed
Your emergency scratch codes are:
40315362
42113370
49723453
77076471
97543986

像这样,它会给你几个紧急暂存代码。继续

Do you want me to update your “/root/.google_authenticator” file? (y/n)

您希望我更新您的“/root/.google_authenticator”文件吗?(是/否)

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)

是否要禁止多次使用同一身份验证令牌?这限制您大约每 30 秒登录一次,但它会增加
您注意到甚至防止中间人攻击的机会 (Y/N)

By default, a new token is generated every 30 seconds by the mobile app.
In order to compensate for possible time-skew between the client and the server,
we allow an extra token before and after the current time. This allows for a
time skew of up to 30 seconds between authentication server and client. If you
experience problems with poor time synchronization, you can increase the window
from its default size of 3 permitted codes (one previous code, the current
code, the next code) to 17 permitted codes (the 8 previous codes, the current
code, and the 8 next codes). This will permit for a time skew of up to 4 minutes
between client and server.
Do you want to do so? (y/n)

默认情况下,移动应用每 30 秒生成一个新令牌。
为了补偿客户端和服务器之间可能存在的时间偏差,
我们允许在当前时间之前和之后使用额外的令牌。这允许
身份验证服务器和客户端之间的时间偏差长达 30 秒。如果你
遇到时间同步不良的问题,可以增加窗口
从其默认大小 3 个允许的代码(一个前一个代码,当前代码
code,下一个代码)到 17 个允许的代码(前 8 个代码,当前
代码,以及接下来的 8 个代码)。这将允许长达 4 分钟的时间偏差
在客户端和服务器之间。
是否要这样做?(是/否)

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)

如果您登录的计算机未针对暴力破解进行强化
登录尝试时,您可以为身份验证模块启用速率限制。
默认情况下,这会将攻击者限制为每 30 秒不超过 3 次登录尝试。
是否要启用速率限制?(是/否)

到此,配置完成。让我们sshd -t重载配置。

现在,要三重验证才能SSH到你的服务器。密钥,系统密码,TOTP。

如果不需要密钥,把AuthenticationMethods "publickey,keyboard-interactive"

改成

AuthenticationMethods "keyboard-interactive"PubkeyAuthentication no

直接关闭密钥登录的后果就是你的sshd会报错。

关闭密码的方式前面已经写出。

3.导出2FA密钥

什么?你说你想在Bitwarden之类的第三方软件显示你服务器的TOTP?

好,让我们继续。

不不不!不要直接把google-authenticator给你的密钥给别的软件,那样是没有用的。

这里准备一个github上的密钥提取软件。可以直接在这里下载。

点击下载

还有,你需要安装好python。

安装后,在cmd里执行

pip install protobuf
pip install qrcode[pil]

首先,我们打开手机的google authenticator,点击左上角的菜单,找到转移账号。

点击“导出账号”

选择你服务器的账号

然后,保存这个二维码,扫描它。

你就会得到这么一串东西

otpauth-migration://offline?data=Ci0KENq5fOWgFQUEUW8QLLy3hAgSC3Jvb3RAdWJ1bnR1GgZ1YnVudHUgASgBMAIQARgBIAAok5SRs%2Fv%2F%2F%2F%2F%2FAQ%3D%3D

进入我们的密钥提取软件目录,找到example_export.txt。

把里面的内容全部删除,把你的那一串东西粘贴进去。

进入scr文件夹,把cmd的运行目录设置在这里。

在cmd执行

python extract_otp_secrets.py -p ..\example_export.txt

Secret就是你的密钥。

然后把这个密钥给其他的2FA验证软件。

现在打开你手机的google authenticator,对一下,如果TOTP一样,那么就成功了。

4.参考文章

如何在 Linux 中为 SSH 设置双因素身份验证

在 Ubuntu 和 Debian 上启用双因子身份验证的三种备选方案

Extract Secret Keys from Google Authenticator QR Code

作者信息:

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇