SSH + Google Authenticator 安全加固

1. SSH鏈接

image.png | center | 320x233

Secure Shell(安全外殼協議,簡稱SSH)是一種加密的網絡傳輸協議,可在不安全的網絡中爲網絡服務提供安全的傳輸環境。SSH經過在網絡中建立安全隧道來實現SSH客戶端與服務器之間的鏈接。雖然任何網絡服務均可以經過SSH實現安全傳輸,SSH最多見的用途是遠程登陸系統,人們一般利用SSH來傳輸命令行界面和遠程執行命令。使用頻率最高的場合類Unix系統,可是Windows操做系統也能有限度地使用SSH。
SSH自己是一個很是安全的認證鏈接方式。不過因爲人過等方面的緣由,不免會形成密碼的泄露。針對這種問題咱們不妨給SSH再加一把鎖。固然,增長這層鎖的方式有不少種。例如:knockd、S/KEY、OPIE/OPTW、Two-factor authentication等。html

2. Google Authenticator

image.png | center | 559.746835443038x180

Google身份驗證器是一款基於時間與哈希的一次性密碼算法的兩步驗證軟件令牌,此軟件用於Google的認證服務。此項服務所使用的算法已列於 RFC 6238 和 RFC 4226 中。 
Google身份驗證器給予用戶一個六位到八位的一次性密碼用於進行登陸Google或其餘站點時的附加驗證。其一樣能夠給第三方應用生成口令,例如密碼管家程序或網絡硬盤。先前版本的Google身份驗證器開放源代碼,但以後的版本以專有軟件的形式公開。python

3.Linux 中安裝

3.1 系統環境說明

[root@clsn.io /root] clsn.io Blog WebSite
#cat  /etc/redhat-release 
CentOS release 6.8 (Final)

[root@clsn.io /root] clsn.io Blog WebSite
#uname -a
Linux clsn.io 4.10.5-1.el6.elrepo.x86_64 #1 SMP Wed Mar 22 14:55:33 EDT 2017 x86_64 x86_64 x86_64 GNU/Linux

[root@clsn.io /root] clsn.io Blog WebSite
#sestatus 
SELinux status:                 disabled

3.2 安裝 Google Authenticator

3.2.1 安裝依賴包

yum -y install wget gcc make pam-devel libpng-devel

3.2.2 Google Authenticator PAM插件安裝

# 可在google的github下載
wget  https://github.com/google/google-authenticator/archive/1.02.tar.gz 
tar xf 1.02.tar.gz
cd google-authenticator-1.02/libpam/
./bootstrap.sh
./configure 
make && make install

安裝完成後會在 /usr/local/lib/security/pam_google_authenticator.so生成一個 庫文件,
系統還會多在/usr/local/bin目錄生成一個google-authenticator可執行文件,經過運行該命令進行配置。linux

3.2.3 複製so文件

# cp  /usr/local/lib/security/pam_google_authenticator.so /lib64/security/

4. 配置 SSH + Google Authenticator

4.1 初始配置 Google Authenticator

[root@clsn.io /lib64/security] clsn.io Blog WebSite
#google-authenticator 
Do you want authentication tokens to be time-based (y/n) n
# 是否基於時間的認證,爲了防止不一樣跨時區的問題,這裏選擇n
https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://hotp/root@clsn.io%3Fsecret%3*****%26issuer%3Dclsn.io
# s生成的二維碼
Your new secret key is: ****
Your verification code is 5****0
Your emergency scratch codes are:
  40****84
  19****95
  60****78
  83****92
  31****58
# 這5個碼用於在取不到或錯的驗證碼有錯時,用於應急用的。不過每一個只能用一次,不能重複使用。

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

By default, three tokens are valid at any one time.  This accounts for
generated-but-not-used tokens and failed login attempts. In order to
decrease the likelihood of synchronization problems, this window can be
increased from its default size of 3 to 17. 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

4.2 SSH調用及客戶端配置

添加pam認證,在第一行添加 android

# vim  /etc/pam.d/sshd
auth       required     pam_google_authenticator.so
------------------------------------------------------------------
#cat  /etc/pam.d/sshd 
#%PAM-1.0
auth       required pam_sepermit.so
auth       required     pam_google_authenticator.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    required     pam_namespace.so
session    optional     pam_keyinit.so force revoke
session    include      password-auth

修改sshd配置git

# vim /etc/ssh/sshd_config
ChallengeResponseAuthentication no
#把上面配置改爲

重啓 sshd 服務github

# service sshd restart

5. 客戶端使用

5.1 Android客戶端

(版本5.00,更新日期 2017年9月27日)
下載地址:https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2&hl=zh
CLSN鏡像地址 https://clsn.io/files/google/com.google.android.apps.authenticator.apkweb

image.png | center | 189.4736842105263x400

6.2 瀏覽器客戶端

獲取30秒一次的動態碼的客戶端是瀏覽器(僅支持chrome、firefox)、Android設備、蘋果IOS設備、Blackberry、WP手持設備。各自程序的下載地址爲:
chrome google-authenticator插件
firefox google-authenticator插件 算法

6.3 Python 客戶端

import hmac, base64, struct, hashlib, time

def calGoogleCode(secretKey):
    input = int(time.time())//30
    key = base64.b32decode(secretKey)
    msg = struct.pack(">Q", input)
    googleCode = hmac.new(key, msg, hashlib.sha1).digest()
    o = ord(googleCode[19]) & 15
    googleCode = str((struct.unpack(">I", googleCode[o:o+4])[0] & 0x7fffffff) % 1000000)
    if len(googleCode) == 5:
        googleCode = '0' + googleCode
    return googleCode

secretKey = '***這裏填祕鑰***'
print calGoogleCode(secretKey)

6. 參考文獻

http://www.361way.com/google-authenticator-ssh/2186.html
http://netsecurity.51cto.com/art/201305/392443.htm
https://blog.csdn.net/bwlab/article/details/51321746
https://github.com/google/google-authenticator/wiki
https://blog.csdn.net/RBPicsdn/article/details/81155054chrome

相關文章
相關標籤/搜索