Page 1 of 2

Failed Logon Alert Configuration Assistance

Posted: Mon Dec 16, 2019 12:35 pm
by rferebee
Good morning,

I'm attempting to setup an alert that triggers whenever any account has failed a logon attempt more than 10 times in a 10 minute period. I understand that I can do this with either a custom JSON query or it looks like I can do it by setting up a Real-Time alert?

I found some helpful information in other support threads: https://support.nagios.com/forum/viewto ... s&start=10

But, I'm not super familiar with JSON and the alert isn't behaving like I expect it to, here's my example:

Code: Select all

{
    "query": {
        "filtered": {
            "query": {
                "bool": {
                    "should": [
                        {
                            "query_string": {
                                "query": "*"
                            }
                        }
                    ]
                }
            },
            "filter": {
                "bool": {
                    "must": [
                        {
                            "range": {
                                "@timestamp": {
                                    "from": 1576514353823,
                                    "to": 1576515253823
                                }
                            }
                        },
                        {
                            "fquery": {
                                "query": {
                                    "query_string": {
                                        "query": "_type:(\"eventlog\")"
                                    }
                                },
                                "_cache": true
                            }
                        },
                        {
                            "fquery": {
                                "query": {
                                    "query_string": {
                                        "query": "EventID:(\"4625\" \"4771\" \"4776\")"
                                    }
                                },
                                "_cache": true
                            }
                        },
                        {
                            "fquery": {
                                "query": {
                                    "query_string": {
                                        "query": "TargetUserName:((count >10))"
                                    }
                                },
                                "_cache": true
                            }
                        }
                    ],
                    "must_not": [
                        {
                            "fquery": {
                                "query": {
                                    "query_string": {
                                        "query": "TargetUserSid:(\"S-1-0-0\")"
                                    }
                                },
                                "_cache": true
                            }
                        },
                        {
                            "fquery": {
                                "query": {
                                    "query_string": {
                                        "query": "TargetUserName:(\"Altiris\")"
                                    }
                                },
                                "_cache": true
                            }
                        }
                    ]
                }
            }
        }
    }
}
With the above query I think I'm saying, during x time period look for event ID 4625 or 4771 or 4776 where any TargetUserName occurs more than 10 times and does not equal Altiris and does not have S-1-0-0 in the message field.

It's reporting that I have 17746 matching entries, so I either my environment is on the brink of collapse or I'm doing something wrong. My guess is the latter... Would I be better off with the custom query or should I use the Real-Time alert function?

The other thing I want to have happen is, in the email that's generated, I want it to provide a list of the unique TargetUserNames. I can do this with other alerts by using a line like this: <li>%uniquehosts%</li> but I don't know the comparable variable to get a list of users rather than hosts.

Can you folks help me out?

Re: Failed Logon Alert Configuration Assistance

Posted: Mon Dec 16, 2019 5:56 pm
by cdienger
I wasn't able to get it to work with teh count option, but this seems to do the trick:

Code: Select all

{
        "query": {
            "filtered": {
                "query": {
                    "bool": {
                        "should": [
                            {
                                "query_string": {
                                    "query": "*"
                                }
                            }
                        ]
                    }
                },
                "filter": {
                    "bool": {
                        "must": [
                            {
                                "range": {
                                    "@timestamp": {
                                        "from": 1576514353823,
                                        "to": 1576515253823
                                    }
                                }
                            },
                            {
                                "fquery": {
                                    "query": {
                                        "query_string": {
                                            "query": "_type:(\"eventlog\")"
                                        }
                                    },
                                    "_cache": true
                                }
                            },
                            {
                                "fquery": {
                                    "query": {
                                        "query_string": {
                                            "query": "EventID:(\"4625\" \"4771\" \"4776\")"
                                        }
                                    },
                                    "_cache": true
                                }
                            },
                        ],
                        "must_not": [
                            {
                                "fquery": {
                                    "query": {
                                        "query_string": {
                                            "query": "TargetUserSid:(\"S-1-0-0\")"
                                        }
                                    },
                                    "_cache": true
                                }
                            },
                            {
                                "fquery": {
                                    "query": {
                                        "query_string": {
                                            "query": "TargetUserName:(\"Altiris\")"
                                        }
                                    },
                                    "_cache": true
                                }
                            }
                        ]
                    }
                }
            }
        }
		
		"aggs" : {
"byUser" : {
    "terms" : { "field" : "TargetUserName.raw", "min_doc_count": 10 }
}
}
}

Re: Failed Logon Alert Configuration Assistance

Posted: Mon Dec 16, 2019 6:10 pm
by rferebee
Ok, great. I'll try this out.

The other part I needed was this:
The other thing I want to have happen is, in the email that's generated, I want it to provide a list of the unique TargetUserNames. I can do this with other alerts by using a line like this: <li>%uniquehosts%</li> but I don't know the comparable variable to get a list of users rather than hosts.
Can you help me with that piece as well?

Re: Failed Logon Alert Configuration Assistance

Posted: Mon Dec 16, 2019 6:39 pm
by rferebee
It keeps telling me the query you provided is not valid JSON.

I thought it might be as simple as a missing bracket or something like that, but I pasted it into Notepad++ and even with the missing 3 brackets it still won't work in Log Server. Got anything else for me?

Re: Failed Logon Alert Configuration Assistance

Posted: Tue Dec 17, 2019 12:38 pm
by mbellerue
Line 72 needed a comma. It's always the little things. :)

Code: Select all

{
        "query": {
            "filtered": {
                "query": {
                    "bool": {
                        "should": [
                            {
                                "query_string": {
                                    "query": "*"
                                }
                            }
                        ]
                    }
                },
                "filter": {
                    "bool": {
                        "must": [
                            {
                                "range": {
                                    "@timestamp": {
                                        "from": 1576514353823,
                                        "to": 1576515253823
                                    }
                                }
                            },
                            {
                                "fquery": {
                                    "query": {
                                        "query_string": {
                                            "query": "_type:(\"eventlog\")"
                                        }
                                    },
                                    "_cache": true
                                }
                            },
                            {
                                "fquery": {
                                    "query": {
                                        "query_string": {
                                            "query": "EventID:(\"4625\" \"4771\" \"4776\")"
                                        }
                                    },
                                    "_cache": true
                                }
                            },
                        ],
                        "must_not": [
                            {
                                "fquery": {
                                    "query": {
                                        "query_string": {
                                            "query": "TargetUserSid:(\"S-1-0-0\")"
                                        }
                                    },
                                    "_cache": true
                                }
                            },
                            {
                                "fquery": {
                                    "query": {
                                        "query_string": {
                                            "query": "TargetUserName:(\"Altiris\")"
                                        }
                                    },
                                    "_cache": true
                                }
                            }
                        ]
                    }
                }
            }
        },
      
      "aggs" : {
"byUser" : {
    "terms" : { "field" : "TargetUserName.raw", "min_doc_count": 10 }
}
}
}

Re: Failed Logon Alert Configuration Assistance

Posted: Tue Dec 17, 2019 12:44 pm
by rferebee
This is the error I'm getting after pasting in your updated query (with the comma).

Re: Failed Logon Alert Configuration Assistance

Posted: Tue Dec 17, 2019 1:33 pm
by cdienger
We have a feature request in place to allow custom fields to be returned, but at the moment you're limited to:
macros.png
Try:

Code: Select all

{
        "query": {
            "filtered": {
                "query": {
                    "bool": {
                        "should": [
                            {
                                "query_string": {
                                    "query": "*"
                                }
                            }
                        ]
                    }
                },
                "filter": {
                    "bool": {
                        "must": [
                            {
                                "range": {
                                    "@timestamp": {
                                        "from": 1576514353823,
                                        "to": 1576515253823
                                    }
                                }
                            },
                            {
                                "fquery": {
                                    "query": {
                                        "query_string": {
                                            "query": "_type:(\"eventlog\")"
                                        }
                                    },
                                    "_cache": true
                                }
                            },
                            {
                                "fquery": {
                                    "query": {
                                        "query_string": {
                                            "query": "EventID:(\"4625\" \"4771\" \"4776\")"
                                        }
                                    },
                                    "_cache": true
                                }
                            }
                        ],
                        "must_not": [
                            {
                                "fquery": {
                                    "query": {
                                        "query_string": {
                                            "query": "TargetUserSid:(\"S-1-0-0\")"
                                        }
                                    },
                                    "_cache": true
                                }
                            },
                            {
                                "fquery": {
                                    "query": {
                                        "query_string": {
                                            "query": "TargetUserName:(\"Altiris\")"
                                        }
                                    },
                                    "_cache": true
                                }
                            }
                        ]
                    }
                }
            }
        },
	"aggs" : {
    "byUser" : {
        "terms" : { "field" : "TargetUserName.raw", "min_doc_count": 10 }
    }
    }
    }
and also running it from the command line with:

Code: Select all

curl -XGET 'http://localhost:9200/logstash-*/_search?pretty' -d    '{
        "query": {
            "filtered": {
                "query": {
                    "bool": {
                        "should": [
                            {
                                "query_string": {
                                    "query": "*"
                                }
                            }
                        ]
                    }
                },
                "filter": {
                    "bool": {
                        "must": [
                            {
                                "range": {
                                    "@timestamp": {
                                        "from": 1576514353823,
                                        "to": 1576515253823
                                    }
                                }
                            },
                            {
                                "fquery": {
                                    "query": {
                                        "query_string": {
                                            "query": "_type:(\"eventlog\")"
                                        }
                                    },
                                    "_cache": true
                                }
                            },
                            {
                                "fquery": {
                                    "query": {
                                        "query_string": {
                                            "query": "EventID:(\"4625\" \"4771\" \"4776\")"
                                        }
                                    },
                                    "_cache": true
                                }
                            }
                        ],
                        "must_not": [
                            {
                                "fquery": {
                                    "query": {
                                        "query_string": {
                                            "query": "TargetUserSid:(\"S-1-0-0\")"
                                        }
                                    },
                                    "_cache": true
                                }
                            },
                            {
                                "fquery": {
                                    "query": {
                                        "query_string": {
                                            "query": "TargetUserName:(\"Altiris\")"
                                        }
                                    },
                                    "_cache": true
                                }
                            }
                        ]
                    }
                }
            }
        },
	"aggs" : {
    "byUser" : {
        "terms" : { "field" : "TargetUserName.raw", "min_doc_count": 10 }
    }
    }
    }'

Re: Failed Logon Alert Configuration Assistance

Posted: Tue Dec 17, 2019 2:13 pm
by rferebee
It was happy with that query, but it's weird. I can run it directly on the server and get the results I expect, but when I run it from the Log Server console it's showing me the previous queries results and isn't updating the output. Maybe it's timing out somewhere?

I sent you a PM with the output since it has a bunch of user email addresses in it.

Re: Failed Logon Alert Configuration Assistance

Posted: Tue Dec 17, 2019 3:29 pm
by cdienger
Looking into the code, it doesn't appear this will work because NLS first does a query against the count API(https://www.elastic.co/guide/en/elastic ... count.html) which doesn't like the agg parameter. We have a couple of feature requests in place to better support aggregation like this. I will add your information to it to show the demand for this kind of functionality.

Re: Failed Logon Alert Configuration Assistance

Posted: Tue Dec 17, 2019 3:47 pm
by rferebee
Ok so, at this time, we are unable to get this kind of data from Log Server using a query?

Would the "real-time" alerts help me at all?