[toc] ## 傳輸層安全(Transport Layer Security, TLS) ### 1. TLS 基本概念 * TLS 是部署最廣的網路安全協定,運作在傳輸層之上(HTTPS) * 提供三大功能: 1. 機密性:對稱式加密 2. 完整性:密碼學雜湊(MAC) 3. 身分驗證:公鑰密碼 ### 2. TLS 需要的四大程序 1. **Handshake** 雙方用證書與私鑰互相驗證、交換或產生共享密鑰。 2. **Key Derivation** 由共享密鑰(master secret)推導出一系列 session keys。 3. **Data Transfer** 資料以 record 為單位傳送,並非一次性的傳輸。 4. **Connection Closure** 使用特定訊息安全地關閉連線。 ### 3. 教學版協定 t-tls(toy TLS protocol) #### Initial Handshake * Bob 建立 TCP 連線後驗證 Alice。 * Bob 將 master secret(MS)送給 Alice,用於產生之後的 session keys。 * 整體需要 3 RTT(包含 TCP 三向交握),延遲較高。 ### 4. t-tls 的金鑰設計(cryptographic keys) * 不應使用同一個金鑰執行多種密碼學功能。 * TLS 會區分四種金鑰 ($K$: Encryption Key, $M$: MAC Key): * $Kc$:Client → Server 加密金鑰 * $Mc$:Client → Server MAC 金鑰 * $Ks$:Server → Client 加密金鑰 * $Ms$:Server → Client MAC 金鑰 * 所有金鑰皆由 KDF(Key Derivation Function)從 Master Secret 派生。 ### 5. t-tls 的資料加密 ![image](https://hackmd.io/_uploads/ryySVxAxbe.png) * 資料以 record 格式包裝,含加密內容與 MAC。 * 使用對稱式加密與 MAC 共同確保機密性與完整性。 * 避免重複使用 key,並搭配 record 編號避免重放攻擊。 * 為了解決重排序和 重放攻擊,TLS 在 MAC 的計算中會包含 TLS 序列號 。 * 為防止 truncation attack(攻擊者偽造 TCP 連接關閉分段),t-tls 引入了 record type,其中一種類型用於表示連接關閉。MAC 的計算現在會包含數據、類型和序列號 ![image](https://hackmd.io/_uploads/Hk2-9C7GWg.png) ### 6. TLS 1.3 的加密套件(Cipher Suite) Cipher Suite: 可以被用來做 key generation, 加密, MAC, 數位簽章 * TLS 1.3 大幅簡化 cipher suite: * 只有 5 種可選(TLS 1.2 有 37 種)。 * 必須使用 Diffie-Hellman(DH)作為金鑰交換方法(不再使用 RSA key exchange)。 * 使用「Authenticated Encryption」,同時實作加密與驗證。 * 大多基於 AES,加密驗證採用 SHA-256 或 SHA-284。 ### TLS 1.3:1-RTT Handshake ![image](https://hackmd.io/_uploads/Bk0wqR7MWx.png) 流程: 1. Client Hello * 提供支援的 cipher suites * 提供 DH 協議與參數 2. Server Hello * 選擇 cipher suite * 回傳伺服器的 DH 參數 * 提供 server certificate 3. Client * 驗證證書 * 基於 DH 產生共享密鑰 * 立即可發出第一個 HTTPS GET(握手後即可傳輸資料) 相較 TLS 1.2,可在 1 RTT 內完成握手,大幅減少延遲。 ## TLS 1.3:0-RTT(Zero Round-Trip Time) ![image](https://hackmd.io/_uploads/SkIj5CQGbe.png) ### 0-RTT 的核心概念 * Client Hello 中可夾帶「已加密的應用資料」。 * 使用之前連線的 **resumption master secret**。 * Server 在回傳 Server Hello 時,也可直接回應加密資料。 ### 優點 * 可在 0 RTT 下傳送第一份資料,達到最低延遲。 ### 主要問題 * 易受 replay attack(重放攻擊)影響。 * 只適用於「不改變伺服器狀態」的請求,如 HTTP GET。 ## IPsec:Network-Layer Security 為每個 IP 封包提供: * 加密(confidentiality) * 認證(authentication) * 完整性保護(integrity) ### IPsec 運作模式 #### Transport Mode * 僅加密與認證 **IP payload**。 * IP header 不加密。 * 適用:End-to-End(主機到主機)。 #### Tunnel Mode * 加密 **整個 IP datagram(header + payload)**。 * 封裝於新的 IP header,建立安全隧道。 * 適用:VPN、Gateway-to-Gateway。 ### IPsec 的兩大協定 * AH(Authentication Header) * 有來源認證與 data 完整性,沒加密 * ESP(Encapsulation Security Protocol) * 提供來源認證、完整性與加密。 * 為最常使用的 IPsec 協定。 ## Security Association(SA) ### SA 的意義 * IP 是 connectionless, IPsec 為 connection-oriented,因此雙方需先建立 SA。 * SA 記錄該方向的安全參數,包括: * 加密演算法與金鑰 * MAC 演算法與金鑰 * 序號與生命週期 ### 特性 * SA 為單向(A→B 與 B→A 分開建立)。 * SA 通常由 IKE(Internet Key Exchange)動態建立。 ## SA 的組成 ### SA 識別內容 * SPI(Security Parameter Index) * origin, destination SA interface IP * Security protocol(AH 或 ESP) * encryption key, authentication key * type of integrity check ## TLS 與 IPsec 比較 ### 層級差異 | 項目 | TLS | IPsec | | ------ | ------------- | ----------- | | OSI 層級 | Transport Layer | Network Layer | | 保護對象 | TCP stream | IP datagram | | 使用情境 | Web、應用層協議 | VPN、路由器安全 | | 握手流程 | TLS Handshake | IKE | ## IPsec Datagram 結構(ESP Tunnel Mode) ![image](https://hackmd.io/_uploads/SkEJYgAebl.png) * ESP Header * SPI:讓接收者知道使用哪個 SA。 * Sequence number:避免 replay attack。 * ESP Trailer * Padding:用於 block cipher 對齊。 * Next header:告知上層協定類型。 * ESP Auth * MAC 由 shared secret key 製作,用於 integrity 驗證。 ### 整體流程 * 原始 IP datagram 加上 ESP trailer → 加密 by SA → 前面加入 ESP header → MAC → 加上新 IP header ## Tunnel Mode 在 Router 的操作流程(於 R1) ### 加工步驟 1. 附加 ESP trailer(包含 padding)。 2. 依 SA 使用的演算法與金鑰加密整個 datagram(包含原始 IP header)。 3. 將 ESP header 加到前面。 4. 根據 SA 使用的金鑰與演算法計算認證用的 MAC。 5. 將 MAC 加在後面。 6. 產生新的外層 IP header 指向隧道終點。 ### IPsec Sequence Numbers * 防止攻擊者重播封包,造成流量干擾或服務中斷。 * 新 SA 序號從 0 開始。 * 每送一次封包,序號遞增。 * 接收者使用「sliding window」檢查是否為重複封包。 ### Security Policy Database (SPD) * 記錄每個 datagram 是否需要套用 IPsec。 * SPD 決定「要做什麼」、「是否要加密」、「要用哪個 SA」。 ### Security Association Database (SAD) * 儲存 SA 的所有參數。 * SAD 決定「如何處理」封包。 * 發送端與接收端皆需查詢 SAD: * 發送前依 SA 決定如何加密與加 MAC。 * 接收端依 SPI 查出 SA,進行解密與驗證。 ### 整理常見威脅與保護能力 * Attackers 無法查看封包內容(ESP)。 * 無法修改封包內容而不被偵測(MAC)。 * 無法偽裝來源位址(因認證)。 * 無法重播封包(序號與窗⼝機制)。 ## IKE(Internet Key Exchange) * 早期 IPsec SA 需要**手動建立**(手動配置金鑰、演算法、SPI)。 * 當 VPN 有大量端點(如上百台)時,手動方式完全不可行。 * 因此使用 **IKE** 自動協商: * SA(Security Associations) * 加密與認證演算法 * 金鑰產生與交換 * SPI(Security Parameter Index) ## IKE 的身分驗證方式 ### PSK(Pre-Shared Key) * 雙方事先共享相同秘密(例如密碼或金鑰)。 * 在 IKE 過程中證明「自己知道這個秘密」。 * 用 PSK 建立: * 雙向的 IPsec SA(A→B、B→A) * 每個 SA 自己的加密金鑰與 MAC 金鑰 ### PKI(Public Key Infrastructure) * 每方擁有: * 公鑰 / 私鑰 * 數位證書 * IKE 期間會驗證證書並建立 SA。 * 與 TLS handshake 的設計概念類似。 ## IKE 的兩個階段(Phases) ### Phase 1:建立 IKE SA(ISAKMP SA) * 用來保護後續的 IKE 通訊。 * IKE SA 與 IPsec SA 不同。 * 兩種模式: * **Aggressive Mode**:較少封包,效率高,但身份保護較弱。 * **Main Mode**:更多訊息交換,但提供更強的身份保護。 ### Phase 2:建立 IPsec SAs * 使用 Phase 1 建立好的 IKE SA 來安全地協商 IPsec SA。 * 最終會產生: * 一對 SA(inbound/outbound) * 加密金鑰與 MAC 金鑰 * SPI * 加密演算法與完整性演算法 ### IKE 最主要的任務 * 協商演算法(加密、驗證) * 建立共享秘密(秘密金鑰) * 建立 SPI 編號 * 建立一對 IPsec SA ### 與 IPsec 互動方式 * IKE 負責「協商」、「鑰匙交換」、「身分驗證」。 * IPsec(AH/ESP)負責: * 加密 * 完整性 * 認證 * ESP 可提供完整功能(認證 + 加密),使用最普遍。 ### IPsec 整體運作流程(統整) 1. **SPD 決定是否需要 IPsec**。 2. **查 SAD**:若無 SA → 進入 IKE 流程。 3. **IKE Phase 1**:建立 IKE SA → 保護後續 IKE 訊息。 4. **IKE Phase 2**:協商出兩個 IPsec SA(雙向)。 5. **資料傳輸**:ESP/AH 依 SA 加密、認證封包。 6. **序號機制**:避免 replay。 7. **接收端**依 SPI 查 SAD → 解密 → 驗證 → 上交 IP 層。 ## 802.11: authentication, encryption ![image](https://hackmd.io/_uploads/Bk4cBXe-bl.png) ![image](https://hackmd.io/_uploads/ryR6SQl-Wl.png) ## WiFi ### 到站手機需要做的兩件事 * 手機必須與 access point 建立無線連線(association) * 手機需要被網路驗證其身份後才能正式通訊 ### 步驟 1:Discovery of security capabilities * AP 會告知支援的 authentication 與 encryption 方式 * 裝置向 AP 表示希望使用哪種認證、加密方式 * 雖然雙方開始交換訊息 * **但 mobile 尚未被認證,也沒有任何加密金鑰** ### 步驟 2:Mutual authentication and symmetric key derivation * AS(Authentication Server)與 mobile 有shared common secret(如密碼) #### Mutual authentication * Mobile 與 AS 交換 nonce * 使用 shared secret + nonce + hashing 技術 * 互相驗證對方身份 * nonce -> 避免 replay attack * hashing 確保訊息完整性 (integrity) #### Deriving symmetric session key * Mobile 與 AS 計算出相同的 session key * 用於後續 WiFi 加密 ### WPA3 handshake #### AS → mobile * AS 產生 NonceAS * 傳送給 mobile #### mobile → AS * Mobile 產生 NonceM * 使用: * initial shared secret * NonceAS * NonceM 生成共享 session key KM-AP * 將 NonceM 與 HMAC(NonceAS, shared secret) 傳回 AS #### AS * 使用相同資訊推導 KM-AP * 與 mobile 得到相同 session key * 完成 mutual authentication ### 步驟 3:Shared symmetric key distribution #### AS 產生的 session key(KM-AP) * mobile 與 AS 皆能推導相同 session key #### AS → AP * Authentication Server 將此 session key 分發給 AP * 讓 AP 能與 mobile 使用相同金鑰進行加密通訊 ### 步驟 4:Encrypted communication #### AP、mobile、AS 三方一致金鑰 * Mobile 與 AS 推導相同 session key * AP 從 AS 獲得 session key * AP 與 mobile 之間即可基於此 key 加密 WiFi 通訊 #### Encrypted WiFi link * 之後 mobile–AP 間的無線流量皆以該 session key 加密 * Mobile 也可透過 AP 與遠端主機建立加密連線 ### 802.11 使用的協定(EAP 架構) #### EAP(Extensible Authentication Protocol) * RFC 3748 * 定義手機與 Authentication Server 之間的 end-to-end 認證程序 #### 協定組成 * **EAP over LAN (EAPoL)**:mobile ↔ AP * **RADIUS(UDP/IP)**:AP ↔ AS * **EAP/TLS**:mobile ↔ AS(真正的 mutual authentication) ![image](https://hackmd.io/_uploads/BJqHw7gZbl.png) ![image](https://hackmd.io/_uploads/B11hwmlbWx.png) ## 4G v.s. 5G ### 4G * 認證決策:visited network(MME) * 金鑰:事先共享(SIM 長期金鑰) * IMSI:以明碼傳送 ### 5G * 認證決策:home network * 金鑰:IoT 可不使用事先共享金鑰 * IMSI:使用公鑰加密後再傳送 ## Operational Security: Firewalls and IDS ### Firewalls(防火牆) * 將組織內部網路與外部 Internet 隔離 * 允許部分封包進出 * 阻擋不允許的封包 ### 防火牆的主要用途 1. **防止 DoS 攻擊** * 例如 SYN flooding(建立大量假 TCP 連線耗盡資源) 2. **防止未經授權修改 / 存取內部資料** * 例如攻擊者竄改 CIA 官方首頁 3. **只允許授權使用者或主機連入** ### Firewall 的三種類型 * **Stateless packet filters** * **Stateful packet filters** * **Application gateways** ## Stateless Packet Filtering * 逐一檢查每個封包(packet-by-packet) * 根據以下欄位做決策: * source IP、destination IP * TCP/UDP source、destination port * ICMP type * TCP SYN / ACK bit ### 判斷例子(Security: 2-103) * **Example 1**:阻擋所有 protocol=17(UDP)或 port=23(telnet)→ 所有 UDP 與 telnet 全阻擋 * **Example 2**:阻擋 inbound TCP segments with ACK=0 → 阻擋外部連線,但允許內部對外連線 ### 更多例子(Security: 2-104) PPT 舉出 firewall policy 與對應設定: * 阻擋外出 Web → drop outgoing to port 80 * 僅允許 institution 的 Web server → drop SYN except to port 80 * 防止廣播攻擊(smurf DoS)→ drop ICMP to broadcast * 防止 traceroute → drop outgoing ICMP TTL expired --- ## Access Control Lists(ACL) ACL 是一組規則,自上而下匹配: * action(allow/deny) * source address * dest address * protocol * source port * dest port * flag bits ## Stateful Packet Filtering PPT 指出 stateless “admits packets that make no sense”,例如: * 目的 port=80 * ACK 設 1 * 但沒有已建立的 TCP 連線 ### Stateful firewall 做的事 * 追蹤每條 TCP 連線的 state(SYN、FIN、time-out) * 只允許在現有連線中「合理」的封包 * 若連線超時,後續封包一律拒絕 ### 在 ACL 中加入狀態檢查 * 規則中出現「check connection state table」欄位 ## Application Gateways 檢查 **application-level data**(此為 stateless/stateful 無法做到) 流程: 1. 所有 telnet 必須先通過 application gateway 2. 如果 user 授權,gateway 代表其發起 telnet 到外部主機 3. Router 過濾所有不來自 gateway 的 telnet 封包 ## Limitations of Firewalls & Gateways PPT 指出多個限制: * **IP spoofing**:router 無法知道來源 IP 是否真的正確 * 若多種應用需要特殊處理 → 每個都要自己的 application gateway * client software 必須知道如何使用 gateway(例如設定 proxy) * UDP 常用「all or nothing」策略 → 要嘛全放要嘛全擋 * 安全性與可用性 trade-off * 即使高度保護的站點仍可能遭攻擊 ## Intrusion Detection Systems(IDS) packet filtering 的限制: * 只看 header,不檢查 payload * 不會分析跨封包行為(session correlation) IDS 在做什麼: * Deep packet inspection(比對封包內容與病毒/攻擊字串資料庫) * 分析多重封包之間的關聯 * 偵測: * port scanning * network mapping * DoS attack ## IDS Deployment * Web/FTP/DNS 伺服器放在 **demilitarized zone(DMZ)** * Firewall 阻隔外部 * **IDS sensors** 放在多個位置(不同位置做不同檢查) * 內部網路再做保護 ## 總結 ### Firewalls * three types: **stateless**, **stateful**, **application gateways** * 阻擋 DoS / 未授權修改 / 控制流量 * stateless: 只看 IP/TCP/UDP header * stateful: 看 connection state * gateway: 檢查 application data ## IDS * deep packet inspection * 比對攻擊特徵 * 分析跨封包行為 * 偵測:port scan、network mapping、DoS * 多個 IDS sensor 佈署在 DMZ 與內網