-
Notifications
You must be signed in to change notification settings - Fork 191
Add initial Eclipse attack simulation module with metrics collection #950
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
Fatumayattani
wants to merge
60
commits into
libp2p:main
Choose a base branch
from
Fatumayattani:feature/attack-simulation
base: main
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.
+10,793
−10
Open
Changes from 15 commits
Commits
Show all changes
60 commits
Select commit
Hold shift + click to select a range
7a6b530
tls support
Fatumayattani d0345b6
update tls docs
Fatumayattani 7dd3469
update tls support
Fatumayattani bfbc389
update changes
Fatumayattani a9b510e
tls support
Fatumayattani 6ea3016
tls support doc #700
Fatumayattani 77f4764
Add tls-support.rst to docs toctree
Fatumayattani cc68f63
Merge branch 'main' into main
seetadev 73d44ed
Replace asyncio with trio, fix newline, and run pre-commit checks on …
Fatumayattani 4f9eabe
Add initial Eclipse attack simulation module with tests
Fatumayattani d801a91
Merge branch 'main' into feature/attack-simulation
seetadev c1df722
Merge branch 'libp2p:main' into feature/attack-simulation
yashksaini-coder 5691e6e
Merge branch 'main' into feature/attack-simulation
yashksaini-coder 6312c95
Refactor code for consistency and clarity; update README and configur…
yashksaini-coder 999d293
Add real integration tests and metrics collection for Eclipse attack …
yashksaini-coder b46cdef
Merge branch 'main' into feature/attack-simulation
yashksaini-coder 44044f8
Linting errors resolved
yashksaini-coder efffe92
Merge remote-tracking branch 'refs/remotes/origin/feature/attack-simu…
yashksaini-coder ba04a4f
Refactor real metrics collector and test structure
yashksaini-coder 32483ac
Remove non-existent file
yashksaini-coder 16708e3
Enhance type annotations and improve metrics collection in Eclipse at…
yashksaini-coder fd4d578
performed more test and test suite for multiple config scenarios and …
yashksaini-coder 7004d94
add newsfragment for PR #950 (Eclipse attack simulation module)
Fatumayattani e19929e
Merge branch 'main' into feature/attack-simulation
yashksaini-coder 82531b1
Add connection exhaustion attack implementation and tests passed
yashksaini-coder d9bd1b7
Implement flooding attack module and corresponding tests
yashksaini-coder a3dfead
Add protocol attack implementation and tests
yashksaini-coder c417fce
Add Sybil attacks and tests
yashksaini-coder 0cd848c
Add attack analysis framework and metrics collection with comprehensi…
yashksaini-coder 7afdad7
Phase 2 completed:
yashksaini-coder 950b155
Performed make fix & typecheck run to fix all linting and typechecke…
yashksaini-coder 5ec1eec
Remove obsolete attack simulation result files to streamline testing …
yashksaini-coder e9d3215
Add results directory handling and update file paths in eclipse attac…
yashksaini-coder a1c8a31
Refactor real network builder to create multiple honest and malicious…
yashksaini-coder 463f6ac
Fix host initialization in TLS examples by removing unnecessary await
yashksaini-coder a7b963e
Update test to use a random port for address changes in DHT reissue test
yashksaini-coder 587aa6f
Merge branch 'main' into feature/attack-simulation
yashksaini-coder 6b3a421
Merge branch 'main' into feature/attack-simulation
acul71 d9e51e9
Merge branch 'main' into feature/attack-simulation
yashksaini-coder f437fcf
Merge branch 'main' into feature/attack-simulation
yashksaini-coder 4c36e6c
Address pacrob feedback: remove duplicate AttackMetrics and cleanup t…
Fatumayattani 8d9e7c8
refactor: improve temporary file handling and reduce test peer count …
yashksaini-coder 003f4e5
refactor: standardize list type annotations in AttackMetrics class an…
yashksaini-coder 0f81749
Merge branch 'main' into feature/attack-simulation
yashksaini-coder 8ef6ebf
update README
yashksaini-coder 8b9c4b4
fix: correct whitespace in README and improve comment formatting in m…
yashksaini-coder f61574e
Merge branch 'main' into feature/attack-simulation
yashksaini-coder 1a5d9a9
Merge branch 'libp2p:main' into feature/attack-simulation
yashksaini-coder 7777dd2
test: add replay and routing_poisoning attack simulations
Fatumayattani 8bb588c
Merge branch 'main' into feature/attack-simulation
yashksaini-coder 0838067
Merge branch 'main' into feature/attack-simulation
yashksaini-coder 4bfd5e7
Enhance attack simulation framework with extended threat model and me…
yashksaini-coder 14b8be7
Fix minor bugs and add mitigation documentation for attack simulations
Fatumayattani 3cae619
Merge branch 'main' into feature/attack-simulation
yashksaini-coder db52311
Merge branch 'main' into feature/attack-simulation
yashksaini-coder 34c24c9
Add time, latency, and topology attacks plus updated mitigations
Fatumayattani 5a44b49
Merge branch 'main' into feature/attack-simulation
yashksaini-coder 4270535
Merge branch 'main' into feature/attack-simulation
yashksaini-coder 0d23489
Merge branch 'main' into feature/attack-simulation
yashksaini-coder 6d47ec1
chore: resolved all the lint, typecheck, and test case errors
yashksaini-coder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| Py-libp2p – TLS Support Documentation | ||
| ====================================================== | ||
|
|
||
| .. contents:: | ||
| :depth: 2 | ||
| :local: | ||
|
|
||
| Overview of TLS in Libp2p | ||
| ------------------------- | ||
|
|
||
| **Purpose of TLS in P2P networking** | ||
|
|
||
| - Encrypts data between peers. | ||
| - Authenticates peer identity using certificates. | ||
| - Prevents man-in-the-middle attacks. | ||
|
|
||
| **Integration in libp2p security modules** | ||
|
|
||
| - TLS is one of the supported secure channel protocols (alongside Noise). | ||
| - Negotiated during connection setup. | ||
|
|
||
| **Current status** | ||
|
|
||
| - **py-libp2p**: Experimental, usable for local and interop tests. | ||
| - **go-libp2p / js-libp2p**: Stable and production-ready. | ||
|
|
||
| Installation Requirements | ||
| ------------------------- | ||
|
|
||
| **Python requirements** | ||
|
|
||
| - Python 3.8+ | ||
|
|
||
| **Install with TLS support** | ||
|
|
||
| .. code-block:: bash | ||
|
|
||
| pip install "libp2p[tls]" | ||
|
|
||
| **Additional dependencies** | ||
|
|
||
| Ubuntu / Debian: | ||
|
|
||
| .. code-block:: bash | ||
|
|
||
| sudo apt install build-essential python3-dev libffi-dev libssl-dev | ||
|
|
||
| macOS: | ||
|
|
||
| .. code-block:: bash | ||
|
|
||
| brew install openssl | ||
|
|
||
| Enabling TLS in py-libp2p | ||
| ------------------------- | ||
|
|
||
| **Working example – Listener and Dialer** | ||
|
|
||
| Listener node: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| import trio | ||
| from libp2p import new_host | ||
| from libp2p.security.tls.transport import TLSTransport | ||
|
|
||
| async def main(): | ||
| host = await new_host(security_transports=[TLSTransport()]) | ||
| await host.listen("/ip4/0.0.0.0/tcp/8000") | ||
| print("TLS-enabled listener at:", host.get_addrs()) | ||
|
|
||
| await trio.sleep_forever() | ||
|
|
||
| if __name__ == "__main__": | ||
| trio.run(main()) | ||
|
|
||
| Dialer node: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| import trio | ||
| from libp2p import new_host | ||
| from libp2p.security.tls.transport import TLSTransport | ||
| from libp2p.peer.peerinfo import info_from_p2p_addr | ||
|
|
||
| async def main(): | ||
| host = await new_host(security_transports=[TLSTransport()]) | ||
|
|
||
| addr = "/ip4/127.0.0.1/tcp/8000/p2p/QmPeerIDHere" | ||
| peer_info = info_from_p2p_addr(addr) | ||
|
|
||
| await host.connect(peer_info) | ||
| print("Connected securely to", peer_info.peer_id) | ||
|
|
||
| if __name__ == "__main__": | ||
| trio.run(main()) | ||
|
|
||
| **Defaults if no configuration is provided** | ||
|
|
||
| - Generates a self-signed certificate automatically. | ||
|
|
||
| Certificate Management | ||
| ---------------------- | ||
|
|
||
| **Generate a development certificate** | ||
|
|
||
| .. code-block:: bash | ||
|
|
||
| openssl req -x509 -newkey rsa:2048 \ | ||
| -keyout key.pem -out cert.pem \ | ||
| -days 365 -nodes -subj "/CN=py-libp2p" | ||
|
|
||
| - Store keys outside version control. | ||
| - Rotate certificates every 90 days in production. | ||
|
|
||
| Testing TLS Connections | ||
| ----------------------- | ||
|
|
||
| **Local test steps** | ||
|
|
||
| 1. Run the listener example. | ||
| 2. Start the dialer with the listener's multiaddress. | ||
| 3. Confirm the secure connection in logs. | ||
|
|
||
| **Interop testing** | ||
|
|
||
| - Ensure both nodes advertise `/tls/1.0.0`. | ||
| - Peer IDs must match certificate public keys. | ||
|
|
||
| Security Considerations | ||
| ----------------------- | ||
|
|
||
| - Never disable certificate verification in production. | ||
| - Use TLS 1.3 or later. | ||
| - Pin certificates for critical peers. | ||
|
|
||
| Troubleshooting | ||
| --------------- | ||
|
|
||
| .. list-table:: | ||
| :header-rows: 1 | ||
| :widths: 30 30 40 | ||
|
|
||
| * - Problem | ||
| - Cause | ||
| - Solution | ||
| * - Certificate not trusted | ||
| - Self-signed without trust store entry | ||
| - Add cert to local trust store or disable verification **only** in testing. | ||
| * - Protocol negotiation failed | ||
| - One peer does not support `/tls/1.0.0` | ||
| - Enable TLS on both peers or use Noise. | ||
| * - SSL handshake failure | ||
| - TLS version mismatch or clock skew | ||
| - Enforce TLS 1.3, sync system clock. | ||
| * - `ImportError: No module named libp2p.security.tls` | ||
| - TLS extras not installed | ||
| - Run `pip install "libp2p[tls]"`. | ||
| * - Connection refused | ||
| - Port blocked or listener not running | ||
| - Check firewall rules and listener status. | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import pytest | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def security_protocol(): | ||
| return None | ||
| return None |
Empty file.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| # Network Attack Simulation Module (py-libp2p) | ||
|
|
||
| This module provides a **network attack simulation framework** for py-libp2p, focused on testing **P2P network security**. It is implemented as a **submodule** inside py-libp2p to simulate attacks, measure metrics, and analyze network resilience. | ||
|
|
||
| ______________________________________________________________________ | ||
|
|
||
| ## Table of Contents | ||
|
|
||
| 1. [Module Overview](#module-overview) | ||
| 1. [Module Structure](#module-structure) | ||
| 1. [Setup and Usage](#setup-and-usage) | ||
| 1. [Testing](#testing) | ||
| 1. [Implementation Details](#implementation-details) | ||
| 1. [Metrics and Analysis](#metrics-and-analysis) | ||
| 1. [Roadmap](#roadmap) | ||
| 1. [Eclipse Attack Flow](#eclipse-attack-flow) | ||
| 1. [Contributing](#contributing) | ||
|
||
|
|
||
| ______________________________________________________________________ | ||
|
|
||
| ## Module Overview | ||
|
|
||
| This module simulates network attacks in a controlled py-libp2p environment. Current focus: | ||
|
|
||
| - **Eclipse attacks** by poisoning DHTs | ||
| - Metrics collection for network health and attack effectiveness | ||
| - Configurable attack scenarios and network topologies | ||
| - Foundation for future attack types (Sybil, flooding, protocol exploits) | ||
|
|
||
| All work is **local in py-libp2p** with a planned migration path to `libp2p/interop`. | ||
|
|
||
| ______________________________________________________________________ | ||
|
|
||
| ## Module Structure | ||
|
|
||
| ``` | ||
| tests/security/attack_simulation/ | ||
| ├── eclipse_attack/ | ||
| │ ├── test_eclipse_simulation.py # Main test suite for Eclipse attacks | ||
| │ ├── malicious_peer.py # Malicious peer behavior implementation | ||
| │ ├── metrics_collector.py # Collects attack metrics during simulation | ||
| │ ├── attack_scenarios.py # Defines Eclipse attack scenarios | ||
| │ ├── network_builder.py # Builds test networks with honest/malicious nodes | ||
| │ ├── real_network_builder.py # Real libp2p host integration | ||
| │ ├── real_metrics_collector.py # Real network performance metrics | ||
| │ └── test_real_eclipse_simulation.py # Integration tests with actual DHTs | ||
| ├── utils/ | ||
| │ ├── attack_metrics.py # Metrics calculation utilities | ||
| │ ├── peer_behavior_simulator.py # Simulates peer behaviors (honest and malicious) | ||
| │ └── network_monitor.py # Monitors network state and connectivity | ||
| ├── config/ | ||
| │ ├── attack_configs.py # Configuration options for attacks | ||
| │ └── network_topologies.py # Predefined network topologies | ||
| └── README.md # Module documentation and usage guide | ||
| ``` | ||
|
|
||
| ______________________________________________________________________ | ||
|
|
||
| ## Setup and Usage | ||
|
|
||
| 1. **Activate py-libp2p virtual environment**: | ||
|
|
||
| ```bash | ||
| source .venv/bin/activate | ||
| ``` | ||
|
|
||
| 2. **Run the simulation framework tests**: | ||
|
|
||
| ```bash | ||
| pytest -v tests/security/attack_simulation/eclipse_attack/test_eclipse_simulation.py | ||
| ``` | ||
|
|
||
| 3. **Run the REAL integration tests** (🆕 **Actual libp2p network attacks**): | ||
|
|
||
| ```bash | ||
| pytest -v tests/security/attack_simulation/eclipse_attack/test_real_eclipse_simulation.py | ||
| ``` | ||
|
|
||
| 4. **Run individual attack demo**: | ||
|
|
||
| ```bash | ||
| python tests/security/attack_simulation/eclipse_attack/test_real_eclipse_simulation.py demo | ||
| ``` | ||
|
|
||
| > Tests validate both simulated and real network attack scenarios. | ||
|
|
||
| ______________________________________________________________________ | ||
|
|
||
| ## Testing | ||
|
|
||
| The module provides **two levels** of testing: | ||
|
|
||
| ### **Level 1: Simulation Framework** (Original Implementation) | ||
| - Eclipse attack tests (`eclipse_attack/test_eclipse_simulation.py`) | ||
| - Utilities: metrics, network monitoring, peer behavior | ||
| - **Fast execution**, **conceptual validation** | ||
|
|
||
| ### **Level 2: Real Integration Tests** 🆕 (New Enhancement) | ||
| - Real libp2p host creation using `HostFactory` | ||
| - Actual DHT manipulation with `KadDHT` instances | ||
| - Real network performance measurement | ||
| - **Slower execution**, **actual security testing** | ||
|
|
||
| Passing tests confirm: | ||
|
|
||
| - ✅ **Simulation Framework**: Network setup, malicious behaviors, metrics collection | ||
| - ✅ **Real Integration**: Actual libp2p attacks, DHT poisoning, performance degradation | ||
|
|
||
| ______________________________________________________________________ | ||
|
|
||
| ## Implementation Details | ||
|
|
||
| ### **Simulation Layer** (Original) | ||
|
|
||
| #### Malicious Peer | ||
| ```python | ||
| class MaliciousPeer: | ||
| """Simulates malicious peer behavior""" | ||
| ``` | ||
|
|
||
| #### Network Builder | ||
| ```python | ||
| class AttackNetworkBuilder: | ||
| """Constructs configurable test networks for attack simulations""" | ||
| ``` | ||
|
|
||
| ### **Real Integration Layer** 🆕 (New Enhancement) | ||
|
|
||
| #### Real Malicious Peer | ||
| ```python | ||
| class RealMaliciousPeer(MaliciousPeer): | ||
| """Real malicious peer that manipulates actual DHT instances""" | ||
|
|
||
| async def poison_real_dht_entries(self, target_dht: KadDHT): | ||
| # Actually poison real DHT routing tables | ||
|
|
||
| async def flood_real_peer_table(self, target_dht: KadDHT): | ||
| # Flood real DHT with malicious entries | ||
| ``` | ||
|
|
||
| #### Real Network Builder | ||
| ```python | ||
| class RealNetworkBuilder(AttackNetworkBuilder): | ||
| """Builds networks with real libp2p hosts and DHT instances""" | ||
|
|
||
| async def create_real_eclipse_test_network(self): | ||
| # Uses HostFactory to create actual libp2p hosts | ||
| # Creates real KadDHT instances | ||
| # Forms realistic network topologies | ||
| ``` | ||
|
|
||
| #### Real Metrics Collector | ||
| ```python | ||
| class RealAttackMetrics(AttackMetrics): | ||
| """Collects actual performance metrics from real libp2p networks""" | ||
|
|
||
| async def measure_complete_attack_cycle(self): | ||
| # Measures real DHT lookup degradation | ||
| # Tracks actual network connectivity loss | ||
| # Calculates genuine recovery metrics | ||
| ``` | ||
|
|
||
| ______________________________________________________________________ | ||
|
|
||
| ## Metrics and Analysis | ||
|
|
||
| Tracked metrics: | ||
|
|
||
| - DHT lookup success/failure rates | ||
| - Peer table contamination | ||
| - Network connectivity | ||
| - Attack effectiveness and recovery metrics | ||
|
|
||
| ```python | ||
| class AttackMetrics: | ||
| """Metrics collection and analysis framework""" | ||
| ``` | ||
|
|
||
| ______________________________________________________________________ | ||
|
|
||
| ## Roadmap | ||
|
|
||
| **Phase 1 (Current)**: Eclipse attack simulation | ||
|
|
||
| **Phase 2**: Extended attacks (Sybil, flooding, connection exhaustion) | ||
|
|
||
| **Phase 3**: Cross-implementation testing in `libp2p/interop` | ||
|
|
||
| ______________________________________________________________________ | ||
|
|
||
| ## Eclipse Attack Flow | ||
|
|
||
| ```mermaid | ||
| flowchart TD | ||
| A[Network Builder] --> B[Honest Peers] | ||
| A --> C[Malicious Peers] | ||
| C --> D[Poison DHT Entries] | ||
| C --> E[Flood Peer Tables] | ||
| B --> F[Perform Lookups] | ||
| D --> F | ||
| E --> F | ||
| F --> G[Metrics Collector] | ||
| G --> H[Attack Analysis & Reporting] | ||
| ``` | ||
|
|
||
| > This flow illustrates the lifecycle of an Eclipse attack: the network is built, malicious peers poison the DHT and flood peer tables, honest peers perform lookups, and metrics are collected and analyzed. | ||
|
|
||
| ______________________________________________________________________ | ||
|
|
||
| ## Contributing | ||
|
|
||
| 1. Add new Eclipse attack scenarios under eclipse_attack, and shared utilities for any attack under utils. | ||
| 1. Implement new metrics or monitoring tools | ||
| 1. Write corresponding pytest tests | ||
| 1. Submit PR to py-libp2p for review | ||
Empty file.
Empty file.
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new_hostis not an async function. Please verify all your examples work as expected.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the
awaitkeywords from the example code blocks.