Scripts to detect, disconnect, and ban Bitcoin peers that signal Libre relay.
- Identifiers (from the Libre fork commit 076012a):
- connection_type:
"libre" - servicesnames contains:
"PREFERENTIAL_PEERING"
- connection_type:
- Both conditions must be true; this avoids blocking other preferential peering.
-
The blocker is intentionally strict and will act only when BOTH identifiers are present:
- connection_type == "libre"
- servicesnames includes "PREFERENTIAL_PEERING"
-
Exact checks used by the scripts:
- Bash (jq):
.[] | select((.connection_type? == "libre") and ((.servicesnames? // []) | any(. == "PREFERENTIAL_PEERING")))
- PowerShell:
if ($connType -eq 'libre' -and $hasPref) { $targets += $p.addr }
- Bash (jq):
-
We do not block on only one of these signals. This AND-only policy is derived from the identifiers introduced in the cited commit 076012a and is intended to minimize false positives and preserve normal preferential peering that is not Libre.
block-libre.sh(macOS/Linux)block-libre.ps1(Windows PowerShell)
- Common: Running
bitcoindwith RPC enabled,bitcoin-cliavailable. - macOS/Linux:
jq. - Windows: PowerShell 5+ or PowerShell 7+.
# Default continuous monitoring (checks every 3 seconds - recommended)
./block-libre.sh
# High-frequency monitoring (checks every 1 second - maximum protection)
MONITOR_INTERVAL=1 ./block-libre.sh
# Conservative monitoring (checks every 10 seconds - lower resource usage)
MONITOR_INTERVAL=10 ./block-libre.sh
# Specify config and bantime (seconds)
BITCOIN_ARGS="-conf=/path/to/bitcoin.conf" BAN_TIME_SECONDS=7200 ./block-libre.sh
# Single run mode (legacy behavior - for cron/scripts)
MONITOR_MODE=false ./block-libre.sh# Default continuous monitoring (checks every 3 seconds - recommended)
PowerShell -ExecutionPolicy Bypass -File .\block-libre.ps1
# High-frequency monitoring (checks every 1 second - maximum protection)
PowerShell -ExecutionPolicy Bypass -File .\block-libre.ps1 -MonitorInterval 1
# Conservative monitoring (checks every 10 seconds - lower resource usage)
PowerShell -ExecutionPolicy Bypass -File .\block-libre.ps1 -MonitorInterval 10
# Specify config and bantime (seconds)
PowerShell -ExecutionPolicy Bypass -File .\block-libre.ps1 `
-BitcoinCliPath "C:\Program Files\Bitcoin\daemon\bitcoin-cli.exe" `
-BitcoinArgs "-conf=C:\Users\you\AppData\Roaming\Bitcoin\bitcoin.conf" `
-BanTimeSeconds 7200
# Single run mode (legacy behavior - for scheduled tasks)
PowerShell -ExecutionPolicy Bypass -File .\block-libre.ps1 -SingleRun- Real-time monitoring: Continuously monitors Bitcoin peers every 3 seconds by default
- Smart detection: Identifies peers where
connection_type == "libre"ANDservicesnamescontains"PREFERENTIAL_PEERING"(per 076012a) - Immediate action: Disconnects the specific connection (address with port) and bans the host
- Safe banning: Bans for a relative duration (IPv4/IPv6 only; hostnames such as
.onionare skipped to avoid collateral) - Overlap prevention: Ensures checks don't overlap - if a check takes longer than the interval, the next check starts immediately without stacking
- Detection speed: Libre peers are detected and blocked within 3 seconds of connection
- System load: Optimized for responsiveness while maintaining low resource usage
- Adaptive timing: If a peer check takes longer than 3 seconds, the next check starts immediately
The scripts use intelligent timing to prevent overlapping checks:
Timeline Example (3-second interval):
T=0s: Check starts
T=1s: Check completes → Sleep 2s
T=3s: Next check starts
Timeline Example (slow check):
T=0s: Check starts
T=5s: Check completes → No sleep, immediate next check
T=5s: Next check starts
- Fast networks: Maintains consistent 3-second rhythm
- Slow networks: Adapts automatically, no check stacking
- High load: Gracefully handles delays without resource waste
- Low latency: Near real-time protection against libre relay attacks
| Interval | Use Case | Protection Level | Resource Usage |
|---|---|---|---|
| 1 second | Maximum security, high-risk environments | Highest (1s detection) | Higher CPU/network |
| 3 seconds | Recommended default - balanced protection | High (3s detection) | Moderate |
| 10 seconds | Lower resource usage, stable networks | Good (10s detection) | Low |
| 60+ seconds | Legacy compatibility, scheduled runs | Basic (up to 60s+ detection) | Minimal |
When no libre peers are detected, the script runs silently after the initial startup message:
[block-libre] starting continuous monitoring mode (checking every 3s). Press Ctrl+C to stop.
# ... silence during normal monitoring (this is expected and good!)The script immediately logs detailed action when libre relay peers are found:
[block-libre] detected 2 suspect peer(s). disconnecting and banning for 604800s...
[block-libre] disconnected: 192.168.1.100:8333
[block-libre] banned host: 192.168.1.100
[block-libre] disconnected: 203.0.113.45:8333
[block-libre] banned host: 203.0.113.45
[block-libre] done. disconnected=2, banned=2[block-libre] skip ban (non-IP host): example.onion # .onion addresses aren't banned
[block-libre] error during peer check, retrying in 3s... # RPC connection issues
[block-libre] monitoring stopped. # Graceful shutdown (Ctrl+C)Note: Silence during monitoring means your node is libre-relay-free - exactly what you want!
The scripts now run in continuous monitoring mode by default. To run as a background service:
# Linux/macOS: Run in background with nohup (default 3s interval)
nohup ./block-libre.sh >/dev/null 2>&1 &
# Linux/macOS: Custom interval
nohup env MONITOR_INTERVAL=10 ./block-libre.sh >/dev/null 2>&1 &
# Windows: Run as background job
Start-Job -ScriptBlock { .\block-libre.ps1 }Only needed if you prefer single-run mode with external scheduling:
- macOS/Linux (cron):
* * * * * /full/path/block-libre.sh MONITOR_MODE=false >/dev/null 2>&1 - systemd timer: run
MONITOR_MODE=false ./block-libre.shevery minute. - Windows Task Scheduler: run
.\block-libre.ps1 -SingleRunevery minute.
bitcoin-cli -netinfo 4 | cat
bitcoin-cli getpeerinfo | jq -r '.[] | select(.connection_type=="libre" and ((.servicesnames//[])|any(.=="PREFERENTIAL_PEERING"))) | [.id,.addr,.subver] | @tsv'- Maximum security posture: AND-only detection prevents false positives; only bans IP literals (skips .onion); uses safe JSON parsing.
- Real-time protection: 3-second default detection provides near-instantaneous response to libre relay connections.
- Production ready: Cross-platform compatible, proper error handling, smart timing, graceful shutdown.
- Resource efficient: Intelligent overlap prevention ensures optimal system resource usage under all conditions.
- Operational excellence: Silent monitoring with action-based logging, configurable intervals, suitable for 24/7 background operation.
- Adaptive performance: Automatically adjusts to network conditions and system load without manual tuning.
- Adjust
BAN_TIME_SECONDS/-BanTimeSecondsandMONITOR_INTERVAL/-MonitorIntervalas needed for your environment.