Google OTP를 이용하여 CentOS 7 SSH접속하기
Google OTP(Google Authentication)를 이용하여
2FA(Two-Factor-Authentication)인증을 구성한뒤 SSH프로토콜로 접속테스트.
기존 패스워드 방식의 인증보다 보안이 강화된 2FA 인증을 추가하여
좀더 안전하게 계정정보를 보호할 수 있다.
1. CentOS 7 에서 Google Authenticator 설치
Linux(CentOS 7)에서 'Google Authenticator'를 Install한다.
[방법 1] : 패키지 설치 (권장)
▼ 'google-authenticator' 패키지를 yum으로 설치한다.
# yum install google-authenticator
▼ Error가 발생할경우, 아래와 같이 'epel-release' 를 설치한 후 재시도 한다.
# yum install epel-release
[방법 2] : 수동 설치
사전설치가 필요한 PKG파일들
- pam-devel
- gcc
- autoconf
- automake
- libtool
- git
▼ 'google-authenticator' 설치에 필요한 의존성 패키지 설치
# yum install google-authenticator
▼ 'git clone' 명령어로 'google-authenticator-libpam' 파일을 다운로드 한다.
# yum install google-authenticator
▼ 다운로드한 폴더로 이동하여 패키지를 컴파일한 후에 설치.
# yum install google-authenticator
2. SSH 인증을 위한 PAM 모듈 설정
패키지 설치 완료 후 PAM(Pluggable Authentication Modules) 모듈을 사용하기 위한
Google Authenticator 라이브러리를 추가한다
▼ PAM설정 파일수정(/etc/pam.d/sshd)
# vi /etc/pam.d/sshd
▼ 다음 한 줄을 하단에 추가. 다음 한 줄을 하단에 추가.
auth required pam_google_authenticator.so nullok
auth required pam_google_authenticator.so nullok
*nullok : 리눅스 내의 모든 사용자에게 각각의 'secet key'를 생성하지만,
동일한 secret key를 다 같이 사용하고자 할 때에는 삭제한다.
▼ 아래 화면 참조,
▼ SSH 서버의 설정 파일 수정(/etc/ssh/sshd_config)
# vi /etc/ssh/sshd_config
▼ 다음항목을 수정하거나 주석을 해제한다.
PasswordAuthentication no
ChallengeResponseAuthentication yes
UsePAM yes
PermitEmptyPasswords no
PasswordAuthentication no
ChallengeResponseAuthentication yes
UsePAM yes
PermitEmptyPasswords no
PasswordAuthentication : 패스워드 인증을 사용할 것인가 (OTP 인증 시에는 패스워드 인증을 사용하지 않음
ChallengeResponseAuthentication : 시도-응답 인증 방식을 사용할 것인가 (2FA인증을 위해서도 필요)
UsePAM : 인증에 PAM 모듈을 사용할 것인가
PermitEmptyPasswords : 비어있는 사용자 암호를 허용할 것인가
▼ SSH 서버스를 재시작.
# systemctl restart sshd.service
3. Google Authenticator 인증 파일 생성
OTP 사용자 인증을 위해서는 Google Authenticator 인증 파일을 생성해야 한다
(각 사용자 홈디렉토리의 .google_authenticator 파일생성 필요)
이 파일을 생성한후 발생되는 시크릿 키(Secret Key)는
개인 키와 비슷한 역할을 하므로 인증할 대상자를 제외하고는 절대 공유하지 않는다.
3-1. google authenticator 인증 파일 생성 (1)
▼ google Authenticator 명령어를 실행한다.
# google-authenticator
3-1. google authenticator 인증 파일 생성 (1)
▼ google Authenticator 명령어를 실행한다.
# google-authenticator
▼ 다음 massage는 시간 기반(Time-based) 인증을 사용할 것인지에 대한 여부로 y 를 입력한다.
# Do you want authentication tokens to be time-based (y/n) y
▼ Secret key와 백업코드가 출력된다.
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.localdomain%
3Fsecret%3DNRTFWC5D4SVCM4GD5GVJXXNLW4%26issuer%3Dlocalhost.localdomain
[QR코드가 출력되는 위치]
Your new secret key is: NRT#####################
Your verification code is 4#5###
Your emergency scratch codes are:
67##01##
##72##3#
#96#1##2
56#0##6#
9#9##2##
▼ 시크릿 키를 간단하게 추가할 수 있는 QR코드가 생성된다.
3-2. Google Authenticator 인증 파일 생성 (과정 2)
인증 파일 생성과정을 진행하다 보면 출력되는 massage에 대한 설명이다.
▼ 사용자 디렉토리 확인 항목으로 해당 파일을 발급받으려는 사용자의 홈디렉토리인지 확인하다.
# Do you want me to update your "/root/.google_authenticator" file? (y/n) y
▼ 중간자공격(man-in-middle-attack)에 대한 예방 옵션
# 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
30초마다 생성되는 한 코튼에 대해 두 번 이상 인증하게한다.
n 을 입력하면 이미 인증된 후 다음 토큰이 생성될 때 까지 인증이 거부된다.
▼ 시간상 오차 및 왜곡을 방지하는 옵션.
# 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
기존에 허용되는 OTP 토큰은 현재 시간에 생성된 토큰의 이전과 다음 각각 1개씩 허용된다.
만료된 토큰 123456, 현재토큰 789123, 다음에 발생 될 토큰이 456789 이라 가정하면
위 세가지 토큰이 모두 입력가능하다.
이러한 범위를 17개(이전8개, 다음8개)까지 허용할 것인지에 대한 옵셥니다.
시간동기화의 실패로 인한 시간차이로 인증실패에 걱정이 된다면 y를 입력한다.
▼ 무차별 대입 공격(brute-force-attack)의 방지하는 옵션.
# 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
30초 마다 생성되는 토큰에 3회 이상 인증에 실패하는 경우 일시적으로 로그인을 차단한다.(y 를 입력.)
▼ '.google_authenticator' 파일이 생성완료되면 vi편집기로 해당 파일을 확인한다.
# vi .google_authenticator
▼ 파일에는 다음과 같이 secret key와 Backup-code, Configure 내용들이 포함된다.
NR##WC5#####M4GD####XX###4
" RATE_LIMIT 3 30 1552556014
" WINDOW_SIZE 17
" DISALLOW_REUSE 51751867
" TOTP_AUTH
6#######
#4######
19######
######62
9#######
NR##WC5#####M4GD####XX###4
" RATE_LIMIT 3 30 1552556014
" WINDOW_SIZE 17
" DISALLOW_REUSE 51751867
" TOTP_AUTH
6#######
#4######
19######
######62
9#######
4. Google OTP 클라이언트 설치.
▼ 모바일에서 OTP 클라이언트를 설치한다.(Google OTP 혹은 Google authenticator 로 검색한다.)
안드로이드 다운로드 : https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2
애플 앱스토어 다운로드 : https://itunes.apple.com/kr/app/google-authenticator/id388497605?mt=8
▼ 설치가 완료되었다면 실행하여 Secret Key를 등록한다.
Google OTP 화면이 보이면 하단 우측에 있는 추가(+) 버튼을 터치한다.
▼ QR코드스캔 & 수동입력
QR코드를 스캔하고자 하면 ‘바코드 스캔’을 선택하여 추가하면 즉시 OTP코드가 추가되어 바로 사용이 가능하다.
QR코드가 제대로 인식되지 않거나 Shell에서 QR코드가 정상적으로 출력되지 않았을 경우 하단의 ‘제공된 키 입력’메뉴로 추가한다.
▼ 제공된 키로 수동입력을 하는방법
‘제공된 키 입력’을 선택했다면 다음과 같이 시크릿 키를 수동으로 입력해야한다.
계정 이름은 임의작성해도 무방하다.
▼ 이제 아래와 같이 OTP 코드가 나타난다면 등록이 완료된다.
토큰은 일정시간이 지나면 변경된다.
5. OTP 정보를 사용하여 SSH 접속하기
OTP 인증 파일을 생성하였고 사용 할 준비가되면 SSH 서버에 접속하여 테스트해 본다.(Xshell기준으로 작성)
▼세션 파일의 이름과 접속할 서버의 호스트 주소를 입력한 후 확인 또는 연결을 클릭.
▼ 세션에 연결한 후에는 사용자 계정 및 암호를 입력한다.
인증 방법은 ‘Keyboard Interactive’로 선택한 후 (또는 자동으로 선택된다.)
하단의 프롬프트에 나타난 내용대로 암호를 입력한다.
먼저 Password 프롬프트에는 기존 사용자의 일반 암호를 입력한다.
▼ 다음 Verification code 에는 OTP 인증 토큰을 입력한다.
OTP 클라이언트(Google OTP 앱)를 실행한 후
방금 전에 생성했던 키의 토큰 값 6자리를 해당 필드에 입력한다.
▼ 토큰이 정상 확인되면 SSH 서버에 정상적으로 접속이 되는지 확인한다.