bsv_magic_guard.py monitors incoming Bitcoin SV peer‑to‑peer traffic and blocks any connection that does not present a valid magic header and version banner. It uses scapy to inspect packets and iptables/ip6tables to immediately drop offending hosts. In addition it polls your node via JSON‑RPC to eject peers that are lagging behind or responding too slowly.
- Python 3
- scapy
- requests
iptablesandip6tables(for managing firewall rules)- Root privileges (required for packet capture and firewall changes)
- A running BSV node with JSON-RPC enabled
sudo apt-get update
sudo apt-get install python3 python3-pip iptables ip6tables
sudo pip3 install scapy requestsEdit bsv_magic_guard.py and set the variables near the top:
NETWORK_INTERFACE– interface to monitor (defaultens3)CLIENT_PORT– peer-to-peer port (default5333)- RPC credentials (
RPC_USER,RPC_PASSWORD,RPC_HOST,RPC_PORT) WHITELIST_V4/WHITELIST_V6– peers that should never be blockedPING_THRESHOLD– drop peers whose pingtime or pingwait exceed this value
Run the script with root privileges so it can sniff packets and manipulate the firewall:
sudo python3 bsv_magic_guard.pyLog output is sent to /var/log/bsv_magic_guard.log and to the console. The script listens on the configured interface and port and drops offending IPv4 and IPv6 addresses. Two example addresses are whitelisted by default.
- Captures TCP packets destined for
CLIENT_PORT(default5333) on both IPv4 and IPv6. - Checks that the first four bytes match Bitcoin SV's magic header (
E8 F3 E1 E3). - Searches the first 160 bytes of the payload for one of the allowed version banners (
/Bitcoin SV:1.1.0/or/Bitcoin SV:1.0.16/). - Any peer failing these checks is immediately blocked via
iptablesorip6tables. - Every
SYNC_CHECK_INTERVALseconds the script calls the node's RPC interface to obtaingetblockcountandgetpeerinfo. - Peers whose
synced_blocksorsynced_headersare behind the local height are blocked and disconnected. - Peers reporting
pingtimeorpingwaitlonger thanPING_THRESHOLDare also dropped.
This tool modifies your system firewall. Review the code and understand the implications before running it in production.