使用经典ASP的2FA身份验证总是返回无效的响应



我使用的代码来自GitHub

https://github.com/as08/ClassicASP.TwoFactorAuthentication

我下载了演示网站,在服务器上安装了我需要的东西,一切都运行得很好。演示网站有很多代码,所以我把它分解成Github页面上显示的两个组件1)生成密钥和QR码2)验证验证码。

我做了2个非常基本的asp页面

index.asp

<%
Dim TwoFA : Set TwoFA = Server.CreateObject("ClassicASP.TwoFactorAuthentication")

TwoFA.SecretKeyLength(20)
TwoFA.HashMode("SHA1")
TwoFA.totpSize(6)
RecoveryPasswordLength = 18

Dim SecretKey, RecoveryPassword, GenerateQR

SecretKey = TwoFA.GenerateSecretKey()
RecoveryPassword = TwoFA.RecoveryPassword(RecoveryPasswordLength)

response.write("Secret Key: " & secretKey & "<br>")
response.write("Recovery Password: " & RecoveryPassword & "<br />")

' Generate the QR code
GenerateQR = "<img src=""https://chart.googleapis.com/chart" &_
"?chs=320x320" &_
"&chld=H|0" &_
"&cht=qr" &_
"&chl=" & Server.URLencode("otpauth://totp/test@test.com" &_ 
"?secret=" & SecretKey &_ 
"&issuer=examplesite.com" &_ 
"&algorithm=SHA1" &_ 
"&digits=6" &_ 
"&period=30") & "&choe=UTF-8"" " &_
"class=""img-fluid border-info border mt-4 QRframe"" " &_  
"width=""320px"" height=""320px"">"
Set TwoFA = Nothing
%>
<%=GenerateQR%>

Validate.asp

<%
Dim TwoFA : Set TwoFA = Server.CreateObject("ClassicASP.TwoFactorAuthentication")
TwoFA.SecretKeyLength(20)
TwoFA.HashMode("SHA1")
TwoFA.totpSize(6)
TOTP = request("totp")

response.write(totp & "<br />")
If TwoFA.Verify("EDSLKFQENTEFPATYN5LAZ5BCGD2UOR4R",cStr(TOTP)) Then
' Valid Time-based One-time Password (TOTP)
response.write("valid")
Else
' Invalid TOTP
response.write("invalid")
End If
Set TwoFA = Nothing
%>

为了测试,我去了我的index.asp页面,生成了一个QRcode,并在Microsoft Authenticator中设置了它。然后,我将密钥硬编码到验证页面的验证调用中。然后我进入Authenticator,获得代码并通过validate.asp?totp=123456进行测试。不管我怎么做,我都做不到。我总是得到response.write("invalid")的结果。

为什么这不是返回一个有效的响应,即使我在使用正确的秘密密钥输入正确的6位数代码?

我明白了。我希望这能对将来的人有所帮助。不幸的是,在文档中,它在cStr(TOTP)中有TOTP,从而将其转换为字符串…但我删除了cStr(),并在请求中,我强制它是一个整数做:

TOTP = request("totp")+0

现在它工作了!

这不是一个真正的答案,但它太长太详细了,不能作为评论留下。

我已经从我的GitHub存储库下载并运行演示,并使用微软认证器,谷歌认证器和授权。它们都很好用,包括30秒的公差。我甚至将我的时区更改为各种不同的时区(尽管这不能解释你描述的4秒到期,他们没有。我只是把所有的基础都覆盖了)。

所有我能想到的建议是,下载并运行这个UTC.asp页面:

<%=UTC_DateTime()%>
<script language="JScript" runat="server">

// Return the current UTC date and time regardless of what timezone the server is set to

function UTC_DateTime() {

var date = new Date();

// date.getUTCMonth() returns a value from 0 - 11 (?) so we need to  + 1

var result = date.getUTCFullYear() + "-" + (date.getUTCMonth() + 1) + "-" + date.getUTCDate() + " " + date.getUTCHours() + ":" + date.getUTCMinutes() + ":" + date.getUTCSeconds();

// Pad month/day/hour/minute/second values with a 0 if necessary

return result.replace(/(D)(d)(?!d)/g, "$10$2");

}
</script>

然后将其与timeanddate.com等网站进行比较,如果时间不相同(给予或采取几秒钟),那么您的服务器时间设置一定有问题,它必须生成一个不同步的UTC时间。某种NTP故障?

如果是这样的话,我没有答案,希望有人知道,但这必须是问题,因为它对我来说工作得很好。(我在iOS上运行所有应用程序,在Windows 10和Windows Server 2016上运行演示,只是为了澄清)

最新更新