Thứ Ba, 8 tháng 11, 2022

BẢO MẬT SSH VỚI GOOGLE AUTHENTICATOR

Mục lục
  • Hướng dẫn cài đặt:
    • Bước 1: Cài đặt thư viện cho Google Authenticator
    • Bước 2: Cấu hình Google Authenticator cho user
    • Bước 3: Cấu hình SSH sử dụng mật khẩu
    • Bước 4: Cấu hình SSH sử dụng file key
Bảo mật SSH với Google Authenticator có thể xem là hình thức bảo mật hàng đầu cho tất cả các dịch vụ liên quan đến an toàn thông tin. Cách gọi thông dụng là bảo mật 2 bước. Hình thức bảo mật này thường thấy khi thực hiện các giao dịch ngân hàng. Sử dụng mật khẩu để sử dụng các dịch vụ và mã code SMS để xác thực khi có giao dịch phát sinh.

Với server Linux thì bảo mật 2 bước cũng được sử dụng xác thực đăng nhập trong nhiều ứng dụng khác nhau. Vì cơ chế này được cung cấp bởi Google nên tính bảo mật rất cao.

Cơ chế làm việc xác thực 2 bước trong SSH như hình sau:Bảo mật SSH

Bài viết này sẽ hướng dẫn bạn cài đặt 2FA trên server Linux sử dụng Google Authenticator

Hướng dẫn cài đặt:

  • Server Debian 10 (hoặc server Debian phiên bản khác đều được)
  • User không phải user root có quyền sudo
  • Smartphone cài đặt app Google Authenticator

Các bước cài đặt Google Authenticator như sau:

Bước 1: Cài đặt thư viện cho Google Authenticator

sudo apt update

sudo apt install libpam-google-authenticator –y

Bước 2: Cấu hình Google Authenticator cho user

Chạy lệnh sau:

google-authenticator

Hiện ra câu hỏi: “Do you want authentication tokens to be time-based (y/n)”

Chọn Y

Sau đó sẽ hiện ra một mã QR, sử dụng App trên Smartphone thêm user sau đó quét mã code này. Hoặc có thể truy cập đường link chứa mã QR này trên web.

Bảo mật SSH

Ngoài ra hãy lưu lại các mã code phía dưới, đó là các mã có thể dùng phòng trường hợp khẩn cấp ví dụ Smartphone bị đánh cắp có thể truy cập vào server để cách ly đảm bảo an toàn cho server.

Do you want me to update your “/home/debian/.google_authenticator” file? (y/n)

Chọ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)

Chọ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)

Chọn Y

Một lỗi thường gặp là thời gian sai lệch giữa server và google dẫn đến mã code không thể đăng nhập được.

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)

Chọn Y

Như vậy đã cấu hình xong Google Authenticator

Bước 3: Cấu hình SSH sử dụng mật khẩu

Mở file config pam

sudo nano /etc/pam.d/sshd

Tìm dòng @include common-auth và comment (thêm dấu # ở đầu dòng)

# Standard Un*x authentication.

#@include common-auth

Sau đó thêm dòng này ở cuối file

auth    required      pam_unix.so   no_warn try_first_pass

auth    required      pam_google_authenticator.so

Nhấn Ctrl+x để lưu và thoát

Chỉnh sửa file config của SSH

sudo nano /etc/ssh/sshd_config

Tìm dòng ChallengeResponseAuthentication và đặt giá trị là yes.

ChallengeResponseAuthentication yes

Thêm dòng này vào cuối file

AuthenticationMethods keyboard-interactive

Nhấn Ctrl+x để lưu và thoát

Khởi động lại dịch vụ SSH để apply các cấu hình mới

sudo service ssh restart

Thử đăng nhập vào server để kiểm tra lại

ssh debian@192.168.1.126

Password: nhập mật khẩu

Verification code: nhập mã code trên app

Bước 4: Cấu hình SSH sử dụng file key

Nếu sử dụng SSH thông qua file key thì sửa config SSH như sau:

PasswordAuthentication no

AuthenticationMethods publickey,keyboard-interactive

Cấu hình /etc/pam.d/sshd như sau:

# @include common-auth

# auth    required     pam_unix.so   no_warn try_first_pass

auth    required      pam_google_authenticator.so

Khởi động lại dịch vụ SSH để apply các cấu hình mới

sudo service ssh restart

Lưu ý: Để đảm bảo cơ chế hoạt động đúng thì cấu hình thời gian giữa server và smart phone phải đồng bộ thời gian với nhau.

=============================
* KHOÁ HỌC ORACLE DATABASE A-Z ENTERPRISE trực tiếp từ tôi giúp bạn bước đầu trở thành những chuyên gia DBA, đủ kinh nghiệm đi thi chứng chỉ OA/OCP, đặc biệt là rất nhiều kinh nghiệm, bí kíp thực chiến trên các hệ thống Core tại VN chỉ sau 1 khoá học.
* CÁCH ĐĂNG KÝ: Gõ (.) hoặc để lại số điện thoại hoặc inbox https://m.me/tranvanbinh.vn hoặc Hotline/Zalo 090.29.12.888
* Chi tiết tham khảo:
https://bit.ly/oaz_w
=============================
KẾT NỐI VỚI CHUYÊN GIA TRẦN VĂN BÌNH:
📧 Mail: binhoracle@gmail.com
☎️ Mobile: 0902912888
⚡️ Skype: tranbinh48ca
👨 Facebook: https://www.facebook.com/BinhOracleMaster
👨 Inbox Messenger: https://m.me/101036604657441 (profile)
👨 Fanpage: https://www.facebook.com/tranvanbinh.vn
👨 Inbox Fanpage: https://m.me/tranvanbinh.vn
👨👩 Group FB: https://www.facebook.com/groups/DBAVietNam
👨 Website: https://www.tranvanbinh.vn
👨 Blogger: https://tranvanbinhmaster.blogspot.com
🎬 Youtube: http://bit.ly/ytb_binhoraclemaster
👨 Tiktok: https://www.tiktok.com/@binhoraclemaster?lang=vi
👨 Linkin: https://www.linkedin.com/in/binhoracle
👨 Twitter: https://twitter.com/binhoracle
👨 Địa chỉ: Tòa nhà Sun Square - 21 Lê Đức Thọ - Phường Mỹ Đình 1 - Quận Nam Từ Liêm - TP.Hà Nội

=============================
BẢO MẬT SSH VỚI GOOGLE AUTHENTICATOR, học oracle database, Tự học Oracle, Tài liệu Oracle 12c tiếng Việt, Hướng dẫn sử dụng Oracle Database, Oracle SQL cơ bản, Oracle SQL là gì, Khóa học Oracle Hà Nội, Học chứng chỉ Oracle ở đầu, Khóa học Oracle online,khóa học pl/sql, học dba, học dba ở việt nam, khóa học dba, khóa học dba sql, tài liệu học dba oracle, Khóa học Oracle online, học oracle sql, học oracle ở đâu tphcm, học oracle bắt đầu từ đâu, học oracle ở hà nội, oracle database tutorial, oracle database 12c, oracle database là gì, oracle database 11g, oracle download, oracle database 19c, oracle dba tutorial, oracle tunning, sql tunning , oracle 12c, oracle multitenant, Container Databases (CDB), Pluggable Databases (PDB), oracle cloud, oracle security, oracle fga, audit_trail, oracle dataguard, oracle goldengate, mview, oracle exadata, oracle oca, oracle ocp, oracle ocm , oracle weblogic, middleware, hoc solaris, hoc linux, hoc aix, unix, securecrt, xshell, mobaxterm, putty

ĐỌC NHIỀU

Trần Văn Bình - Oracle Database Master