Hi Renan!
I hope you are doing fine!
I did some test in different section of your steps by steps configuration, and found some interesting things.
In a Windows box I added a
<localfile> configuration, but with some changes, a space between
'QUERY HKLM' in
<command> section, and also added a
<frequency> label, which indicates how often the command will fires.
<localfile>
<log_format>full_command</log_format>
<command>reg QUERY HKLM\SYSTEM\CurrentControlSet\Enum\USBSTOR</command>
<frequency>30</frequency>
</localfile>
After this changes I restarted the Wazuh agent, and I had events in
archive.json file in Wazuh server side.
/var/ossec/logs/archives/archives.json
Event generated:
{"timestamp":"2022-06-23T16:33:49.985-0300","agent":{"id":"004","name":"DESKTOP-WIN","ip":"192.16.10.27"},"manager":{"name":"VBox"},"id":"1656012829.4212515","full_log":"ossec: output: 'reg QUERY HKLM\\SYSTEM\\CurrentControlSet\\Enum\\USBSTOR':\nHKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum\\USBSTOR\\Disk&Ven_Generic&Prod_Flash_Disk&Rev_8.07\nHKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum\\USBSTOR\\Disk&Ven_Kingston&Prod_DataTraveler_2.0&Rev_1.00","decoder":{"name":"ossec"},"location":"reg QUERY HKLM\\SYSTEM\\CurrentControlSet\\Enum\\USBSTOR"}
Then I created the custom rule without the
<check_diff /> section to fires every event, just for to be sure if the rule is working.
<rule id="140125" level="7">
<if_sid>530</if_sid>
<match>ossec: output: 'reg QUERY HKLM</match>
<description>New USB device connected</description>
</rule>
/var/ossec/logs/alerts/alerts.json{"timestamp":"2022-06-23T16:53:25.414-0300","rule":{"level":7,"description":"New USB device connected","id":"140125","firedtimes":2,"mail":false,"groups":["hp","custom"]},"agent":{"id":"004","name":"DESKTOP-WIN","ip":"192.16.10.27"},"manager":{"name":"VBox"},"id":"1656014005.4272785","full_log":"ossec: output: 'reg QUERY HKLM\\SYSTEM\\CurrentControlSet\\Enum\\USBSTOR':\nHKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum\\USBSTOR\\Disk&Ven_Generic&Prod_Flash_Disk&Rev_8.07\nHKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum\\USBSTOR\\Disk&Ven_Kingston&Prod_DataTraveler_2.0&Rev_1.00","decoder":{"name":"ossec"},"location":"reg QUERY HKLM\\SYSTEM\\CurrentControlSet\\Enum\\USBSTOR"}And finally I tested with
<check_diff /> option, I added a new USB storage device and it works! It only fires when the command message changes.
<rule id="140125" level="7">
<if_sid>530</if_sid>
<match>ossec: output: 'reg QUERY HKLM</match>
<check_diff />
<description>New USB device connected</description>
</rule>
Alert generated:
{"timestamp":"2022-06-23T17:00:55.834-0300","rule":{"level":7,"description":"New USB device connected","id":"140125","firedtimes":1,"mail":false,"groups":["hp","custom"]},"agent":{"id":"004","name":"DESKTOP-WIN","ip":"192.16.10.27"},"manager":{"name":"VBox"},"id":"1656014455.4328292","previous_output":"Previous output:\nossec: output: 'reg QUERY HKLM\\SYSTEM\\CurrentControlSet\\Enum\\USBSTOR':\nHKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum\\USBSTOR\\Disk&Ven_Generic&Prod_Flash_Disk&Rev_8.07\nHKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum\\USBSTOR\\Disk&Ven_Kingston&Prod_DataTraveler_2.0&Rev_1.00","full_log":"ossec: output: 'reg QUERY HKLM\\SYSTEM\\CurrentControlSet\\Enum\\USBSTOR':\nHKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum\\USBSTOR\\Disk&Ven_Generic&Prod_Flash_Disk&Rev_8.07\nHKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum\\USBSTOR\\Disk&Ven_Kingston&Prod_DataTraveler_2.0&Rev_1.00\nHKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum\\USBSTOR\\Disk&Ven_SanDisk&Prod_Cruzer_Blade&Rev_1.00","decoder":{"name":"ossec"},"previous_log":"ossec: output: 'reg QUERY HKLM\\SYSTEM\\CurrentControlSet\\Enum\\USBSTOR':\nHKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum\\USBSTOR\\Disk&Ven_Generic&Prod_Flash_Disk&Rev_8.07\nHKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum\\USBSTOR\\Disk&Ven_Kingston&Prod_DataTraveler_2.0&Rev_1.00","location":"reg QUERY HKLM\\SYSTEM\\CurrentControlSet\\Enum\\USBSTOR"}
It took some seconds to trigger but it's working.
Let me know if this information is useful to you,
Regards!