電驢、BitTorrent、Kazaa 等 P2P 協議以動態端口、加密傳輸、協議混淆著稱,傳統 L4 防火牆難以精準識別。本文示範在 Linux 2.4/2.6 核心層透過 connmark + ipp2p/ipt_p2p 實現應用層協議識別與流量控制。
| 元件 | 版本/來源 | 用途 |
|---|---|---|
| Linux Kernel | 2.4.20 / 2.6.x | 基礎核心 |
| patch-o-matic-ng | 20040609 / 最新 git | 核心擴展補丁集(含 CONNMARK) |
| iptables | 1.2.8 / 1.4.x | 用戶空間控制工具 |
| ipt_p2p | 0.3.0a (netfilter.org) | 核心模組:識別 FastTrack, Gnutella, eDonkey, BitTorrent, DC++, OpenFT |
| ipp2p | 0.5c (Eicke Friedrich) | 核心模組:支援更多 P2P 協議與特徵碼 |
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 退出
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 # 進入新核心
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/
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
iptables -A FORWARD -m ipp2p --ipp2p -j DROP
iptables -A FORWARD -m ipp2p --bit --edk -j DROP
# 標記 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
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 抓取測試流量,確認特徵碼匹配。xt_connmark、xt_ipp2p(部分發行版需安裝 xtables-addons)。-m connmark、-m ipp2p,規則邏輯不變。nftables 新語法,效能更佳且語法統一。在核心層識別並控制 P2P 流量,能繞過應用層加密與端口混淆,是網關/邊界防火牆的必備能力。雖然 ipp2p 專案更新較緩,但特徵庫仍覆蓋主流協議;若需支援最新加密 P2P(如 BitTorrent uTP、μTP),建議結合 nDPI / Zeek / Suricata 做深度封包檢測(DPI)並動態下發封鎖規則。