LINUX: OTP und SSH mit oath-toolkit und FreeOTP

folgende Pakete sind Sinnvoll

apt-get install libpam-oath oathtool qrencode ntpdate

das oathtool zum generieren von otps auf der Konsole
lib-pam um die aktuelle pam zu ersetzen
für eine neuere Version sollte man es neu compilieren von
http://www.nongnu.org/oath-toolkit/

 

qrencode um ein QR-Code für FreeOTP zu erstellen
ntpdate um immer die aktuelle zeit zu haben, bei Verwendung von TOTPs

die Datei /etc/users.oath anlegen und editieren:

HOTP/T30/6 root - 2e4e8564c0d13e0fea6b7729c9146d7e27ab473d

T30 definiert ein TOTP von 30sec und 6 die Länge
root ist der benutzer und nach dem – kommt das Secret in HEX kann wie folgt generiert werden:

head -c 1024 /dev/urandom | openssl sha1

QR-Code erstellen:

wobei das Secret hier Base32 kodiert ist:

qrencode -t ASCIIi otpauth://totp/Bezeichnung:root@host?secret=FZHIKZGA2E7A72TLO4U4SFDNPYT2WRZ5

Das URI Format nach: KeyUriFormat

 

Die Pam muss noch angepasst werden. Für ssh in der /etc/pam.d/sshd muss folgendes stehen:

# Standard Un*x authentication.

#OATH OTP
auth    required     pam_oath.so usersfile=/etc/users.oath

# default
@include common-auth

Falls was nicht klappt: debuggen mit:

auth    requisite pam_oath.so debug usersfile=/etc/users.oath

 

sshd restart mit

service ssh restart

 

 

Hier die Passwort Erstellung mal in einem Bashscript, die letzte Zeile der Ausgabe muss noch einmal manuell ausgeführt werden.

#!/bin/bash
read -p "User eingeben (ohne Leerzeichen): " USR
read -p "Bezeichnung eingeben (ohne Leerzeichen): " BEZ
read -p "Host eingeben (ohne Leerzeichen): " HST
read -p "Event(e) oder Zeitbasiertes(z) OTP?" KND

echo Random Secret wird generiert
SEC=$(head -c 1024 /dev/urandom | openssl sha1 | awk '{print $2}')
echo Secret: $SEC
SEC_BASE32=$(oathtool -v $SEC | awk 'NR == 2 {print $3}')
echo Secret Base32: $SEC_BASE32


if [[ $KND == "z" ]]
then
  echo HOTP/T30/6 $USR - $SEC
  QR=$(echo otpauth://totp/$BEZ:$USR@$HST?secret=$SEC_BASE32)
  qrencode -t ASCIIi $QR
  echo 
  echo "echo \"HOTP/T30/6 $USR - $SEC\" >> /etc/users.oath "
fi

if [[ $KND == "e" ]]
then
  echo HOTP $USR - $SEC
  QR=$(echo otpauth://hotp/$BEZ:$USR@$HST?secret=$SEC_BASE32)
  qrencode -t ASCIIi $QR
  echo 
  echo "echo \"HOTP $USR - $SEC\" >> /etc/users.oath "
fi

 

Testausgabe:

root@host:/opt/otp# ./add_user.sh 
User eingeben (ohne Leerzeichen): root
Bezeichnung eingeben (ohne Leerzeichen): test
Host eingeben (ohne Leerzeichen): test
Event(e) oder Zeitbasiertes(z) OTP?z
Random Secret wird generiert
Secret: d9996f181b87604662a18db97c44c0f1f6432a5d
Secret Base32: 3GMW6GA3Q5QEMYVBRW4XYRGA6H3EGKS5
HOTP/T30/6 root - d9996f181b87604662a18db97c44c0f1f6432a5d
##################################################################################
##################################################################################
##################################################################################
##################################################################################
########              ######  ##    ####                ####              ########
########  ##########  ##  ##  ##############  ##  ##########  ##########  ########
########  ##      ##  ##########  ####    ##    ##    ######  ##      ##  ########
########  ##      ##  ##      ##  ############  ####  ##  ##  ##      ##  ########
########  ##      ##  ####  ##########  ########        ####  ##      ##  ########
########  ##########  ##    ##    ##      ##    ##    ##  ##  ##########  ########
########              ##  ##  ##  ##  ##  ##  ##  ##  ##  ##              ########
##########################      ####          ##    ##############################
########          ##        ##  ######  ####  ##  ########  ##  ##  ##  ##########
############    ##  ########  ##    ##  ##      ####    ##    ####        ########
########  ##########  ##  ##  ####      ##  ##  ####  ##    ####        ##########
########  ##  ##  ####    ######    ##  ##  ##        ##  ######        ##########
########  ####        ##      ##  ##          ######  ##    ##      ##############
##########      ######      ##################        ######    ##    ##  ########
########    ####            ##    ####  ######  ########    ##  ####    ##########
########        ########  ##    ####      ####  ##    ####  ##  ##    ############
##########  ##        ######    ######  ####      ##  ####  ####  ################
########  ##    ######        ##      ##    ##      ##  ##    ########    ########
##########                    ####    ######    ##    ########  ####    ##########
################  ######      ##    ##      ######      ##    ######  ############
########      ##      ##  ##  ##  ##  ##  ##  ######        ####    ##  ##########
########  ##  ########  ####################    ##  ##  ##    ########    ########
########  ########    ##  ##      ####      ####  ########  ####  ####  ##########
########  ####  ######    ##    ####  ######    ##      ########  ##      ########
########  ####  ####            ######    ######  ##              ####    ########
########################  ######      ##  ####  ##    ##  ######    ####  ########
########              ##      ####    ##  ##  ##  ####    ##  ##    ##  ##########
########  ##########  ####    ##    ##    ##  ##    ##    ######  ##  ##  ########
########  ##      ##  ##    ####  ##  ##  ####    ##  ##          ####  ##########
########  ##      ##  ##      ################  ##  ##########      ####  ########
########  ##      ##  ##  ####    ################    ##      ####  ##############
########  ##########  ##  ##    ####        ####        ####    ##    ############
########              ##    ##  ######  ##    ##  ##      ##  ####  ##  ##########
##################################################################################
##################################################################################
##################################################################################
##################################################################################

echo "HOTP/T30/6 root - d9996f181b87604662a18db97c44c0f1f6432a5d" >> /etc/users.hotp

 

Diesen QR mit dem FreeOTP App scannen und voilà 😉