FreeRadius+GoogleAuthenticator實現linux動態口令認證

簡介

在運維管理中,服務器的密碼管理十分重要。服務器數量少的時候還好說,能夠定時來改密碼。一旦數量多了,再來改密碼就不現實了。linux

前提

咱們假定運維訪問服務器是這樣的:git

  1. 建立一個普通用戶用於登陸服務器,給出基礎權限,能進行簡單的平常操做。但不能修改系統層面的東西。這個權限不能作太多的事。
  2. 切換到root用戶時,不能使用root密碼來進行認證切換。否則密碼泄露,服務器最高權限就算是沒了。這裏可使用動態口令的方式來進行認證切換。

原理

原理圖以下:github

這裏經過FreeRadius+GoogleAuthenticator實現linux動態口令認證,其原理是:bootstrap

  1. 經過開源的GoogleAuthenticator來實現動態口令
  2. 服務器經過FreeRadius進行認證
  3. FreeRadius使用PAM+GoogleAuthenticator來進行認證

這樣說可能有點不清楚,我這裏再整理下整個流程。流程是這樣的:安全

  1. 用戶使用普通用戶登陸業務服務器A 進行操做
  2. 用戶須要提權,切換到root,須要輸入動態口令來進行切換
  3. 輸入動態口令
  4. 業務服務器A 就向動態口令認證服務器B 的FreeRadius進行認證
  5. 認證服務器B的FreeRadius收到認證請求後,開始認證
  6. FreeRadius會向GoogleAuthenticator來進行動態口令的校驗,而後將結果返回給業務服務器。
  7. 業務服務器成功切換到root用戶

安裝部署

服務端

系統:CentOS 7.2服務器

NTP安裝

因爲Google Authenticator依賴於時間,因此你的服務器時間必須老是正確的。這裏經過ntp服務自動同步網絡時間。網絡

yum install ntp -y
systemctl enable ntpd
systemctl start ntpd

Google Authenticator Pam安裝配置

安裝session

git clone https://github.com/google/google-authenticator-libpam.git
cd google-authenticator-libpam/
./bootstrap.sh
./configure
make
make install
ln -s /usr/local/lib/security/pam_google_authenticator.so /usr/lib64/security/pam_google_authenticator.so

若是出現:app

configure: error: Unable to find the PAM library or the PAM header files

請安裝此套件後,從新執行./configure:運維

# yum install pam-devel

配置pam

/etc/pam.d/sshd

#添加下面一行
auth    required        pam_google_authenticator.so
#註釋下面一行
#auth       substack     password-auth

配置ssh

/etc/ssh/sshd_config

# Change to no to disable s/key passwords
ChallengeResponseAuthentication yes
#ChallengeResponseAuthentication no

重啓sshd

systemctl restart sshd.service

爲用戶啓用google-authenticator

輸入命令:google-authenticator

1)屏幕提示Do you want authentication tokens to be time-based (y/n) ,回答y選用基於時間的token

2)屏幕提示二維碼,拿出手機打開google authenticator軟件(沒有請自行下載),點擊+後選擇「條形碼掃描"添加認證條目。 


---

注意:將屏幕顯示的secret key, verification code 和 recovery codes 保存在安全的地方,供密碼恢復使用。

---


3)Do you want me to update your "/home/sammy/.google_authenticator" file (y/n) y

4)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

4)By default, tokens are good for 30 seconds. 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 +-1min (window size of 3) to about +-4min (window size of 17 acceptable tokens). Do you want to do so? (y/n) n

5)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

新建ssh鏈接(不要關閉當前的防止沒法訪問)測試配置是否成功。

FreeRadius 安裝配置

安裝

yum install freeradius freeradius-utils -y

配置

/etc/raddb/radusd.conf

user = radiusd
group = radiusd
#改爲
user = root
group = root

/etc/raddb/users

#取消下面2行註釋
DEFAULT         Group == "disabled", Auth-Type := Reject
                Reply-Message = "Your account has been disabled."

#添加一行
DEFAULT        Auth-Type := PAM

/etc/raddb/sites-enabled/default

authenticate {
...
        #  Pluggable Authentication Modules
#       pam
...
}

#取消pam行前面的註釋

freeradius啓用pam模塊

ln -s /etc/raddb/mods-available/pam /etc/raddb/mods-enabled/pam

/etc/raddb/clients.conf

#在最後添加一段認證類型
client macolee_sudo {
        ipaddr = 0.0.0.0
        netmask = 0
        secret = macolee_sudo
        require_message_authenticator = no
}

/etc/pam.d/radiusd

#%PAM-1.0
#註釋掉下面一行
#auth       include password-auth
#添加下面一行。secret是google Authenticator認證的配置文件,user是切換爲root的時候須要認證
auth       required     pam_google_authenticator.so secret=/etc/raddb/gauth user=root
account    required pam_nologin.so
account    include  password-auth
password   include  password-auth
session    include  password-auth

啓動freeradius

systemctl enable radiusd
systemctl start radiusd

打開防火牆限制,開通FreeRadius的1812端口

/etc/sysconfig/iptables

-A INPUT -s 10.1.2.97 -p udp -m udp --dport 1812 -j ACCEPT

客戶端

pam_radius安裝配置

安裝

#添加epel yum源
yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm -y
#安裝
yum install pam_radius -y

配置

...

# server[:port] shared_secret      timeout (s)
# 127.0.0.1 secret             1
# other-server    other-secret       3

#添加認證服務器地址 認證類型
10.214.60.37:1812  macolee_sudo       10

修改sudo的pam配置

#%PAM-1.0
#添加下面一行
auth       required      pam_radius_auth.so
#註釋下面一行
#auth       include      system-auth
account    include      system-auth
password   include      system-auth
session    optional     pam_keyinit.so revoke
session    required     pam_limits.so

驗證

前提

客戶端服務器上的用戶名,在radius服務端也必須存在。好比客戶端要切換爲root的普通用戶是test1,那麼radius服務端也必須有test1這個普通用戶

客戶端和服務端添加用戶

#添加用戶
useradd test1
#給用戶sudo權限
命令:visudo
#添加
macolee  ALL=(ALL) /bin/su - root

測試登陸

> 1. 以test1用戶登陸客戶端服務器
> 2. 輸入名sudo su - root
> 3. 輸入手機app上google驗證器上的驗證碼
> 4. 能成功切換,表示配置成功
相關文章
相關標籤/搜索