使用oath作totp一次性口令openssh认证

官方地址
https://www.nongnu.org/oath-toolkit/
实现: 使用 google-authenticator 产生totp token 然后使用 oath的pam模块认证

安装

debian系安装
sudo apt install oathtool libpam-oath libpam0g-dev
centos 系安装
需要epel
sudo yum install liboath liboath-devel liboath-doc pam_oath oathtool

pam模块 参数

http://www.nongnu.org/oath-toolkit/pam_oath.html

pam模块 参数
“debug”: Enable debug output to stdout.
“alwaysok”: Enable that all authentication attempts should succeed
(aka presentation mode). 任何认证尝试都会显示成功 演示模式
“try_first_pass”: Before prompting the user for their password, the
module first tries the previous stacked module´s
password in case that satisfies this module as
well.
“use_first_pass”: The argument use_first_pass forces the module to
use a previous stacked modules password and will
never prompt the user - if no password is
available or the password is not appropriate, the
user will be denied access.
“usersfile”: Specify filename where credentials are stored, for
example “/etc/users.oath”.
“digits”: Specify number of digits in the one-time password,
required when using passwords in usersfile. Supported
values are 6, 7, and 8.
“window”: Specify search depth, an integer typically from 5 to 50
but other values can be useful too.

编辑
sudo vim /etc/pam.d/sshd
最后一行添加 或者第一行 这样首先验证totp 再验证本地shadows

auth requisite pam_oath.so usersfile=/etc/oath/sshd_pam window=20 digits=6

注意 /etc/oath/sshd_pam 需要创建该文件

使用 google-authenticator 产生totp token

安装 libpam-google-authenticator

产生totp token 并保存到本机的 /tmp/"$USER"@totp, 例如在本地为远程用户和主机创建totp token

google-authenticator  -t -d -r 3 -R 30 -W -Q UTF8 -l  "$USER"@`hostname` -f -s /tmp/"$USER"@totp

其中 “$USER”@hostname 为 用户名@主机名 例如为 用户 aaa , 主机 TESTHOST 创建 则为

google-authenticator -t -d -r 3 -R 30 -W -Q UTF8 -l aaa@TESTHOST -f -s /tmp/aaa@totp

使用oath作totp一次性口令openssh认证_第1张图片
得到token为 ZONXBFEMYE4Y4CALFXLFIKNQTE

 oathtool -v --totp --base32 ZONXBFEMYE4Y4CALFXLFIKNQTE

得到 oathtool 识别的hex格式
cb9b70948cc1398e080b2dd65429b099

oath文件格式

https://github.com/archiecobbs/mod-authn-otp/wiki/UsersFile

1 Token Type 详见下面
2 Username 用户名
3 pin 一个pin码 - 不使用 + 由外部的OTPAuthPINAuthProvider校检
4 Token Key 令牌值 RFC 4226 创建令牌的hash算法 详见 RFC2104
HMAC-SHA-1
oathtool 不使用hmac possible values=“sha1”, “sha256”, “sha512” default=’sha1’)
sha1 40字节 但是freeotp使用32字节

5 Counter/Offset 下一个预期计数器值(事件令牌)或计数器偏移量(时间令牌)
6 Failure counter 此用户提供的连续错误OTP计数
7 Last OTP 上一个成功使用的 一次性密码
8 Time of Last OTP 最后一次产生OTP的时间戳 格式 2009-06-12T17:52:32L
9 Last IP address 最近成功尝试的ip地址

第5-9项 是可选的 新用户可忽略第6-9项

Token Type 格式 ALGORITHM [ / COUNTERINFO [ / DIGITS ] ]

算法 要么是 HOTP" (RFC 4226) 要么是 “MOTP” (http://motp.sourceforge.net/).

COUNTERINFO E 基于事件的令牌 TNN基于时间的令牌 NN是间隔的秒数

HTOP默认是E MOTP默认是 T10

Token Type
HOTP - HOTP event-based token with six digit OTP
HOTP/E - HOTP event-based token with six digit OTP
HOTP/E/8 - HOTP event-based token with eight digit OTP
HOTP/T30 - HOTP time-based token with 30 second interval and six digit OTP
HOTP/T60 - HOTP time-based token with 60 second interval and six digit OTP
HOTP/T60/5 - HOTP time-based token with 60 second interval and five digit OTP
MOTP - Mobile-OTP time-based token 10 second interval and six digit OTP
MOTP/E - Mobile-OTP event-based token with six digit OTP

按照google-authenticator 的算法 每30秒产生出新的6个10进制数字 对应到oath就是 HOTP/T30

按照上述 创建文件 /etc/oath/sshd_pam

sudo mkdir /etc/oath
sudo vim /etc/oath/sshd_pam 
sudo chmod 600 /etc/oath/sshd_pam 

/etc/oath/sshd_pam 的内容如下

HOTP/T30 aaa - cb9b70948cc1398e080b2dd65429b099 

说明
第一列 算法HOTP/T30
第二列用户名 在/etc/passwd存在的或ldap账户
第三列 使用 - 表示不使用
第四列 由oathtool -v --totp --base32 ZONXBFEMYE4Y4CALFXLFIKNQTE 产生的hex
其余由oath自己管理

配置ssh

sshd配置
PasswordAuthentication yes
ChallengeResponseAuthentication yes
UsePAM yes

同步ntp

需要服务器和登录端的utc时间一致
重启sshd 再次ssh进入就可以看到 oath 认证了
在这里插入图片描述

你可能感兴趣的:(linux,linux,totp,ssh)