(self, rule: str, ctx: Dict[str, Any])
| 206 | return count |
| 207 | |
| 208 | def _evaluate_rule(self, rule: str, ctx: Dict[str, Any]) -> Dict[str, Any]: |
| 209 | findings = ctx["findings"] |
| 210 | hosts = ctx["hosts"] |
| 211 | lynis = ctx["lynis"] |
| 212 | credentials = ctx["credentials"] |
| 213 | has_lynis = bool(lynis) |
| 214 | evidence: List[str] = [] |
| 215 | |
| 216 | if rule == "firewall_config": |
| 217 | hits = self._lynis_prefix_count(lynis, ("FIRE",)) |
| 218 | if not has_lynis: |
| 219 | return {"status": "not_assessed", "matched": 0, "evidence": ["No authenticated host audit available."]} |
| 220 | if hits: |
| 221 | return {"status": "attention", "matched": hits, "evidence": [f"{hits} firewall finding(s) from Lynis."]} |
| 222 | return {"status": "ok", "matched": 0, "evidence": ["No host-firewall gaps detected by Lynis."]} |
| 223 | |
| 224 | if rule == "secure_config": |
| 225 | hits = sum(len(r["warnings"]) for r in lynis) |
| 226 | if not has_lynis: |
| 227 | return {"status": "not_assessed", "matched": 0, "evidence": ["No authenticated host audit available."]} |
| 228 | if hits: |
| 229 | return {"status": "attention", "matched": hits, "evidence": [f"{hits} hardening warning(s) from Lynis."]} |
| 230 | return {"status": "ok", "matched": 0, "evidence": ["No insecure-config warnings from Lynis."]} |
| 231 | |
| 232 | if rule == "default_credentials": |
| 233 | if credentials is None: |
| 234 | return {"status": "not_assessed", "matched": 0, "evidence": ["Credential data not supplied."]} |
| 235 | if credentials: |
| 236 | sample = ", ".join(sorted({c.get("ip", "?") for c in credentials})[:8]) |
| 237 | return {"status": "attention", "matched": len(credentials), |
| 238 | "evidence": [f"{len(credentials)} credential(s) recovered (hosts: {sample})."]} |
| 239 | return {"status": "ok", "matched": 0, "evidence": ["No credentials recovered by brute-force."]} |
| 240 | |
| 241 | if rule == "strong_crypto_transit": |
| 242 | flagged = [] |
| 243 | for host in hosts: |
| 244 | ports = {int(p) for p in re.findall(r"\d+", host.get("ports", "") or "")} |
| 245 | cleartext = sorted(ports & set(_CLEARTEXT_PORTS)) |
| 246 | if cleartext: |
| 247 | flagged.append((host.get("ip", "?"), cleartext)) |
| 248 | if not hosts: |
| 249 | return {"status": "not_assessed", "matched": 0, "evidence": ["No hosts discovered."]} |
| 250 | if flagged: |
| 251 | ev = [f"{ip}: {', '.join(_CLEARTEXT_PORTS[p] for p in ports)}" for ip, ports in flagged[:12]] |
| 252 | return {"status": "attention", "matched": len(flagged), "evidence": ev} |
| 253 | return {"status": "ok", "matched": 0, "evidence": ["No cleartext services exposed."]} |
| 254 | |
| 255 | if rule == "anti_malware": |
| 256 | hits = self._lynis_prefix_count(lynis, ("MALW",)) |
| 257 | if not has_lynis: |
| 258 | return {"status": "not_assessed", "matched": 0, "evidence": ["No authenticated host audit available."]} |
| 259 | if hits: |
| 260 | return {"status": "attention", "matched": hits, "evidence": [f"{hits} malware-defense finding(s) from Lynis."]} |
| 261 | return {"status": "ok", "matched": 0, "evidence": ["No malware-defense gaps detected by Lynis."]} |
| 262 | |
| 263 | if rule == "patch_vulns": |
| 264 | high = [f for f in findings if (f.get("severity") or "").lower() in _HIGH_SEVERITIES] |
| 265 | pkgs = sum(len(r["vulnerable_packages"]) for r in lynis) |
no test coverage detected