fix: dual-stack ipv6 audit and cleanup unused lapi function
This commit is contained in:
1 parent
884860ab17
commit
c23f2b7a47
1 file changed
+17
-39
+17
-39
@@ -16,40 +16,11 @@ class FirewallAuditor:
|
|||||||
self.API_KEY = os.getenv("BOUNCER_KEY")
|
self.API_KEY = os.getenv("BOUNCER_KEY")
|
||||||
self.TRITON = "triton.beane.me"
|
self.TRITON = "triton.beane.me"
|
||||||
|
|
||||||
def get_lapi_state(self):
|
|
||||||
"""Precision parser: Only harvests IPs from active remediation decisions."""
|
|
||||||
try:
|
|
||||||
res = subprocess.run(["/usr/bin/cscli", "decisions", "list", "-a", "-o", "json"],
|
|
||||||
capture_output=True, text=True, check=True)
|
|
||||||
data = json.loads(res.stdout) or []
|
|
||||||
|
|
||||||
ips = set()
|
|
||||||
for item in data:
|
|
||||||
if item.get('remediation') is True:
|
|
||||||
for d in item.get('decisions', []):
|
|
||||||
duration = d.get('duration', '')
|
|
||||||
if d.get('type') != 'ban':
|
|
||||||
continue
|
|
||||||
|
|
||||||
if duration.startswith('-'):
|
|
||||||
continue
|
|
||||||
|
|
||||||
val = d.get('value')
|
|
||||||
if val and ':' not in val:
|
|
||||||
ips.add(val)
|
|
||||||
|
|
||||||
logging.info(f"LAPI Parser (IPv4-Only) found {len(ips)} active bans.")
|
|
||||||
return ips
|
|
||||||
except Exception as e:
|
|
||||||
logging.error(f"LAPI Query Failed: {e}")
|
|
||||||
return None
|
|
||||||
|
|
||||||
def get_remote_lapi_truth(self):
|
def get_remote_lapi_truth(self):
|
||||||
url = f"https://{self.TRITON}:8081/v1/decisions/stream?startup=true"
|
url = f"https://{self.TRITON}:8081/v1/decisions/stream?startup=true"
|
||||||
headers = {"X-Api-Key": self.API_KEY}
|
headers = {"X-Api-Key": self.API_KEY}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# We add a shorter timeout and explicitly disable redirects
|
|
||||||
r = requests.get(
|
r = requests.get(
|
||||||
url,
|
url,
|
||||||
headers=headers,
|
headers=headers,
|
||||||
@@ -61,7 +32,7 @@ class FirewallAuditor:
|
|||||||
data = r.json() or {}
|
data = r.json() or {}
|
||||||
|
|
||||||
new_decisions = data.get('new') or []
|
new_decisions = data.get('new') or []
|
||||||
unique_ips = {d['value'] for d in new_decisions if d.get('origin') != 'CAPI' and ':' not in d['value']}
|
unique_ips = {d['value'] for d in new_decisions if d.get('origin') != 'CAPI'}
|
||||||
|
|
||||||
return unique_ips
|
return unique_ips
|
||||||
|
|
||||||
@@ -73,13 +44,18 @@ class FirewallAuditor:
|
|||||||
return set()
|
return set()
|
||||||
|
|
||||||
def get_kernel_state(self):
|
def get_kernel_state(self):
|
||||||
"""Scans ALL sets in the crowdsec table to find every blocked IP."""
|
"""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:
|
try:
|
||||||
cmd = ["/usr/sbin/nft", "-j", "list", "table", "ip", "crowdsec"]
|
cmd = ["/usr/sbin/nft", "-j", "list", "table", target["family"], target["table"]]
|
||||||
res = subprocess.run(cmd, capture_output=True, text=True, check=True)
|
res = subprocess.run(cmd, capture_output=True, text=True, check=True)
|
||||||
data = json.loads(res.stdout)
|
data = json.loads(res.stdout)
|
||||||
|
|
||||||
ips = set()
|
|
||||||
for item in data.get('nftables', []):
|
for item in data.get('nftables', []):
|
||||||
nft_set = item.get('set')
|
nft_set = item.get('set')
|
||||||
if nft_set:
|
if nft_set:
|
||||||
@@ -90,14 +66,16 @@ class FirewallAuditor:
|
|||||||
if isinstance(inner, dict):
|
if isinstance(inner, dict):
|
||||||
val = inner.get('val')
|
val = inner.get('val')
|
||||||
if val:
|
if val:
|
||||||
ips.add(val)
|
all_ips.add(val)
|
||||||
else:
|
else:
|
||||||
ips.add(e)
|
all_ips.add(e)
|
||||||
return ips
|
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:
|
except Exception as e:
|
||||||
logging.error(f"Kernel Table Scan Failed: {e}")
|
logging.error(f"Kernel Table Scan Failed for {target['table']}: {e}")
|
||||||
return set()
|
|
||||||
|
return all_ips
|
||||||
|
|
||||||
def heal(self):
|
def heal(self):
|
||||||
"""Directly restarts the bouncer service."""
|
"""Directly restarts the bouncer service."""
|
||||||
|
|||||||
Reference in new issue
Block a user