Nettacker icon indicating copy to clipboard operation
Nettacker copied to clipboard

Optimization: Pre-emptive Module Filtration via Service Discovery

Open D0NTASKME opened this issue 8 months ago β€’ 5 comments

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-commit and 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: image image

After code additions: image image

D0NTASKME avatar Mar 21 '26 20:03 D0NTASKME

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.

❀️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Mar 21 '26 20:03 coderabbitai[bot]

@arkid15r @securestep9 - Hi! CI/CD workflow needs maintainer approval to run tests. Could you approve the workflow? Thanks!

D0NTASKME avatar Mar 22 '26 00:03 D0NTASKME

@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!

D0NTASKME avatar Mar 22 '26 10:03 D0NTASKME

^ 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:

  1. Try lower the number
  2. Switch to the ORM (you can do this using the config.py: set use_apsw_for_sqlite to False). 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.

pUrGe12 avatar Mar 22 '26 10:03 pUrGe12

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.

D0NTASKME avatar Mar 24 '26 19:03 D0NTASKME