clouddetectx 3.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. clouddetectx/__init__.py +3 -0
  2. clouddetectx/__main__.py +5 -0
  3. clouddetectx/account.py +87 -0
  4. clouddetectx/alerts.py +32 -0
  5. clouddetectx/analysis.py +132 -0
  6. clouddetectx/checks/__init__.py +24 -0
  7. clouddetectx/checks/aws_account.py +281 -0
  8. clouddetectx/checks/azure_account.py +202 -0
  9. clouddetectx/checks/gcp_account.py +233 -0
  10. clouddetectx/checks/rest.py +96 -0
  11. clouddetectx/cli.py +967 -0
  12. clouddetectx/compliance.py +269 -0
  13. clouddetectx/config.py +390 -0
  14. clouddetectx/config_ui.py +484 -0
  15. clouddetectx/engine.py +355 -0
  16. clouddetectx/export.py +176 -0
  17. clouddetectx/history.py +122 -0
  18. clouddetectx/iam.py +344 -0
  19. clouddetectx/interactive.py +659 -0
  20. clouddetectx/models.py +189 -0
  21. clouddetectx/overview.py +131 -0
  22. clouddetectx/probe.py +110 -0
  23. clouddetectx/prompts.py +52 -0
  24. clouddetectx/providers/__init__.py +32 -0
  25. clouddetectx/providers/aws.py +1044 -0
  26. clouddetectx/providers/azure.py +559 -0
  27. clouddetectx/providers/base.py +90 -0
  28. clouddetectx/providers/gcp.py +555 -0
  29. clouddetectx/reporting.py +383 -0
  30. clouddetectx/risk.py +71 -0
  31. clouddetectx/schedule.py +152 -0
  32. clouddetectx/sensitive.py +442 -0
  33. clouddetectx/state.py +132 -0
  34. clouddetectx/waivers.py +123 -0
  35. clouddetectx/web/__init__.py +1 -0
  36. clouddetectx/web/server.py +1069 -0
  37. clouddetectx/web/static/app.js +1578 -0
  38. clouddetectx/web/static/index.html +37 -0
  39. clouddetectx/web/static/style.css +497 -0
  40. clouddetectx-3.0.0.dist-info/METADATA +166 -0
  41. clouddetectx-3.0.0.dist-info/RECORD +45 -0
  42. clouddetectx-3.0.0.dist-info/WHEEL +5 -0
  43. clouddetectx-3.0.0.dist-info/entry_points.txt +2 -0
  44. clouddetectx-3.0.0.dist-info/licenses/LICENSE +21 -0
  45. clouddetectx-3.0.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,3 @@
1
+ """CloudDetectX: detect, investigate and remediate public cloud storage."""
2
+
3
+ __version__ = "3.0.0"
@@ -0,0 +1,5 @@
1
+ import sys
2
+
3
+ from clouddetectx.cli import main
4
+
5
+ sys.exit(main())
@@ -0,0 +1,87 @@
1
+ """Account-level review: IAM principals plus account-wide findings (audit logging, root/owner
2
+ security, network exposure, public databases and snapshots, key rotation).
3
+
4
+ One AccountReview per scanned account (AWS account, Azure subscription, GCP project).
5
+ Several reviews (all-accounts scans) are merged with merge_reviews().
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ from dataclasses import dataclass, field
11
+ from datetime import datetime, timezone
12
+
13
+ from clouddetectx.models import Finding
14
+
15
+ ACCOUNT_PREFIX = "account:" # waiver resource for account-level findings: "account:<account id>"
16
+
17
+
18
+ def account_resource(account_id: str | None) -> str:
19
+ return f"{ACCOUNT_PREFIX}{account_id or 'default'}"
20
+
21
+
22
+ @dataclass
23
+ class AccountReview:
24
+ provider: str
25
+ account_id: str
26
+ iam: object | None = None # iam.IamReport
27
+ findings: list[Finding] = field(default_factory=list) # active (not waived), incl. IAM findings
28
+ waived: list[Finding] = field(default_factory=list)
29
+ notes: list[str] = field(default_factory=list)
30
+ errors: dict[str, str] = field(default_factory=dict) # check area -> error (e.g. missing permission)
31
+ analyzed_at: datetime = field(default_factory=lambda: datetime.now(timezone.utc))
32
+
33
+ @property
34
+ def waiver_resource(self) -> str:
35
+ return account_resource(self.account_id)
36
+
37
+ def counts(self) -> dict:
38
+ c = self.iam.counts() if self.iam is not None else {}
39
+ return {**c, "findings": len(self.findings), "waived": len(self.waived)}
40
+
41
+ def to_dict(self) -> dict:
42
+ return {
43
+ "provider": self.provider, "account_id": self.account_id,
44
+ "analyzed_at": self.analyzed_at.isoformat(),
45
+ "waiver_resource": self.waiver_resource,
46
+ "iam": self.iam.to_dict() if self.iam is not None else None,
47
+ "findings": [f.to_dict() for f in self.findings],
48
+ "waived": [f.to_dict() for f in self.waived],
49
+ "notes": self.notes, "errors": self.errors,
50
+ }
51
+
52
+
53
+ def merge_reviews(reviews: list[AccountReview]) -> AccountReview | None:
54
+ """Combine per-account reviews (all-accounts scans) into one view."""
55
+ reviews = [r for r in reviews if r is not None]
56
+ if not reviews:
57
+ return None
58
+ if len(reviews) == 1:
59
+ return reviews[0]
60
+ from clouddetectx.iam import IamReport
61
+
62
+ def tag(f: Finding, acct: str) -> Finding:
63
+ f.evidence = {**(f.evidence or {}), "account": acct}
64
+ return f
65
+
66
+ principals, iam_notes, skipped = [], [], 0
67
+ for r in reviews:
68
+ if r.iam is not None:
69
+ for p in r.iam.principals:
70
+ if not p.name.startswith(f"{r.account_id}/"):
71
+ p.name = f"{r.account_id}/{p.name}"
72
+ principals.append(p)
73
+ skipped += r.iam.skipped_service_roles
74
+ iam_notes += [n for n in r.iam.notes if n not in iam_notes]
75
+ merged_iam = None
76
+ if any(r.iam is not None for r in reviews):
77
+ merged_iam = IamReport("multiple", principals,
78
+ [f for r in reviews if r.iam is not None for f in r.iam.findings], iam_notes, skipped)
79
+ return AccountReview(
80
+ provider=reviews[0].provider,
81
+ account_id="all",
82
+ iam=merged_iam,
83
+ findings=[tag(f, r.account_id) for r in reviews for f in r.findings],
84
+ waived=[tag(f, r.account_id) for r in reviews for f in r.waived],
85
+ notes=[f"{r.account_id}: {n}" for r in reviews for n in r.notes],
86
+ errors={f"{r.account_id}: {k}": v for r in reviews for k, v in r.errors.items()},
87
+ )
clouddetectx/alerts.py ADDED
@@ -0,0 +1,32 @@
1
+ """Webhook alerts (Slack, Microsoft Teams incoming webhooks, or any endpoint accepting JSON).
2
+
3
+ The payload carries a plain `text` field (rendered by Slack and Teams) plus a structured
4
+ `clouddetectx` object for SIEMs and automation.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import json
10
+ import urllib.error
11
+ import urllib.request
12
+
13
+ from clouddetectx import __version__
14
+
15
+
16
+ class AlertError(RuntimeError):
17
+ pass
18
+
19
+
20
+ def send(webhook_url: str, title: str, lines: list[str], data: dict | None = None, timeout: float = 10.0) -> None:
21
+ text = f"*{title}*\n" + "\n".join(f"• {line}" for line in lines)
22
+ body = json.dumps({"text": text, "clouddetectx": {"title": title, "details": lines, **(data or {})}}).encode()
23
+ req = urllib.request.Request(webhook_url, data=body, method="POST",
24
+ headers={"Content-Type": "application/json", "User-Agent": f"CloudDetectX/{__version__}"})
25
+ try:
26
+ with urllib.request.urlopen(req, timeout=timeout) as resp:
27
+ if resp.status >= 300:
28
+ raise AlertError(f"Webhook returned HTTP {resp.status}")
29
+ except urllib.error.HTTPError as exc:
30
+ raise AlertError(f"Webhook returned HTTP {exc.code}: {exc.reason}") from exc
31
+ except (urllib.error.URLError, TimeoutError, OSError) as exc:
32
+ raise AlertError(f"Webhook request failed: {getattr(exc, 'reason', exc)}") from exc
@@ -0,0 +1,132 @@
1
+ """Provider-neutral analysis of access logs: did anyone outside actually get the data?"""
2
+
3
+ from __future__ import annotations
4
+
5
+ import ipaddress
6
+ from collections import Counter, defaultdict
7
+
8
+ from clouddetectx.models import AccessEvent, ChangeEvent, Indicator, Investigation, Severity
9
+
10
+ DOWNLOAD_OPS = {"GetObject"}
11
+ LIST_OPS = {"ListBucket"}
12
+ WRITE_OPS = {"PutObject", "DeleteObject", "PutObjectAcl"}
13
+
14
+ BULK_OBJECTS_PER_IP = 10
15
+ BULK_REQUESTS_PER_IP = 100
16
+ PROBING_DENIED_THRESHOLD = 5
17
+ TOOLING_AGENTS = ("curl", "wget", "python-requests", "python-urllib", "go-http-client", "aiohttp", "masscan",
18
+ "zgrab", "nuclei", "sqlmap", "nmap", "scrapy", "httpx", "bucket", "s3scanner", "grayhatwarfare")
19
+
20
+ VERDICT_TEXT = {
21
+ "DATA_ACCESSED": "Untrusted parties successfully read or modified data while it was exposed.",
22
+ "EXPOSURE_PROBED": "Untrusted parties reached the resource but no successful data read was recorded.",
23
+ "NO_EVIDENCE_OF_ACCESS": "Logs were available and show no access from untrusted parties.",
24
+ "NO_LOGS_AVAILABLE": "No access logs are available; it cannot be ruled out that data was accessed.",
25
+ }
26
+
27
+
28
+ def is_external(ip: str | None, trusted: list) -> bool:
29
+ if not ip:
30
+ return False
31
+ try:
32
+ addr = ipaddress.ip_address(ip.strip("[]"))
33
+ except ValueError:
34
+ return False # e.g. "AWS Internal" or a service hostname
35
+ if any(addr in net for net in trusted):
36
+ return False
37
+ return addr.is_global
38
+
39
+
40
+ def analyze_access(
41
+ events: list[AccessEvent],
42
+ changes: list[ChangeEvent],
43
+ sources: list[str],
44
+ notes: list[str],
45
+ trusted_networks: list,
46
+ ) -> Investigation:
47
+ def untrusted(e: AccessEvent) -> bool:
48
+ return e.anonymous or is_external(e.source_ip, trusted_networks)
49
+
50
+ suspect = [e for e in events if untrusted(e)]
51
+ downloads = [e for e in suspect if e.operation in DOWNLOAD_OPS and e.succeeded]
52
+ listings = [e for e in suspect if e.operation in LIST_OPS and e.succeeded]
53
+ writes = [e for e in suspect if e.operation in WRITE_OPS and e.succeeded]
54
+ denied = [e for e in suspect if e.status in (401, 403)]
55
+
56
+ per_ip_requests = Counter(e.source_ip or "unknown" for e in suspect)
57
+ per_ip_objects: dict[str, set] = defaultdict(set)
58
+ for e in downloads:
59
+ per_ip_objects[e.source_ip or "unknown"].add(e.key)
60
+
61
+ summary = {
62
+ "total_events": len(events),
63
+ "first_event": events[0].timestamp.isoformat() if events else None,
64
+ "last_event": events[-1].timestamp.isoformat() if events else None,
65
+ "unique_source_ips": len({e.source_ip for e in events if e.source_ip}),
66
+ "untrusted_requests": len(suspect),
67
+ "anonymous_requests": sum(1 for e in events if e.anonymous),
68
+ "external_ips": sorted({e.source_ip for e in suspect if e.source_ip}),
69
+ "objects_downloaded_by_untrusted": sorted({e.key for e in downloads if e.key}),
70
+ "bytes_downloaded_by_untrusted": sum(e.bytes_sent for e in downloads),
71
+ "successful_listings_by_untrusted": len(listings),
72
+ "writes_by_untrusted": len(writes),
73
+ "denied_requests_from_untrusted": len(denied),
74
+ "top_untrusted_ips": per_ip_requests.most_common(10),
75
+ }
76
+
77
+ indicators: list[Indicator] = []
78
+ if downloads:
79
+ indicators.append(Indicator(
80
+ Severity.CRITICAL, "Data downloaded by untrusted parties",
81
+ f"{len(summary['objects_downloaded_by_untrusted'])} object(s), {summary['bytes_downloaded_by_untrusted']:,} bytes, "
82
+ f"from {len({e.source_ip for e in downloads})} source IP(s).", "T1530",
83
+ ))
84
+ deletes = [e for e in writes if e.operation == "DeleteObject"]
85
+ if writes:
86
+ indicators.append(Indicator(
87
+ Severity.CRITICAL, "Data modified by untrusted parties",
88
+ f"{len(writes)} successful write/delete request(s): "
89
+ + ", ".join(sorted({f'{e.operation} {e.key}' for e in writes})[:10]),
90
+ "T1485" if deletes and len(deletes) == len(writes) else "T1565.001",
91
+ ))
92
+ if listings:
93
+ indicators.append(Indicator(Severity.HIGH, "Bucket contents enumerated",
94
+ f"{len(listings)} successful anonymous/external listing request(s).", "T1619"))
95
+ for ip, objs in per_ip_objects.items():
96
+ if len(objs) >= BULK_OBJECTS_PER_IP:
97
+ indicators.append(Indicator(Severity.HIGH, f"Bulk download from {ip}", f"{len(objs)} distinct objects downloaded.", "T1530"))
98
+ for ip, count in per_ip_requests.items():
99
+ if count >= BULK_REQUESTS_PER_IP and len(per_ip_objects.get(ip, ())) < BULK_OBJECTS_PER_IP:
100
+ indicators.append(Indicator(Severity.MEDIUM, f"High request volume from {ip}", f"{count} requests."))
101
+ if len(denied) >= PROBING_DENIED_THRESHOLD:
102
+ indicators.append(Indicator(Severity.MEDIUM, "Access-denied probing",
103
+ f"{len(denied)} denied requests from untrusted sources.", "T1595"))
104
+ tools = sorted({e.user_agent for e in suspect if e.user_agent and any(t in e.user_agent.lower() for t in TOOLING_AGENTS)})
105
+ if tools:
106
+ indicators.append(Indicator(Severity.MEDIUM, "Automated tooling observed", "; ".join(t[:80] for t in tools[:5])))
107
+ for c in changes:
108
+ indicators.append(Indicator(Severity.INFO, f"Configuration change: {c.action}",
109
+ f"{c.timestamp:%Y-%m-%d %H:%M:%S} UTC by {c.actor or 'unknown'} from {c.source_ip or 'unknown IP'}"))
110
+
111
+ if not sources:
112
+ verdict = "NO_LOGS_AVAILABLE"
113
+ elif downloads or writes:
114
+ verdict = "DATA_ACCESSED"
115
+ elif suspect:
116
+ verdict = "EXPOSURE_PROBED"
117
+ else:
118
+ verdict = "NO_EVIDENCE_OF_ACCESS"
119
+
120
+ # Keep reports bounded: every untrusted event plus a sample of the rest.
121
+ kept = suspect + [e for e in events if not untrusted(e)][: max(0, 2000 - len(suspect))]
122
+ kept.sort(key=lambda e: e.timestamp)
123
+
124
+ return Investigation(
125
+ verdict=verdict,
126
+ summary=summary,
127
+ indicators=indicators,
128
+ access_events=kept,
129
+ change_events=changes,
130
+ log_sources=sources,
131
+ notes=notes,
132
+ )
@@ -0,0 +1,24 @@
1
+ """Account-wide checks (beyond individual buckets) and Azure/GCP identity analysis."""
2
+
3
+ # Ports that should never be open to the whole internet.
4
+ EXPOSED_PORTS = {
5
+ 22: "SSH", 23: "Telnet", 445: "SMB", 1433: "SQL Server", 1521: "Oracle", 2375: "Docker API",
6
+ 3306: "MySQL", 3389: "RDP", 5432: "PostgreSQL", 5601: "Kibana", 6379: "Redis", 9200: "Elasticsearch",
7
+ 11211: "Memcached", 27017: "MongoDB",
8
+ }
9
+ OPEN_CIDRS = {"0.0.0.0/0", "::/0", "*", "Internet", "Any", "0.0.0.0"}
10
+
11
+
12
+ def risky_ports(from_port, to_port, all_traffic: bool = False) -> list[str]:
13
+ """Names of sensitive ports inside [from_port, to_port]."""
14
+ if all_traffic or from_port in (None, -1):
15
+ return ["all ports"]
16
+ lo, hi = int(from_port), int(to_port if to_port not in (None, -1) else from_port)
17
+ if lo == 0 and hi >= 65535:
18
+ return ["all ports"]
19
+ return [f"{p} ({name})" for p, name in sorted(EXPOSED_PORTS.items()) if lo <= p <= hi]
20
+
21
+
22
+ def short_list(items: list, limit: int = 8) -> str:
23
+ items = [str(i) for i in items]
24
+ return ", ".join(items[:limit]) + (f" and {len(items) - limit} more" if len(items) > limit else "")
@@ -0,0 +1,281 @@
1
+ """AWS account-wide checks: CloudTrail, root account, password policy, security groups, public
2
+ databases and snapshots, KMS key rotation, EBS default encryption, EKS endpoints, account-level
3
+ S3 Block Public Access. Read-only; every check degrades to a note when a permission is missing.
4
+ """
5
+
6
+ from __future__ import annotations
7
+
8
+ from concurrent.futures import ThreadPoolExecutor
9
+
10
+ from clouddetectx.checks import risky_ports, short_list
11
+ from clouddetectx.models import Finding, Severity
12
+
13
+ PAB_FLAGS = ("BlockPublicAcls", "IgnorePublicAcls", "BlockPublicPolicy", "RestrictPublicBuckets")
14
+ MAX_PER_REGION = 500
15
+
16
+
17
+ def _code(exc) -> str:
18
+ return getattr(exc, "response", {}).get("Error", {}).get("Code", type(exc).__name__)
19
+
20
+
21
+ def run(provider) -> tuple[list[Finding], list[str], dict[str, str]]:
22
+ from botocore.exceptions import BotoCoreError, ClientError
23
+
24
+ findings: list[Finding] = []
25
+ notes: list[str] = []
26
+ errors: dict[str, str] = {}
27
+ home = provider.session.region_name or "us-east-1"
28
+
29
+ def guard(area: str, fn):
30
+ try:
31
+ return fn()
32
+ except (ClientError, BotoCoreError) as exc:
33
+ errors[area] = _code(exc)
34
+ return None
35
+
36
+ wanted = [r.strip() for r in (provider.settings.aws_check_regions or "").split(",") if r.strip()]
37
+ regions = wanted or guard("regions", lambda: [r["RegionName"] for r in provider._client("ec2", home).describe_regions()["Regions"]]) or [home]
38
+
39
+ # ---- global checks
40
+ findings += guard("cloudtrail", lambda: cloudtrail(provider, home)) or []
41
+ findings += guard("root account", lambda: root_account(provider)) or []
42
+ findings += guard("password policy", lambda: password_policy(provider)) or []
43
+ findings += guard("account Block Public Access", lambda: account_bpa(provider)) or []
44
+
45
+ # ---- regional checks, in parallel
46
+ def regional(region):
47
+ out = {}
48
+ for area, fn in (("security groups", open_security_groups), ("RDS", public_rds),
49
+ ("snapshots", public_snapshots), ("KMS", kms_rotation),
50
+ ("EBS encryption", ebs_default_encryption), ("EKS", eks_endpoints)):
51
+ try:
52
+ out[area] = fn(provider, region)
53
+ except (ClientError, BotoCoreError) as exc:
54
+ code = _code(exc)
55
+ if code not in ("OptInRequired", "AuthFailure", "UnrecognizedClientException", "InvalidClientTokenId"):
56
+ errors.setdefault(area, f"{code} ({region})")
57
+ out[area] = []
58
+ return out
59
+
60
+ with ThreadPoolExecutor(max_workers=min(8, len(regions)), thread_name_prefix="clouddetectx-region") as pool:
61
+ per_region = dict(zip(regions, pool.map(regional, regions)))
62
+
63
+ def gather(area):
64
+ return [dict(item, region=region) for region, data in per_region.items() for item in data.get(area, [])]
65
+
66
+ sgs = gather("security groups")
67
+ if sgs:
68
+ findings.append(Finding(
69
+ "AWS-ACCT-005", "Security groups expose admin or database ports to the internet", Severity.HIGH,
70
+ f"{len(sgs)} security group rule(s) attached to running resources allow 0.0.0.0/0 or ::/0 on "
71
+ f"sensitive ports: " + short_list([f"{g['group']} ({g['region']}): {', '.join(g['ports'])}" for g in sgs], 6) + ".",
72
+ "Restrict these rules to your own IP ranges, or use SSM Session Manager / a VPN / private endpoints instead.",
73
+ evidence={"rules": sgs[:200]}))
74
+ rds = gather("RDS")
75
+ if rds:
76
+ findings.append(Finding(
77
+ "AWS-ACCT-006", "Databases are publicly accessible", Severity.HIGH,
78
+ f"{len(rds)} RDS instance(s) have PubliclyAccessible=true: "
79
+ + short_list([f"{d['id']} ({d['region']})" for d in rds]) + ".",
80
+ "Set PubliclyAccessible=false and reach databases from inside the VPC.",
81
+ evidence={"instances": rds[:200]}))
82
+ snaps = gather("snapshots")
83
+ if snaps:
84
+ findings.append(Finding(
85
+ "AWS-ACCT-007", "Snapshots are shared publicly", Severity.CRITICAL,
86
+ f"{len(snaps)} snapshot(s) can be restored by ANY AWS account: "
87
+ + short_list([f"{x['type']} {x['id']} ({x['region']})" for x in snaps]) + ". Anyone can copy the full disk or database.",
88
+ "Remove the public 'all' restore / createVolumePermission, and enable EBS snapshot Block Public Access.",
89
+ exposes_data=True, evidence={"snapshots": snaps[:200]}))
90
+ keys = gather("KMS")
91
+ if keys:
92
+ findings.append(Finding(
93
+ "AWS-ACCT-008", "KMS key rotation is disabled", Severity.MEDIUM,
94
+ f"{len(keys)} customer-managed symmetric key(s) do not rotate automatically: "
95
+ + short_list([f"{k['id']} ({k['region']})" for k in keys]) + ".",
96
+ "Enable automatic key rotation on customer-managed KMS keys.", evidence={"keys": keys[:200]}))
97
+ ebs = [r for r, d in per_region.items() if d.get("EBS encryption")]
98
+ if ebs:
99
+ findings.append(Finding(
100
+ "AWS-ACCT-009", "EBS encryption by default is off", Severity.LOW,
101
+ f"New EBS volumes are not encrypted by default in {len(ebs)} region(s): {short_list(sorted(ebs))}.",
102
+ "Enable EBS encryption by default in every region you use.", evidence={"regions": sorted(ebs)}))
103
+ eks = gather("EKS")
104
+ if eks:
105
+ findings.append(Finding(
106
+ "AWS-ACCT-010", "Kubernetes API endpoints are open to the internet", Severity.HIGH,
107
+ f"{len(eks)} EKS cluster(s) expose the API endpoint to 0.0.0.0/0: "
108
+ + short_list([f"{c['name']} ({c['region']})" for c in eks]) + ".",
109
+ "Make the endpoint private, or restrict publicAccessCidrs to your own networks.",
110
+ evidence={"clusters": eks[:200]}))
111
+ if errors:
112
+ notes.append("Some account checks were skipped for missing permissions: "
113
+ + ", ".join(f"{k} ({v})" for k, v in sorted(errors.items())) + ".")
114
+ return findings, notes, errors
115
+
116
+
117
+ # --------------------------------------------------------------------------- global
118
+
119
+ def cloudtrail(provider, home) -> list[Finding]:
120
+ ct = provider._client("cloudtrail", home)
121
+ trails = ct.describe_trails(includeShadowTrails=True).get("trailList", [])
122
+ logging_multi = []
123
+ for t in trails:
124
+ if not t.get("IsMultiRegionTrail"):
125
+ continue
126
+ status = ct.get_trail_status(Name=t["TrailARN"])
127
+ if status.get("IsLogging"):
128
+ logging_multi.append(t["Name"])
129
+ if logging_multi:
130
+ return []
131
+ return [Finding(
132
+ "AWS-ACCT-001", "No multi-region CloudTrail trail is logging", Severity.HIGH,
133
+ f"{len(trails)} trail(s) found, but none is multi-region and logging. API activity (including who made a bucket "
134
+ "public) is only kept for 90 days in event history and is not delivered anywhere.",
135
+ "Create a multi-region trail with log file validation, delivered to a locked-down S3 bucket.",
136
+ evidence={"trails": [t.get("Name") for t in trails]})]
137
+
138
+
139
+ def root_account(provider) -> list[Finding]:
140
+ summary = provider._client("iam", "us-east-1").get_account_summary()["SummaryMap"]
141
+ out = []
142
+ if not summary.get("AccountMFAEnabled"):
143
+ out.append(Finding(
144
+ "AWS-ACCT-002", "Root account has no MFA", Severity.CRITICAL,
145
+ "The root user can do anything in the account, including deleting every bucket, and is protected only by a password.",
146
+ "Enable a hardware or virtual MFA device on the root user and stop using it for daily work."))
147
+ if summary.get("AccountAccessKeysPresent"):
148
+ out.append(Finding(
149
+ "AWS-ACCT-003", "Root account has access keys", Severity.CRITICAL,
150
+ "Root access keys give unrestricted, unscoped API access and can't be limited by IAM policies.",
151
+ "Delete the root access keys and use IAM roles instead."))
152
+ return out
153
+
154
+
155
+ def password_policy(provider) -> list[Finding]:
156
+ from botocore.exceptions import ClientError
157
+
158
+ iam = provider._client("iam", "us-east-1")
159
+ try:
160
+ policy = iam.get_account_password_policy()["PasswordPolicy"]
161
+ except ClientError as exc:
162
+ if _code(exc) != "NoSuchEntity":
163
+ raise
164
+ return [Finding("AWS-ACCT-004", "No IAM password policy", Severity.MEDIUM,
165
+ "IAM users can set short, simple passwords that never expire.",
166
+ "Set a password policy: at least 14 characters and prevent reuse of the last 24.")]
167
+ weak = []
168
+ if policy.get("MinimumPasswordLength", 0) < 14:
169
+ weak.append(f"minimum length {policy.get('MinimumPasswordLength', 0)} (< 14)")
170
+ if not policy.get("PasswordReusePrevention"):
171
+ weak.append("password reuse allowed")
172
+ if weak:
173
+ return [Finding("AWS-ACCT-004", "Weak IAM password policy", Severity.MEDIUM, "Password policy: " + "; ".join(weak) + ".",
174
+ "Require at least 14 characters and prevent reuse of the last 24 passwords.",
175
+ evidence={"policy": policy})]
176
+ return []
177
+
178
+
179
+ def account_bpa(provider) -> list[Finding]:
180
+ from botocore.exceptions import ClientError
181
+
182
+ try:
183
+ cfg = provider._client("s3control").get_public_access_block(AccountId=provider.account_id())["PublicAccessBlockConfiguration"]
184
+ except ClientError as exc:
185
+ if _code(exc) != "NoSuchPublicAccessBlockConfiguration":
186
+ raise
187
+ cfg = {}
188
+ missing = [f for f in PAB_FLAGS if not cfg.get(f)]
189
+ if not missing:
190
+ return []
191
+ return [Finding("AWS-ACCT-011", "Account-level S3 Block Public Access is not fully on", Severity.MEDIUM,
192
+ f"Off at account level: {', '.join(missing)}. Any bucket without its own Block Public Access can be made public.",
193
+ "Turn on all four Block Public Access settings for the account (S3 console > Block Public Access settings for this account).",
194
+ evidence={"account": cfg})]
195
+
196
+
197
+ # --------------------------------------------------------------------------- regional
198
+
199
+ def open_security_groups(provider, region) -> list[dict]:
200
+ ec2 = provider._client("ec2", region)
201
+ in_use = set()
202
+ for page in ec2.get_paginator("describe_network_interfaces").paginate():
203
+ for eni in page.get("NetworkInterfaces", []):
204
+ in_use.update(g["GroupId"] for g in eni.get("Groups", []))
205
+ out = []
206
+ for page in ec2.get_paginator("describe_security_groups").paginate():
207
+ for sg in page.get("SecurityGroups", []):
208
+ if sg["GroupId"] not in in_use:
209
+ continue
210
+ for perm in sg.get("IpPermissions", []):
211
+ open_v4 = any(r.get("CidrIp") == "0.0.0.0/0" for r in perm.get("IpRanges", []))
212
+ open_v6 = any(r.get("CidrIpv6") == "::/0" for r in perm.get("Ipv6Ranges", []))
213
+ if not (open_v4 or open_v6):
214
+ continue
215
+ ports = risky_ports(perm.get("FromPort"), perm.get("ToPort"), perm.get("IpProtocol") == "-1")
216
+ if ports:
217
+ out.append({"group": f"{sg.get('GroupName')} ({sg['GroupId']})", "group_id": sg["GroupId"],
218
+ "ports": ports, "cidr": "0.0.0.0/0" if open_v4 else "::/0"})
219
+ if len(out) >= MAX_PER_REGION:
220
+ return out
221
+ return out
222
+
223
+
224
+ def public_rds(provider, region) -> list[dict]:
225
+ rds = provider._client("rds", region)
226
+ out = []
227
+ for page in rds.get_paginator("describe_db_instances").paginate():
228
+ for db in page.get("DBInstances", []):
229
+ if db.get("PubliclyAccessible"):
230
+ out.append({"id": db["DBInstanceIdentifier"], "engine": db.get("Engine")})
231
+ return out
232
+
233
+
234
+ def public_snapshots(provider, region) -> list[dict]:
235
+ out = []
236
+ ec2 = provider._client("ec2", region)
237
+ for page in ec2.get_paginator("describe_snapshots").paginate(OwnerIds=["self"], RestorableByUserIds=["all"]):
238
+ own = provider.account_id() # only this account's snapshots (never AWS's own public ones)
239
+ out += [{"type": "EBS", "id": s["SnapshotId"]} for s in page.get("Snapshots", []) if s.get("OwnerId") in (None, own)]
240
+ rds = provider._client("rds", region)
241
+ checked = 0
242
+ for page in rds.get_paginator("describe_db_snapshots").paginate(SnapshotType="manual"):
243
+ for snap in page.get("DBSnapshots", []):
244
+ checked += 1
245
+ if checked > MAX_PER_REGION:
246
+ return out
247
+ attrs = rds.describe_db_snapshot_attributes(DBSnapshotIdentifier=snap["DBSnapshotIdentifier"])
248
+ for a in attrs["DBSnapshotAttributesResult"].get("DBSnapshotAttributes", []):
249
+ if a.get("AttributeName") == "restore" and "all" in (a.get("AttributeValues") or []):
250
+ out.append({"type": "RDS", "id": snap["DBSnapshotIdentifier"]})
251
+ return out
252
+
253
+
254
+ def kms_rotation(provider, region) -> list[dict]:
255
+ kms = provider._client("kms", region)
256
+ out = []
257
+ for page in kms.get_paginator("list_keys").paginate():
258
+ for k in page.get("Keys", [])[:MAX_PER_REGION]:
259
+ meta = kms.describe_key(KeyId=k["KeyId"])["KeyMetadata"]
260
+ if (meta.get("KeyManager") != "CUSTOMER" or meta.get("KeyState") != "Enabled"
261
+ or meta.get("KeySpec", "SYMMETRIC_DEFAULT") != "SYMMETRIC_DEFAULT" or meta.get("Origin") != "AWS_KMS"):
262
+ continue
263
+ if not kms.get_key_rotation_status(KeyId=k["KeyId"]).get("KeyRotationEnabled"):
264
+ out.append({"id": k["KeyId"]})
265
+ return out
266
+
267
+
268
+ def ebs_default_encryption(provider, region) -> list[dict]:
269
+ enabled = provider._client("ec2", region).get_ebs_encryption_by_default().get("EbsEncryptionByDefault")
270
+ return [] if enabled else [{"region": region}]
271
+
272
+
273
+ def eks_endpoints(provider, region) -> list[dict]:
274
+ eks = provider._client("eks", region)
275
+ out = []
276
+ for page in eks.get_paginator("list_clusters").paginate():
277
+ for name in page.get("clusters", []):
278
+ vpc = eks.describe_cluster(name=name)["cluster"].get("resourcesVpcConfig", {})
279
+ if vpc.get("endpointPublicAccess") and "0.0.0.0/0" in (vpc.get("publicAccessCidrs") or ["0.0.0.0/0"]):
280
+ out.append({"name": name})
281
+ return out