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等。

2. Google Authenticator

image.png | center | 559.746835443038x180

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

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可執行文件,通過運行該命令進行配置。

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認證,在第一行添加 

# 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配置

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

重啟 sshd 服務

# 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.apk

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/81155054


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM