Linux 下用 iptables 预防 DDOS


DDOS 是十分常见的攻击,即使是一般使用者,下载一套 DDOS 软件,或者直接安装 kali linux, 便可以很简单发动 DDOS 攻击,除了遇到 DDOS 攻击才采取拦截外,也可以透过 iptables 或一些 Linux 设定来预防 DDOS 攻击,以下会列出一些预防 DDOS 的设定及 iptables 规则。

Linux Kernel
透过修改 Linux Kernel 的设定,同样可以有效减低 DDOS 的威胁,RHEL 7 及 CentOS 7 支援 SYNPROXY 设定,以下会以 CentOS 7 为例,透过修改 Kernel 的参数来减低 DDOS 的威胁。

开启档案 /etc/sysctl.conf,加入以下设定:

kernel.printk = 4 4 1 7
kernel.panic = 10
kernel.sysrq = 0
kernel.shmmax = 4294967296
kernel.shmall = 4194304
kernel.core_uses_pid = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
vm.swappiness = 20
vm.dirty_ratio = 80
vm.dirty_background_ratio = 5
fs.file-max = 2097152
net.core.netdev_max_backlog = 262144
net.core.rmem_default = 31457280
net.core.rmem_max = 67108864
net.core.wmem_default = 31457280
net.core.wmem_max = 67108864
net.core.somaxconn = 65535
net.core.optmem_max = 25165824
net.ipv4.neigh.default.gc_thresh1 = 4096
net.ipv4.neigh.default.gc_thresh2 = 8192
net.ipv4.neigh.default.gc_thresh3 = 16384
net.ipv4.neigh.default.gc_interval = 5
net.ipv4.neigh.default.gc_stale_time = 120
net.netfilter.nf_conntrack_max = 10000000
net.netfilter.nf_conntrack_tcp_loose = 0
net.netfilter.nf_conntrack_tcp_timeout_established = 1800
net.netfilter.nf_conntrack_tcp_timeout_close = 10
net.netfilter.nf_conntrack_tcp_timeout_close_wait = 10
net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 20
net.netfilter.nf_conntrack_tcp_timeout_last_ack = 20
net.netfilter.nf_conntrack_tcp_timeout_syn_recv = 20
net.netfilter.nf_conntrack_tcp_timeout_syn_sent = 20
net.netfilter.nf_conntrack_tcp_timeout_time_wait = 10
net.ipv4.tcp_slow_start_after_idle = 0
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.ip_no_pmtu_disc = 1
net.ipv4.route.flush = 1
net.ipv4.route.max_size = 8048576
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.icmp_ignore_bogus_error_responses = 1
net.ipv4.tcp_congestion_control = htcp
net.ipv4.tcp_mem = 65536 131072 262144
net.ipv4.udp_mem = 65536 131072 262144
net.ipv4.tcp_rmem = 4096 87380 33554432
net.ipv4.udp_rmem_min = 16384
net.ipv4.tcp_wmem = 4096 87380 33554432
net.ipv4.udp_wmem_min = 16384
net.ipv4.tcp_max_tw_buckets = 1440000
net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_max_orphans = 400000
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rfc1337 = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_timestamps = 1
net.ipv4.tcp_sack = 1
net.ipv4.tcp_fack = 1
net.ipv4.tcp_ecn = 2
net.ipv4.tcp_fin_timeout = 10
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_intvl = 60
net.ipv4.tcp_keepalive_probes = 10
net.ipv4.tcp_no_metrics_save = 1
net.ipv4.ip_forward = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.all.rp_filter = 1

储存盘案后,重新开机会自动加载新参数设定,也可以输入以下指令重新加载 /etc/sysctl.conf 的设定,而无需重新开机:

# sysctl –system

iptables 规则
DDOS 有很多不同的方法,要用 iptables 预方全部的攻击几乎不可能,幸好透过 connection tracking (nf_conntrack 核心模组),可以拦截大部份基于 TCP 的非 SYN packets 攻击,以下的 iptables 规则也可以拦截大部份的 TCP DDOS 攻击。

拦截 Invalid Packets:

以下 iptables 规则会拦截所有不是 SYN packet,及不会放到 established TCP 连线。

限制每个 ip 连线数量:

这个规则可以限制每个 ip 最多建立的 established connections, 以下设定为每个 ip 最多 80 个连线:

以下 iptables 规则会对指定埠号,限制每个 ip 的连线数量,以下例子会限制每个 ip 在 port 25 建立 4 个连线;对 port 80 建立 20 个连线:

限制每个 ip 每秒建立连线数量:

RHEL 7 / CentOS 7 使用的 Kernel 及 iptables 已经支援 SYNPROXY, SYNPROXY 的作用是检查收到的 SYN packet 是真实连线,还是传送 SYN packet 后什么都不做,即达到霸占连线 DDOS 的目的,如果什么都不做,便会将 packet 取消,将 DDOS 的影响减都最低。

以下是用使用 SYNPROXY 预防 SYN Floods 的 iptables 规则,以下的规则会对所有埠号生效,如果只想对指定的埠号生劾,可以在规则后面加上 “- -dport xx”

要检查 SYNPROXY 是否生效,可以用以下指令监测档案 /proc/net/stat/synproxy, 如果当建立新 TCP 连线时,该档案的数值有转变,那便表示 SYNPROXY 生效了。

Leave a Reply