萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 網路安全 >  >> 病毒防護

Linux 核心層封鎖 P2P 流量:Netfilter/iptables + ipp2p/kernel patch 完整實作

問題背景:P2P 佔據頻寬的治理難題

電驢、BitTorrent、Kazaa 等 P2P 協議以動態端口、加密傳輸、協議混淆著稱,傳統 L4 防火牆難以精準識別。本文示範在 Linux 2.4/2.6 核心層透過 connmark + ipp2p/ipt_p2p 實現應用層協議識別與流量控制。

核心補丁與工具鏈準備

元件版本/來源用途
Linux Kernel2.4.20 / 2.6.x基礎核心
patch-o-matic-ng20040609 / 最新 git核心擴展補丁集(含 CONNMARK)
iptables1.2.8 / 1.4.x用戶空間控制工具
ipt_p2p0.3.0a (netfilter.org)核心模組:識別 FastTrack, Gnutella, eDonkey, BitTorrent, DC++, OpenFT
ipp2p0.5c (Eicke Friedrich)核心模組:支援更多 P2P 協議與特徵碼

步驟 1:應用核心補丁啟用 CONNMARK

cd /usr/src
tar xjf patch-o-matic-20040609.tar.bz2
cd patch-o-matic-20040609
KERNEL_DIR=/usr/src/linux-2.4 ./runme extra
# 介面中選擇 CONNMARK (Connection mark tracking) 與 CONNMARK target 支援
# 按 Y 確認,Q 退出

步驟 2:重編核心並安裝

cd /usr/src/linux-2.4
make mrproper
make menuconfig   # 勾選 Networking → Netfilter → Connection mark tracking support
make dep && make bzImage && make modules && make modules_install
make install      # 自動更新 grub.conf
reboot            # 進入新核心

步驟 3:編譯安裝 iptables 1.2.8

tar xjf iptables-1.2.8.tar.bz2
cd iptables-1.2.8
make KERNEL_DIR=/usr/src/linux-2.4
make install KERNEL_DIR=/usr/src/linux-2.4
cp iptables iptables-save iptables-restore /sbin/

步驟 4:編譯載入 ipp2p 模組(推薦,特徵庫較新)

tar xzf ipp2p-0.5c.tar.gz
cd ipp2p-0.5c
# 修正 Makefile 路徑
sed -i 's|/usr/src/linux|/usr/src/linux-2.4|; s|/usr/src/iptables-1.2.7a|/usr/src/iptables-1.2.8|; s|1.2.7a|1.2.8|' Makefile
make
cp libipt_ipp2p.so /lib/iptables/
insmod ipt_ipp2p.ko   # 或 modprobe ipt_ipp2p

步驟 5:實戰規則範例

5.1 封鎖所有已知 P2P 連線請求

iptables -A FORWARD -m ipp2p --ipp2p -j DROP

5.2 僅封鎖 BitTorrent 與 eDonkey

iptables -A FORWARD -m ipp2p --bit --edk -j DROP

5.3 限速而非封鎖(配合 tc/htb)

# 標記 P2P 封包
iptables -t mangle -A FORWARD -m ipp2p --ipp2p -j MARK --set-mark 99
# tc 針對 mark 99 限制頻寬至 512kbps
tc qdisc add dev eth0 root handle 1: htb default 10
tc class add dev eth0 parent 1: classid 1:1 htb rate 100mbit
tc class add dev eth0 parent 1:1 classid 1:99 htb rate 512kbit ceil 512kbit
tc filter add dev eth0 protocol ip parent 1:0 prio 1 handle 99 fw flowid 1:99

5.4 記錄 P2P 流量供審計

iptables -A FORWARD -m ipp2p --ipp2p -j LOG --log-prefix "P2P_DETECT: " --log-level 6

驗證與除錯

  • lsmod | grep ipp2p 確認模組載入。
  • iptables -m ipp2p --help 查看支援協議清單。
  • dmesg | grep ipp2p 檢視核心日誌。
  • 使用 wireshark 抓取測試流量,確認特徵碼匹配。

遷移至 Linux 2.6/3.x/4.x/5.x 核心的注意事項

  • 核心已內建 xt_connmark、xt_ipp2p(部分發行版需安裝 xtables-addons)。
  • 語法改為 -m connmark、-m ipp2p,規則邏輯不變。
  • 建議改用 nftables 新語法,效能更佳且語法統一。

結語

在核心層識別並控制 P2P 流量,能繞過應用層加密與端口混淆,是網關/邊界防火牆的必備能力。雖然 ipp2p 專案更新較緩,但特徵庫仍覆蓋主流協議;若需支援最新加密 P2P(如 BitTorrent uTP、μTP),建議結合 nDPI / Zeek / Suricata 做深度封包檢測(DPI)並動態下發封鎖規則。

copyright © 萬盛學電腦網 all rights reserved