RouterOS的优化配置方案

ROS的防火墙配置方案

/ip firewall address-list

add address=192.168.100.1 comment=”onuconf: local ONU address” list=local_onu_ipv4
add address=172.16.1.0/24 comment=”lanconf: local LAN address” list=local_lan_ipv4

add address=172.16.1.2 comment=”lanconf: local DNS server” list=local_dns_ipv4
add address=172.16.1.3 comment=”lanconf: local DNS server” list=local_dns_ipv4
add address=172.16.1.50 comment=”lanconf: local DNS server” list=local_dns_ipv4

add address=0.0.0.0/8 comment=”defconf: RFC6890 - this network” list=no_forward_ipv4
add address=169.254.0.0/16 comment=”defconf: RFC6890 - link local” list=no_forward_ipv4
add address=224.0.0.0/4 comment=”defconf: RFC5771 - multicast” list=no_forward_ipv4
add address=255.255.255.255/32 comment=”defconf: RFC6890 - limited broadcast” list=no_forward_ipv4

add address=127.0.0.0/8 comment=”defconf: RFC6890 - loopback” list=bad_ipv4
add address=192.0.0.0/24 comment=”defconf: RFC6890 - reserved” list=bad_ipv4
add address=192.0.2.0/24 comment=”defconf: RFC6890 - TEST-NET-1” list=bad_ipv4
add address=198.51.100.0/24 comment=”defconf: RFC6890 - TEST-NET-2” list=bad_ipv4
add address=203.0.113.0/24 comment=”defconf: RFC6890 - TEST-NET-3” list=bad_ipv4
add address=240.0.0.0/4 comment=”defconf: RFC6890 - reserved” list=bad_ipv4

add address=0.0.0.0/8 comment=”defconf: RFC6890 - this network” list=not_global_ipv4
add address=10.0.0.0/8 comment=”defconf: RFC6890 - private networks” list=not_global_ipv4
add address=100.64.0.0/10 comment=”defconf: RFC6890 - shared address” list=not_global_ipv4
add address=169.254.0.0/16 comment=”defconf: RFC6890 - link local” list=not_global_ipv4
add address=172.16.0.0/12 comment=”defconf: RFC6890 - private networks” list=not_global_ipv4
add address=192.0.0.0/29 comment=”defconf: RFC6890 - DS-Lite” list=not_global_ipv4
add address=192.168.0.0/16 comment=”defconf: RFC6890 - private networks” list=not_global_ipv4
add address=198.18.0.0/15 comment=”defconf: RFC6890 - benchmarking” list=not_global_ipv4
add address=255.255.255.255/32 comment=”defconf: RFC6890 - limited broadcast” list=not_global_ipv4

add address=224.0.0.0/4 comment=”defconf: RFC5771 - multicast” list=bad_src_ipv4
add address=255.255.255.255/32 comment=”defconf: RFC6890 - limited broadcast” list=bad_src_ipv4
add address=0.0.0.0/8 comment=”defconf: RFC6890 - this network” list=bad_dst_ipv4

add address=0.0.0.0 comment=”ddosconf: DDoS” list=ddos_targets_ipv4
add address=0.0.0.0 comment=”ddosconf: DDoS” list=ddos_attackers_ipv4

/ip firewall filter

add action=accept chain=input comment=”defconf: accept ICMP after RAW” protocol=icmp
add action=accept chain=input comment=”defconf: accept established,related,untracked” connection-state=established,related,untracked
add action=drop chain=input comment=”defconf: drop invalid” connection-state=invalid
add action=drop chain=input comment=”defconf: drop all not from LAN” in-interface-list=!LAN

add action=fasttrack-connection chain=forward comment=”defconf: fasttrack” connection-state=established,related
add action=accept chain=forward comment=”defconf: accept established,related,untracked” connection-state=established,related,untracked
add action=drop chain=forward comment=”defconf: drop invalid” connection-state=invalid
add action=drop chain=forward comment=”defconf: drop all from WAN not DSTNATed” connection-nat-state=!dstnat connection-state=new in-interface-list=WAN log=yes log-prefix=”[wan-not-dnat]”
add action=drop chain=forward comment=”onuconf: drop all from ONU not DSTNATed” connection-nat-state=!dstnat connection-state=new in-interface-list=ONU log=yes log-prefix=”[onu-not-dnat]”
add action=drop chain=forward comment=”defconf: drop bad forward IPs” src-address-list=no_forward_ipv4
add action=drop chain=forward comment=”defconf: drop bad forward IPs” dst-address-list=no_forward_ipv4

add action=jump chain=forward comment=”ddosconf: DDoS SYN-ACK” protocol=tcp tcp-flags=syn,ack jump-target=detect-syn-ack
add action=return chain=detect-syn-ack comment=”ddosconf: DDoS SYN-ACK” dst-limit=64,64,src-and-dst-addresses/10s
add action=add-dst-to-address-list chain=detect-syn-ack comment=”ddosconf: DDoS SYN-ACK” address-list=ddos_targets_ipv4 address-list-timeout=10m
add action=add-src-to-address-list chain=detect-syn-ack comment=”ddosconf: DDoS SYN-ACK” address-list=ddos_attackers_ipv4 address-list-timeout=10m log=yes log-prefix=”[sa-flood-ipv4]”

add action=jump chain=forward comment=”ddosconf: DDoS” connection-state=new jump-target=detect-ddos
add action=return chain=detect-ddos comment=”ddosconf: DDoS” dst-limit=256,256,src-and-dst-addresses/10s
add action=add-dst-to-address-list chain=detect-ddos comment=”ddosconf: DDoS” address-list=ddos_targets_ipv4 address-list-timeout=10m
add action=add-src-to-address-list chain=detect-ddos comment=”ddosconf: DDoS” address-list=ddos_attackers_ipv4 address-list-timeout=10m log=yes log-prefix=”[ddos-ipv4]”

/ip firewall nat

add action=masquerade chain=srcnat comment=”defconf: masquerade IPv4” out-interface-list=WAN
add action=masquerade chain=srcnat comment=”onuconf: access to ONU” out-interface-list=ONU src-address-list=local_lan_ipv4 dst-address-list=local_onu_ipv4

add action=accept chain=dstnat comment=”lanconf: accept local DNS server’s query (UDP)” dst-port=53 in-interface-list=LAN protocol=udp src-address-list=local_dns_ipv4
add action=accept chain=dstnat comment=”lanconf: accept local DNS server’s query (TCP)” dst-port=53 in-interface-list=LAN protocol=tcp src-address-list=local_dns_ipv4
add action=redirect chain=dstnat comment=”lanconf: redirect DNS query (UDP)” dst-port=53 in-interface-list=LAN protocol=udp to-ports=53
add action=redirect chain=dstnat comment=”lanconf: redirect DNS query (TCP)” dst-port=53 in-interface-list=LAN protocol=tcp to-ports=53

/ip firewall mangle

add action=change-mss chain=forward comment=”defconf: fix IPv4 mss for WAN” new-mss=clamp-to-pmtu passthrough=yes protocol=tcp tcp-flags=syn
add action=accept chain=prerouting comment=”onuconf: access to ONU” src-address-list=local_lan_ipv4 dst-address-list=local_onu_ipv4

/ip firewall raw

add action=accept chain=prerouting comment=”defconf: enable for transparent firewall” disabled=yes

add action=drop chain=prerouting comment=”ddosconf: DDoS” dst-address-list=ddos_targets_ipv4 src-address-list=ddos_attackers_ipv4

add action=accept chain=prerouting comment=”defconf: accept DHCPv4 discover” dst-address=255.255.255.255 dst-port=67 in-interface-list=LAN protocol=udp src-address=0.0.0.0 src-port=68

add action=drop chain=prerouting comment=”defconf: drop bogon IPs” src-address-list=bad_ipv4
add action=drop chain=prerouting comment=”defconf: drop bogon IPs” dst-address-list=bad_ipv4
add action=drop chain=prerouting comment=”defconf: drop bad SRC IPv4” src-address-list=bad_src_ipv4
add action=drop chain=prerouting comment=”defconf: drop bad DST IPv4” dst-address-list=bad_dst_ipv4

add action=drop chain=prerouting comment=”defconf: drop non global from WAN” in-interface-list=WAN src-address-list=not_global_ipv4
add action=drop chain=prerouting comment=”defconf: drop forward to local LAN from WAN” in-interface-list=WAN dst-address-list=local_lan_ipv4 log=yes log-prefix=”[wan-to-lan]”

add action=drop chain=prerouting comment=”onuconf: drop if not from ONU address” in-interface-list=ONU src-address-list=!local_onu_ipv4
add action=drop chain=prerouting comment=”onuconf: drop forward to local LAN from ONU” in-interface-list=ONU dst-address-list=local_lan_ipv4 log=yes log-prefix=”[onu-to-lan]”

add action=drop chain=prerouting comment=”defconf: drop if not from default IPv4 range” in-interface-list=LAN src-address-list=!local_lan_ipv4

add action=drop chain=prerouting comment=”defconf: drop UDP port 0” port=0 protocol=udp log=yes log-prefix=”[udp-port-0]”

add action=jump chain=prerouting comment=”defconf: jump to TCP chain” jump-target=bad-tcp protocol=tcp
add action=jump chain=prerouting comment=”defconf: jump to ICMP chain” jump-target=icmp4 protocol=icmp

add action=accept chain=prerouting comment=”defconf: accept everything else from LAN” in-interface-list=LAN
add action=accept chain=prerouting comment=”defconf: accept everything else from WAN” in-interface-list=WAN
add action=accept chain=prerouting comment=”onuconf: accept everything else from ONU” in-interface-list=ONU

add action=drop chain=prerouting comment=”defconf: drop the rest”

add action=drop chain=bad-tcp comment=”defconf: drop TCP port 0” port=0 protocol=tcp log=yes log-prefix=”[tcp-port-0]”
add action=drop chain=bad-tcp comment=”defconf: TCP flag filter” protocol=tcp tcp-flags=!fin,!syn,!rst,!ack
add action=drop chain=bad-tcp comment=”defconf: drop flags fin,syn” protocol=tcp tcp-flags=fin,syn
add action=drop chain=bad-tcp comment=”defconf: drop flags fin,rst” protocol=tcp tcp-flags=fin,rst
add action=drop chain=bad-tcp comment=”defconf: drop flags fin,!ack” protocol=tcp tcp-flags=fin,!ack
add action=drop chain=bad-tcp comment=”defconf: drop flags fin,urg” protocol=tcp tcp-flags=fin,urg
add action=drop chain=bad-tcp comment=”defconf: drop flags syn,rst” protocol=tcp tcp-flags=syn,rst
add action=drop chain=bad-tcp comment=”defconf: drop flags rst,urg” protocol=tcp tcp-flags=rst,urg

add action=accept chain=icmp4 comment=”defconf: echo reply” icmp-options=0:0 protocol=icmp

add action=drop chain=icmp4 comment=”onuconf: drop other ICMP from ONU” protocol=icmp in-interface-list=ONU log=yes log-prefix=”[onu-icmp]”

add action=accept chain=icmp4 comment=”defconf: host unreachable” icmp-options=3:1 protocol=icmp
add action=accept chain=icmp4 comment=”defconf: port unreachable” icmp-options=3:3 protocol=icmp
add action=accept chain=icmp4 comment=”defconf: fragmentation needed” icmp-options=3:4 protocol=icmp
add action=accept chain=icmp4 comment=”defconf: time exceeded” icmp-options=11:0-255 protocol=icmp

add action=drop chain=icmp4 comment=”lanconf: drop other ICMP from WAN” protocol=icmp in-interface-list=WAN

add action=accept chain=icmp4 comment=”defconf: net unreachable” icmp-options=3:0 protocol=icmp
add action=accept chain=icmp4 comment=”defconf: protocol unreachable” icmp-options=3:2 protocol=icmp

add action=accept chain=icmp4 comment=”lanconf: echo to local device” icmp-options=8:0 protocol=icmp dst-address-list=local_lan_ipv4
add action=accept chain=icmp4 comment=”onuconf: echo to ONU” icmp-options=8:0 protocol=icmp dst-address-list=local_onu_ipv4
add action=drop chain=icmp4 comment=”lanconf: echo to non global” icmp-options=8:0 protocol=icmp dst-address-list=not_global_ipv4
add action=accept chain=icmp4 comment=”lanconf: echo to WAN” icmp-options=8:0 protocol=icmp

add action=drop chain=icmp4 comment=”defconf: drop all other ICMP” protocol=icmp

/ip firewall connection tracking

set tcp-syn-sent-timeout=120s
set tcp-syn-received-timeout=60s
set tcp-established-timeout=7440s
set tcp-fin-wait-timeout=120s
set tcp-close-wait-timeout=60s
set tcp-last-ack-timeout=30s
set tcp-time-wait-timeout=120s
set tcp-close-timeout=10s
set tcp-max-retrans-timeout=300s
set tcp-unacked-timeout=300s
set udp-timeout=30s
set udp-stream-timeout=120s
set icmp-timeout=30s
set generic-timeout=600s

/ip settings
set max-neighbor-entries=1024 rp-filter=loose tcp-syncookies=yes

/ip neighbor discovery-settings
set discover-interface-list=none

/ip proxy
set enabled=no

/ip socks
set enabled=no

/ip upnp
set enabled=no

/ip cloud
set ddns-enabled=no update-time=no

/ip ssh
set strong-crypto=yes

/tool mac-server
set allowed-interface-list=none

/tool mac-server mac-winbox
set allowed-interface-list=none

/tool mac-server ping
set enabled=no

/tool bandwidth-server
set enabled=no


RouterOS的优化配置方案
http://anximin.github.io/2024/03/25/net_routerOS_config/
作者
Sylar
发布于
2024年3月25日
许可协议