Optimization: Pre-emptive Module Filtration via Service Discovery
Implements service-aware task allocation to reduce thread overhead and eliminate SQLite database lock contention.
Proposed change
Currently, the engine allocates threads for every selected module regardless of whether the required service (HTTP, FTP, SSH, etc.) exists on the target. This results in $O(N \times M)$ thread overhead and massive SQLite lock contention.
This PR introduces Pre-emptive Module Filtration. By checking discovered services from the port_scan phase against module requirements before thread spawning, we eliminate redundant resource allocation.
Type of change
- [ ] New core framework functionality
- [ ] Bugfix (non-breaking change which fixes an issue)
- [ ] Code refactoring without any functionality changes
- [ ] New or existing module/payload change
- [ ] Documentation/localization improvement
- [ ] Test coverage improvement
- [ ] Dependency upgrade
- [x] Other improvement (best practice, cleanup, optimization, etc)
Checklist
- [x] I've followed the contributing guidelines
- [x] I have digitally signed all my commits in this PR
- [x] I've run
make pre-commitand confirm it didn't generate any warnings/changes - [x] I've run
make test, I confirm all tests passed locally - [ ] I've added/updated any relevant documentation in the
docs/folder - [x] I've linked this PR with an open issue
- [x] I've tested and verified that my code works as intended and resolves the issue as described
- [x] I have attached screenshots demonstrating my code works as intended
- [x] I've checked all other open PRs to avoid submitting duplicate work
- [x] I confirm that the code and comments in this PR are not direct unreviewed outputs of AI
- [x] I confirm that I am the Sole Responsible Author for every line of code, comment, and design decision
Any feedback to further optimise my solution would be greatly appreciated! Some tests showing comparisons of time taken on scan running with and without code I have added, to stress test:
Before code:
After code additions:
Summary by CodeRabbit
- New Features
- Added intelligent module filtering based on service discovery results.
- Modules are now skipped when their required services aren't detected on targets, improving scan efficiency.
Walkthrough
Adds service-requirement extraction to modules and runtime applicability checks that skip running modules when their required services are not discovered for a target; introduces optional per-scan caching and integrates the check into target-group scanning flow.
Changes
| Cohort / File(s) | Summary |
|---|---|
Module service requirements nettacker/core/module.py |
Added Module.get_required_services() to load the module template and return a set of required services/libraries; errors return an empty set. |
Scan applicability and filtering nettacker/core/app.py |
Added Nettacker.is_module_applicable(target, module_name, scan_id, cache=None) which compares module-required services with discovered services from stored port_scan events; scan_target_group() now builds a per-group scan_cache and skips scheduling modules when service discovery is enabled and the module is not applicable, while bypassing checks for specific module names and when service discovery is skipped. |
Estimated code review effort
π― 3 (Moderate) | β±οΈ ~25 minutes
π₯ Pre-merge checks | β 3
β Passed checks (3 passed)
| Check name | Status | Explanation |
|---|---|---|
| Title check | β Passed | The title accurately summarizes the main change: pre-emptive module filtration via service discovery to optimize thread allocation. |
| Description check | β Passed | The description clearly explains the problem (O(N Γ M) thread overhead and SQLite lock contention) and the proposed solution (service-aware task allocation). |
| Docstring Coverage | β Passed | No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. |
βοΈ Tip: You can configure your own custom pre-merge checks in the settings.
β¨ Finishing Touches
π§ͺ Generate unit tests (beta)
- [ ] Create PR with unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands and usage tips.
@arkid15r @securestep9 - Hi! CI/CD workflow needs maintainer approval to run tests. Could you approve the workflow? Thanks!
@arkid15r @securestep9 Following upβI've now fully resolved the CodeRabbit feedback: β- Added try/except blocks to is_module_applicable to ensure the engine "fails open" on corrupted templates/events. β - Re-signed all commits with GPG (Verified badge). β- Fixed the service vs. library mapping mismatch caught in the analysis. βReady for the CI/CD run whenever you have a moment to approve the workflow. Thanks!
^ my two cents on your PR
The database locking issue that you're seeing is probably due to the number of threads you were running (was is 125?). The default database handler (that is, for sqlite) has been changed to APSW which is a raw implementation over sqlite3 for lower overhead on database tasks.
If your system cannot handle 125 threads:
- Try lower the number
- Switch to the ORM (you can do this using the
config.py: setuse_apsw_for_sqlitetoFalse). The ORM will ALSO lock your database if you're using SQLite because sqlite just doesn't support concurrent additions, it'll just manage resources better.
Thanks for the insight on the thread limits! I agree that high thread counts stress the SQLite I/O. However, the main goal of this PR is to stop ineligible modules before they even attempt a connection or a database write. By filtering them out pre-emptively, we eliminate thousands of redundant 'failed' transactions from being accessed by DB handler in the first place.