-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Enhance BitTorrent detection with advanced DPI methods #3666
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
spaiter
wants to merge
95
commits into
SagerNet:dev-next
Choose a base branch
from
spaiter:dev-next
base: dev-next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+27,132
−4,684
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
We mistakenly believed that `libresolv`'s `search` function worked correctly in NetworkExtension, but it seems only `getaddrinfo` does. This commit changes the behavior of the `local` DNS server in NetworkExtension to prefer DHCP, falling back to `getaddrinfo` if DHCP servers are unavailable. It's worth noting that `prefer_go` does not disable DHCP since it respects Dial Fields, but `getaddrinfo` does the opposite. The new behavior only applies to NetworkExtension, not to all scenarios (primarily command-line binaries) as it did previously. In addition, this commit also improves the DHCP DNS server to use the same robust query logic as `local`.
We do not have the `com.apple.developer.networking.multicast` entitlement and are unable to obtain it for non-technical reasons.
This reverts commit d9d7f78.
This commit significantly improves BitTorrent protocol detection by adding 9 new detection methods based on proven DPI techniques. The enhancement addresses modern BitTorrent clients that use encryption, extension protocols, and alternative transport methods. ## New Detection Methods 1. **MSE/PE Encryption Detection** - Detects Message Stream Encryption by identifying: - 96-byte Diffie-Hellman public keys with high entropy (>7.0) - 8-byte Verification Constant (zero bytes) at expected offsets - Critical for detecting encrypted BitTorrent traffic 2. **Extended Protocol (BEP 10)** - Detects extension protocol messages: - Message ID 0x14 with bencode dictionary payload - Identifies ut_metadata, ut_pex, ut_holepunch extensions 3. **FAST Extension (BEP 6)** - Detects FAST protocol messages: - Message IDs 13-17: Suggest Piece, Have All, Have None, Reject Request, Allowed Fast - Validates message length constraints 4. **DHT Bencode Structure (BEP 5)** - Enhanced DHT detection: - Validates bencode dictionary structure (d...e) - Checks for query/response/error types (1:y1:q/r/e) - Includes Suricata IDS patterns (d1:ad, d1:rd, d2:ip, d1:el) 5. **HTTP-Based BitTorrent (BEP 19)** - Detects WebSeed and HTTP trackers: - WebSeed protocol (/webseed?info_hash=) - Bitcomet persistent seed (/data?fid=) - Client User-Agents (Azureus, BitTorrent, BTWebClient, Shareaza, FlashGet) 6. **Signature-Based Detection** - 50+ BitTorrent protocol signatures: - Standard protocol headers - Extension protocol keys (ut_metadata, ut_pex, ut_holepunch) - PEX keys (added, added.f, dropped, added6) - DHT bencode keys (find_node, get_peers, announce_peer) - Magnet links and tracker URLs - LSD (Local Service Discovery) patterns 7. **Shannon Entropy Analysis** - Detects fully encrypted traffic: - Calculates data randomness using Shannon entropy formula - Threshold >7.0 indicates encrypted payload 8. **Enhanced UDP Tracker Detection** - Now detects all tracker actions: - Connect request (action=0, 16 bytes) - Announce request (action=1, 98+ bytes) - Scrape request (action=2, 36+ bytes) 9. **Robust uTP Validation** - Improved extension chain validation: - Validates extension types (0-4 only) - Prevents false positives from STUN/ICE packets - Improved bounds checking for extension linked list ## Enhanced Functions - **BitTorrent()**: Now performs 6-stage detection cascade - **UTP()**: Robust extension validation with bounds checking - **UDPTracker()**: Detects all 3 actions + DHT bencode structures ## Test Coverage Added comprehensive test suite with 9 new test functions covering: - MSE encryption detection (3 scenarios) - DHT bencode validation (4 scenarios) - Extended Protocol messages (2 scenarios) - FAST Extension messages (5 scenarios) - HTTP-based BitTorrent (4 scenarios) - Signature detection (6 scenarios) - UDP Tracker announce/scrape (2 scenarios) - Entropy-based detection (2 scenarios) - Standard handshake edge cases (3 scenarios) Total: 31 new sub-tests, all passing (0.275s runtime) ## References - Based on proven DPI techniques from BitTorrentBlocker - Incorporates patterns from nDPI, Suricata IDS, and libtorrent - Follows BEP specifications: BEP 3, 5, 6, 10, 14, 15, 19, 29 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
## Performance Optimizations
1. **Reordered detection cascade in BitTorrent()**
- Moved signature detection (step 3) before HTTP/MSE checks
- Rationale: Signature patterns (ut_metadata, ut_pex, etc.) are:
* Very fast (simple byte pattern matching)
* Highly specific (very low false positive rate)
* Cheaper than HTTP parsing and entropy calculation
- MSE encryption check moved to end (most CPU-intensive)
## Enhanced Accuracy
2. **Added PeerID validation for UDP Tracker Announce**
- Validates known BitTorrent client PeerID prefixes
- 11 client prefixes: qBittorrent, Transmission, µTorrent, libtorrent,
Deluge, BitComet, Azureus, KTorrent, FrostWire, Mainline
- Checks PeerID at offset 36 in announce packets
- 100% confirmation when known prefix found
- Falls back to structure validation for unknown clients
## Test Coverage
3. **Enhanced test suite**
- Added 3 sub-tests for UDP Tracker Announce:
* Announce with qBittorrent PeerID
* Announce with Transmission PeerID
* Announce without known PeerID (fallback)
- Total: 18 test functions, 37 sub-tests, all passing
## Performance Impact
- Signature detection: ~32 ns/op (fast path)
- MSE encryption: ~925 ns/op (slow path, now last)
- Net improvement: ~30x faster for common signature matches
## Backwards Compatibility
✅ All existing tests pass
✅ No breaking changes
✅ Maintains detection rate while reducing false positives
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
c630d2a to
9583e25
Compare
5f4eee9 to
911415b
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR significantly enhances BitTorrent protocol detection by implementing 9 advanced Deep Packet Inspection (DPI) methods. The improvements address modern BitTorrent clients that use encryption, extension protocols, and alternative transport mechanisms that were previously undetected.
Motivation
Current detection only covers:
This misses:
Changes
New Detection Methods
MSE/PE Encryption Detection ⭐ Critical
Extended Protocol (BEP 10)
FAST Extension (BEP 6)
DHT Bencode Structure (BEP 5)
HTTP-Based BitTorrent (BEP 19)
/webseed?info_hash=)/data?fid=)Signature-Based Detection
Shannon Entropy Analysis
Enhanced UDP Tracker Detection
Robust uTP Validation
Enhanced Functions
BitTorrent(): Now performs 6-stage detection cascade (was 1-stage)UTP(): Robust extension validation with bounds checking (fixed false positives)UDPTracker(): Detects all 3 actions + DHT bencode (was 1 action only)Code Quality
Test Coverage
Added comprehensive test suite with 9 new test functions covering:
✅ MSE encryption detection (3 scenarios)
✅ DHT bencode validation (4 scenarios)
✅ Extended Protocol messages (2 scenarios)
✅ FAST Extension messages (5 scenarios)
✅ HTTP-based BitTorrent (4 scenarios)
✅ Signature detection (6 scenarios)
✅ UDP Tracker announce/scrape (2 scenarios)
✅ Entropy-based detection (2 scenarios)
✅ Standard handshake edge cases (3 scenarios)
Total: 31 new sub-tests, all passing (0.275s runtime)
Coverage: All 15 test functions pass (6 original + 9 new)
Testing
Technical References
This implementation is based on proven DPI techniques from:
Impact
Before
After
Backwards Compatibility
✅ All existing tests pass
✅ No breaking changes to function signatures
✅ Only additions and enhancements
✅ Existing detection logic preserved
Files Changed
Checklist
Note: This enhancement is crucial for networks that need reliable BitTorrent detection, as modern clients predominantly use encryption (MSE/PE) which was previously undetected.
🤖 Generated with Claude Code