Skip to content

Nuface Blog

隨意隨手記 Casual Notes

Menu
  • Home
  • About
  • Services
  • Blog
  • Contact
  • Privacy Policy
  • Login
Menu

OPNsense GeoIP 設定與應用教學|以國家為單位的防火牆過濾策略

Posted on 2025-11-102025-11-11 by Rico

🧠 一、什麼是 GeoIP?

在企業網路安全管理中,我們常常會遇到這樣的需求:

「我想封鎖來自特定國家的連線,或只允許某些國家能登入 VPN。」

這時,OPNsense 的 GeoIP 功能就派上用場。
它能根據 IP 所屬的地理位置(國家/地區) 建立動態 IP 清單,
讓防火牆能夠用「國家」為單位進行過濾、允許或封鎖。


⚙️ 二、GeoIP 的原理與資料來源

  • OPNsense 內建使用 MaxMind GeoLite2 或 Zenarmor 內建的地理 IP 資料庫。
  • 系統會下載 IP 對應國家的資料檔 (GeoLite2-Country.mmdb),
    並定期更新,確保涵蓋最新的網段變化。
  • 建立別名(Alias)時,系統會根據你選的國家自動生成對應的 IP 集合。

📌 簡單說:

你只要建立一個「GeoIP 別名」,例如 BLOCK_CN_RU,
OPNsense 就會自動抓取中國與俄羅斯的所有 IP 段,
之後在防火牆規則中選這個別名,就能一次封鎖整個國家。


🧩 三、GeoIP 的設定步驟

步驟 1️⃣:取得 MaxMind 授權金鑰

  1. 到 MaxMind 官方網站 註冊免費帳號。
  2. 登入後建立一組 License Key(GeoLite2 專用)。

步驟 2️⃣:在 OPNsense 中啟用 GeoIP

  1. 進入 防火牆 → 別名 → GeoIP 設定
  2. 輸入:https://AccountID:LicenseKey@download.maxmind.com/geoip/databases/GeoLite2-Country-CSV/download?suffix=zip
  3. 點擊「套用 (Apply)」,系統會自動下載並更新 GeoIP 資料庫。

步驟 3️⃣:建立 GeoIP 別名

  1. 前往 防火牆 → 別名 → 新增
  2. 類型選擇 GeoIP
  3. 選擇欲包含的國家,例如:
    • CN(中國)
    • RU(俄羅斯)
    • TW(台灣)
  4. 儲存後,系統會自動產生對應 IP 清單。

🔒 四、實際應用案例

案例 1:封鎖特定國家的連線

別名名稱: BLOCK_CN_RU
類型: GeoIP
國家: China、Russia

防火牆規則:

Action: Block
Source: BLOCK_CN_RU
Destination: any

→ 一次封鎖整個國家 IP 範圍,防止海外掃描與攻擊。


案例 2:僅允許特定國家登入 VPN

別名名稱: VPN_ALLOWED
國家: Taiwan、Malaysia、Thailand

規則設定:

Action: Pass
Source: VPN_ALLOWED
Destination: WAN address (VPN Port)

→ 只允許特定國家用戶連線至 VPN 伺服器。


案例 3:流量分析應用

建立多個 GeoIP 別名:

  • ASIA_COUNTRIES
  • EUROPE_COUNTRIES
    然後透過 Insight 報表觀察不同地區的流量趨勢,
    作為頻寬規劃與安全策略參考。

⚡ 五、最佳實務與注意事項

項目建議
更新頻率建議每週或每月自動更新 GeoIP 資料庫。
效能GeoIP 建立大型 pf table,建議記憶體 8GB 以上。
使用範圍適用於 inbound/outbound、VPN、NAT 規則。
封鎖策略可建立「BLOCK_ALL_EXCEPT_TW」以白名單方式放行。
整合黑名單可與 URL Table 結合,加強威脅防禦能力。

✅ 六、結論

GeoIP 是 OPNsense 中最實用的國家級封鎖與控管工具。

透過簡單設定,你就能:

  • 以國家為單位控制連線方向;
  • 降低外部攻擊風險;
  • 提升 VPN 與伺服器的安全性;
  • 搭配自動更新機制,免除人工維護負擔。

📘 Meta Description

本文介紹 OPNsense GeoIP 功能,說明如何啟用 MaxMind 資料庫、建立國家別 IP 清單,
並透過防火牆別名實現以國家為單位的封鎖與允許策略。

Recent Posts

  • Postfix + Let’s Encrypt + BIND9 + DANE Fully Automated TLSA Update Guide
  • Postfix + Let’s Encrypt + BIND9 + DANE TLSA 指紋自動更新完整教學
  • Deploying DANE in Postfix
  • 如何在 Postfix 中部署 DANE
  • DANE: DNSSEC-Based TLS Protection

Recent Comments

  1. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on High Availability Architecture, Failover, GeoDNS, Monitoring, and Email Abuse Automation (SOAR)
  2. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on MariaDB + PostfixAdmin: The Core of Virtual Domain & Mailbox Management
  3. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on Daily Operations, Monitoring, and Performance Tuning for an Enterprise Mail System
  4. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on Final Chapter: Complete Troubleshooting Guide & Frequently Asked Questions (FAQ)
  5. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on Network Architecture, DNS Configuration, TLS Design, and Postfix/Dovecot SNI Explained

Archives

  • December 2025
  • November 2025
  • October 2025

Categories

  • AI
  • Apache
  • Cybersecurity
  • Database
  • DNS
  • Docker
  • Fail2Ban
  • FileSystem
  • Firewall
  • Linux
  • LLM
  • Mail
  • N8N
  • OpenLdap
  • OPNsense
  • PHP
  • QoS
  • Samba
  • Switch
  • Virtualization
  • VPN
  • WordPress
© 2025 Nuface Blog | Powered by Superbs Personal Blog theme