MikroTik RouterOS: serial console and useful commands
Serial console access to a MikroTik router from Linux, SSH brute-force limiting on a non-standard port, configuration export and a DHCP lease e-mail alert.
This note covers reaching a MikroTik RouterOS device through its serial console from Linux, and a few RouterOS commands for hardening and maintenance.
Serial console from Linux
You need a USB-to-serial adapter with an FTDI chip. Drivers are available from FTDI.
Identify the adapter:
lsusb
dmesg | grep tty
ls /dev/tty*
Give your user access to the serial port by adding it to the dialout group, then log out and back in:
sudo usermod -aG dialout "$USER"
Connect with screen:
sudo screen /dev/ttyUSB0 115200,cs8,-parenb,-parodd,-cstopb
In screen, Ctrl-A d detaches and Ctrl-A k kills the current window.
Or with minicom, configured once:
sudo minicom -s
In the "Serial port setup" menu, set:
| Setting | Value |
|---|---|
| Serial device | /dev/ttyUSB0 |
| Bps/Par/Bits | 115200 8N1 |
| Hardware flow control | No |
| Software flow control | No |
Then choose "Save setup as default", exit, and start minicom. Ctrl-A z opens the help menu.
Change the MAC address of an interface
/interface ethernet set ether1-gateway mac-address=00:11:22:33:44:55
Limit SSH brute-force attempts on a non-standard port
The rules below move SSH to TCP port 5004 and blacklist clients that open several new connections in a short time. A client that opens a new connection is moved to stage 1, then to stage 2 and stage 3 with each further new connection within one minute; from stage 3 it is added to the blacklist for 10 days.
add chain=input protocol=tcp dst-port=5004 src-address-list=blacklist_ssh action=drop \
comment="drop ssh brute forcers" disabled=no
add chain=input protocol=tcp dst-port=5004 connection-state=new \
src-address-list=ssh_stage3 action=add-src-to-address-list address-list=blacklist_ssh \
address-list-timeout=10d comment="" disabled=no
add chain=input protocol=tcp dst-port=5004 connection-state=new \
src-address-list=ssh_stage2 action=add-src-to-address-list address-list=ssh_stage3 \
address-list-timeout=1m comment="" disabled=no
add chain=input protocol=tcp dst-port=5004 connection-state=new src-address-list=ssh_stage1 \
action=add-src-to-address-list address-list=ssh_stage2 address-list-timeout=1m comment="" disabled=no
add chain=input protocol=tcp dst-port=5004 connection-state=new action=add-src-to-address-list \
address-list=ssh_stage1 address-list-timeout=1m comment="" disabled=no
See the RouterOS firewall documentation for raw filtering.
Export the configuration
/export hide-sensitive file=router-backup-2021-12-20
Send an e-mail when a DHCP lease is handed out
The script needs /tool e-mail to be configured first. It is meant as the lease-script of a DHCP server.
:local recipient "user@example.com"
:if (($leaseBound=1) && ([/ip dhcp-server find where dynamic mac-address=$leaseActMAC]!="")) do {
:do {
/tool e-mail send to=$recipient subject="DHCP Address Alert [MAC: $leaseActMAC]" body="The following MAC address [$leaseActMAC] received an IP address [$leaseActIP] from the DHCP Server [$leaseServerName]"
:log info "Sent DHCP alert for MAC $leaseActMAC"
} on-error={
:log error "Failed to send alert email to $recipient"
}
}
Notes
- The SSH rules block any client that opens several new connections within about a minute, including legitimate ones. Test from a second session and place the rules before the rule that accepts SSH traffic on that port.
- A
chmod 777on the serial device is not recommended; group membership is the correct approach.