import re


def rule(event):
    if all(
        [
            any(
                [
                    any(
                        [
                            event.deep_get("Image", default="").endswith("\\powershell.exe"),
                            event.deep_get("Image", default="").endswith("\\pwsh.exe"),
                        ]
                    ),
                    event.deep_get("OriginalFileName", default="")
                    in ["PowerShell.EXE", "pwsh.dll"],
                ]
            ),
            any(
                [
                    re.match(
                        r"\\+.*\\+.*\\+.*\\+.*\\+.*\\+.*\\+.*\\+.*\\+.*\\+.*\\+.*\\+.*\\+.*\\+",
                        event.deep_get("CommandLine", default=""),
                    ),
                    re.match(
                        r"\\{.*\\{.*\\{.*\\{.*\\{.*\\{.*\\{.*\\{.*\\{.*\\{",
                        event.deep_get("CommandLine", default=""),
                    ),
                    re.match(r"\\^.*\\^.*\\^.*\\^.*\\^", event.deep_get("CommandLine", default="")),
                    re.match(r"`.*`.*`.*`.*`", event.deep_get("CommandLine", default="")),
                ]
            ),
            not any(
                [
                    event.deep_get("ParentImage", default="")
                    == "C:\\Program Files\\Amazon\\SSM\\ssm-document-worker.exe",
                    any(
                        [
                            'new EventSource("Microsoft.Windows.Sense.Client.Management"'
                            in event.deep_get("CommandLine", default=""),
                            "public static extern bool InstallELAMCertificateInfo(SafeFileHandle handle);"
                            in event.deep_get("CommandLine", default=""),
                        ]
                    ),
                ]
            ),
        ]
    ):
        return True
    return False
