• Health tips to prevent the spread of COVID-19:
    • Wash your hands frequently;
    • Maintain 2 meters social distancing;
    • Minimise outdoor activities.
    Stay safe in your bubble!

MikroTik DDoS Detection & Blocking Firewall Filter Rule

Andy

Administrator
User ID
1
Joined
7 Jan 2019
Messages
212
Reaction score
8
Points
18
I found this method is the best way to prevent DDoS Attack from your users to attacked resources and drop DDoS directed to your clients.

First, we catch all new connections and send them to dedicated firewall chain:
Code:
/ip firewall filter
add chain=forward connection-state=new action=jump jump-target=detect-ddos
Note: In RouterOS, any single UDP packet is considered to be a new connection by Connection Tracking in any Firewall section (except NAT) until the packet in the opposite direction is sent. It's good behaviour because legitimate traffic (like uTorrent's or Skype's UDP stream) is bidirectional, so it triggers 'connection-state=new' only once per stream; on the other side, any unidirectional flooding (the only exception I can imagine is NetFlow traffic) generates 'connection-state=new' per each packet even if all packets have the same Src and Dst Addresses and Ports, so it can be easily detected as DoS Attack.

Then, for each "SrcIP:DstIP" pair we allow some number of new connections. One may want also add some exceptions (like DNS servers - it won't be good if they will be blocked):
Code:
/ip firewall filter
add chain=detect-ddos dst-limit=32,32,src-and-dst-addresses/10s action=return
add chain=detect-ddos src-address=10.0.0.1 action=return
Note: At least up to version 5.6, 'dst-limit' matcher has two bugs:
  • 'Expire' value is 10 times lower than you set; so '10s' is 1 second
  • 'dst-limit' matches first 'Burst' packets (as it should be) plus one, and then skips packets for the first second; so if you have Rate set to 32 and Burst set to 0, and you start to flood packets, the rule will match 1 packet, and on 2nd packet, it won't match until 1sec passes - that's why you need 'Burst' value at least as high as 'Rate' value

Now, we have only packets which exceed our limits - and we add their source to 'ddoser' and the target to 'ddosed' address lists:
Code:
/ip firewall filter
add chain=detect-ddos action=add-dst-to-address-list address-list=ddosed address-list-timeout=10m
add chain=detect-ddos action=add-src-to-address-list address-list=ddoser address-list-timeout=10m

Then packet processing returns to 'forward' chain, where we block any packets from ddosers to ddosed resources:
Code:
/ip firewall filter
add chain=forward connection-state=new src-address-list=ddoser dst-address-list=ddosed action=drop

I have tested on RouterOS v6.47.3, and I found this method is very effective, and I believe this tutorial will help somebody to protect their routers from zombie horde attacks aka Distributed denial-of-service attacks... 🧟‍♂️🚫🧟‍♀️🚷👨‍💻👩‍💻👍
Reference: http://forum.mikrotik.com/viewtopic.php?f=2&t=54607
 
 Short URL:
Top