在VPS上禁止BT下载

Page content

为什么要禁止BT下载?

  1. BT下载受版权保护的资源在国外是非法的。同时管理员还会收到侵权投诉信,甚至可能吃官司。
  2. BT下载会大量占用服务器带宽,消耗流量;一不小心AWS账单爆表。

如何在服务器屏蔽BT上传下载?

如今的BT客户端很智能,以往封杀某个端口的方法不能用了,BT软件会自动更换其他端口,使得防火墙无可奈何。

但可以利用iptables屏蔽常见的BT关键词。原理:不封锁端口,而是阻止BT软件发出的特定请求,定向 kill request。

iptables -A INPUT -m string --string "BitTorrent" --algo bm -j DROP
iptables -A INPUT -m string --string "BitTorrent protocol" --algo bm -j DROP
iptables -A INPUT -m string --string "peer_id=" --algo bm -j DROP
iptables -A INPUT -m string --string ".torrent" --algo bm -j DROP
iptables -A INPUT -m string --string "announce.php?passkey=" --algo bm -j DROP
iptables -A INPUT -m string --string "torrent" --algo bm -j DROP
iptables -A INPUT -m string --string "announce" --algo bm -j DROP
iptables -A INPUT -m string --string "info_hash" --algo bm -j DROP
iptables -A INPUT -m string --string "tracker" --algo bm -j DROP
iptables -A INPUT -m string --string "get_peers" --algo bm -j DROP
iptables -A INPUT -m string --string "announce_peer" --algo bm -j DROP
iptables -A INPUT -m string --string "find_node" --algo bm -j DROP

iptables -A FORWARD -m string --string "BitTorrent" --algo bm --to 65535 -j DROP
iptables -A FORWARD -m string --string "BitTorrent protocol" --algo bm --to 65535 -j DROP
iptables -A FORWARD -m string --string "peer_id=" --algo bm --to 65535 -j DROP
iptables -A FORWARD -m string --string ".torrent" --algo bm --to 65535 -j DROP
iptables -A FORWARD -m string --string "announce.php?passkey=" --algo bm --to 65535 -j DROP
iptables -A FORWARD -m string --string "torrent" --algo bm --to 65535 -j DROP
iptables -A FORWARD -m string --string "announce" --algo bm --to 65535 -j DROP
iptables -A FORWARD -m string --string "info_hash" --algo bm --to 65535 -j DROP
iptables -A FORWARD -m string --string "tracker" --algo bm -j DROP
iptables -A FORWARD -m string --string "get_peers" --algo bm -j DROP
iptables -A FORWARD -m string --string "announce_peer" --algo bm -j DROP
iptables -A FORWARD -m string --string "find_node" --algo bm -j DROP

检查iptables配置

更新完之后,查看iptables,检查防火墙规则是否已经生效。

sudo iptables -L -n -v
Chain INPUT (policy ACCEPT 4 packets, 416 bytes)
...
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            STRING match  "BitTorrent" ALGO name bm TO 65535
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            STRING match  "BitTorrent protocol" ALGO name bm TO 65535
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            STRING match  "peer_id=" ALGO name bm TO 65535
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            STRING match  ".torrent" ALGO name bm TO 65535
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            STRING match  "announce.php?passkey=" ALGO name bm TO 65535
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            STRING match  "torrent" ALGO name bm TO 65535
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            STRING match  "announce" ALGO name bm TO 65535
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            STRING match  "info_hash" ALGO name bm TO 65535
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            STRING match  "tracker" ALGO name bm TO 65535
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            STRING match  "get_peers" ALGO name bm TO 65535
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            STRING match  "announce_peer" ALGO name bm TO 65535
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            STRING match  "find_node" ALGO name bm TO 65535

iptables备份与重新加载

如果有多台服务器需要配置同样的规则,在一台机器上配置好后,备份iptables,然后在其他服务器重新加载即可。

备份规则

sudo iptables-save > firewall.txt

重新加载

sudo iptables-restore < firewall.txt
本文由 络壳 原创或整理,转载请注明出处