flush ruleset
table inet filter {
chain forward {
type filter hook forward priority 0; policy drop
iifname "docker0" counter accept comment "允許來自 Docker 的轉發流量"
iifname "br-*" counter accept comment "允許來自 Docker 網橋的轉發流量"
oifname { "docker0", "br-*" } ct state established,related counter accept comment "允許返回 Docker 的回應流量"
meta l4proto { tcp, udp } th dport 53 counter accept comment "允許 DNS 查詢轉發"
limit rate 60/minute burst 100 packets \
log prefix "Forward - Drop: " \
comment "Log any unmatched traffic"
counter \
comment "Count any unmatched traffic"
}
chain input {
type filter hook input priority 0; policy drop
iif lo \
accept \
comment "Permit all traffic in from loopback interface"
iifname { "docker0", "br-*" } counter accept comment "允許來自 Docker 網路的流量"
tcp flags & (fin|syn|rst|ack) == 0 counter drop comment "空封包丟棄"
tcp flags syn tcp option maxseg size 0 counter drop comment "丟棄異常的 MSS 值封包"
ct state established,related \
counter \
accept \
comment "Permit established/related connections"
tcp flags != syn ct state new \
limit rate 100/minute burst 150 packets \
log prefix "IN - New !SYN: " \
comment "Rate limit logging for new connections that do not have the SYN TCP flag set"
tcp flags != syn ct state new \
counter \
drop \
comment "Drop new connections that do not have the SYN TCP flag set"
tcp flags & (fin|syn) == (fin|syn) \
limit rate 100/minute burst 150 packets \
log prefix "IN - TCP FIN|SIN: " \
comment "Rate limit logging for TCP packets with invalid fin/syn flag set"
tcp flags & (fin|syn) == (fin|syn) \
counter \
drop \
comment "Drop TCP packets with invalid fin/syn flag set"
tcp flags & (syn|rst) == (syn|rst) \
limit rate 100/minute burst 150 packets \
log prefix "IN - TCP SYN|RST: " \
comment "Rate limit logging for TCP packets with invalid syn/rst flag set"
tcp flags & (syn|rst) == (syn|rst) \
counter \
drop \
comment "Drop TCP packets with invalid syn/rst flag set"
tcp flags & (fin|syn|rst|psh|ack|urg) < (fin) \
limit rate 100/minute burst 150 packets \
log prefix "IN - FIN:" \
comment "Rate limit logging for invalid TCP flags (fin|syn|rst|psh|ack|urg) < (fin)"
tcp flags & (fin|syn|rst|psh|ack|urg) < (fin) \
counter \
drop \
comment "Drop TCP packets with flags (fin|syn|rst|psh|ack|urg) < (fin)"
tcp flags & (fin|syn|rst|psh|ack|urg) == (fin|psh|urg) \
limit rate 100/minute burst 150 packets \
log prefix "IN - FIN|PSH|URG:" \
comment "Rate limit logging for invalid TCP flags (fin|syn|rst|psh|ack|urg) == (fin|psh|urg)"
tcp flags & (fin|syn|rst|psh|ack|urg) == (fin|psh|urg) \
counter \
drop \
comment "Drop TCP packets with flags (fin|syn|rst|psh|ack|urg) == (fin|psh|urg)"
ct state invalid \
limit rate 100/minute burst 150 packets \
log flags all prefix "IN - Invalid: " \
comment "Rate limit logging for traffic with invalid connection state"
ct state invalid \
counter \
drop \
comment "Drop traffic with invalid connection state"
ip protocol icmp icmp type { echo-reply, echo-request } \
limit rate 2000/second \
counter \
accept \
comment "Permit inbound IPv4 echo (ping) limited to 2000 PPS"
ip protocol icmp \
counter \
accept \
comment "Permit all other IPv4 ICMP"
icmpv6 type { echo-reply, echo-request } \
limit rate 2000/second \
counter \
accept \
comment "Permit inbound IPv6 echo (ping) limited to 2000 PPS"
meta l4proto { icmpv6 } \
counter \
accept \
comment "Permit all other IPv6 ICMP"
udp dport 33434-33524 \
limit rate 500/second \
counter \
accept \
comment "Permit inbound UDP traceroute limited to 500 PPS"
tcp dport ssh ct state new \
counter \
accept \
comment "Permit inbound SSH connections"
tcp dport { http, https } ct state new \
counter \
accept \
comment "Permit inbound HTTP and HTTPS connections"
limit rate 60/minute burst 100 packets \
log prefix "IN - Drop: " \
comment "Log any unmatched traffic"
counter \
comment "Count any unmatched traffic"
}
chain output {
type filter hook output priority 0; policy drop
oif lo \
accept \
comment "Permit all traffic out to loopback interface"
oifname { "docker0", "br-*" } counter accept comment "允許 Docker 網路輸出"
ct state established,related \
counter \
accept \
comment "Permit established/related connections"
ct state invalid \
limit rate 100/minute burst 150 packets \
log flags all prefix "OUT - Invalid: " \
comment "Rate limit logging for traffic with invalid connection state"
ct state invalid \
counter \
drop \
comment "Drop traffic with invalid connection state"
ip protocol icmp icmp type { echo-reply, echo-request } \
limit rate 2000/second \
counter \
accept \
comment "Permit outbound IPv4 echo (ping) limited to 2000 PPS"
ip protocol icmp \
counter \
accept \
comment "Permit all other IPv4 ICMP"
icmpv6 type { echo-reply, echo-request } \
limit rate 2000/second \
counter \
accept \
comment "Permit outbound IPv6 echo (ping) limited to 2000 PPS"
meta l4proto { icmpv6 } \
counter \
accept \
comment "Permit all other IPv6 ICMP"
udp dport 33434-33524 \
limit rate 500/second \
counter \
accept \
comment "Permit outbound UDP traceroute limited to 500 PPS"
tcp dport { http, https } ct state new \
counter \
accept \
comment "Permit outbound HTTP and HTTPS connections"
meta l4proto { tcp, udp } th dport 53 \
counter \
accept \
comment "Permit outbound TCP and UDP DNS requests"
udp dport 123 \
counter \
accept \
comment "Permit outbound NTP requests"
counter comment "計數即將丟棄的流量"
limit rate 60/minute burst 100 packets \
log prefix "OUT - Drop: " \
comment "Log any unmatched traffic"
counter \
comment "Count any unmatched traffic"
}
}