Bar*_*Vos 26
#!/bin/bash
WHITELIST=/whitelist.txt
BLACKLIST=/blacklist.txt
#THIS WILL CLEAR ALL EXISTING RULES!
echo 'Clearing all rules'
iptables -F
#
## Whitelist
#
for x in `grep -v ^# $WHITELIST | awk '{print $1}'`; do
echo "Permitting $x..."
$IPTABLES -A INPUT -t filter -s $x -j ACCEPT
done
#
## Blacklist
#
for x in `grep -v ^# $BLACKLIST | awk '{print $1}'`; do
echo "Denying $x..."
$IPTABLES -A INPUT -t filter -s $x -j DROP
done
Run Code Online (Sandbox Code Playgroud)
#!/bin/bash
ALLOWEDTCP="80 3128 3784"
ALLOWEDUDP="3128 3784"
#
## Permitted Ports
#
for port in $ALLOWEDTCP; do
echo "Accepting port TCP $port..."
$IPTABLES -A INPUT -t filter -p tcp --dport $port -j ACCEPT
done
for port in $ALLOWEDUDP; do
echo "Accepting port UDP $port..."
$IPTABLES -A INPUT -t filter -p udp --dport $port -j ACCEPT
done
Run Code Online (Sandbox Code Playgroud)
# Attempt to block portscans
# Anyone who tried to portscan us is locked out for an entire day.
iptables -A INPUT -m recent --name portscan --rcheck --seconds 86400 -j DROP
iptables -A FORWARD -m recent --name portscan --rcheck --seconds 86400 -j DROP
# Once the day has passed, remove them from the portscan list
iptables -A INPUT -m recent --name portscan --remove
iptables -A FORWARD -m recent --name portscan --remove
# These rules add scanners to the portscan list, and log the attempt.
iptables -A INPUT -p tcp -m tcp --dport 139 -m recent --name portscan --set -j LOG --log-prefix "Portscan:"
iptables -A INPUT -p tcp -m tcp --dport 139 -m recent --name portscan --set -j DROP
iptables -A FORWARD -p tcp -m tcp --dport 139 -m recent --name portscan --set -j LOG --log-prefix "Portscan:"
iptables -A FORWARD -p tcp -m tcp --dport 139 -m recent --name portscan --set -j DROP
Run Code Online (Sandbox Code Playgroud)
# Reject spoofed packets
# These adresses are mostly used for LAN's, so if these would come to a WAN-only server, drop them.
iptables -A INPUT -s 10.0.0.0/8 -j DROP
iptables -A INPUT -s 169.254.0.0/16 -j DROP
iptables -A INPUT -s 172.16.0.0/12 -j DROP
iptables -A INPUT -s 127.0.0.0/8 -j DROP
#Multicast-adresses.
iptables -A INPUT -s 224.0.0.0/4 -j DROP
iptables -A INPUT -d 224.0.0.0/4 -j DROP
iptables -A INPUT -s 240.0.0.0/5 -j DROP
iptables -A INPUT -d 240.0.0.0/5 -j DROP
iptables -A INPUT -s 0.0.0.0/8 -j DROP
iptables -A INPUT -d 0.0.0.0/8 -j DROP
iptables -A INPUT -d 239.255.255.0/24 -j DROP
iptables -A INPUT -d 255.255.255.255 -j DROP
# Drop all invalid packets
iptables -A INPUT -m state --state INVALID -j DROP
iptables -A FORWARD -m state --state INVALID -j DROP
iptables -A OUTPUT -m state --state INVALID -j DROP
Run Code Online (Sandbox Code Playgroud)
# Stop smurf attacks
iptables -A INPUT -p icmp -m icmp --icmp-type address-mask-request -j DROP
iptables -A INPUT -p icmp -m icmp --icmp-type timestamp-request -j DROP
iptables -A INPUT -p icmp -m icmp -j DROP
# Drop excessive RST packets to avoid smurf attacks
iptables -A INPUT -p tcp -m tcp --tcp-flags RST RST -m limit --limit 2/second --limit-burst 2 -j ACCEPT
Run Code Online (Sandbox Code Playgroud)
# Don't allow pings through
iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j DROP
Run Code Online (Sandbox Code Playgroud)
pep*_*uan 26
ipset如果您仅基于 IP、端口或两者编写了很多类似的规则,请考虑使用ipset来优化 netfilter 的性能。
例如:
iptables -s 192.168.1.11 -j ACCEPT
iptables -s 192.168.1.27 -j ACCEPT
iptables -s 192.168.1.44 -j ACCEPT
... hundreds of similar rules ...
iptables -s 192.168.251.177 -j ACCEPT
Run Code Online (Sandbox Code Playgroud)
这意味着源地址为 192.168.251.177 的数据包必须先遍历数百条规则,然后才能得到 ACCEPT 的判断。
当然,有经验的系统管理员会按子网划分规则。但这仍然意味着数百条规则。
ipset 来救援!
首先,定义一个 IP Setipmap类型:
ipset -N Allowed_Hosts ipmap --network 192.168.0.0/16
Run Code Online (Sandbox Code Playgroud)
然后,用地址填充它:
for ip in $LIST_OF_ALLOWED_IP; do ipset -A Allowed_Hosts $ip; done
Run Code Online (Sandbox Code Playgroud)
最后,将上面数百条 iptables 规则替换为一条规则:
iptables -m set --match-set Allowed_Hosts src -j ACCEPT
Run Code Online (Sandbox Code Playgroud)
当数据包到达时,netfilter 将根据IP 集对数据包的源 (src) IP执行非常快速的位图搜索Allowed_Hosts。来自 192.168.0.0/16 的所有数据包都将经历一个规则。请相信我,搜索位图至少比执行数百个 iptables 规则检查快两个数量级。
ipset不限于 IP 地址。它还可以基于端口、IP-端口元组、网络/子网地址、IP-MAC 元组等进行匹配。它可以匹配这些标准作为源或目标或两者的混合(在元组的情况下)。
最后,ipset您可以自动将 IP 地址放入黑名单/白名单。这些黑名单/白名单也可以“老化”,从而在经过可配置的时间后自动删除 IP 地址。
有关更多详细信息,请参阅ipset的手册页。
一些 Linux 发行版可能没有“开箱即用”的支持ipset(例如 Ubuntu 10.04 有这个问题)。在这些系统上,一种方法是ipset从源代码安装。
相反,ipset从其网站下载的源代码:http : //ipset.netfilter.org/install.html
另外,如果你使用xtables-addons,IPSET被包含在它的源:http://xtables-addons.sourceforge.net/
小智 22
向您的规则添加评论:
-m comment --comment "Comments help to read output of iptables -nvL"
Run Code Online (Sandbox Code Playgroud)
pep*_*uan 18
添加以下规则,最好在 -t raw -A PREROUTING
-p tcp --tcp-flags FIN,SYN FIN,SYN -j DROP
-p tcp --tcp-flags SYN,RST SYN,RST -j DROP
-p tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -j DROP
-p tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN -j DROP
-p tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j DROP
-p tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,SYN,RST,PSH,ACK,URG -j DROP
Run Code Online (Sandbox Code Playgroud)
被阻止的攻击分别是:
(随意编辑上述攻击的名称)
echo 1 > /proc/sys/net/ipv4/ip_forward/sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE第 1 步设置内核参数以允许 ip 转发,第 2 步设置一个 iptables 规则,在接口 eth0 上启用 NAT。
添加以下规则,最好在 -t raw -A PREROUTING
-p icmp -m u32 ! --u32 "4&0x3FFF=0" -j DROP
-p icmp -m length --length 1492:65535 -j DROP
Run Code Online (Sandbox Code Playgroud)
第一条规则阻止所有的ICMP报文“碎片化标志”是不为0(ICMP应该永远不会被分割,他们应该携带小型有效载荷)
第二条规则阻止过大的未分段 ICMP 数据包。
(来自我的 iptables_tricks.txt 文件,从很多地方重新编译:P)
使 iptables 在端口 22 (SSH) 上来自同一 IP 的新连接之间等待 15 秒:
iptables -A INPUT -p tcp -i eth0 -m state --state NEW --dport 22 -m recent --update --seconds 15 -j DROP
iptables -A INPUT -p tcp -i eth0 -m state --state NEW --dport 22 -m recent --set -j ACCEPT
Run Code Online (Sandbox Code Playgroud)