I am sending my Cisco ASA logs to Logstash, and am attempting to write a watch that will, when an invalid login is detected, perform a webhook action to post JSON containing some pertinent information about the alert as well as the source log messages that matched the query to a third-party listener.
The problem I'm running into is that I can't seem to get it to post ctx.payload.hits.hits as valid JSON - if I use the mustache template {{ctx.payload.hits.hits}}, I get keys and values without the right quotes for JSON, but if use {#toJson}ctx.payload.hits.hits{{/toJson}}, I get otherwise valid JSON in which some double quotes are not properly escaped, resulting in invalid JSON. Example excerpts are below:
When querying the watcher history, the "message" portion of the hit appears as below:
ASA-6-605004: Login denied from 10.42.42.104/6001 to inside:10.42.42.1/https for user \"*****\"\n
{{ctx.payload.hits.hits}} posts the same "message", but without the backslash escapes on the double quotes, as expected since it's not JSON formatted:
%ASA-6-605004: Login denied from 10.42.42.104/6545 to inside:10.42.42.1/https for user "*****"\n
But if I use {{#toJson}}ctx.payload.hits.hits{{/toJson}}, it posts a body which is properly JSON formatted with the exception of the message:
%ASA-6-605004: Login denied from 10.42.42.104/6559 to inside:10.42.42.1/https for user \\"*****\\"\\n
The last part should correctly be \\"\\" to properly escape both the backslash and the double quotes, or it should remove the original backslashes entirely (since they're just there to escape the quotes anyway) and render it as "" .
Am I doing something wrong here? I've gone at this from a couple of different directions, but it seems to come back to the text being escaped improperly by the built-in function. Any ideas for a workaround?
Thank you in advance for any help - I have added the full Watch below. I can provide the result from the watch history as well if needed (too many chars to fit in this post)
Eric
## Watch:
PUT _xpack/watcher/watch/cisco_failed_login
{
"trigger": {
"schedule": {
"interval": "15s"
}
},
"input": {
"search": {
"request": {
"indices": [
"logstash-*"
],
"body": {
"min_score": 2,
"query": {
"bool": {
"must": [
{
"range": {
"@timestamp": {
"gt": "now-15s"
}
}
},
{
"match": {
"ciscotag": "ASA-6-605004"
}
}
]
}
}
}
}
}
},
"condition": {
"compare": {
"ctx.payload.hits.total": {
"gt": 0
}
}
},
"actions": {
"test_webhook": {
"webhook": {
"method": "POST",
"host": "10.42.42.18",
"port": 80,
"path": "/test",
"headers": {
"Content-Type": "application/json"
},
"body": "{\"{{ctx.watch_id}}\" : \"{{ctx.payload.hits.total}}\",\"Base Events\": {{#toJson}}ctx.payload.hits.hits{{/toJson}}}"
}
}
}
}