Skip to content

fix(portscan): add nf_tables fallback for Debian 12+/Ubuntu 22.04+#483

Open
pippocom wants to merge 1 commit into
thinkst:masterfrom
pippocom:fix/portscan-nftables-compatibility
Open

fix(portscan): add nf_tables fallback for Debian 12+/Ubuntu 22.04+#483
pippocom wants to merge 1 commit into
thinkst:masterfrom
pippocom:fix/portscan-nftables-compatibility

Conversation

@pippocom

Copy link
Copy Markdown

Proposed changes

The portscan module fails silently on modern Linux distributions (Debian 12+, Ubuntu 22.04+, Debian 13 Trixie) where iptables-legacy is unavailable or the ip_tables kernel module is absent. The module throws an exception and never starts, with no port scan detection at all.
The existing wiki workaround (update-alternatives --set iptables iptables-legacy) does not help on Debian 13 where the kernel module itself is missing.

Types of changes

Bugfix (non-breaking change which fixes an issue)

Checklist

Lint and unit tests pass locally with my changes (if applicable)

Further comments

The fix adds automatic backend detection at runtime:

_iptables_legacy_works() probes the mangle table directly rather than assuming legacy mode works just because the binary exists. On Debian 13 the ip_tables kernel module may be absent, making the mangle table inaccessible even with iptables-legacy installed.
set_iptables_rules() now selects the backend automatically: legacy mangle table when available (original behaviour, fully backward compatible); nf_tables filter/INPUT chain as fallback, preserving the same canaryfw: log prefix so SynLogWatcher requires no changes.
Logic extracted into _set_legacy_rules() and _set_nftables_rules() for readability.

Tested on: Debian 13 Trixie, kernel 6.18, Python 3.13, Raspberry Pi Zero 2W.

Reported-by: Marco Iannacone ianna@pippo.com

The portscan module blocked startup on modern Linux distributions where
iptables-legacy is unavailable or the ip_tables kernel module is absent
(Debian 13 Trixie, Ubuntu 22.04+), throwing:

  'Please install iptables-legacy'

even when iptables (nf_tables backend) is fully functional.

Changes:
- Added _iptables_legacy_works() to probe the mangle table at runtime
  rather than assuming legacy mode is available just because the binary
  exists. On Debian 13 the ip_tables kernel module may be absent, making
  the mangle table inaccessible even with iptables-legacy installed.
- Refactored set_iptables_rules() with automatic backend detection:
  legacy mangle table is used when available (original behaviour);
  nf_tables filter/INPUT chain is used as a fallback, preserving the
  same 'canaryfw: ' log prefix so SynLogWatcher requires no changes.
- Extracted _set_legacy_rules() and _set_nftables_rules() for clarity.

The existing wiki workaround (update-alternatives --set iptables
iptables-legacy) does not help on Debian 13 where the ip_tables kernel
module is absent. This patch makes the module work out of the box on
both legacy and modern kernels without manual intervention.

Tested on: Debian 13 Trixie, kernel 6.18, Python 3.13, Raspberry Pi Zero 2W.
Reported-by: Marco Iannacone <ianna@pippo.com>
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.

1 participant