def rule(event):
    if all(
        [
            any(
                [
                    all(
                        [
                            event.deep_get("EventID", default="") == 4720,
                            "$" in event.deep_get("SamAccountName", default=""),
                        ]
                    ),
                    all(
                        [
                            event.deep_get("EventID", default="") == 4781,
                            "$" in event.deep_get("NewTargetUserName", default=""),
                        ]
                    ),
                ]
            ),
            not all(
                [
                    event.deep_get("EventID", default="") == 4720,
                    event.deep_get("TargetUserName", default="") == "HomeGroupUser$",
                ]
            ),
        ]
    ):
        return True
    return False
