openwrt 使用google-authenticator为openssh做totp登录认证

google-authenticator 简介

totp 基于时间变化,每隔一定的时间 如google-authenticator的是30秒 ; 根据token产生一次新的hash,也就是说在30秒内 hash的值是固定.

修改 dropbear监听端口

dropbear 不提供pam认证模块 因此不能使用totp认证,openssh-pam提供了该功能,因此以下操作为了使用openssh而禁用dropbear

默认的 dropbear 监听在22端口 为了不和下面的openssh 混淆,先改到其他端口,等openssh配置正确再关闭dropbear服务,这样是为了在openssh配置不正确时,而dropbear停止服务,没有任何ssh的方法进入系统

在这里插入图片描述
如上图 dropbear监听在2222端口

创建google-authenticator令牌

在linux系统下载
ubuntu
sudo apt-get install libpam-google-authenticator libpam0g-dev
centos
sudo yum install google-authenticator

如果你使用win10 可以 下载ubuntu20子系统 然后下载
sudo apt-get install libpam-google-authenticator
运行
google-authenticator
手动根据提示选择
或者指定选项
google-authenticator -t -d -r 3 -R 30 -W -Q UTF8 -l root@openwrt -f -s /tmp/root@openwrt
说明
-l root@openwrt 用户名@设备名 这是显示在 freeotp 或 手机端 google-authenticator 的名称

在这里插入图片描述
1.手机端可以下载
Google Authenticator APK for Android
freeotp 推荐这个
https://freeotp.github.io/
将app打开扫描上面产生的二维码 点开该项 可以看到产生的一次性数字密码
2. 处理 /tmp/root@openwrt
在这里插入图片描述
将 /tmp/root@openwrt 的内容复制到 openwrt
192.168.2.1 为路由器地址
上面改了ssh端口为 2222
scp -o Port=2222 /tmp/root@openwrt root@192.168.2.1:~/.google_authenticator
并且设置访问权限
chmod 600 ~/.google_authenticator

openwrt18安装openwrt19的软件包

硬件 newifi3-d2 系统 OpenWrt 18.06.5
openwrt18没有google-authenticator的包, 19版本才有,但18版本的系统可以安装19版本的包

从软件源 手动下载两个包 google-authenticator-libpam 和 libqrencode

url 根据你的设备决定,这里是 newifi3-d2 使用 mipsel_24kc,如果不知道,可以查阅
/etc/opkg/distfeeds.conf
类似于
src/gz openwrt_base http://downloads.openwrt.org/releases/18.06.5/packages/mipsel_24kc/base
可以得知为mipsel_24kc平台的 因此对应到openwrt19也是一样的路径

ssh登录到openwrt

cd /tmp
wget https://mirrors.cloud.tencent.com/openwrt/releases/19.07.6/packages/mipsel_24kc/packages/google-authenticator-libpam_1.06-1_mipsel_24kc.ipk

wget
https://mirrors.cloud.tencent.com/openwrt/releases/19.07.6/packages/mipsel_24kc/packages/libqrencode_4.0.2-2_mipsel_24kc.ipk

安装本地包
opkg install /tmp/libqrencode_4.0.2-2_mipsel_24kc.ipk
opkg install /tmp/google-authenticator-libpam_1.06-1_mipsel_24kc.ipk

安装配置openssh

opkg update
opkg install openssh-server-pam
注意,需要带pam的openssh 因为google-authenticator需要pam模块认证
创建连接
ln -s /usr/lib/security/pam_google_authenticator.so /lib/security/pam_google_authenticator.so
配置 sshd
编辑 /etc/ssh/sshd_config
指定端口
Port 22
#启动口令认证
ChallengeResponseAuthentication yes
#启动pam模块
UsePAM yes
#允许root登录到ssh ,openwrt默认使用root登录系统的
PermitRootLogin yes

配置 pam
编辑 /etc/pam.d/sshd
可以在第一行插入 内容如下
auth requisite pam_google_authenticator.so
或者 放在最后一行
效果是 如果在第一行 则登录ssh式 首先验证totp口令再验证用户shadow口令
如果放在最后一行 则先验证用户shadow口令 再验证totp口令
重启openssh
/etc/init.d/sshd enable
/etc/init.d/sshd restart

同步ntp时间

google_authenticator 以utc时间作为计算源,确保客户端(如手机使用的freeotp)和服务器(ssh)的utc时间误差不超过30秒,否则不能得到正确的结果导致ssh无法登录
跟大多数路由器一样,新3路由器没有内置的电池保存时钟,每次重启后时间记录会丢失

1.如果在能连接网络的情况下 可以使用 时间同步 来设置路由器的时间
例如 编写一个开机同步ntp时间服务器的脚本
opkg install ntpdate

/etc/rc.local

PROCNAME=`which ntpdate`
 [ -n $PROCNAME  ] && ntpdate ntp1.aliyun.com>/dev/null 2>&1 &

2.使用 非对称算法 rsa dsa 的认证方式验证 作为保留的登录方式,例如在固定的电脑使用免密登录,而在其他场合使用口令+totp登录

最后

关闭dropbear服务 但不卸载
/etc/init.d/dropbear disable

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值