From b08e4bec293841ea04db94d572903d77f797d57f Mon Sep 17 00:00:00 2001 From: patrick Date: Fri, 23 Jan 2026 13:18:55 -0500 Subject: [PATCH] feat: implement dual-stack IPv4/IPv6 auditing and updated logs --- auditor.py | 57 +++++++++++++++++++++----------------- threat-agent-state.history | 8 ++++++ 2 files changed, 40 insertions(+), 25 deletions(-) create mode 100644 threat-agent-state.history diff --git a/auditor.py b/auditor.py index 077d0bf..14497fd 100644 --- a/auditor.py +++ b/auditor.py @@ -29,41 +29,48 @@ class FirewallAuditor: continue val = d.get('value') - if val and ':' not in val: + if val: ips.add(val) - logging.info(f"LAPI Parser (IPv4-Only) found {len(ips)} active bans.") + logging.info(f"LAPI Parser: found {len(ips)} active bans (IPv4+IPv6).") return ips except Exception as e: logging.error(f"LAPI Query Failed: {e}") return None def get_kernel_state(self): - """Scans ALL sets in the crowdsec table to find every blocked IP.""" - try: - cmd = ["/usr/sbin/nft", "-j", "list", "table", "ip", "crowdsec"] - res = subprocess.run(cmd, capture_output=True, text=True, check=True) - data = json.loads(res.stdout) + """Scans BOTH IPv4 and IPv6 sets in the crowdsec tables.""" + all_ips = set() + targets = [ + {"family": "ip", "table": "crowdsec"}, + {"family": "ip6", "table": "crowdsec6"} + ] + for target in targets: + try: + cmd = ["/usr/sbin/nft", "-j", "list", "table", target["family"], target["table"]] + res = subprocess.run(cmd, capture_output=True, text=True, check=True) + data = json.loads(res.stdout) - ips = set() - for item in data.get('nftables', []): - nft_set = item.get('set') - if nft_set: - elements = nft_set.get('elem', []) - for e in elements: - if isinstance(e, dict): - inner = e.get('elem', e) - if isinstance(inner, dict): - val = inner.get('val') - if val: - ips.add(val) - else: - ips.add(e) - return ips + for item in data.get('nftables', []): + nft_set = item.get('set') + if nft_set: + elements = nft_set.get('elem', []) + for e in elements: + if isinstance(e, dict): + inner = e.get('elem', e) + if isinstance(inner, dict): + val = inner.get('val') + if val: + all_ips.add(val) + else: + all_ips.add(e) + except subprocess.CalledProcessError: + # This happens if a node doesn't have IPv6 enabled/initialized + logging.debug(f"Table {target['table']} not found, skipping.") + except Exception as e: + logging.error(f"Kernel Table Scan Failed for {target['table']}: {e}") - except Exception as e: - logging.error(f"Kernel Table Scan Failed: {e}") - return set() + return all_ips def heal(self): """Directly restarts the bouncer service.""" diff --git a/threat-agent-state.history b/threat-agent-state.history new file mode 100644 index 0000000..d826f00 --- /dev/null +++ b/threat-agent-state.history @@ -0,0 +1,8 @@ +2026-01-22T23:17:42.801219 - HEAL: Fixed desync of 99 IPs +2026-01-22T23:47:43.636016 - HEAL: Fixed desync of 102 IPs +2026-01-23T00:37:44.806560 - HEAL: Fixed desync of 104 IPs +2026-01-23T04:37:49.240762 - HEAL: Fixed desync of 94 IPs +2026-01-23T07:57:52.612421 - HEAL: Fixed desync of 96 IPs +2026-01-23T10:37:55.353533 - HEAL: Fixed desync of 77 IPs +2026-01-23T11:27:56.544553 - HEAL: Fixed desync of 77 IPs +2026-01-23T13:14:06.888027 - HEAL: Fixed desync of 84 IPs