SecureCRT自动输入二次认证码
为了保证安全,公司的Jump Server堡垒机默认开启了MFA多因子认证。众所周知,安全和便捷总是对立的。导致每次登录服务器时都需要打开手机或者浏览器的Bitwarden。
那么有没有便捷的方式不用每次输入二次认证码呢?当然是有的。
登录脚本
打开SecureCRT\Scripts文件夹,参考: SecureCRT logon script for google authenticator 新建 totp.py 文件,内容如下:
# $language = "python"
# $interface = "1.0"
import hmac, base64, struct, hashlib, time, json, os
"""YOUR_GOOGLE_AUTH_KEY 修改为自己的验证器密钥"""
TOTP_KEY = 'YOUR_GOOGLE_AUTH_KEY'
def get_hotp_token(secret, intervals_no):
"""This is where the magic happens."""
key = base64.b32decode(normalize(secret), True) # True is to fold lower into uppercase
msg = struct.pack(">Q", intervals_no)
h = hmac.new(key, msg, hashlib.sha1).digest()
o = ord(h[19]) & 15
h = str((struct.unpack(">I", h[o:o+4])[0] & 0x7fffffff) % 1000000)
return prefix0(h)
def get_totp_token(secret):
"""The TOTP token is just a HOTP token seeded with every 30 seconds."""
return get_hotp_token(secret, intervals_no=int(time.time())//30)
def normalize(key):
"""Normalizes secret by removing spaces and padding with = to a multiple of 8"""
k2 = key.strip().replace(' ','')
# k2 = k2.upper() # skipped b/c b32decode has a foldcase argument
if len(k2)%8 != 0:
k2 += '='*(8-len(k2)%8)
return k2
def prefix0(h):
"""Prefixes code with leading zeros if missing."""
if len(h) < 6:
h = '0'*(6-len(h)) + h
return h
def main():
tab = crt.GetScriptTab()
if tab.Session.Connected != True:
crt.Dialog.MessageBox("Session Not Connected")
return
tab.Screen.Synchronous = True
tab.Screen.WaitForStrings(['OTP Code'])
vc = get_totp_token(TOTP_KEY)
tab.Screen.Send("{vc}\r".format(vc=vc))
return
main()
SecureCRT设置
参考: SecureCRT两步验证自动登录脚本 ,右键打开连接的属性
- 连接 -> 登录动作 -> 勾上 “登录脚本”,并选择刚才新建的totp.py
- 连接 -> 登录动作 -> 勾上 “Display logon prompts in terminal window”

验证
完成以上设置后即可进行登录验证。登录时会一闪而过
Please Enter MFA Code.[OTP Code]:
随后即可看到熟悉的登录页面了
***, JumpServer 开源堡垒机
1) 输入 部分IP,主机名,备注 进行搜索登录(如果唯一).
2) 输入 / + IP,主机名,备注 进行搜索,如:/192.168.
3) 输入 p 进行显示您有权限的资产.
4) 输入 g 进行显示您有权限的节点.
5) 输入 h 进行显示您有权限的主机.
6) 输入 d 进行显示您有权限的数据库.
7) 输入 k 进行显示您有权限的Kubernetes.
8) 输入 r 进行刷新最新的机器和节点信息.
9) 输入 s 进行中文-English-日本語语言切换.
10) 输入 ? 进行显示帮助.
11) 输入 q 进行退出.
Opt>
后续
因为找到了一款命令行工具来生成二次认证码,详见: arcanericky/totp
因此对脚本也做了对应的修改,使用totp命令获取二次认证码,并且登录后直接发送p以显示服务器列表:
# $language = "python"
# $interface = "1.0"
import subprocess
"""YOUR_GOOGLE_AUTH_KEY 修改为自己的验证器密钥"""
TOTP_KEY = 'YOUR_GOOGLE_AUTH_KEY'
def totp(key):
totp = subprocess.check_output(["totp.exe", "-s", TOTP_KEY]).rstrip()
return totp
def main():
tab = crt.GetScriptTab()
if tab.Session.Connected != True:
crt.Dialog.MessageBox("Session Not Connected")
return
tab.Screen.Synchronous = True
tab.Screen.WaitForStrings(['OTP Code'])
vc = totp(TOTP_KEY)
tab.Screen.Send("{vc}\r".format(vc=vc))
tab.Screen.WaitForStrings(['Opt>'])
tab.Screen.Send("p\r")
return
main()