在 Linux 系统中 iptables 可是一大保护神,但是苦于不懂如何配置 iptables 所以一直都是直接关掉 iptables 来使用。今天简要记录一下,利用 iptables 抵御一部分的 DDos 攻击。

0x01

在 Bash 中敲入:

1
vim /etc/sysconfig/iptables

然后,在iptables中加入下面几行

1
2
3
4
#anti syn,ddos
-A FORWARD -p tcp --syn -m limit --limit 1/s --limit-burst 5 -j ACCEPT
-A FORWARD -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s -j ACCEPT
-A FORWARD -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT

第一行:每秒中最多允许5个新连接。第二行:防止各种端口扫描。第三行:Ping洪水攻击(Ping of Death),可以根据需要调整或关闭

保存 iptables 后,我们再敲入:

1
service iptables restart

0x02 常用规则

以下是 iptables 的常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 屏蔽一个IP 
iptables -I INPUT -s 192.168.0.1 -j DROP

# 防 Ping
iptables -A INPUT -p icmp -j DROP

# 防止同步包洪水(Sync Flood)
iptables -A FORWARD -p tcp --syn -m limit --limit 1/s -j ACCEPT

# 防止端口扫描
iptables -A FORWARD -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s -j ACCEPT

# 防止 Ping 洪水攻击(Ping of Death)
iptables -A FORWARD -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT

# 防止 NMAP FIN/URG/PSH
iptables -A INPUT -i eth0 -p tcp --tcp-flags ALL FIN,URG,PSH -j DROP

# Xmas Tree
iptables -A INPUT -i eth0 -p tcp --tcp-flags ALL ALL -j DROP

# Another Xmas Tree
iptables -A INPUT -i eth0 -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP

# Null Scan(possibly)
iptables -A INPUT -i eth0 -p tcp --tcp-flags ALL NONE -j DROP

# SYN/RST
iptables -A INPUT -i eth0 -p tcp --tcp-flags SYN,RST SYN,RST -j DROP

# SYN/FIN -- Scan(possibly)
iptables -A INPUT -i eth0 -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP

# 限制对内部封包的发送速度
iptables -A INPUT -f -m limit --limit 100/s --limit-burst 100 -j ACCEPT

# 限制建立联机的转
iptables -A FORWARD -f -m limit --limit 100/s --limit-burst 100 -j ACCEPT

0x03 Reference

Linux iptables 防止 syn ddos ping 等 攻击