├── local_rules.xml
├── 1001-mikrotik_decoders.xml
├── script.rsc
└── README.md
/local_rules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | mikrotik_log
4 | Mikrotik log: $(type) $(target) $(action) by $(username) from $(srcip): $(rule_details)
5 |
6 |
7 |
8 | user_login
9 | Mikrotik log: User $(username) logged $(action) from $(srcip) via $(access_method)
10 |
11 |
12 |
13 | user_login_failure
14 | Mikrotik log: Login failure for user $(username) from $(srcip) via $(access_method)
15 |
16 |
17 |
18 | wireguard
19 | Mikrotik log: Wireguard user $(username) logged $(action) from $(srcip)
20 |
21 |
22 |
23 | ovpn
24 | Mikrotik log: $(action) logged, $(localip) from $(srcip)
25 |
26 |
27 |
28 | filter_rule_change
29 | Mikrotik log: Filter rule $(action) by $(username) from $(srcip): $(rule_details)
30 |
31 |
32 |
33 | raw_rule_change
34 | Mikrotik log: Raw rule $(action) by $(username) from $(srcip): $(rule_details)
35 |
36 |
37 |
38 | user_change
39 | Mikrotik log: User $(newuser) $(action) by $(username) from $(srcip): $(rule_details)
40 |
41 |
42 |
--------------------------------------------------------------------------------
/1001-mikrotik_decoders.xml:
--------------------------------------------------------------------------------
1 |
2 | user (\S+) logged (\S+) from (\S+) via (\S+)
3 | user (\S+) logged (\S+) from (\S+) via (\S+)
4 | username, action, srcip, access_method
5 |
6 |
7 |
8 | login failure for user (\S+) from (\S+) via (\S+)
9 | login failure for user (\S+) from (\S+) via (\S+)
10 | username, srcip, access_method
11 |
12 |
13 |
14 | wireguard user (\S+) logged (\S+) from (\S+)
15 | wireguard user (\S+) logged (\S+) from (\S+)
16 | username, action, srcip
17 |
18 |
19 |
20 | (\S+) logged (\S+), (\S+) from (\S+)
21 | (\S+) logged (\S+), (\S+) from (\S+)
22 | username, action, localip, srcip
23 |
24 |
25 |
26 | filter rule (changed|removed|added) by
27 | filter rule (changed|removed|added) by tcp-msg\(winbox\):(\S+)@(\S+) \((.*)\)
28 | action, username, srcip, rule_details
29 |
30 |
31 |
32 | raw rule (changed|removed|added) by
33 | raw rule (changed|removed|added) by tcp-msg\(winbox\):(\S+)@(\S+) \((.*)\)
34 | action, username, srcip, rule_details
35 |
36 |
37 |
38 | user (\S+) (added|password changed|removed) by
39 | user (\S+) (added|password changed|removed) by tcp-msg\(winbox\):(\S+)@(\S+) \((.*)\)
40 | action, newuser, username, srcip, rule_details
41 |
42 |
43 |
44 | (\S+) (\S+) (\S+) by
45 | (\S+) (\S+) (\S+) by tcp-msg\(winbox\):(\S+)@(\S+) \((.*)\)
46 | type, target, action, username, srcip, rule_details
47 |
48 |
--------------------------------------------------------------------------------
/script.rsc:
--------------------------------------------------------------------------------
1 | # Wireguard Online Status
2 | /system script
3 | add dont-require-permissions=no name=WGPeerStatus owner=admin policy=\
4 | ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=":\
5 | local interfaceName \"Wireguard\"\r\
6 | \n\r\
7 | \n:global OfflinePeerList\r\
8 | \n:global OnlinePeerList\r\
9 | \n\r\
10 | \n:local OfflinePeerListTmp [ :toarray \"\" ]\r\
11 | \n:local OnlinePeerListTmp [ :toarray \"\" ]\r\
12 | \n\r\
13 | \n#:log info \"Avvio script per monitorare i peer dell'interfaccia \$inter\
14 | faceName\"\r\
15 | \n\r\
16 | \n:foreach peer in=[/interface wireguard peers find interface=\$interfaceN\
17 | ame] do={\r\
18 | \n :set \$peerAddress [/interface wireguard peer get \$peer allowed-add\
19 | res]\r\
20 | \n :set \$peerAddress [:pick \$peerAddress 0 [:find \$peerAddress \"/\
21 | \"]]\r\
22 | \n :local peerName [/interface wireguard peers get \$peer comment]\r\
23 | \n :local remoteIpAddress [/interface wireguard peers get \$peer curren\
24 | t-endpoint-address]\r\
25 | \n\r\
26 | \n:if ( ([/interface wireguard peers get \$peer last-handshake] > 180) || \
27 | ([:len [/interface/wireguard/peers/get \$peer last-handshake]] = 0)) do={ \
28 | \r\
29 | \n :if ( \$OfflinePeerList~\"\$peerAddress(;|\\\$)\" ) do={\
30 | \r\
31 | \n #:log info \"\$peerName - \$peerAddress still Offline\
32 | \"\r\
33 | \n :set OfflinePeerListTmp ( \$OfflinePeerListTmp, \$pee\
34 | rAddress );\r\
35 | \n } else={\r\
36 | \n :log info \"wireguard user \$peerName logged out fro\
37 | m \$remoteIpAddress\"\r\
38 | \n :set OfflinePeerListTmp ( \$OfflinePeerListTmp, \$pe\
39 | erAddress );\r\
40 | \n }\r\
41 | \n } else {\r\
42 | \n :if ( \$OnlinePeerList~\"\$peerAddress(;|\\\$)\" ) do={\r\
43 | \n #:log info \"\$peerName - \$peerAddress still Online\
44 | \"\r\
45 | \n :set OnlinePeerListTmp ( \$OnlinePeerListTmp, \$peerA\
46 | ddress );\r\
47 | \n } else={\r\
48 | \n :log info \"wireguard user \$peerName logged in from\
49 | \_\$remoteIpAddress\"\r\
50 | \n :set OnlinePeerListTmp ( \$OnlinePeerListTmp, \$peer\
51 | Address );\r\
52 | \n }\r\
53 | \n }\r\
54 | \n}\r\
55 | \n\r\
56 | \n:set \$OfflinePeerList \$OfflinePeerListTmp\r\
57 | \n:set \$OnlinePeerList \$OnlinePeerListTmp"
58 |
59 | /system scheduler
60 | add interval=30s name=WGPeerCheck on-event="/system script run WGPeerStatus;" \
61 | policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon
62 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # WazuhMikrotik
2 |
3 | This repository provides Wazuh decoders for Mikrotik and a script for monitoring Wireguard peers' login/logout activities.
4 |
5 | **Tested on:**
6 | - RouterOS 7.15.1
7 | - Wazuh 4.8.0
8 |
9 | ## 🚀 Setup Instructions
10 |
11 | ### Step 1: Configure Wazuh Manager to Receive Syslog Messages
12 |
13 | Follow the guide at [Wazuh Blog](https://wazuh.com/blog/how-to-configure-rsyslog-client-to-send-events-to-wazuh/) to configure your Wazuh manager to receive Syslog messages.
14 |
15 | ### Step 2: Deploy Mikrotik Decoders and Rules
16 |
17 | 1. Copy `1001-mikrotik_decoders.xml` to the Wazuh decoders directory:
18 | ```sh
19 | cp /path/to/1001-mikrotik_decoders.xml /var/ossec/etc/decoders/1001-mikrotik_decoders.xml
20 | ```
21 | or if you are using Docker, run:
22 | ```sh
23 | docker cp /path/to/1001-mikrotik_decoders.xml single-node-wazuh.manager-1:/var/ossec/etc/decoders/1001-mikrotik_decoders.xml
24 | ```
25 |
26 | 2. Copy `local_rules.xml` to the Wazuh rules directory:
27 | ```sh
28 | cp /path/to/local_rules.xml /var/ossec/etc/rules/local_rules.xml
29 | ```
30 | or if you are using Docker, run:
31 | ```sh
32 | docker cp /path/to/local_rules.xml single-node-wazuh.manager-1:/var/ossec/etc/rules/local_rules.xml
33 | ```
34 |
35 | ### Step 3: Restart Wazuh
36 |
37 | 1. Restart the Wazuh manager to apply the new configurations:
38 | ```sh
39 | systemctl restart wazuh-manager
40 | ```
41 | or if you are using Docker, run:
42 | ```sh
43 | docker restart single-node-wazuh.manager-1
44 | ```
45 |
46 | ### Step 4: Configure Mikrotik to Send Logs to Syslog Server (Wazuh)
47 |
48 | 1. Configure the remote logging server:
49 | ```sh
50 | /system logging action add name=remote target=remote remote=YOUR_WAZUH_SERVER_IP
51 | ```
52 |
53 | 2. Add a logging rule to send all logs to the remote server:
54 | ```sh
55 | /system logging add action=remote topics=system
56 | /system logging add action=remote topics=info
57 | ```
58 |
59 | Make sure to replace `YOUR_WAZUH_SERVER_IP` with the IP address of your Wazuh server.
60 |
61 | ### Step 5: Monitor Wireguard Peers Activity
62 |
63 | 1. Copy the script `script.rsc` from the repository to your Mikrotik device.
64 |
65 | 2. Import and execute the script from the Mikrotik terminal:
66 | ```sh
67 | /import script.rsc
68 | ```
69 |
70 | ℹ️ **Note:** It is crucial to assign a unique comment to each Wireguard peer configured on your Mikrotik server. This comment acts as an identifier in the monitoring script and ensures accurate tracking of each peer's activity.
71 |
72 | ## Author
73 |
74 | 👤 **Giuseppe Trifilio**
75 |
76 | * Website: https://github.com/angolo40/WazuhMikrotik
77 | * GitHub: [@angolo40](https://github.com/angolo40)
78 |
79 | ## 🤝 Contributing
80 |
81 | Contributions, issues, and feature requests are welcome! Feel free to check the [issues page](https://github.com/angolo40/WazuhMikrotik).
82 |
83 | ## Show your support
84 |
85 | Give a ⭐️ if this project helped you!
86 |
87 | - **XMR**: `87LLkcvwm7JUZAVjusKsnwNRPfhegxe73X7X3mWXDPMnTBCb6JDFnspbN8qdKZA6StHXqnJxMp3VgRK7DcS2sgnW3wH7Xhw`
88 |
--------------------------------------------------------------------------------