Last active
April 14, 2021 06:09
-
-
Save darrenjrobinson/fd30339cd0baa47dddff3f391b390cb4 to your computer and use it in GitHub Desktop.
v1 PowerShell Script to validate YubiKey using Single Factor. Associated Blogpost https://blog.darrenjrobinson.com/validating-a-yubico-yubikeys-one-time-password-otp-using-single-factor-authentication-and-powershell/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Client ID obtained after registering here https://upgrade.yubico.com/getapikey/ | |
$clientID = '12345' | |
# Read in the key | |
$getKey = Read-Host 'Insert your YubiKey and touch it until the OTP is received (~2 seconds)' | |
$credentialID = $getKey.Substring(0,12) | |
# Generate a Nonce | |
$nonce = -join ((65..90) + (97..122) | Get-Random -Count 32 | % {[char]$_}) | |
$nonce = $nonce.ToLower() | |
# Build the URI | |
$uri = "http://api.yubico.com/wsapi/2.0/verify?id=$($clientID)&nonce=$($nonce)&otp=$($getKey)" | |
# Invoke Web Request | |
$response = Invoke-WebRequest -Method GET -Uri $uri | |
# Split the response into an array | |
$lines = $response.ParsedHtml.body.innerText -split " " | |
# Check to the response and output | |
if ($response.ParsedHtml.body.innerText.Contains("status=OK")){ | |
# SUCCESSFUL | |
write-host "***********SUCCESS****************" | |
write-host "Credential ID $($credentialID)" | |
$i=0 | |
foreach ($line in $lines){ | |
$linenumber = $i | |
switch ($linenumber) { | |
0 {write-host "Signature $($line)"} | |
1 {write-host "Time $($line)"} | |
2 {write-host "$($line)"} | |
3 {write-host "$($line)"} | |
4 {write-host "% $($line)"} | |
5 {write-host "$($line)"} | |
} | |
$i++ | |
} | |
} else { | |
$i=0 | |
# ERROR | |
write-host "============ERROR===============" | |
write-host "Credential ID $($credentialID)" | |
foreach ($line in $lines){ | |
$linenumber = $i | |
switch ($linenumber) { | |
4 {write-host "$($line)"} | |
5 {write-host "$($line)"} | |
} | |
$i++ | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment