2021年5月25日 星期二

【IT Notes】FortiGate 100D串Windows AD,並用FortiToken收發VPN

大概兩周前肺炎的疫情加劇,接下來每天都是本土幾百人確診,完全控制不住也措手不及,上上禮拜三突然宣布要在家工作後,當下筆電和VPN的需求暴增,在家上班要連入公司網路就只能靠VPN,但後來出現了一些問題,花了好多天時間逐一排除,這邊就先做個紀錄。

公司買的防火牆是Fortigate 100D,它也有提供VPN功能,但是認證方式可以分很多種,複雜度也都不同,一般來說要用到VPN的情況都很緊急,通常都會選最快能連線的認證方式,但快就代表著有需多缺陷;同樣如果要設計出安全的認證方式,操作就會更複雜難懂且費時。無法兩全的時候就只能先採用可以使用優先,然後再不斷地優化。目前研究出的三種方式:


一.Local帳號密碼+Gmail雙認證

在「User Difibition」選「+Create New」

選「Local User」

建立帳號和密碼

一定要輸入email,因為要靠信箱接收雙因認證的Token

賦予帳號群組

開啟CLI指令介面

前面建立帳號時的UI上面的Two-Factor Authentication是沒有email可以選的,懷疑可能是廠商希望管理員購買他們家的Token,如果要靠email收Token,只能按照此方式輸入指令來開啟

這時候到帳號介面看,就會發現雙因認證已經開啟並出現了email收認證的選項

優點:
設定快且簡單易懂。
缺點:
1.透過Mail Server和Gmail收OTP,容易有時間差等不可抗拒原因,經常出現收不到信或收到信時已經超過認證時間。
2.獨立的本地帳號密碼,增加使用者記憶和管理者的不便。
3.使用個人信箱當接收OTP亦難符合公司稽核規定。

二.AD帳號+FortiToken雙認證

1.建立AD帳號

這邊就很簡單,其實不需要特別說明

在AD主機上建立一個帳號

帳號也要賦予群組

2.連接AD主機

Fortigate防火牆要開始跟AD做連結,然後再跟AD帳號串連
先跟AD主機作連接,特別留意Distinguish Name,其實就是網域名稱;使用者帳密要用AD管理者,然後測試連線,成功的話就會看到Successfully

如果連線都成功的話,AD主機就會出現在上面,這邊我新增了兩台

接下來去User Definition,準備新增一個AD帳號


新增的帳號要選擇Remote LDAP User

選任何一台AD主機,建議選主要建帳號用的那台AD


搜尋要找AD帳號

點帳號、按右鍵「Add Selected」


點下面的「Submit」

帳號提交後,選帳號按「Edit User」

將帳號加入所屬群組,防火牆政策才會mapping,還要填入Email

再把Token給予此帳號,系統就會QR Code寄到Email內

可以先去Fortitoken看一下共有那些可用Token,這邊看到免費的Token只有兩個,給完就只能再去跟廠商買,這邊就可以感受到這種方式簡單但要花錢

確認一下已經啟用雙因認證的Token就可以按「OK」
3.使用FortiToken APP
手機先要去下載Fortitoken APP,是他們家用來專門認證的Token的APP,無法做其他的認證。
下載後掃描QR Code,就會出現六位數的Token,登入VPN時就可以輸入了




優點:
1.帳號串AD減少使用者記憶和不便,管理者也只需要顧好AD帳號。
2.有現成的FortiToken既安全也方便做認證。
缺點:
1.FotiToken必須付費,有成本考量。
2.多下載FotiToken APP的動作對使用者來說不一定比較容易理解,也難跟其他使用Google Authenticator的認證服務做整合。

三.AD帳號+Freeradius雙認證

RADIUS是遠端認證用的伺服器,而Freeradius則是一個Linux based的免費且開源的服務,很好用之外也符合安全需求,只是要架設它的過程需要做點功課,我在架設過程中遇到很多障礙,三種方式中這個最花時間。

1.建立一台Freeradius主機

其實硬體需求不用太高,但我是第一次架設,還是保守點給4 Core CPU、8 GB RAM、100GB硬碟、一張網卡。

2.基本設定

更新、安裝Freeradius、關閉防火牆、SElinux並重開電腦。

[root@freeradius ~]# yum update -y
[root@freeradius ~]# yum install vim net-tools -y
[root@freeradius ~]# yum install freeradius freeradius-utils -y
[root@freeradius ~]# systemctl status firewalld
[root@freeradius ~]# systemctl stop firewalld
[root@freeradius ~]# systemctl disable firewalld
[root@freeradius ~]# vi /etc/selinux/config
[root@freeradius ~]# shutdown -r -t 0

3.更改使用者和群組

要執行goole-authenticator 必須用到root權限。

[root@freeradius ~]# vi /etc/raddb/radiusd.conf 
#group = radiusd 
#user = radiusd
user = root 
group = root

4.sites-enabled/default檔將pam取消註解 

[root@freeradius ~]# vi /etc/raddb/sites-enabled/default
# Pluggable Authentication Modules. 
 pam

5.client端防火牆連線設定 

這邊的設定很重要,如果錯誤了就無法跟Fortigate做連接。

[root@freeradius ~]# vi /etc/raddb/clients.conf
client 10.113.112.10/24 { 
           secret = testing123 
           require_message_authenticator = no 
           nas_type = other 
}
\\有些文件上寫還要ipaddr的位置,但我花很多時間測試才發現這是多餘的。

6.變更認證類型

[root@freeradius ~]# vi /etc/raddb/users
# DEFAULT Group == "disabled", Auth-Type := Reject 
# Reply-Message = "Your account has been disabled." 
#
\\把上面的設定取消註解並改成下面的
DEFAULT Group == "disabled", Auth-Type := Reject 
Reply-Message = "Your account has been disabled." 
DEFAULT Auth-Type := PAM

7.建立本機帳號並測試認證

[root@freeradius ~]# useradd raduser 
[root@freeradius ~]# passwd raduser 
Changing password for user raduser. 
New password: 1qaz@WSX
Retype new password: 1qaz@WSX
passwd: all authentication tokens updated successfully.
[root@freeradius ~]# radiusd -X \\這是debug模式,有錯誤的話會顯示在最下方結果
[root@freeradius ~]# radtest raduser 1qaz@WSX localhost 0 testing123
Sent Access-Request Id 71 from 0.0.0.0:41478 to 127.0.0.1:1812 length 77
    User-Name = "raduser"
    User-Password = "1qaz@WSX"
    NAS-IP-Address = 10.113.112.10
    NAS-Port = 0
    Message-Authenticator = 0x00
    Cleartext-Password = "1qaz@WSX"
Received Access-Accept Id 71 from 127.0.0.1:1812 to 0.0.0.0:0 length 20
 \\Received Access-Accept就表示本機的單因認證測試通過了

8.安裝網域套件並加入網域

[root@freeradius ~]# yum install sssd realmd adcli 
[root@freeradius ~]# yum install oddjob oddjob-mkhomedir sssd samba-commontools [root@freeradius ~]# realm join interpay.com.tw  \\網域就看公司用什麼
Password for Administrator:    \\要輸入AD主機的管理者密碼
測試看看能不能連線到AD上面的帳號
[root@freeradius ~]# su - testuser@interpay.com.tw
上一次登入:五  5月 21 10:17:10 CST 2021在 pts/0
[testuser@interpay.com.tw@freeradius ~]$    \\這樣就表示網域已經在這台radius上串連成功
[testuser@interpay.com.tw@freeradius ~]$ radiusd -X     \\再跑一次debug模式
[testuser@interpay.com.tw@freeradius ~]# radtest testuser@interpay.com.tw 1qaz@WSX localhost 0 testing123 
Sent Access-Request Id 144 from 0.0.0.0:35469 to 127.0.0.1:1812 length 84 
           User-Name = "testuser@interpay.com.tw" 
           User-Password = "1qaz@WSX" 
           NAS-IP-Address = 10.113.112.10 
           NAS-Port = 0 
           Message-Authenticator = 0x00 
           Cleartext-Password = "1qaz@WSX" 
           Received Access-Accept Id 144 from 127.0.0.1:1812 to 0.0.0.0:0 length 20
\\這次的測試是以AD帳號且只有單因認證,下面要再做雙因認證

9.安裝Google Authenticator PAM

[root@freeradius ~]# yum install pam-devel make gcc-c++ git 
[root@freeradius ~]# yum install automake autoconf libtool 
[root@freeradius ~]# cd ~ 
[root@freeradius ~]# git clone https://github.com/google/google-authenticatorlibpam.git 
Cloning into 'google-authenticator-libpam'... remote: Enumerating objects: 796, done. remote: Total 796 (delta 0), reused 0 (delta 0), pack-reused 796 Receiving objects: 100% (796/796), 538.35 KiB | 381.00 KiB/s, done. Resolving deltas: 100% (508/508), done. 
[root@freeradius ~]# cd ~/google-authenticator-libpam/ 
[root@freeradius google-authenticator-libpam]# ./bootstrap.sh 
[root@freeradius google-authenticator-libpam]# ./configure 
[root@freeradius google-authenticator-libpam]# make 
[root@freeradius google-authenticator-libpam]# make install

10.測試Google Authenticator雙因認證

[root@freeradius ~]# su - testuser@interpay.com.tw 
Creating home directory for adtom@eden.com. 
[testuser@interpay.com.tw@freeradius ~]$ google-authenticator 
Do you want authentication tokens to be time-based (y/n) y   \\按y 會接著產生QR Code 
Warning: pasting the following URL into your browser exposes the OTP secret to Google:

https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://tot p/adtom@eden.com@centos7radius%3Fsecret%3DWASLQBOJ7SC5CWN3CB RT62AMOY%26issuer%3Dcentos7radius


\\手機要下載Google Authenticator APP,並掃描上述方法產生的QR Code
Your new secret key is: WASLQBOJ7SC5CWN3CBRT62AMOY Enter code from app (-1 to skip): -1
633617
Code confirmed 
Your emergency scratch codes are: 
 43322639 
 34705877 
 32173950 
 41646850 
 82907757 
Do you want me to update your "/home/adtom@eden.com/.google_authenticator" file? (y/n)
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, 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)
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
\\接下來一路選y就好了

11.調整PAM設定檔
[testuser@interpay.com.tw@freeradius ~]$ su - root 
Password: 
[root@freeradius testuser@interpay.com.tw]# vi /etc/pam.d/radiusd 
#%PAM-1.0 #auth include password-auth 
#account required pam_nologin.so 
#account include password-auth 
#password include password-auth 
#session include password-auth 
\\把上面的設定取消註解並改成下面的
auth requisite /usr/local/lib/security/pam_google_authenticator.so forward_pass 
auth required pam_sss.so use_first_pass 
account required pam_nologin.so 
account include password-auth 
session include password-auth

12.測試AD帳號搭配Google Authenticator雙因認證

[testuser@interpay.com.tw@freeradius ~]$ radtest testuser@interpay.com.tw 1qaz@WSX696193 localhost 0 testing123
Sent Access-Request Id 71 from 0.0.0.0:39594 to 127.0.0.1:1812 length 94
User-Name = "testuser@interpay.com.tw"
User-Password = "1qaz@WSX696193"
NAS-IP-Address = 10.113.112.10
NAS-Port = 0
Message-Authenticator = 0x00
Cleartext-Password = "1qaz@WSX696193"
Received Access-Accept Id 71 from 127.0.0.1:1812 to 0.0.0.0:0 length 20
\\這關得要過才能在接下來的Fortigate上設定下一步

13.帳號已簡易方式登入

為了避免登入帳號後面帶很長的domain,增加登入的麻煩和輸入錯誤機率,這邊要先設定sssd.conf,讓帳號只需要前綴。例如,我的ad帳號是testuser@interpay.com.tw,希望登入的時候只要輸入testuser,而不是落落長的testuser@interpay.com.tw。

[root@freeradius /]# vi /etc/sssd/sssd.conf

[sssd]
domains = interpay.com.tw
config_file_version = 2
services = nss, pam
default_domain_suffix = interpay.com.tw
[domain/interpay.com.tw]
ad_domain = interpay.com.tw
krb5_realm = INTERPAY.COM.TW
realmd_tags = manages-system joined-with-samba
cache_credentials = True
id_provider = ad
krb5_store_password_if_offline = True
default_shell = /bin/bash
ldap_id_mapping = True
use_fully_qualified_names = True
fallback_homedir = /home/%u@%d
access_provider = simple \\這邊要填上simple
simple_allow_group = MIS  \\這段要自己加,可以指定AD群組,讓其成員登入不帶domain
simple_allow_users = testuser \\這段也要自己加,直接指定帳號登入不需要帶domain

14.Fortigate與Freeradius做連線

連線設定的secret要對照前面client.conf裡面的的配置

也可以測試一下AD+radius認證

要跟freeradius做串連,Fortigate上面也要新增帳號才行

這個帳號必須跟前面在Freeradius上測試通過的帳號一致,不能有誤差

這邊就不需要填任何資料,直接下一步完成,然後就可以測試VPN了




優點:
1.帳號串AD減少使用者記憶和不便,管理者也只需要顧好AD帳號。
2.OTP是免費的認證,節省成本,且符合稽核規定。
缺點:
1.Freeradius是Open Source資源,架設和維護都缺乏官方支援,從零到有需付出時間成本。
2.Google Authenticator認證方式對部分使用者來說不容易理解。


整體來說建立帳號一點也不難,主要是要如何讓VPN能夠做到雙認證,因VPN有資安的基本需求,不可能不做雙認證,偏偏這個技術是最麻煩的,也因此學習到一些觀念及重點。

沒有留言:

張貼留言

【當兵回憶】八里連

 在我心目中新兵日記是天花板等級的軍教片,並不是說沒有比它更好的作品,只是因為它的在拍攝的背景和時間,也正是我在服兵役的同時,老婆很多次好奇問我,為什麼我總是放在YouTube的新兵日記直播,我都說我只放在有個聲音在,我沒有真正在看,但這行為仔細想想也好多年了吧!台詞聽到我都會背...