Skip to content

Instantly share code, notes, and snippets.

@aliafshany
Forked from ioxorg/block_bittorrent.sh
Created March 6, 2025 20:13
Show Gist options
  • Save aliafshany/4fba5e7dd89ba5f4a619704f476aee02 to your computer and use it in GitHub Desktop.
Save aliafshany/4fba5e7dd89ba5f4a619704f476aee02 to your computer and use it in GitHub Desktop.
Block BitTorrent via iptabls.
#!/usr/bin/env bash
echo "Applying Torrent blocking rules..."
# Block Torrent algo string using Boyer-Moore (bm)
BLOCK_STRINGS_BM=(
"BitTorrent"
"BitTorrent protocol"
"peer_id="
".torrent"
"announce.php?passkey="
"torrent"
"announce"
"info_hash"
"/default.ida?"
".exe?/c+dir"
".exe?/c_tftp"
)
# Block Torrent keys using Knuth-Morris-Pratt (kmp)
BLOCK_STRINGS_KMP=(
"peer_id"
"BitTorrent"
"BitTorrent protocol"
"bittorrent-announce"
"announce.php?passkey="
"find_node"
"info_hash"
"get_peers"
"announce"
"announce_peers"
)
# Apply rules for Boyer-Moore algorithm
for STRING in "${BLOCK_STRINGS_BM[@]}"; do
echo "Blocking (BM): $STRING"
iptables -A FORWARD -m string --algo bm --string "$STRING" -j DROP
done
# Apply rules for Knuth-Morris-Pratt algorithm
for STRING in "${BLOCK_STRINGS_KMP[@]}"; do
echo "Blocking (KMP): $STRING"
iptables -A FORWARD -m string --algo kmp --string "$STRING" -j DROP
done
echo "All BitTorrent blocking rules applied successfully!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment