使用Google兩段式驗證套件強化AWS EC2的安全性

使用Google兩段式驗證套件強化AWS EC2的安全性

AWS一向都以安全性為最優先的目標等級,我們可以使用IAM開啟兩階段式驗證來加強AWS Management Console登入,但是對於EC2的部分就需要由我們自己來進行加強。我自己的公司主機已經使用這樣的方式一段時間了,今天分享如何安裝、設定google兩段式驗證在我們的EC2上面,當然只要是Linux主機都可以裝上這個套件,另外也會針對後續使用上做一些說明。

這裡我用 Amazon Linux AMI 2013.03.1 這個AMI來進行說明

1. 修改hostname
我們先修改一下hostname,如果已經修改過的就可以跳過,當然也可以不修改,修改的原因是為了讓我們比較好識別動態密碼

[root@ip-10-150-143-120 ~]# hostname hsdn_jp_g2a

看下面的這張截圖,大家就知道為什麼要改hostname,當我們有很多主機都啟用兩段式驗證的時候,我們自己定義名稱是不是比ip來的容易識別呢?
g2a

 

2. 這樣的修改當重開機之後,hostname就會被還原,所以建議繼續下面的步驟

[root@ip-10-150-143-120 ~]# vim /etc/sysconfig/network
修改這行 HOSTNAME=localhost.localdomain
把localhost.localdomain改成跟我們剛剛設定的hostname相同就可以

 

3. 接下來就要開始進行google兩段式驗證套件的安裝

[root@ip-10-150-143-120 ~]# yum install pam-devel make gcc-c++ wget
[root@ip-10-150-143-120 ~]# cd /root
[root@ip-10-150-143-120 ~]# wget https://google-authenticator.googlecode.com/files/libpam-google-authenticator-1.0-source.tar.bz2
[root@ip-10-150-143-120 ~]# tar -jxvf libpam-google-authenticator-1.0-source.tar.bz2
[root@ip-10-150-143-120 ~]# cd libpam-google-authenticator-1.0
[root@ip-10-150-143-120 libpam-google-authenticator-1.0]# make
[root@ip-10-150-143-120 libpam-google-authenticator-1.0]# make install

 

4. 建立與設定root帳號的兩段式驗證

[root@ip-10-150-143-120 ~]# google-authenticator

 

5. 按下enter之後,會出現以下畫面,基本上都使用預設值按 y 就可以了

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

https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/root@ip-10-150-143-120%3Fsecret%3D7QEK5IN6PAWOIWGI
Your new secret key is: 7QEK5IN6PAWOIWGI
Your verification code is 642474
Your emergency scratch codes are:
  38475479
  46335490
  27751304
  68197658
  49032270

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, tokens are good for 30 seconds and 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. If you experience problems with poor
time synchronization, you can increase the window from its default
size of 1:30min to about 4min. 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

這裡有三個地方要注意

  • 畫面中的網址,是這個套件自動產出的QR Code,請使用手機上的Google Authenticator掃描,手機上就會看到動態密碼
  • verification code相當於是私鑰,請妥善保存,避免外洩
  • emergency scratch codes,是當我們忘記帶手機的時候,可以暫時用來登入的驗證碼,一組號碼只能用一次,用完之後就會失效

 

6. 修改sshd_config
做完以上的設定之後,我們接下來要進行SSH相對應的修改

[root@ip-10-150-143-120 ~]# vim /etc/ssh/sshd_config
修改兩個地方
ChallengeResponseAuthentication yes
UsePAM yes

 

7. 修改pam.d

[root@ip-10-150-143-120 ~]# vim /etc/pam.d/sshd
加入這行
add    auth       required     pam_google_authenticator.so

 

8. 重啟SSH Server

[root@ip-10-150-143-120 ~]# service sshd restart

 

9. 測試
這時候已經完成全部的設定了,但是請不要急著關掉你現在連線的console進行測試,最保險的方式是再開一個新的console進行測試。如果成功,你的登入畫面應該會像下面的截圖一樣,先輸入你的帳號,接著是密碼,最後是輸入手機上Google Authenticator顯示的動態密碼。
Screen shot 2013-07-24 at 11.07.13 AM

 

10. 驗證失敗的處理
因為Google Authenticator是使用時間為基準來產生動態密碼,所以當發生錯誤的時候,請將主機和裝置進行網路教時,以我實務上的經驗,通常最常發生問題的裝置是Android裝置。

 

11. 停用兩段式驗證
由於各種狀況,我們可能會需要停用兩段式驗證,例如剛剛的安裝設定失敗,也可能是使用者抱怨實在是太麻煩了,也可能是上面的長官不想使用。不管是什麼情況,我們只需要還原以下的設定,就可以停用兩段式驗證

11-1 修改sshd_config

[root@ip-10-150-143-120 ~]# vim /etc/ssh/sshd_config
把 ChallengeResponseAuthentication yes
改成 ChallengeResponseAuthentication no

11-2 修改pam.d

[root@ip-10-150-143-120 ~]# vim /etc/pam.d/sshd
刪除這行
add    auth       required     pam_google_authenticator.so

11-3 最後重新啟動SSH Server

[root@ip-10-150-143-120 ~]# service sshd restart

 

12. 啟用其他使用者帳號的兩段式驗證
這裡我舉ec2-user為例,相信大家都可以舉一反三

[root@ip-10-150-143-120 ~]# su ec2-user
[ec2-user@hsdn_jp_g2a ~]$ google-authenticator

Do you want authentication tokens to be time-based (y/n) y
https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/ec2-user@hsdn_jp_g2a%3Fsecret%3DF7EBI7LSBQ4NBIH4
Your new secret key is: F7EBI7LSBQ4NBIH4
Your verification code is 128218
Your emergency scratch codes are:
  54124944
  54781417
  59253059
  54476534
  31034872

Do you want me to update your "/home/ec2-user/.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, tokens are good for 30 seconds and 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. If you experience problems with poor
time synchronization, you can increase the window from its default
size of 1:30min to about 4min. 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

 

13. 重新產生私鑰
如果發生使用者手機掉了,或者是離職但是帳號不能刪除的原因,我們可以使用下面的方式重新產生私鑰。這裡一樣使用ec2-user為例

[root@ip-10-150-143-120 ~]# su ec2-user
[ec2-user@hsdn_jp_g2a ~]$ google-authenticator

然後請使用者重新掃描新的QR-Code,把舊的動態密碼刪除就可以了。

 

14. FileZilla的設定
當我們啟用了兩段式驗證之後,使用者如果有使用SFTP連接主機進行檔案的操作,必須做下面兩個設定

14-1 將登入形式修改為交談式
這樣一來,當使用者登入的時候,會先需要輸入動態密碼,接著再輸入密碼,這樣就可以登入主機進行操作了。
Screen shot 2013-07-24 at 2.16.41 PM

14-2 設定傳輸設定
現在的ftp client預設都是使用多線連接進行傳輸,但是當我們啟用兩段式驗證之後,上傳檔案會被一直跳出輸入動態密碼和密碼的對話框煩死,為了解決這個問題,我們必須捨棄一些傳輸的便利性,請按照這張截圖的設定,就可以免除剛剛所說的困擾
Screen shot 2013-07-24 at 2.19.32 PM

阿維

阿維雜記本的偷懶維護者