Download demo/src/ruleset.py from AriaAICompany/cloudtrail-lab: direct link, hf CLI and curl.
- Browser
- Download file 6.4 kB
-
https://huggingface.co/datasets/AriaAICompany/cloudtrail-lab/resolve/main/demo/src/ruleset.py
- Command line
-
hf download hf://datasets/AriaAICompany/cloudtrail-lab/demo/src/ruleset.py
-
curl -L -o ruleset.py https://huggingface.co/datasets/AriaAICompany/cloudtrail-lab/resolve/main/demo/src/ruleset.py
6.4 kB
| from __future__ import annotations | |
| from typing import Any | |
| from src.domain import ( | |
| ADMIN_POLICY_MARKERS, | |
| MAINTENANCE_ACCOUNT, | |
| MAINTENANCE_ACTOR, | |
| MAINTENANCE_ALLOWED_TYPES, | |
| MAINTENANCE_TICKET, | |
| PUBLIC_MARKERS, | |
| ChangeType, | |
| Severity, | |
| ) | |
| EVENT_MAP: dict[str, ChangeType] = { | |
| "AttachUserPolicy": ChangeType.IAM_PRIVILEGE_GRANT, | |
| "AttachRolePolicy": ChangeType.IAM_PRIVILEGE_GRANT, | |
| "AttachGroupPolicy": ChangeType.IAM_PRIVILEGE_GRANT, | |
| "PutUserPolicy": ChangeType.IAM_PRIVILEGE_GRANT, | |
| "PutRolePolicy": ChangeType.IAM_PRIVILEGE_GRANT, | |
| "PutGroupPolicy": ChangeType.IAM_PRIVILEGE_GRANT, | |
| "CreateAccessKey": ChangeType.IAM_CREDENTIAL_CREATE, | |
| "CreateLoginProfile": ChangeType.IAM_CREDENTIAL_CREATE, | |
| "CreateUser": ChangeType.IAM_CREDENTIAL_CREATE, | |
| "AuthorizeSecurityGroupIngress": ChangeType.SECURITY_GROUP_INGRESS, | |
| "PutBucketAcl": ChangeType.S3_PUBLIC_EXPOSURE, | |
| "PutBucketPolicy": ChangeType.S3_PUBLIC_EXPOSURE, | |
| "DeletePublicAccessBlock": ChangeType.S3_PUBLIC_EXPOSURE, | |
| "PutPublicAccessBlock": ChangeType.S3_PUBLIC_EXPOSURE, | |
| "StopLogging": ChangeType.TRAIL_LOGGING_CHANGE, | |
| "DeleteTrail": ChangeType.TRAIL_LOGGING_CHANGE, | |
| "UpdateTrail": ChangeType.TRAIL_LOGGING_CHANGE, | |
| "StartLogging": ChangeType.TRAIL_LOGGING_CHANGE, | |
| "PutKeyPolicy": ChangeType.KMS_CONTROL_CHANGE, | |
| "DisableKey": ChangeType.KMS_CONTROL_CHANGE, | |
| "ScheduleKeyDeletion": ChangeType.KMS_CONTROL_CHANGE, | |
| "AssociateAddress": ChangeType.INSTANCE_EXPOSURE, | |
| "ModifyInstanceAttribute": ChangeType.INSTANCE_EXPOSURE, | |
| "UpdateAssumeRolePolicy": ChangeType.ROLE_TRUST_CHANGE, | |
| "UpdateDetector": ChangeType.DETECTOR_DISABLE, | |
| "DeleteDetector": ChangeType.DETECTOR_DISABLE, | |
| "StopConfigurationRecorder": ChangeType.DETECTOR_DISABLE, | |
| "DeleteConfigRule": ChangeType.DETECTOR_DISABLE, | |
| "ConsoleLogin": ChangeType.ROOT_ACTIVITY, | |
| } | |
| HARDENING_EVENTS = {"StartLogging"} | |
| RESTRICT_PUBLIC_KEYS = ("restrictpublicbuckets", "blockpublicacls", "ignorepublicacls", "blockpublicpolicy") | |
| def _lower_blob(params: Any) -> str: | |
| if params is None: | |
| return "" | |
| if isinstance(params, dict): | |
| parts = [] | |
| for key, value in params.items(): | |
| parts.append(str(key).lower()) | |
| parts.append(_lower_blob(value)) | |
| return " ".join(parts) | |
| if isinstance(params, list): | |
| return " ".join(_lower_blob(item) for item in params) | |
| return str(params).lower() | |
| def classify_event(event_name: str, actor_kind: str = "", request: Any = None) -> ChangeType: | |
| name = str(event_name or "").strip() | |
| mapped = EVENT_MAP.get(name, ChangeType.NONE) | |
| if actor_kind == "root" and name in {"ConsoleLogin", "CreateAccessKey", "CreateLoginProfile", "CreateUser"}: | |
| return ChangeType.ROOT_ACTIVITY | |
| if name == "ConsoleLogin" and actor_kind != "root": | |
| return ChangeType.NONE | |
| if name == "PutPublicAccessBlock" and _is_restrict_public(request): | |
| return ChangeType.NONE | |
| if name == "StartLogging": | |
| return ChangeType.TRAIL_LOGGING_CHANGE | |
| return mapped | |
| def _is_restrict_public(request: Any) -> bool: | |
| blob = _lower_blob(request) | |
| if not blob: | |
| return False | |
| truths = sum(f"{key} true" in blob or f"{key}\":true" in blob or f"{key}=true" in blob for key in RESTRICT_PUBLIC_KEYS) | |
| return truths >= 1 | |
| def flags_from_request(change_type: str, event_name: str, request: Any, actor_kind: str) -> dict[str, bool]: | |
| blob = _lower_blob(request) | |
| cidr_open = "0.0.0.0/0" in blob or "::/0" in blob | |
| policy_public = any(marker in blob for marker in PUBLIC_MARKERS) or "allusers" in blob | |
| if change_type == ChangeType.S3_PUBLIC_EXPOSURE.value and event_name == "DeletePublicAccessBlock": | |
| policy_public = True | |
| admin_policy = any(marker in blob for marker in ADMIN_POLICY_MARKERS) | |
| logging_off = event_name in {"StopLogging", "DeleteTrail"} or ( | |
| event_name == "UpdateTrail" and ("islogging false" in blob or "islogging\":false" in blob) | |
| ) | |
| detector_off = event_name in {"DeleteDetector", "StopConfigurationRecorder", "DeleteConfigRule"} or ( | |
| event_name == "UpdateDetector" and ("enable false" in blob or "enable\":false" in blob) | |
| ) | |
| wildcard_trust = change_type == ChangeType.ROLE_TRUST_CHANGE.value and ( | |
| '"principal":"*"' in blob.replace(" ", "") or "principal *" in blob or '"aws":"*"' in blob.replace(" ", "") | |
| ) | |
| return { | |
| "cidr_open": cidr_open, | |
| "policy_public": policy_public, | |
| "admin_policy": admin_policy, | |
| "logging_off": logging_off, | |
| "detector_off": detector_off, | |
| "wildcard_trust": wildcard_trust, | |
| "is_root": actor_kind == "root", | |
| "hardening": event_name in HARDENING_EVENTS or _is_restrict_public(request), | |
| } | |
| def severity_for(change_type: str, flags: dict[str, bool]) -> Severity: | |
| if flags.get("is_root") or flags.get("logging_off") or flags.get("detector_off"): | |
| return Severity.HIGH | |
| if flags.get("policy_public") or flags.get("cidr_open") or flags.get("wildcard_trust") or flags.get("admin_policy"): | |
| return Severity.HIGH | |
| if change_type in { | |
| ChangeType.IAM_PRIVILEGE_GRANT.value, | |
| ChangeType.IAM_CREDENTIAL_CREATE.value, | |
| ChangeType.KMS_CONTROL_CHANGE.value, | |
| ChangeType.INSTANCE_EXPOSURE.value, | |
| ChangeType.TRAIL_LOGGING_CHANGE.value, | |
| ChangeType.S3_PUBLIC_EXPOSURE.value, | |
| ChangeType.ROLE_TRUST_CHANGE.value, | |
| ChangeType.DETECTOR_DISABLE.value, | |
| ChangeType.ROOT_ACTIVITY.value, | |
| }: | |
| if flags.get("hardening"): | |
| return Severity.LOW | |
| return Severity.MEDIUM | |
| if change_type == ChangeType.SECURITY_GROUP_INGRESS.value: | |
| return Severity.LOW | |
| return Severity.LOW | |
| def maintenance_match(account_alias: str, actor_alias: str, change_type: str, flags: dict[str, bool], ticket: str, in_window: bool) -> bool: | |
| if not in_window: | |
| return False | |
| if account_alias != MAINTENANCE_ACCOUNT: | |
| return False | |
| if actor_alias != MAINTENANCE_ACTOR: | |
| return False | |
| if ticket and ticket != MAINTENANCE_TICKET: | |
| return False | |
| if change_type not in MAINTENANCE_ALLOWED_TYPES: | |
| return False | |
| if flags.get("admin_policy") or flags.get("cidr_open") or flags.get("policy_public") or flags.get("is_root"): | |
| return False | |
| return True | |