Hello again, I found out that it is only using the first decoder because the prematch is equal between them.
Here you have three workarounds for avoid this:
Using your decoders, you can add the prematch as follows:
<decoder name="mikrotik_winbox_login_fail">
<prematch>login failure for</prematch>
<parent>mikrotik</parent>
<regex>^\S+\s+(\S+\s+\d+\s+\S+)\s+\S+\s+login failure for user (\S+) from (\S+)</regex>
<order>time,user,srcip</order>
</decoder>
<decoder name="mikrotik_winbox_logout">
<prematch>logged out</prematch>
<parent>mikrotik</parent>
<regex>^\S+\s+(\S+\s+\d+\s+\S+)\s+\S+ user (\S+) logged out from (\S+)</regex>
<order>time,user,srcip</order>
</decoder>
<decoder name="mikrotik_winbox_login">
<prematch>logged in</prematch>
<parent>mikrotik</parent>
<regex>^\S+\s+(\S+\s+\d+\s+\S+)\s+\S+\s+user (\S+) logged in from (\S+)</regex>
<order>time,user,srcip</order>
</decoder>
Fix the log format(syslog) by forwarding the log to a file and creating new decoders:
syslog
events to a file.Monitor that file using a localfile
block in your ossec.conf
file. I added the timestamp, so now it has the syslog timestamp
, NoName
as hostname
, and mikrotik
as program_name
.
<localfile>
<log_format>syslog</log_format>
<location>/var/log/mikrotik.log</location>
<out_format>$(timestamp) NoName mikrotik: $(log)</out_format>
</localfile>
Now, you will have logs like these within your alerts log files:
Jun 13 09:16:43 NoName mikrotik: 192.168.0.1 Jun 10 15:51:05 NoName login failure for user admin from 192.168.0.204 via winbox
Jun 13 09:16:43 NoName mikrotik: 192.168.0.1 Jun 10 15:51:05 NoName user admin logged out from 192.168.0.204 via winbox
Jun 13 09:16:43 NoName mikrotik: 192.168.0.1 Jun 10 15:51:05 NoName user admin logged in from 192.168.0.204 via winbox
Then, you can add these decoders(similars to yours)
<decoder name="mikrotik_winbox_logout">
<program_name>mikrotik</program_name>
<prematch>logged out</prematch>
<regex>user (\S+) logged out from (\d+.\d+.\d+.\d+)</regex>
<order>user, srcip</order>
</decoder>
<decoder name="mikrotik_winbox_login">
<program_name>mikrotik</program_name>
<prematch>logged in</prematch>
<regex>user (\S+) logged in from (\d+.\d+.\d+.\d+)</regex>
<order>user, srcip</order>
</decoder>
<decoder name="mikrotik_winbox_login_fail">
<program_name>mikrotik</program_name>
<prematch>login failure for</prematch>
<regex offset="after_prematch">user (\S+) from (\d+.\d+.\d+.\d+)</regex>
<order>user, srcip</order>
</decoder>
Same as previous one, but using a generic decoder(logic would be added with custom rules):
You have to follow the previous workaround to have the correct syslog
format and then you can add this decoder:
<decoder name="mikrotik_winbox_generic">
<program_name>mikrotik</program_name>
<regex>user (\S+) \.*from (\d+.\d+.\d+.\d+)</regex>
<order>user, srcip</order>
</decoder>
Hope this helps you,
Luis.
Okay, with this info we have a few things:
The log is already in a valid format:
Jun 10 15:51:05 NoName login failure for user nauris from 192.168.0.204 via winbox
So timestamp
and in your case hostname
is obtained by default even with no decoder as you could see:
**Phase 1: Completed pre-decoding.
full event: 'Jun 10 15:51:05 NoName login failure for user nauris from 192.168.0.204 via winbox'
timestamp: 'Jun 10 15:51:05'
hostname: 'NoName'
There is no need to get the time as you are trying because with the valid format you have the timestamp as I said in the previous message.
It is failing because of the parent mikrotik
decoder. Imagine this new log formatted as the out_format
I provided before, <out_format>$(timestamp) NoName mikrotik: $(log)</out_format>
. So, now you have the timestamp
, the hostname
(in your case the program name is missing because of your log), and the log
. You can’t check for NoName
.
You can just use the prematch
label, removing the parent
label. Then, you have the same decoder as I provided you(replacing the IP format with "all characters before the next space" as you have):
<decoder name="mikrotik_winbox_failed_login">
<prematch>login failure for</prematch>
<regex>user (\S+) from (\S+)</regex>
<order>user, srcip</order>
</decoder>
Jun 10 15:51:05 NoName login failure for user nauris from 192.168.0.204 via winbox
**Phase 1: Completed pre-decoding.
full event: 'Jun 10 15:51:05 NoName login failure for user nauris from 192.168.0.204 via winbox'
timestamp: 'Jun 10 15:51:05'
hostname: 'NoName'
**Phase 2: Completed decoding.
name: 'mikrotik_winbox_failed_login'
dstuser: 'nauris'
srcip: '192.168.0.204'
Hope this helps you,
Luis.
If you mean the log format, you can check more here.
For example, you could have this: HEADER (timestamp host) MSG (application: message)
, where you can have the hostname
(host) and the program_name
(application). Or the one you provided the last, where the header is composed of the timestamp
and header
, and then you have the message
. You can customize it using the out_format
within the ossec.conf
as I said in the first message.
If you do not know if it follows the default format expected, yes. You can paste it into the logtest tool and check if the timestamp is correctly decoded. If it is not the case, you can customize it with the out_format
so you can start with a proper log to being decoded.
Do not hesitate if you have any doubts or anything else!
Luis.
If you want to have the timestamp, hostname, program_name, etc. by default without creating any regex, you should, yes. This is suggested because you do not need to waste time creating custom regexes for catching these fields in custom logs. Also, these fields are useful when you are creating a decoder. For example, this allows you to use the field program_name
as I did in the first message: <program_name>mikrotik</program_name>
.
The out_format
provides you the timestamp in two ways:
timestamp
-> Current timestamp (when the log is sent), in RFC3164
format.timestamp <format>
-> Custom timestamp, in strftime
string format.Here you have more about formatting the logs fields.
Hope this helps you,
Luis.