Google Authenticator를 이용하여 SSH 접속하는 방법에 대해 알아보겠습니다.


많은 서비스들이 이중인증방식(Two-Factor Authentication)으로 OTP를 사용하고 있습니다.

OTP 방식 중에는 시간동기화 방식(TOTP), 이벤트 방식(HOTP), 질의/응답방식이 있으며 그 중에 시간 동기화 방식과 이벤트 방식의 차이점은 아래 표를 참고 하시면 됩니다.


구분 

 내용

 시간동기화 방식

 Time-based One-Time Password (TOTP)

 - 일정시간에 따라 지속적으로 암호값이 변함 (기본 30초)

 - 시간동기화 방식 알고리즘으로 HMAC-SHA1은 HOTP와 같은 방법으로 암호 생성

 - 비밀키와 현재 유닉스 시간을 결합하여 암호 생성 (HOTP(K, TC))

 이벤트 방식

 HMAC-based One-Time Password (HOTP)
 (Hash Message Authentication Code)

 - 이벤트가 발생 시 암호값 생성

 - 이벤트 기반 알고리즘으로 HMAC-SHA1를 이용하여 암호 생성

 - 비밀키와 카운터 값을 결합하여 암호 생성 (HOTP(K, C))


TOTP방식은 암호가 주기적으로 변경되고 짧은 시간동안만 유효하기 때문에 HOTP보다 안전한 방식으로 알려져 있습니다.


Google Authenticator는 위 두가지 방식을 모두 지원하고 있으며, 사용자 서비스에 맞게 원하는 방식으로 구현이 가능합니다.

더 자세한 내용을 원하시면 아래 위키를 참조하시면 됩니다.

https://en.wikipedia.org/wiki/Google_Authenticator


그럼 Google Authenticator 설치를 진행 해 보겠습니다.



** Google Authenticator 설치 과정 **


1. 패키지 설치 (설치확인 후 설치 되지 않은 패키지만 설치)

 # yum -y install git pam-devel autoconf automake libtool gcc 


git : 형상관리 도구로 github에 있는 google authenticator 소스를 복사하기 위한 툴

pam : Google 인증을 사용하기 위한 인증 모듈

Autoconf/automake/libtool : 소스코드 빌드를 위한 툴

gcc : 컴파일러



2. Google Authenticator PAM module 다운로드

 # git clone https://github.com/google/google-authenticator-libpam



3. Build & Install

 # cd google-authenticator-libpam/

 # ./bootstrap.sh

 # ./configure --libdir=/lib64  <-- 64비트일 경우 추가

 # make && make install


※ 리눅스 Bit 확인

# getconf LONG_BIT


위 과정을 완료하시면 /usr/lib64/security 디렉토리 안에 pam_google_authenticator.so 모듈이 설치됩니다.



4. PAM Config 설정

PAM 설정에서 auth required pam_google_authenticator.so nullok 부분을 추가하시면 됩니다.

authenticator.so 뒷 부분은 옵션을 설정하는 부분으로 여러가지 옵션을 사용할 수 있으며,

nullok 옵션은 OTP를 설정하지 않은 User는 OTP인증 없이 로그인 할 수 있도록 하는 옵션입니다.

즉, 모든 User에게 OTP를 적용하려면 nullok 옵션을 빼시면 됩니다.

** OTP 해제하는 방법은 기존 설정은 그대로 두고 해당 부분만 주석 처리한 후 SSH를 재시작 해주시면 기존대로 OTP없이 접속 가능합니다.

 # vi /etc/pam.d/sshd



5. SSH Config 설정

sshd_config 에서 아래 부분에 대하여 사용하도록 설정을 변경합니다.

ChallengeResponseAutientication을 제외하곤 기본값으로 yes로 되어 있을 겁니다.

설정이 완료되면 저장 후 ssh 서비스를 재시작 합니다.

 # vi /etc/ssh/sshd_config

 PasswordAuthentication yes
 ChallengeResponseAuthentication yes
 UsePAM yes

 # service sshd restart



6. 계정 TOTP 설정

아래 파란색 질문에 대하여 Y로 진행하시면 되고, 붉은색의 URL과 Secret Key는 비밀키를 전달하는 데 사용 됩니다.

URL의 경우 QR코드를 통하여 자동 등록하는 방법이며, Secret Key는 직접 수동으로 입력하는 방법이라 둘 중 한 가지 방식으로 선택하여 진행하시면 됩니다. 아래에서는 두 가지 방법에 대하여 설명 드리겠습니다.

 # google-authenticator 

Do you want authentication tokens to be time-based (y/n) y
Warning: pasting the following URL into your browser exposes the OTP secret to Google:
https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/root@localhost%3Fsecret%3D5VNTBITJHEH2GGSVOLTFZMDAKA%26issuer%3Dlocalhost
Failed to use libqrencode to show QR code visually for scanning.
Consider typing the OTP secret into your app manually.
Your new secret key is: 5VNTBITJHEH2GGSVOLTFZMDAKA
Your verification code is 024646
Your emergency scratch codes are:
  60700042
  28183432
  27147472
  84160543
  62329722

Do you want me to update your "/root/.google_authenticator" file? (y/n) y

Do you want to disallow multiple uses of the same authentication
token? This restricts you to one login about every 30s, but it increases
your chances to notice or even prevent man-in-the-middle attacks (y/n) y

By default, a new token is generated every 30 seconds by the mobile app.
In order to compensate for possible time-skew between the client and the server,
we allow an extra token before and after the current time. This allows for a
time skew of up to 30 seconds between authentication server and client. If you
experience problems with poor time synchronization, you can increase the window
from its default size of 3 permitted codes (one previous code, the current
code, the next code) to 17 permitted codes (the 8 previous codes, the current
code, and the 8 next codes). This will permit for a time skew of up to 4 minutes
between client and server.
Do you want to do so? (y/n) y

If the computer that you are logging into isn't hardened against brute-force
login attempts, you can enable rate-limiting for the authentication module.
By default, this limits attackers to no more than 3 login attempts every 30s.
Do you want to enable rate-limiting? (y/n) y


여기까지 오셨으면, 서버 작업은 완료되었습니다.

실제 OTP를 사용하기 위해서는 사용자 휴대폰에 Google OTP 앱을 설치하여 진행하여야 합니다.



7. Google OTP 설치 및 등록

Google OTP 추가 방법에는 위에서 말씀 드린 것 처럼 바코드 스캔과 키 입력을 통한 방법이 있으며,

먼저 바코드 스캔을 통한 OTP 등록을 먼저 진행 해 보겠습니다.

아래 순서대로 따라 오시면 됩니다.


①                                                                                                  ②


③                                                                                                 ④





 ** 키 입력을 통한 수동 등록

①                                                                                                  ②




여기까지 Google Authenticator의 설정이 끝났으며, 이제 SSH 접속 시 OTP를 이용하여 접속이 가능합니다.

아래는 PuTTY와 SecureCRT를 이용한 접속 화면이며, SecureCRT의 경우 OTP를 이용하기 위하여 옵션 수정이 필요합니다.


** PuTTY


** SecureCRT





** SecureCRT 옵션 설정 방법

1. Session Options > SSH2 > Keyboard Interactive 순서 변경


2. Keyboard Interactive > Properties > Store password 체크 해제