Tuesday, March 31, 2015

การติดตั้ง Google Authenticator สำหรับบริการ Remote SSH บน Linux



การติดตั้ง Google Authenticator สำหรับบริการ Remote SSH บน Linux

ขั้นตอนแรกสำหรับการติดตั้ง จะแบ่งออกเป็นสองส่วน โดยแยกตาม Linux distribution ซึ่งทำมาสองแบบ คือ  Debian7 กับ CentOS7


สำหรับ Debian7  ให้ Config ตามนี้ครับ

1.     Login  เข้าระบบด้วย Root

2.     ติดตั้ง Module ต่างๆ
      aptitude install libpam0g-dev make gcc bzip2

3.     Download google authenticator มาไว้ที่เครื่อง

4.     แตกไฟล์ zip ออกมา
      tar -xvf libpam-google-authenticator-1.0-source.tar.bz2

5.     เข้าไปที่ Folder ที่ extract ออกมาแล้ว
           cd libpam-google-authenticator-1.0
 
6.     ทำการติดตั้ง ด้วยคำสั่ง  make และ make install ตามลำดับ
      make

      make install

 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA


สำหรับ CentOS7 ให้ Config ตามนี้ครับ

1.     Login  เข้าระบบด้วย Root

2.     ติดตั้ง Module ต่างๆ และเปิดการใช้งาน NTP
     
      yum -y groupinstall "Development Tools"
      yum -y install pam-devel wget
      yum -y install ntp

      systemctl start ntpd
      systemctl enable ntpd

3.     Download google authenticator มาไว้ที่เครื่อง

4.     แตกไฟล์ zip ออกมา
      tar -xvf libpam-google-authenticator-1.0-source.tar.bz2

5.     เข้าไปที่ Folder ที่ extract ออกมาแล้ว
           cd libpam-google-authenticator-1.0
 
6.     ทำการติดตั้ง ด้วยคำสั่ง  make และ make install ตามลำดับ
      make

      make install

BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB

7.      เรียกใช้งานโปรแกรม
      google-authenticator
 
8.     สำหรับคำถามแรก ให้ตอบ y ระบบจะทำการสร้าง Secret key เพื่อให้นำไปใช้กับโปรแกรม Google authen  บนมือถือ สำหรับ emergency code ใช้ในกรณีที่มือถือหายและต้องการเข้าระบบ ดังนั้นข้อมูลส่วนนี้ จะต้องเก็บไว้ และเก็บเป็นความลับด้วย
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@dreamwork%3Fsecret%3D73CV3CXD2HMRZIXX
Your new secret key is: 73CV3CXD2HMRZIXX
Your verification code is 257858
Your emergency scratch codes are:
  46776405
  18571266
  98256248
  59004138
  98459948

9.     คำถามที่เหลือ ให้ตอบ y ทั้งหมด
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

10.  ทำการแก้ไขไฟล์ Pam ของ SSH
vi /etc/pam.d/sshd
            เพิ่ม auth       required     pam_google_authenticator.so ลงไป

# /etc/security/pam_env.conf.
auth       required           pam_google_authenticator.so
auth       required     pam_env.so # [1]
# In Debian 4.0 (etch), locale-related environment variables were moved to
# /etc/default/locale, so read that as well.
auth       required     pam_env.so envfile=/etc/default/locale

11.  ทำการแก้ไขไฟล์ SSH Config
vi /etc/ssh/sshd_config
            เปลี่ยนค่า            ChallengeResponseAuthentication no
            ให้เป็น               ChallengeResponseAuthentication yes

12.  Restart SSH Service
สำหรับ debian7 ให้ใช้คำสั่งนี้        /etc/init.d/ssh restart
สำหรับ CentOS7  ให้ใช้คำสั่งนี้      systemctl reload sshd

ขั้นตอนของทางฝั่ง Server เสร็จแค่นี้

CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC

ขั้นตอนฝั่งมือถือ

1.     ที่มือถือ ให้โหลดโปรแกรม Google authenticator มาลงในเครื่องก่อน
App Atore 
 
Play Store
     
            Windows store

2.     ที่โปรแกรม เลือก Add entry จากนั้น เลือก Manual Entry

3.      ช่อง Account ให้ใส่ชื่อของ Server หรือชื่ออะไรก็ได้ เพื่อให้รู้ว่า นี่เป็น Code ของ SSH เครื่องนี้นะ

4.     ช่อง Key ให้ใส่ Secret key ทีได้จาก Server

เวลา Login ระบบจะเตือนให้ใส่ Verification code ก่อน จากนั้นค่อยใส่ password

เท่านั้ก็สามารถใช้งานได้แล้วครับ