Skip to content

Conversation

@Fatumayattani
Copy link

What was wrong?

This PR implements the initial setup for the Network Attack Simulation module in py-libp2p, as discussed in [[Discussion #929]] and tracked under Issue #57. The primary focus is on Eclipse attack simulation, building the test network, simulating malicious peers, and establishing the framework for network attack testing.

How was it fixed?

  • Added tests/security/attack_simulation/ submodule containing:

    Eclipse attack:

    • malicious_peer.py – Simulates malicious peer behavior
    • network_builder.py – Builds test networks with honest and malicious nodes
    • metrics_collector.py – Collects attack metrics during simulation
    • attack_scenarios.py – Defines different Eclipse attack scenarios
    • test_eclipse_simulation.py – Test suite validating network setup and malicious behavior

    Utilities:

    • Metrics calculation (attack_metrics.py)
    • Network monitoring (network_monitor.py)
    • Peer behavior simulation (peer_behavior_simulator.py)
    • Tests for utilities

    Configuration:

    • attack_configs.py – Attack configuration options
    • network_topologies.py – Predefined network topologies
  • Added README.md documenting module overview, structure, setup, usage, testing, metrics, roadmap, and Eclipse attack flow (Mermaid diagram)

  • All tests pass locally (6/6) confirming:

    • Network setup works as expected
    • Malicious peer behaviors execute correctly
    • Metrics collection framework functions properly

To-Do

  • Clean up commit history if necessary
  • Add or update documentation related to these changes

Cute Animal Picture

Cute White Cat

@seetadev
Copy link
Contributor

@Fatumayattani : Thank you for opening the PR on eclipse attack simulation.

Great effort indeed. CCing @acul71, @yashksaini-coder, @bomanaps for their thoughts and feedback. This PR will indeed impact the py-libp2p library.

There is one CI/CD issue too, which is not related to your pull request.

I investigated it and documented it at #949 .

@yashksaini-coder , @sumanjeet0012 and @acul71 are fixing it. We will soon have all the CI/CD tests passing.

@Fatumayattani : Will re-run CI/CD pipeline once the issue is fixed today or tomorrow. In the meantime, assigning you more projects and issues on py-libp2p.

@yashksaini-coder
Copy link
Contributor

@Fatumayattani can I get the access for this ?

@Fatumayattani
Copy link
Author

Thank you @seetadev for the review and for coordinating on the CI/CD issue. I’ll re-run the checks once that’s resolved.

@yashksaini-coder I’ve added you as a collaborator so you now have access to the feature/attack-simulation branch. Please let me know if you face any issues or need anything else to get started.

Really appreciate everyone’s feedback and support in pushing this forward.

@yashksaini-coder
Copy link
Contributor

yashksaini-coder commented Sep 24, 2025

@Fatumayattani @seetadev Hi, I've reviewed the PR, and executed the checks and all tests on my end. Everything is good, all test cases passed ✔️ and some new changes to fix the linting and minor errors.

I will work on this for adding more attack simulation and review

@seetadev
Copy link
Contributor

@Fatumayattani , @yashksaini-coder : Wonderful, great to hear. Appreciate your efforts.

We will review this PR in detail. Re-ran the CI/CD pipeline.

Since, it is an important module addition, wish to have feedback first from @acul71 , @lla-dane and @sumanjeet0012 .

We will discuss with @pacrob and have his review after discussing in tomorrow's maintainer's call.

Copy link
Contributor

@acul71 acul71 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR #950 Review: Eclipse Attack Simulation Module

Summary

This PR introduces a Network Attack Simulation module for py-libp2p with a well-designed dual-layer architecture for Eclipse attack testing.

Recommendation: ✅ APPROVE with mods

Implementation Analysis

What's Working Well:

  • Dual-layer architecture - Simulation + Real integration
  • Real integration tests - 3/3 tests pass with actual libp2p hosts
  • Comprehensive framework - Well-structured attack simulation foundation
  • Test coverage - 9/9 tests pass (6 simulation + 3 real integration)

Architecture Design:

  • Simulation Layer: Fast framework validation with placeholder metrics (by design)
  • Real Integration Layer: Comprehensive attack simulation with actual libp2p components

Current Assessment

  • Test Coverage: 100% (9/9 tests pass)
  • Code Quality: Excellent structure with real integration
  • Documentation: Comprehensive README with clear usage examples
  • Performance: Fast simulation (0.40s) + Real integration (2.06s)
  • Functionality:COMPLETE - Both layers working as designed

Final Recommendation

✅ APPROVE but answer questions
and also wait is there a newsfragment?

(venv) luca@r17:~/PNL_Launchpad_Curriculum/Libp2p/py-libp2p$ l newsfragments/57
ls: cannot access 'newsfragments/57': No such file or directory

1. [Metrics and Analysis](#metrics-and-analysis)
1. [Roadmap](#roadmap)
1. [Eclipse Attack Flow](#eclipse-attack-flow)
1. [Contributing](#contributing)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are all this 1s here not 1,2,3,....

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed the auto-tab completion

"honest_nodes": 10,
"malicious_nodes": 3,
"attack_intensity": 0.5,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try with different settings? are them handled ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some logs with different settings?
You may want to open a discussion page, and only put a link here, to not over bloat the PR

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will try with different settings and have the screenshot of the results in the Discussion page for this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@acul71 I have done some more attack simulation test with multiple configs, and new test suite, shared the new disucssion here, #960

@Fatumayattani
Copy link
Author

Thanks everyone for the feedback and contributions.

I’ve added the missing newsfragment.

Kudos to @yashksaini-coder for running additional tests, and Discussion #960 contains the comprehensive Eclipse attack simulation results.

Special thanks to @acul71 for the detailed review and @seetadev for coordinating the CI/CD workflow. Really appreciate everyone’s collaboration and support on this.

@@ -0,0 +1,171 @@
class AttackMetrics:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class is duplicated in utils/attack_metrics.py.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @pacrob , I’ve removed the duplicate and now import AttackMetrics from utils/attack_metrics.py.

std_dev = variance**0.5
consistency_ratio = std_dev / mean_score if mean_score > 0 else 0

print(".2f")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the unnecessary print line to keep the tests clean.

Comment on lines +106 to +117
async def _connect_nodes(self, hosts: list[IHost]):
"""Connect hosts in a simple topology"""
# Connect each host to the next one in a ring topology
for i in range(len(hosts)):
# Each node connects to 2 neighbors
for j in range(i + 1, min(i + 3, len(hosts))):
try:
peer_info = PeerInfo(hosts[j].get_id(), hosts[j].get_addrs())
await hosts[i].connect(peer_info)
await trio.sleep(0.1) # Allow connection to establish
except Exception as e:
print(f"Connection failed between {i} and {j}: {e}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function doesn't appear to be used anywhere.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appreciate the note 🙏 we’re keeping this function for Phase 2 as it’ll be used in the topology and interop tests.

@pacrob
Copy link
Member

pacrob commented Oct 5, 2025

In general, I'm concerned about the huge amount of emojis and print statements in the new tests. In the current state, these just clutter up CI/logs. Consider converting them to logging statements.

@yashksaini-coder
Copy link
Contributor

let me work on it, @pacrob

@seetadev
Copy link
Contributor

seetadev commented Oct 6, 2025

@yashksaini-coder and @Fatumayattani : Thank you Yash for the initiative. Very important set of feedback points shared by @pacrob. CCing @acul71 to keep him in the loop of this PR.

@pacrob : Thank you for sharing pointers and feedback. Completely agree with the improvements shared on the test suite front. Appreciate your support. Me and @acul71 will get this resolved soon.

@Fatumayattani
Copy link
Author

Thank you @pacrob 🙏 for the clear and helpful feedback.
@yashksaini-coder let me know if you need any help with the fixes and logging cleanup.
Grateful to you, @seetadev, and @acul71 for all the support and encouragement.

@yashksaini-coder
Copy link
Contributor

Thank you @pacrob 🙏 for the clear and helpful feedback. @yashksaini-coder let me know if you need any help with the fixes and logging cleanup. Grateful to you, @seetadev, and @acul71 for all the support and encouragement.

Appreciate it, once I get this fix up. I will ping you also we need to have a discussion post for this.

Comment on lines 157 to 162
pytest tests/security/attack_simulation/test_eclipse_attack.py -v
pytest tests/security/attack_simulation/test_sybil_attack.py -v
pytest tests/security/attack_simulation/test_flooding_attack.py -v
pytest tests/security/attack_simulation/test_connection_exhaustion.py -v
pytest tests/security/attack_simulation/test_protocol_attack.py -v
pytest tests/security/attack_simulation/test_attack_analysis.py -v
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please review these and other files referenced in this README - many of them don't exist.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed as this was a previous stage change, no longer adding this.

@yashksaini-coder
Copy link
Contributor

@seetadev Requesting a CI/CD run all test cases and review have been addressed. Update the docs and refactoring done.

@seetadev
Copy link
Contributor

seetadev commented Oct 8, 2025

@yashksaini-coder : Thank you so much to you and @Fatumayattani for your efforts. Appreciate it.

It took me sometime to reach this PR as there were close to 12+ PRs in the review queue. Did re-run the CI/CD pipeline and all the test cases are passing.

This PR is looking good. Doing a thorough review today. Also, asking @acul71 to do a peer review on this PR and share feedback points and improvements.

We will discuss this with @pacrob tomorrow before doing a final review + merge, once all the improvements are addressed.

@Fatumayattani
Copy link
Author

Thank you @seetadev 🙏
Appreciate you taking the time to review this. Glad to hear the tests passed. Thank you to @yashksaini-coder for collaborating on this. Looking forward to any feedback from @acul71 and @pacrob .

@Fatumayattani
Copy link
Author

Hi everyone 👋

Added two new attack simulation scenarios: Replay Attack and Routing Poisoning Attack.

They’re organized under tests/security/attack_simulation/ to keep everything consistent with the existing structure.

Also included a short screencast showing the tests running successfully.

replay.mp4

Appreciate any feedback or suggestions from the team.

cc @seetadev @acul71 @pacrob @yashksaini-coder

@seetadev
Copy link
Contributor

@Fatumayattani , @yashksaini-coder : Appreciate the efforts. Re-ran the CI/CD pipeline.

Wish to share that we received some nice pointers from friends at FIL-oz and Polkadot: please visit #929 (comment)

yashksaini-coder and others added 2 commits October 23, 2025 20:29
…trics

- Introduced new attack types: Bootnode Poisoning, Long-Range Fork Replay, Invalid Block Propagation, and Finality Stall.
- Updated README to reflect new features and configurations.
- Added comprehensive mitigation strategies documentation.
- Enhanced attack metrics to include new metrics for the extended threat model.
- Improved configuration options for new attack types in `attack_configs.py`.
- Refactored existing code for better readability and maintainability.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants