HƯỚNG DẪN Xác thực hai bước cho ssh trên CentOS 6 sử dụng Google Authenticator

Thảo luận trong 'KIẾN THỨC VPS/SERVER' bắt đầu bởi quyet1990, 30/11/16.

  1. quyet1990

    quyet1990 New Member

    Tham gia ngày:
    22/10/16
    Bài viết:
    220
    Đã được thích:
    0
    Google Authenticator thực hiện các token bảo mật TOTP (timebased one-time-password) từ RFC6238 qua ứng dụng di động Google Authenticator.

    Xác thực này cung cấp một mật khẩu sáu chữ số chỉ sử dụng một lần, ngoài ra user phải cung cấp thêm tên tài khoản và mật khẩu để đăng nhập, đôi khi nó được gọi là "xác thực hai bước". Ở đây, chúng tôi cài đặt và cấu hình một pluggable authentication module (PAM) cho phép đăng nhập bằng mật mã một lần.

    Tải về và cài đặt
    Tại thời điểm viết bài này, chỉ có một phiên bản cũ của libpam-google-authenticator là có sẵn trong kho gói EPEL. Do đó, chúng ta sẽ biên dịch nó từ nguồn. Đầu tiên, cài đặt điều kiện tiên quyết:
    Mã:
     yum install make gcc pam-devel 
    TOTP (timebased one-time-password) là rất nhạy cảm. Do đó, hãy chắc chắn rằng hệ thống của bạn có ntpd chạy, và được cấu hình để khởi động dịch vụ khi khởi động:
    Mã:
    service ntpd start
    Starting ntpd:   [  OK  ]
    chkconfig  ntpd on
    
    Sau đó tải về và cài đặt libpam-google-authenticator từ nguồn, quá trình cài đặt như sau:
    Mã:
    cd /tmp -->Điều hướng đến thư mục /tmp
    wget http://google-authenticator.googlecode.com/files/libpam-google-authenticator-1.0-source.tar.bz2 -->Tải về source cần cài đặt
    bunzip2 libpam-google-authenticator-1.0-source.tar.bz2 -->Giải nén tập tin mới tải về sang .tar
    tar xf libpam-google-authenticator-1.0-source.tar --> Giải nén tập tin một lần nữa
    cd libpam-google-authenticator-1.0 -->Điều hướng đến thư mục mới giải nén
    make --> Biên dịch source
    make install -->Cài đặt
    cp pam_google_authenticator.so /lib64/security -->Sao chép dữ liệu
    cp google-authenticator /usr/local/bin --> Sao chép dữ liệu
    
    Thiết lập Google Authenticator
    Trước khi cấu hình SSH, việc đầu tiên ta thiết lập Google Authenticator. Chạy "google-authenticator" với user mà bạn sẽ đăng nhập thông qua SSH. Bạn sẽ được nhắc nhở với một vài câu hỏi.
    Mã:
    Do you want me to update your "~/.google_authenticator" file (y/n) y
    https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/user@server%3Fsecret%3DABCD12E3FGHIJKLMN
    Your new secret key is: ABCD12E3FGHIJKLMN
    Your verification code is 98765432
    Your emergency scratch codes are:
      01234567
      89012345
      67890123
      45678901
      23456789
    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
    
    Các thiết lập này được lưu trữ trong tập tin ~/.google_authenticator của user.

    Sao chép và dán URL vào trình duyệt của bạn và quét mã QR được hiển thị với các ứng dụng Google Authenticator trên điện thoại di động của bạn. Nếu bạn không thể quét mã QR, bạn có thể tự nhập thông tin với khóa bí mật và mã xác minh. Một mã xác minh mới sẽ được hiển thị 30 giây mỗi lần.

    Đối với emergency scratch codes bạn nên ghi lại và để ở một nơi an toàn trong trường hợp bạn không có thiết bị di động của bạn với bạn.

    Cấu hình PAM
    PAM yêu cầu Google Authenticator cho SSH. Sửa tập tin /etc/pam.d/sshd và thêm dòng " auth required pam_google_authenticator.so " ở đầu trang.
    Mã:
    #%PAM-1.0
    auth       required     pam_google_authenticator.so
    auth       required     pam_sepermit.so
    auth       include      password-auth
    account    required     pam_nologin.so
    account    include      password-auth
    password   include      password-auth
    # pam_selinux.so close should be the first session rule
    session    required     pam_selinux.so close
    session    required     pam_loginuid.so
    # pam_selinux.so open should only be followed by sessions to be executed in the user context
    session    required     pam_selinux.so open env_params
    session    optional     pam_keyinit.so force revoke
    session    include      password-auth
    
    Điều này sẽ yêu cầu tất cả user phải sử dụng Google Authenticator cho xác thực SSH. Để xác đinh những user với Google Authenticator (tập tin ~ / .google_authenticator tồn tại), thay thế dòng " auth required pam_google_authenticator.so " thành “auth required pam_google_authenticator.so nullok“.

    Với cấu hình này, trước tiên bạn sẽ được nhắc nhập mã xác minh của Google Authenticator, sau đó nhập mật khẩu tài khoản của hệ thống của bạn khi bạn SSH vào hệ thống.

    Cấu hình dịch vụ SSH
    Sửa đổi tập tin /etc/ssh/sshd_config. Xác nhận các thiết lập này:
    Mã:
    PasswordAuthentication yes
    ChallengeResponseAuthentication yes
    UsePAM yes
    
    Khởi động lại dịch vụ SSH:
    Mã:
     service sshd restart 
    Khi bạn SSH vào hệ thống như là một user được cấu hình Google Authenticator, bạn sẽ phải nhập mã xác minh sẽ được hiển thị trong ứng dụng Google Authenticator, và sau đó bằng mật khẩu hệ thống của bạn tại dấu nhắc tiếp theo:
    Mã:
    login as: root
    Verification code: 01234567
    Password: *******
    #
    
    Nếu bạn có bất kỳ vấn đề, hãy tìm trong thư mục /var/log/secure.
    Nếu bạn có SELinux được kích hoạt, bạn có thể không thể đăng nhập, và nhận được lỗi này trong /var/log/secure:
    Mã:
    Jan  3 23:42:50 hostname sshd(pam_google_authenticator)[1654]: Failed to update secret file "/home/username/.google_authenticator"
    Jan  3 23:42:50 hostname  sshd[1652]: error: PAM: Cannot make/remove an entry for the specified session for username from 192.168.0.5
    
    Đây có lẽ là do /home/username/.google_authenticator không có một Type Enforcement (TE) thích hợp:
    Mã:
    ls -Z /home/username/.google_authenticator
    -r--------. username username unconfined_u:object_r:user_home_t:s0 /home/username/.google_authenticator
    
    Xem ở đây để biết thêm thông tin về vấn đề này.

    Bỏ qua xác thực Google Authenticator nếu đăng nhập từ Local Network
    Bạn có thể tin tưởng vào hệ thống local network của bạn đủ tốt để không cần kết nối SSH sử dụng Google Authenticator. Nếu vậy, sửa đổi /etc/pam.d/sshd để nó trông như thế này:
    Mã:
    auth [success=1 default=ignore] pam_access.so accessfile=/etc/security/access-local.conf
    auth  required  pam_google_authenticator.so
    
    Sau đó thêm vào tập tin /etc/security/access-local.conf với các nội dung:
    Mã:
    # Google Authenticator can be skipped on local network
    + : ALL : 192.168.0.0/24
    + : ALL : LOCAL
    - : ALL : ALL
    
    Điều này giả định local network của bạn có subnet là 192.168.0.0/24.
     

Chia sẻ trang này

Đang tải...