在阿里雲CentOS 7.4 簡單快速搭建Strongswan IKEv2類型 教程模板


轉載:https://blog.csdn.net/qq_29364417/article/details/80713440

1、安裝strongswan

yum install strongswan

官網下載:https://pkgs.org/download/strongswan

如果提示沒有包,到下方地址下載,然后使用 yum install strongswan-5.7.2-1.el7.aarch64.rpm 安裝

鏈接: https://pan.baidu.com/s/1VcaOwzHutH9mlAE6lMu2HQ 密碼: 5ofb

2、創建證書

strongswan pki --gen --outform pem > ca.key.pem
strongswan pki --self --in ca.key.pem --dn "C=CN, O=one, CN=one t CA" --ca --lifetime 3650 --outform pem > ca.cert.pem
strongswan pki --gen --outform pem > server.key.pem
strongswan pki --pub --in server.key.pem --outform pem > server.pub.pem
strongswan pki --pub --in server.key.pem | strongswan pki --issue --lifetime 3601 --cacert ca.cert.pem --cakey ca.key.pem --dn "C=CN, O=one, CN=one t CA" --san="你的服務器公網ip" --flag serverAuth --flag ikeIntermediate --outform pem > server.cert.pem

3、安裝證書

cp -r ca.key.pem /etc/strongswan/ipsec.d/private/
cp -r ca.cert.pem /etc/strongswan/ipsec.d/cacerts/
cp -r server.cert.pem /etc/strongswan/ipsec.d/certs/
cp -r server.pub.pem /etc/strongswan/ipsec.d/certs/
cp -r server.key.pem /etc/strongswan/ipsec.d/private/

4、配置

vi /etc/strongswan/ipsec.conf
config setup  
    uniqueids=never #允許多個客戶端使用同一個證書
 
conn %default  #定義連接項, 命名為 %default 所有連接都會繼承它
    compress = yes #是否啟用壓縮, yes 表示如果支持壓縮會啟用.
    dpdaction = clear #當意外斷開后嘗試的操作, hold, 保持並重連直到超時.
    dpddelay = 30s #意外斷開后嘗試重連時長
    dpdtimeout = 60s #意外斷開后超時時長, 只對 IKEv1 起作用
    inactivity = 300s #閑置時長,超過后斷開連接.
    leftdns = 8.8.8.8,8.8.4.4 #指定服務端與客戶端的dns, 多個用","分隔
    rightdns = 8.8.8.8,8.8.4.4
 
conn IKEv2-BASE
    leftca = "C=CN, O=one, CN=one t CA" #服務器端根證書DN名稱,與 --dn 內容一致 
    leftsendcert = always #是否發送服務器證書到客戶端
    rightsendcert = never #客戶端不發送證書
 
conn IKEv2-EAP  
    keyexchange=ikev2       #默認的密鑰交換算法, ike 為自動, 優先使用 IKEv2
    left=%any       #服務器端標識,%any表示任意  
    leftid= 你的服務器公網ip     #服務器端ID標識,你的服務器公網ip  
    leftsubnet=0.0.0.0/0        #服務器端虛擬ip, 0.0.0.0/0表示通配.  
    leftcert = server.cert.pem     #服務器端證書  
    leftauth=pubkey     #服務器校驗方式,使用證書  
    right=%any      #客戶端標識,%any表示任意  
    rightsourceip = 20.1.0.0/16    #客戶端IP地址分配范圍  
    rightauth=eap-mschapv2  #eap-md5#客戶端校驗方式#KEv2 EAP(Username/Password)   
    also=IKEv2-BASE
    eap_identity = %any #指定客戶端eap id
    rekey = no #不自動重置密鑰
    fragmentation = yes #開啟IKE 消息分片
    auto = add  #當服務啟動時, 應該如何處理這個連接項. add 添加到連接表中.
 
#ios 和 mac Psk連接,無需證書 (EAP賬號密碼及psk)
#android Psk 連接,無需證書(XAUTH賬戶密碼及psk)
conn Android_Ios_Mac_XauthPSK
     keyexchange=ikev1
     ike=aes128-aes256-sha1-modp3072-modp2048,3des-sha1-md5-modp1024,aes256-sha512-modp4096,aes128-sha256-modp3072
     esp=aes128-sha1,aes256-sha256_96,3des-sha1,aes256-sha1
     left=%defaultroute
     leftauth=psk
     leftsubnet=0.0.0.0/0
     right=%any
     rightauth=psk
     rightauth2=xauth
     rightsourceip=20.1.0.0/16
     auto=add

5、修改配置 (舊版,可選)

vi /etc/strongswan/strongswan.d/charon.conf
charon {
    duplicheck_enable = no #同時連接多個設備,把冗余檢查關閉.
    i_dont_care_about_security_and_use_aggressive_mode_psk = yes
    # windows 公用 dns
    dns1 = 8.8.8.8
    dns2 = 8.8.4.4
 
    #以下是日志輸出, 生產環境請關閉.
    filelog {
        /var/log/charon.log {
            # add a timestamp prefix
            time_format = %b %e %T
            # prepend connection name, simplifies grepping
            ike_name = yes
            # overwrite existing files
            append = no
            # increase default loglevel for all daemon subsystems
            default = 1
            # flush each line to disk
            flush_line = yes
        }
    }
}

5. 修改配置 (新版)

vi /etc/strongswan/strongswan.conf
charon {
        load_modular = yes
        i_dont_care_about_security_and_use_aggressive_mode_psk = yes
        compress = yes
        plugins {
                 duplicheck{
                            enable=no
                 }
                include strongswan.d/charon/*.conf
        }
 
}
include strongswan.d/*.conf

5-1.(新版)打開另一連接窗口 顯示strongswan 所產生的日志(兩種)

journalctl -f -u strongswan
tail -f /var/log/messages

6、配置用戶和密碼

vi /etc/strongswan/ipsec.secrets
# ipsec.secrets - strongSwan IPsec secrets file
#使用證書驗證時的服務器端私鑰
#格式 : RSA <private key file> [ <passphrase> | %prompt ]
: RSA server.key.pem
 
#使用預設加密密鑰, 越長越好
#格式 [ <id selectors> ] : PSK <secret>
: PSK "pOneAA123456"
 
#EAP 方式, 格式同 psk 相同 (用戶名/密碼 例:oneAA/oneTT)
eOneAA : EAP "eOneTT"
 
#XAUTH 方式, 只適用於 IKEv1
#格式 [ <servername> ] <username> : XAUTH "<password>"
xOneAA : XAUTH "xOneTT"

7、開啟內核轉發

vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
net.ipv6.conf.all.forwarding=1
sysctl -p

 配置 偽裝 及 添加隧道:( GCP專用,阿里雲不用配置此項)

① 添加udp 端口

firewall-cmd --permanent --add-port=500-4500/udp
firewall-cmd --permanent --add-masquerade

② 調整MTU-MSS、添加GRE,否則隧道失敗:

firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 -p gre -j ACCEPT
firewall-cmd --permanent --direct --add-rule ipv4 filter FORWARD 0 -p tcp -i eth0 -j TCPMSS --syn --set-mss 1299

查看是否已添加防火牆參數

firewall-cmd --get-active
firewall-cmd --get-default
vi /etc/firewalld/direct.xml

8、配置防火 (阿里雲)

vi /etc/firewalld/zones/public.xml
<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Public</short>
  <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  <interface name="eth0"/>
  <service name="ssh"/>
  <service name="dhcpv6-client"/>
  <service name="ipsec"/>
  <port protocol="tcp" port="1723"/>
  <port protocol="tcp" port="47"/>
    <port protocol="tcp" port="1701"/>
    <port protocol="tcp" port="22"/>
    <masquerade/>
    <rule family="ipv4">
     <source address="10.1.0.0/16"/>
      <masquerade/>                                                                                                                                                                                    
    </rule>
    <rule family="ipv4">
      <source address="10.1.0.0/16"/>
      <forward-port to-port="4500" protocol="udp" port="4500"/>
    </rule>
    <rule family="ipv4">
      <source address="10.1.0.0/16"/>
     <forward-port to-port="500" protocol="udp" port="500"/>
   </rule>
  <masquerade/>
</zone>

9、開啟 防火牆/strongswan 以及 自動啟動

systemctl enable firewalld
systemctl start firewalld
systemctl enable strongswan
systemctl start strongswan

10、阿里雲開放端口
登錄阿里雲管理控制台- -> 雲服務器ECS- ->網絡和安全- ->安全組- ->添加安全組規則:
授權策略:允許
協議類型:自定義UDP
端口范圍:500/4500
授權類型:地址段訪問
授權對象:0.0.0.0/0
優先級:100
描述:隨便填

注意!添加完成后必須 重啟 服務器

XauthPSK 連接: Android 使用 XAUTH 賬戶密碼及psk 連接
                             Mac 和 Ios 使用 EAP 賬戶密碼及psk 連接


 
        

11、證書安裝及連接,用ftp工具(例:FileZilla)下載 /etc/strongswan/ipsec.d/cacerts/ca.cert.pem 證書到本地。
ios證書安裝:將之前創建的 ca.cert.pem 用 ftp 導出 , 寫郵件以附件的方式發到郵箱, 在ios Safari瀏覽器登錄郵箱, 下載附件, 安裝證書。
步驟:
例:類型:IKEv2
      描述:隨便填
      服務器:你的服務器公網ip
      遠程ID:你的服務器公網ip
      本地ID:不用填,空着
      選擇- ->用戶名,填寫-用戶名-密碼 - ->點擊--完成

mac證書安裝:雙擊 ca.cert.pem -->選中你的證書-->顯示簡介-->信任-->始終信任(然后會彈框填寫mac登錄密碼)。
步驟:系統編好設置- ->網絡- ->左側點擊+號- ->接口:IKEv2 - ->服務名稱:隨便填- ->點擊 創建。
接下來填寫賬戶密碼地址 例:服務器地址:你的服務器公網ip                                                           
                                        遠程ID:你的服務器公網ip                                                           
                                        本地ID:不用填,空着

                                        點擊- -鑒定設置- ->選擇- ->用戶名,填寫-用戶名-密碼 - ->點擊--連接

android:去strongswan官網下載安裝 例:https://download.strongswan.org/Android/strongSwan-1.9.6.apk 
                                                            或:https://download.csdn.net/download/qq_29364417/10482582
                                     或者編譯源碼:https://github.com/strongswan/strongswan/tree/master/src/frontends/android      
步驟:右上角選項-->CA證書-->再選擇右上角選項-->導入證書-->找到ca.cert.pem點擊即可。
         回到主界面-->添加配置-->例:服務器地址:你的服務器公網ip
                                                          類型:IKEv2 EAP(用戶名/密碼),填寫用戶名和密碼
                                                           CA證書:選擇剛才導入的ca.cert.pem證書
                                                           點擊右上角--保存

                                                           回到主界面--點擊配置連接即可


Windows10 連接的坑:

1. windows的vpn虛擬網卡默認mtu是1400

現象:如果服務端的mtu小於1400的話,會出現能ping通地址,但是網頁不能打開的問題
解決辦法:修改vpn虛擬網卡mtu值
連接vpn后,以管理員身份執行以下命令

netsh interface ipv4 set subinterface "vpn名稱" mtu=1350  store=persistent

2. windows默認不支持DH2048_AES256協商協議

現象:提示策略匹配錯誤
解決辦法:添加注冊表項,不需要重啟生效

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Rasman\Parameters里新建類型為DWORD的NegotiateDH2048_AES256 鍵,值設置為1

0 禁用AES-256-CBC和MODP-2048
1 啟用AES-256-CBC和MODP-2048
2 強制使用AES-256-CBC和MODP-2048

3. windows默認不支持Symmetric NAT類型路由器后的ikev2服務器

現象:提示809錯誤,或者提示路由器不支持nat
解決辦法:添加注冊表項,並需要重啟生效

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\PolicyAgent里新建類型為DWORD的AssumeUDPEncapsulationContextOnSendRule 鍵,值設置為2

0
A value of 0 (zero) configures Windows so that it cannot establish security associations with servers that are located behind NAT devices. This is the default value.
1
A value of 1 configures Windows so that it can establish security associations with servers that are located behind NAT devices.
2
A value of 2 configures Windows so that it can establish security associations when both the server and the Windows Vista-based or Windows Server 2008-based VPN client computer are behind NAT devices.

4. windows手動添加的vpn默認沒有開啟默認路由

現象:連接成功,但是對外ip沒有改變
解決辦法:手動添加修改或者使用powershell新建vpn連接,手動路徑:控制面板網絡和 Internet網絡連接,vpn網卡,屬性,網絡,ipv4,屬性,高級,勾選在遠程網絡上使用默認網關

5. windows10默認開啟了smart multi-homed name resolution功能

現象:有線網絡下連接vpn后不使用vpn的dns
解決辦法:修改vpn網卡的躍點數,小於有線網卡即可。腳本自動解決待更新

批處理腳本如下,實際是使用批處理調用powershell命令,由於需要修改注冊表,需要管理員權限運行
@echo off&PUSHD %~DP0 &TITLE ikev2 VPN安裝/卸載程序
mode con cols=160 lines=50
set TempFile_Name=%SystemRoot%\System32\BatTestUACin_SysRt%Random%.batemp
( echo "BAT Test UAC in Temp" >%TempFile_Name% ) 1>nul 2>nul
if exist %TempFile_Name% (
del %TempFile_Name% 1>nul 2>nul&&goto :address
) else (
echo;請以管理員身份運行,按任意鍵退出 &&goto :end
)

:address
cls
echo;請輸入辦公地點:
echo;1:北京
echo;2:成都
echo;3:重慶
set/p choose1=請輸入選項並按回車:
echo %choose1%|findstr /i "[123]">nul&&goto :install
goto :address

:install
cls
echo;請選擇安裝或者卸載VPN(ikev2):
echo;i:安裝
echo;u:卸載
echo;q:退出
set/p choose2=請輸入選項並按回車:
echo %choose2%|findstr /i "[iuq]">nul&&goto :%choose1%%choose2%
goto :install

:1i
cls
echo;安裝北京VPN
echo;===========================
echo;***步驟1:刪除同名VPN***
(powershell -Command "& {Remove-VpnConnection -Name "北京辦公室" -Force -PassThru;}") 1>nul 2>nul
echo;成功
echo;
echo;***步驟2:安裝新的VPN***
powershell -Command "& {Add-VpnConnection -Name "北京辦公室" -ServerAddress "beijing.example.com" -AuthenticationMethod "Eap" -EncryptionLevel "Maximum" -RememberCredential -TunnelType "Ikev2" -PassThru;}"
echo;成功
echo;
echo;***步驟3:清理注冊表***
(powershell -Command "& {Remove-ItemProperty HKLM:\System\CurrentControlSet\Services\Rasman\Parameters -name "NegotiateDH2048_AES256"; Remove-ItemProperty HKLM:\System\CurrentControlSet\Services\PolicyAgent -name "AssumeUDPEncapsulationContextOnSendRule ";}") 1>nul 2>nul
echo;成功
echo;
echo;***步驟4:添加注冊表***
(powershell -Command "& {New-ItemProperty HKLM:\System\CurrentControlSet\Services\Rasman\Parameters -name "NegotiateDH2048_AES256" -value 1 -propertyType dword; New-ItemProperty HKLM:\System\CurrentControlSet\Services\PolicyAgent -name "AssumeUDPEncapsulationContextOnSendRule " -value 2 -propertyType dword;}") 1>nul 2>nul
echo;成功
echo;
echo;***步驟5:設置注冊表***
powershell -Command "& {Set-ItemProperty HKLM:\System\CurrentControlSet\Services\Rasman\Parameters -name "NegotiateDH2048_AES256" -value 1; Set-ItemProperty HKLM:\System\CurrentControlSet\Services\PolicyAgent -name "AssumeUDPEncapsulationContextOnSendRule " -value 2;}"
echo;成功
echo;
echo;***步驟6:重啟電腦***
echo;按任意鍵重啟電腦,或按右上角 X 退出后手動重啟電腦
pause>nul
shutdown -r -t 0
goto :end

:1u
cls
echo;卸載北京VPN
echo;===========================
(powershell -Command "& {Remove-VpnConnection -Name "北京辦公室" -Force -PassThru;}") 1>nul 2>nul
echo;成功,按任意鍵退出...
goto :end

:2i
cls
echo;安裝成都VPN
echo;===========================
echo;***步驟1:刪除同名VPN***
(powershell -Command "& {Remove-VpnConnection -Name "成都辦公室" -Force -PassThru;}") 1>nul 2>nul
echo;成功
echo;
echo;***步驟2:安裝新的VPN***
powershell -Command "& {Add-VpnConnection -Name "成都辦公室" -ServerAddress "chengdu.example.com" -AuthenticationMethod "Eap" -EncryptionLevel "Maximum" -RememberCredential -TunnelType "Ikev2" -PassThru;}"
echo;成功
echo;
echo;***步驟3:清理注冊表***
(powershell -Command "& {Remove-ItemProperty HKLM:\System\CurrentControlSet\Services\Rasman\Parameters -name "NegotiateDH2048_AES256"; Remove-ItemProperty HKLM:\System\CurrentControlSet\Services\PolicyAgent -name "AssumeUDPEncapsulationContextOnSendRule ";}") 1>nul 2>nul
echo;成功
echo;
echo;***步驟4:添加注冊表***
(powershell -Command "& {New-ItemProperty HKLM:\System\CurrentControlSet\Services\Rasman\Parameters -name "NegotiateDH2048_AES256" -value 1 -propertyType dword; New-ItemProperty HKLM:\System\CurrentControlSet\Services\PolicyAgent -name "AssumeUDPEncapsulationContextOnSendRule " -value 2 -propertyType dword;}") 1>nul 2>nul
echo;成功
echo;
echo;***步驟5:設置注冊表***
powershell -Command "& {Set-ItemProperty HKLM:\System\CurrentControlSet\Services\Rasman\Parameters -name "NegotiateDH2048_AES256" -value 1; Set-ItemProperty HKLM:\System\CurrentControlSet\Services\PolicyAgent -name "AssumeUDPEncapsulationContextOnSendRule " -value 2;}"
echo;成功
echo;
echo;***步驟6:重啟電腦***
echo;按任意鍵重啟電腦,或按右上角 X 退出后手動重啟電腦
pause>nul
shutdown -r -t 0
goto :end

:2u
cls
echo;卸載成都VPN
echo;===========================
(powershell -Command "& {Remove-VpnConnection -Name "成都辦公室" -Force -PassThru;}") 1>nul 2>nul
echo;成功,按任意鍵退出...
goto :end

:3i
cls
echo;安裝重慶VPN
echo;===========================
echo;***步驟1:刪除同名VPN***
(powershell -Command "& {Remove-VpnConnection -Name "重慶辦公室" -Force -PassThru;}") 1>nul 2>nul
echo;成功
echo;
echo;***步驟2:安裝新的VPN***
powershell -Command "& {Add-VpnConnection -Name "重慶辦公室" -ServerAddress "chongqing.example.com" -AuthenticationMethod "Eap" -EncryptionLevel "Maximum" -RememberCredential -TunnelType "Ikev2" -PassThru;}"
echo;成功
echo;
echo;***步驟3:清理注冊表***
(powershell -Command "& {Remove-ItemProperty HKLM:\System\CurrentControlSet\Services\Rasman\Parameters -name "NegotiateDH2048_AES256"; Remove-ItemProperty HKLM:\System\CurrentControlSet\Services\PolicyAgent -name "AssumeUDPEncapsulationContextOnSendRule ";}") 1>nul 2>nul
echo;成功
echo;
echo;***步驟4:添加注冊表***
(powershell -Command "& {New-ItemProperty HKLM:\System\CurrentControlSet\Services\Rasman\Parameters -name "NegotiateDH2048_AES256" -value 1 -propertyType dword; New-ItemProperty HKLM:\System\CurrentControlSet\Services\PolicyAgent -name "AssumeUDPEncapsulationContextOnSendRule " -value 2 -propertyType dword;}") 1>nul 2>nul
echo;成功
echo;
echo;***步驟5:設置注冊表***
powershell -Command "& {Set-ItemProperty HKLM:\System\CurrentControlSet\Services\Rasman\Parameters -name "NegotiateDH2048_AES256" -value 1; Set-ItemProperty HKLM:\System\CurrentControlSet\Services\PolicyAgent -name "AssumeUDPEncapsulationContextOnSendRule " -value 2;}"
echo;成功
echo;
echo;***步驟6:重啟電腦***
echo;按任意鍵重啟電腦,或按右上角 X 退出后手動重啟電腦
pause>nul
shutdown -r -t 0
goto :end

:3u
cls
echo;卸載重慶VPN
echo;===========================
(powershell -Command "& {Remove-VpnConnection -Name "重慶辦公室" -Force -PassThru;}") 1>nul 2>nul
echo;成功,按任意鍵退出...
goto :end

:end
pause>nul
install.cmd

powershell腳本,需要以管理員身份運行powershell然后執行install.ps1

Remove-VpnConnection -Name "成都辦公室" -Force

Add-VpnConnection -Name "成都辦公室" -ServerAddress "chengdu.example.com" -AuthenticationMethod "Eap" -EncryptionLevel "Maximum" -RememberCredential -TunnelType "Ikev2"

Remove-ItemProperty HKLM:\System\CurrentControlSet\Services\Rasman\Parameters -name "NegotiateDH2048_AES256"

Remove-ItemProperty HKLM:\System\CurrentControlSet\Services\PolicyAgent -name "AssumeUDPEncapsulationContextOnSendRule"

New-ItemProperty HKLM:\System\CurrentControlSet\Services\Rasman\Parameters -name "NegotiateDH2048_AES256" -value 1 -propertyType dword

New-ItemProperty HKLM:\System\CurrentControlSet\Services\PolicyAgent -name "AssumeUDPEncapsulationContextOnSendRule" -value 2 -propertyType dword

Set-ItemProperty HKLM:\System\CurrentControlSet\Services\Rasman\Parameters -name "NegotiateDH2048_AES256" -value 1

Set-ItemProperty HKLM:\System\CurrentControlSet\Services\PolicyAgent -name "AssumeUDPEncapsulationContextOnSendRule " -value 2
install.ps1

6. 安裝證書的坑

  1. 運行-> 輸入mmc->添加/刪除單元

 

  2.找到證書->添加

 

 3.坑來了,一定要選擇計算機賬戶!!!(否則導入證書無效會出現 IKE身份驗證憑證不可接受 錯誤)

 

4. 后面直接選默認然后完成

5. 導入證書,導入的位置一定要正確

 

 6. 選擇證書導入,找證書的時候選擇所有文件

 


免責聲明!

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



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