import ipaddress


def rule(event):
    if all(
        [
            any(
                [
                    event.deep_get("Image", default="").endswith("\\powershell.exe"),
                    event.deep_get("Image", default="").endswith("\\pwsh.exe"),
                ]
            ),
            event.deep_get("Initiated", default="") == "true",
            not any(
                [
                    all(
                        [
                            any(
                                [
                                    ipaddress.ip_address(
                                        event.deep_get("DestinationIp", default="")
                                    )
                                    in ipaddress.ip_network("127.0.0.0/8"),
                                    ipaddress.ip_address(
                                        event.deep_get("DestinationIp", default="")
                                    )
                                    in ipaddress.ip_network("10.0.0.0/8"),
                                    ipaddress.ip_address(
                                        event.deep_get("DestinationIp", default="")
                                    )
                                    in ipaddress.ip_network("169.254.0.0/16"),
                                    ipaddress.ip_address(
                                        event.deep_get("DestinationIp", default="")
                                    )
                                    in ipaddress.ip_network("172.16.0.0/12"),
                                    ipaddress.ip_address(
                                        event.deep_get("DestinationIp", default="")
                                    )
                                    in ipaddress.ip_network("192.168.0.0/16"),
                                    ipaddress.ip_address(
                                        event.deep_get("DestinationIp", default="")
                                    )
                                    in ipaddress.ip_network("::1/128"),
                                    ipaddress.ip_address(
                                        event.deep_get("DestinationIp", default="")
                                    )
                                    in ipaddress.ip_network("fe80::/10"),
                                    ipaddress.ip_address(
                                        event.deep_get("DestinationIp", default="")
                                    )
                                    in ipaddress.ip_network("fc00::/7"),
                                ]
                            ),
                            any(
                                [
                                    "AUTHORI" in event.deep_get("User", default=""),
                                    "AUTORI" in event.deep_get("User", default=""),
                                ]
                            ),
                        ]
                    ),
                    any(
                        [
                            ipaddress.ip_address(event.deep_get("DestinationIp", default=""))
                            in ipaddress.ip_network("20.184.0.0/13"),
                            ipaddress.ip_address(event.deep_get("DestinationIp", default=""))
                            in ipaddress.ip_network("51.103.210.0/23"),
                        ]
                    ),
                ]
            ),
        ]
    ):
        return True
    return False
