[Server] 미니 홈서버 구축하기 7편 - Google OTP를 이용하여 SSH 보안 강화하기

2021. 1. 14. 11:19Server

최근 들어 많은 서비스들이 보안 강화를 위하여 2FA (Two-factor Authentication)을 사용합니다. 특정 시간마다 특정 규칙에 따라 암호 값이 바뀌는 OTP (One-time Password) 방식의 2FA가 많이 사용되는데, 사용자의 보안을 더욱 튼튼하게 지켜주는 역할을 합니다. 또한 이러한 2FA는 핸드폰에 인터넷이 연결되어 있지 않아도 사용이 가능합니다.

오늘은 Google의 Authenticator를 이용하여 SSH 접속 시 비밀번호뿐만 아니라 OTP 또한 요구하도록 설정해 봅시다.

Google Authenticator 설치

해당 기능을 활성화하기 위해서는 Google Authenticator를 설치해야 합니다.

sudo apt-get install libpam-google-authenticator

위의 명령어를 이용하여 pam google authenticator 라이브러리를 설치합니다.

 

Google OTP 설정

**진행하기전, 만약 root 계정에 로그인되어있다면 exit을 입력해 root를 나가주고 사용할 SSH 계정으로 돌아옵니다.**

설치 후 간편 설정을 시작하는 방법은 다음과 같습니다.

google-authenticator

위의 명령어를 입력하면, 설치 도우미가 실행됩니다.

 

Do you want authentication tokens to be time-based (y/n)

곧이어 위와 같이 time-based인 OTP를 사용할 것인지 물어보는데, y를 입력합니다.

 

그럼 위와 같이 QR코드와 복구 코드가 나타나게 됩니다.

해당 정보를 안전한 곳에 기록해둡니다.

 

모바일 앱 등록하기

해당 OTP를 사용하기 위해서는 코드를 생성해주는 앱이 필요합니다.

Apple의 App Store나 Google Play Store에서 Google Authenticator 앱을 검색하고

설치해줍니다.

 

그러면 위와 같은 화면이 나오게 될 텐데, Scan QR code나 Enter a setup key를 눌러서 아까 콘솔에 나와있던 정보를 입력합니다.

 

그럼 위와 같이 서버가 등록된 것을 확인할 수 있습니다!

 

Google OTP 세부 설정

설정을 마무리 해보도록 합시다.

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

.google_authenticator 파일을 추가할 것인지 물어봅니다. 해당 파일은 필수 파일이기에 없으면 작동을 안 합니다 (...)

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)

여러 유저가 하나의 코드를 가지고 인증하는 것을 막을 것인지 물어보는 항목입니다. 비록 코드 주기가 30초로 재한 되지만 해당 인증코드는 사용 직후 바로 폐기되며, 이는 Man-in-the-middle attack (중간자 공격, 중간에서 코드를 탈취하여 사용하는 공격)을 막아줍니다. 입맛대로 설정하시면 됩니다만, 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)

 사용되는 토큰은 30초마다 초기화가 되지만 시간 서버 동기화의 오류로 OTP 코드 주기가 안 맞게 되는 현상이 발생할 수 있습니다. 해당 오류를 막기 위해 기본적으로 이전 코드와 다음 코드 도한 인증에 사용할 수 있습니다만, 만약 서버나 핸드폰의 시간 동기화에 극심한 문제가 있다면, 이전의 8개의 코드와 앞으로의 8개의 코드 또한 인증에 사용할 수 있도록 바꿀 수 있습니다. 이 또한 사용자가 원하는 대로 설정하시면 됩니다만, 보안을 위하신다면 해당 기능을 사용하지 않는 것이 좋습니다.

 

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)

또 다른 브루트포스의 예방책입니다. 사용자가 30초 동안 코드를 3번 이상 틀리면 차단됩니다. 하지만 지난 글에서 fail2ban을 설정해뒀기에 딱히 필요는 없는 설정입니다. 이 또한 사용자가 원하는 대로 설정하시면 되겠습니다.

 

이것을 마지막으로 기본적인 OTP 설치는 끝났습니다.

SSH에 OTP 추가하기

이제 SSH에 OTP를 추가해보도록 합시다.

* 지금부터 SSH를 통해 SSH의 설정을 변경하는 작업을 진행합니다. 해당 변경사항이 잘 작동하는지 확인될 때까지 지금 사용 중인 세션을 닫지 마세요. 모든 변경사항이 적용된 후, 새로운 세션을 열어 확인하고 나서 닫아도 문제 될 것이 전혀 없습니다.

 

설정 수정

sudo vi /etc/pam.d/sshd

SSH 설정 파일을 열어줍니다.

 

설정 파일을 내리다 보면 아래와 같은 항목이 보이게 됩니다.

@include common-password

이 문구 바로 아래에 이 문구를 추가해 줍니다:

auth required pam_google_authenticator.so

 

다음으로는 sshd_config 파일을 열어줍니다.

sudo vi /etc/ssh/sshd_config

위의 문구를 찾아서 yes로 바꿔줍니다.

 

서비스 재시작

sudo service sshd restart

위의 명령어를 통하여 sshd를 재시작합니다.

 

작동 확인

새로운 세션을 열어서 ssh에 로그인해봅니다.

password를 물어보고 그 뒤에 Verification code를 물어보는 칸이 생겼습니다!

해당 칸에 모바일 앱의 코드를 입력하면 로그인이 정상적으로 진행됩니다.

 

지금까지 SSH에다가 OTP인증까지 적용하는 방법을 알아보았습니다. 이로써 SSH의 보안이 더욱 강화되었습니다!

참고

https://www.digitalocean.com/community/tutorials/how-to-set-up-multi-factor-authentication-for-ssh-on-ubuntu-20-04

 

How To Set Up Multi-Factor Authentication for SSH on Ubuntu 20.04 | DigitalOcean

This article will go over how to enable SSH authentication using an OATH-TOTP app in addition to an SSH key. Logging into your server via SSH will then require two factors across two channels, thereby making it more secure than a password or SSH key a

www.digitalocean.com

반응형