限制会话数
#!/bin/sh
INET=192.168.0.
IPS=1
IPE=253
IDEV=eth1
ODEV=eth0
COUNTER=$IPS
while [ $COUNTER -lt $IPE ]
do
iptables -A FORWARD -i $IDEV -s $INET$COUNTER -m iplimit –iplimit-above 2 -j REJECT
COUNTER=` expr $COUNTER + 1 `
done
限制穿透本机FORWARD链的192.168.0.0/24的每个IP并发连接数不超过2个,超过的被拒绝
脚本2:
针对每个ip限制带宽
#!/bin/bash
tc qdisc del dev eth0 root 2>/dev/null
tc qdisc del dev eth0 ingress 2>/dev/null
tc qdisc del dev eth1 root 2>/dev/null
tc qdisc del dev eth1 ingress 2>/dev/null
DOWNLOAD=128Kbit
UPLOAD=128Kbit
INET=192.168.0.
IPS=1
IPE=253
IDEV=eth1
ODEV=eth0
tc qdisc add dev eth1 root handle 10: cbq bandwidth 100Mbit avpkt 1000
tc qdisc add dev eth0 root handle 20: cbq bandwidth 100Mbit avpkt 1000
tc class add dev eth1 parent 10:0 classid 10:1 cbq bandwidth 100Mbit rate 100Mbit allot 1514 weight 1Mbit prio 8 maxburst 20 avpkt 1000
tc class add dev eth0 parent 20:0 classid 20:1 cbq bandwidth 100Mbit rate 100Mbit allot 1514 weight 1024Kbit prio 8 maxburst 20 avpkt 1000
COUNTER=$IPS
while [ $COUNTER -le $IPE ]
do
tc class add dev $IDEV parent 10:1 classid 10:1$COUNTER cbq bandwidth 100Mbit rate $DOWNLOAD allot 1514 weight 20Kbit prio 5 maxburst 20 avpkt 1000 bounded
tc qdisc add dev $IDEV parent 10:1$COUNTER sfq quantum 1514b perturb 15
tc filter add dev $IDEV parent 10:0 protocol ip prio 100 u32 match ip dst $INET$COUNTER flowid 10:1$COUNTER
COUNTER=` expr $COUNTER + 1 `
done
COUNTER=$IPS
while [ $COUNTER -le $IPE ]
do
tc class add dev $ODEV parent 20:1 classid 20:1$COUNTER cbq bandwidth 1Mbit rate $UPLOAD allot 1514 weight 4Kbit prio 5 maxburst 20 avpkt 1000 bounded
tc qdisc add dev $ODEV parent 20:1$COUNTER sfq quantum 1514b perturb 15
tc filter add dev $ODEV parent 20:0 protocol ip prio 100 handle $COUNTER fw classid 20:1$COUNTER
COUNTER=` expr $COUNTER + 1 `
done
COUNTER=$IPS
while [ $COUNTER -lt $IPE ]
do
iptables -t mangle -A PREROUTING -i $IDEV -s $INET$COUNTER -j MARK –set-mark $COUNTER
COUNTER=` expr $COUNTER + 1 `
done
—————————————
<span style="word-wrap: break-word; color: rgb(102, 102, 102);"><em>#!/bin/sh</em></span> <span style="word-wrap: break-word; color: rgb(102, 102, 102);"><em># xiaoh www.linuxbyte.org</em></span> <span style="word-wrap: break-word; color: rgb(102, 102, 102);"><em># 定义进出设备(eth0 内网,eth1外网)</em></span> <span style="word-wrap: break-word; color: rgb(0, 120, 0);">IDEV</span>=<span style="word-wrap: break-word; color: rgb(255, 0, 0);">"eth0"</span> <span style="word-wrap: break-word; color: rgb(0, 120, 0);">ODEV</span>=<span style="word-wrap: break-word; color: rgb(255, 0, 0);">"eth1"</span> <span style="word-wrap: break-word; color: rgb(102, 102, 102);"><em># 定义总的上下带宽</em></span> <span style="word-wrap: break-word; color: rgb(0, 120, 0);">UP</span>=<span style="word-wrap: break-word; color: rgb(255, 0, 0);">"50mbit"</span> <span style="word-wrap: break-word; color: rgb(0, 120, 0);">DOWN</span>=<span style="word-wrap: break-word; color: rgb(255, 0, 0);">"50mbit"</span> <span style="word-wrap: break-word; color: rgb(102, 102, 102);"><em># 定义每个受限制的IP上下带宽</em></span> #rate 起始带宽 <span style="word-wrap: break-word; color: rgb(0, 120, 0);">UPLOAD</span>=<span style="word-wrap: break-word; color: rgb(255, 0, 0);">"4mbit"</span> <span style="word-wrap: break-word; color: rgb(0, 120, 0);">DOWNLOAD</span>=<span style="word-wrap: break-word; color: rgb(255, 0, 0);">"5mbit"</span> #ceil 最大带宽 <span style="word-wrap: break-word; color: rgb(0, 120, 0);">MUPLOAD</span>=<span style="word-wrap: break-word; color: rgb(255, 0, 0);">"5mbit"</span> <span style="word-wrap: break-word; color: rgb(0, 120, 0);">MDOWNLOAD</span>=<span style="word-wrap: break-word; color: rgb(255, 0, 0);">"10mbit"</span> <span style="word-wrap: break-word; color: rgb(102, 102, 102);"><em>#内网IP段</em></span> <span style="word-wrap: break-word; color: rgb(0, 120, 0);">INET</span>=<span style="word-wrap: break-word; color: rgb(255, 0, 0);">"192.168.0."</span> <span style="word-wrap: break-word; color: rgb(102, 102, 102);"><em># 受限IP范围,IPS 起始IP,IPE 结束IP。</em></span> <span style="word-wrap: break-word; color: rgb(0, 120, 0);">IPS</span>=<span style="word-wrap: break-word; color: rgb(255, 0, 0);">"1"</span> <span style="word-wrap: break-word; color: rgb(0, 120, 0);">IPE</span>=<span style="word-wrap: break-word; color: rgb(255, 0, 0);">"114"</span> <span style="word-wrap: break-word; color: rgb(102, 102, 102);"><em># 清除网卡原有队列规则</em></span> tc qdisc del dev <span style="word-wrap: break-word; color: rgb(0, 120, 0);">$ODEV</span> root <span style="word-wrap: break-word;">2</span><span style="word-wrap: break-word; font-weight: bold;">>/</span>dev<span style="word-wrap: break-word; font-weight: bold;">/</span>null tc qdisc del dev <span style="word-wrap: break-word; color: rgb(0, 120, 0);">$IDEV</span> root <span style="word-wrap: break-word;">2</span><span style="word-wrap: break-word; font-weight: bold;">>/</span>dev<span style="word-wrap: break-word; font-weight: bold;">/</span>null <span style="word-wrap: break-word; color: rgb(102, 102, 102);"><em># 定义最顶层(根)队列规则,并指定 default 类别编号</em></span> tc qdisc add dev <span style="word-wrap: break-word; color: rgb(0, 120, 0);">$ODEV</span> root handle <span style="word-wrap: break-word;">10</span>: htb default <span style="word-wrap: break-word;">256</span> tc qdisc add dev <span style="word-wrap: break-word; color: rgb(0, 120, 0);">$IDEV</span> root handle <span style="word-wrap: break-word;">10</span>: htb default <span style="word-wrap: break-word;">256</span> <span style="word-wrap: break-word; color: rgb(102, 102, 102);"><em># 定义第一层的 10:1 类别 (上行/下行 总带宽)</em></span> tc class add dev <span style="word-wrap: break-word; color: rgb(0, 120, 0);">$ODEV</span> parent <span style="word-wrap: break-word;">10</span>: classid <span style="word-wrap: break-word;">10</span>:<span style="word-wrap: break-word;">1</span> htb rate <span style="word-wrap: break-word; color: rgb(0, 120, 0);">$UP</span> ceil <span style="word-wrap: break-word; color: rgb(0, 120, 0);">$UP</span> tc class add dev <span style="word-wrap: break-word; color: rgb(0, 120, 0);">$IDEV</span> parent <span style="word-wrap: break-word;">10</span>: classid <span style="word-wrap: break-word;">10</span>:<span style="word-wrap: break-word;">1</span> htb rate <span style="word-wrap: break-word; color: rgb(0, 120, 0);">$DOWN</span> ceil <span style="word-wrap: break-word; color: rgb(0, 120, 0);">$DOWN</span> <span style="word-wrap: break-word; color: rgb(102, 102, 102);"><em>#开始iptables 打标和设置具体规则</em></span> <span style="word-wrap: break-word; color: rgb(0, 120, 0);">i</span>=<span style="word-wrap: break-word; color: rgb(0, 120, 0);">$IPS</span>; <span style="word-wrap: break-word; font-weight: bold;">while</span> <span style="word-wrap: break-word; color: rgb(122, 8, 116);"><strong>[</strong></span> <span style="word-wrap: break-word; color: rgb(0, 120, 0);">$i</span> <span style="word-wrap: break-word; color: rgb(102, 0, 51);">-le</span> <span style="word-wrap: break-word; color: rgb(0, 120, 0);">$IPE</span> <span style="word-wrap: break-word; color: rgb(122, 8, 116);"><strong>]</strong></span> <span style="word-wrap: break-word; font-weight: bold;">do</span> tc class add dev <span style="word-wrap: break-word; color: rgb(0, 120, 0);">$ODEV</span> parent <span style="word-wrap: break-word;">10</span>:<span style="word-wrap: break-word;">1</span> classid <span style="word-wrap: break-word;">10</span>:<span style="word-wrap: break-word;">2</span><span style="word-wrap: break-word; color: rgb(0, 120, 0);">$i</span> htb rate <span style="word-wrap: break-word; color: rgb(0, 120, 0);">$UPLOAD</span> ceil <span style="word-wrap: break-word; color: rgb(0, 120, 0);">$MUPLOAD</span> prio <span style="word-wrap: break-word;">1</span> tc qdisc add dev <span style="word-wrap: break-word; color: rgb(0, 120, 0);">$ODEV</span> parent <span style="word-wrap: break-word;">10</span>:<span style="word-wrap: break-word;">2</span><span style="word-wrap: break-word; color: rgb(0, 120, 0);">$i</span> handle <span style="word-wrap: break-word;">100</span><span style="word-wrap: break-word; color: rgb(0, 120, 0);">$i</span>: pfifo tc filter add dev <span style="word-wrap: break-word; color: rgb(0, 120, 0);">$ODEV</span> parent <span style="word-wrap: break-word;">10</span>: protocol ip prio <span style="word-wrap: break-word;">100</span> handle <span style="word-wrap: break-word;">2</span><span style="word-wrap: break-word; color: rgb(0, 120, 0);">$i</span> fw classid <span style="word-wrap: break-word;">10</span>:<span style="word-wrap: break-word;">2</span><span style="word-wrap: break-word; color: rgb(0, 120, 0);">$i</span> tc class add dev <span style="word-wrap: break-word; color: rgb(0, 120, 0);">$IDEV</span> parent <span style="word-wrap: break-word;">10</span>:<span style="word-wrap: break-word;">1</span> classid <span style="word-wrap: break-word;">10</span>:<span style="word-wrap: break-word;">2</span><span style="word-wrap: break-word; color: rgb(0, 120, 0);">$i</span> htb rate <span style="word-wrap: break-word; color: rgb(0, 120, 0);">$DOWNLOAD</span> ceil <span style="word-wrap: break-word; color: rgb(0, 120, 0);">$MDOWNLOAD</span> prio <span style="word-wrap: break-word;">1</span> tc qdisc add dev <span style="word-wrap: break-word; color: rgb(0, 120, 0);">$IDEV</span> parent <span style="word-wrap: break-word;">10</span>:<span style="word-wrap: break-word;">2</span><span style="word-wrap: break-word; color: rgb(0, 120, 0);">$i</span> handle <span style="word-wrap: break-word;">100</span><span style="word-wrap: break-word; color: rgb(0, 120, 0);">$i</span>: pfifo tc filter add dev <span style="word-wrap: break-word; color: rgb(0, 120, 0);">$IDEV</span> parent <span style="word-wrap: break-word;">10</span>: protocol ip prio <span style="word-wrap: break-word;">100</span> handle <span style="word-wrap: break-word;">2</span><span style="word-wrap: break-word; color: rgb(0, 120, 0);">$i</span> fw classid <span style="word-wrap: break-word;">10</span>:<span style="word-wrap: break-word;">2</span><span style="word-wrap: break-word; color: rgb(0, 120, 0);">$i</span> iptables <span style="word-wrap: break-word; color: rgb(102, 0, 51);">-t</span> mangle <span style="word-wrap: break-word; color: rgb(102, 0, 51);">-A</span> PREROUTING <span style="word-wrap: break-word; color: rgb(102, 0, 51);">-s</span> <span style="word-wrap: break-word; color: rgb(0, 120, 0);">$INET</span><span style="word-wrap: break-word; color: rgb(0, 120, 0);">$i</span> <span style="word-wrap: break-word; color: rgb(102, 0, 51);">-j</span> MARK <span style="word-wrap: break-word; color: rgb(102, 0, 51);">--set-mark</span> <span style="word-wrap: break-word;">2</span><span style="word-wrap: break-word; color: rgb(0, 120, 0);">$i</span> iptables <span style="word-wrap: break-word; color: rgb(102, 0, 51);">-t</span> mangle <span style="word-wrap: break-word; color: rgb(102, 0, 51);">-A</span> PREROUTING <span style="word-wrap: break-word; color: rgb(102, 0, 51);">-s</span> <span style="word-wrap: break-word; color: rgb(0, 120, 0);">$INET</span><span style="word-wrap: break-word; color: rgb(0, 120, 0);">$i</span> <span style="word-wrap: break-word; color: rgb(102, 0, 51);">-j</span> RETURN iptables <span style="word-wrap: break-word; color: rgb(102, 0, 51);">-t</span> mangle <span style="word-wrap: break-word; color: rgb(102, 0, 51);">-A</span> POSTROUTING <span style="word-wrap: break-word; color: rgb(102, 0, 51);">-d</span> <span style="word-wrap: break-word; color: rgb(0, 120, 0);">$INET</span><span style="word-wrap: break-word; color: rgb(0, 120, 0);">$i</span> <span style="word-wrap: break-word; color: rgb(102, 0, 51);">-j</span> MARK <span style="word-wrap: break-word; color: rgb(102, 0, 51);">--set-mark</span> <span style="word-wrap: break-word;">2</span><span style="word-wrap: break-word; color: rgb(0, 120, 0);">$i</span> iptables <span style="word-wrap: break-word; color: rgb(102, 0, 51);">-t</span> mangle <span style="word-wrap: break-word; color: rgb(102, 0, 51);">-A</span> POSTROUTING <span style="word-wrap: break-word; color: rgb(102, 0, 51);">-d</span> <span style="word-wrap: break-word; color: rgb(0, 120, 0);">$INET</span><span style="word-wrap: break-word; color: rgb(0, 120, 0);">$i</span> <span style="word-wrap: break-word; color: rgb(102, 0, 51);">-j</span> RETURN <span style="word-wrap: break-word; color: rgb(0, 120, 0);">i</span>=<span style="word-wrap: break-word; font-weight: bold;">`</span><span style="word-wrap: break-word; color: rgb(194, 12, 185);"><strong>expr</strong></span> <span style="word-wrap: break-word; color: rgb(0, 120, 0);">$i</span> + <span style="word-wrap: break-word;">1</span><span style="word-wrap: break-word; font-weight: bold;">`</span> <span style="word-wrap: break-word; font-weight: bold;">done</span>
之前的Iptables+tc 网吧每IP 限速脚本一文中有一个问题需要补充,如果同时使用squid 做透明代理会使该脚本失效。
做透明代理时有一条iptables规则
iptables <span style="word-wrap: break-word; color: rgb(102, 0, 51);">-A</span> PREROUTING <span style="word-wrap: break-word; color: rgb(102, 0, 51);">-s</span> 192.168.0.0<span style="word-wrap: break-word; font-weight: bold;">/</span><span style="word-wrap: break-word;">24</span> <span style="word-wrap: break-word; color: rgb(102, 0, 51);">-i</span> eth0 <span style="word-wrap: break-word; color: rgb(102, 0, 51);">-p</span> tcp <span style="word-wrap: break-word; color: rgb(102, 0, 51);">-m</span> tcp <span style="word-wrap: break-word; color: rgb(102, 0, 51);">--dport</span> <span style="word-wrap: break-word;">80</span> <span style="word-wrap: break-word; color: rgb(102, 0, 51);">-j</span> REDIRECT <span style="word-wrap: break-word; color: rgb(102, 0, 51);">--to-ports</span> <span style="word-wrap: break-word;">3128</span>
这一规则把所有内网80端口的请求都转发到了网关,如此一来所有向外网的普通http 请求者都成了网关(192.168.0.254),而网关是不做限速的,所以所有http下载都不被限速了。
所以为了能现在http下载,我们要使用squid 的限速功能,配置命令如下:
acl LIMIT_IP src 192.168.0.0<span style="word-wrap: break-word; font-weight: bold;">/</span><span style="word-wrap: break-word;">25</span> delay_pools <span style="word-wrap: break-word;">1</span> delay_class <span style="word-wrap: break-word;">1</span> <span style="word-wrap: break-word;">2</span> delay_access <span style="word-wrap: break-word;">1</span> allow LIMIT_IP delay_parameters <span style="word-wrap: break-word;">1</span> -<span style="word-wrap: break-word;">1</span><span style="word-wrap: break-word; font-weight: bold;">/</span>-<span style="word-wrap: break-word;">1</span> <span style="word-wrap: break-word;">500000</span><span style="word-wrap: break-word; font-weight: bold;">/</span><span style="word-wrap: break-word;">500000</span>
squid 的限速控制不是很精确限速500000 bytes/sec 的浮动在4xx/KB 到8XX/KB 之间 -__-!
//
2,iptables脚本二
#!/bin/sh
#———
#
# File: SIG-antiDDoS.sh
#
# Configuration.
#———
# For debugging use iptables -v.
IPTABLES=”/sbin/iptables”
IP6TABLES=”/sbin/ip6tables”
MODPROBE=”/sbin/modprobe”
RMMOD=”/sbin/rmmod”
ARP=”/usr/sbin/arp”
# Logging options.
#———
LOG=”LOG –log-level debug –log-tcp-sequence –log-tcp-options”
LOG=”$LOG –log-ip-options”
# Defaults for rate limiting
#———
RLIMIT=”-m limit –limit 3/s –limit-burst 8″
# Unprivileged ports.
#———
PHIGH=”1024:65535″
PSSH=”1000:1023″
# Load required kernel modules
#———
$MODPROBE ip_conntrack_ftp
$MODPROBE ip_conntrack_irc
# Mitigate ARP spoofing/poisoning and similar attacks.
#———
# Hardcode static ARP cache entries here
# $ARP -s IP-ADDRESS MAC-ADDRESS
# Kernel configuration.
#———
# Disable IP forwarding.
# On => Off = (reset)
echo 1 > /proc/sys/net/ipv4/ip_forward
echo 0 > /proc/sys/net/ipv4/ip_forward
# Enable IP spoofing protection
for i in /proc/sys/net/ipv4/conf/*/rp_filter; do echo 1 > $i; done
# Protect against SYN flood attacks
echo 1 > /proc/sys/net/ipv4/tcp_syncookies
# Ignore all incoming ICMP echo requests
echo 0 > /proc/sys/net/ipv4/icmp_echo_ignore_all
# Ignore ICMP echo requests to broadcast
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
# Log packets with impossible addresses.
for i in /proc/sys/net/ipv4/conf/*/log_martians; do echo 1 > $i; done
# Don’t log invalid responses to broadcast
echo 1 > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses
# Don’t accept or send ICMP redirects.
for i in /proc/sys/net/ipv4/conf/*/accept_redirects; do echo 0 > $i; done
for i in /proc/sys/net/ipv4/conf/*/send_redirects; do echo 0 > $i; done
# Don’t accept source routed packets.
for i in /proc/sys/net/ipv4/conf/*/accept_source_route; do echo 0 > $i; done
# Disable multicast routing
for i in /proc/sys/net/ipv4/conf/*/mc_forwarding; do echo 0 > $i; done
# Disable proxy_arp.
for i in /proc/sys/net/ipv4/conf/*/proxy_arp; do echo 0 > $i; done
# Enable secure redirects, i.e. only accept ICMP redirects for gateways
# Helps against MITM attacks.
for i in /proc/sys/net/ipv4/conf/*/secure_redirects; do echo 1 > $i; done
# Disable bootp_relay
for i in /proc/sys/net/ipv4/conf/*/bootp_relay; do echo 0 > $i; done
# Default policies.
#———
# Drop everything by default.
$IPTABLES -P INPUT DROP
$IPTABLES -P FORWARD DROP
$IPTABLES -P OUTPUT DROP
# Set the nat/mangle/raw tables’ chains to ACCEPT
$IPTABLES -t nat -P PREROUTING ACCEPT
$IPTABLES -t nat -P OUTPUT ACCEPT
$IPTABLES -t nat -P POSTROUTING ACCEPT
$IPTABLES -t mangle -P PREROUTING ACCEPT
$IPTABLES -t mangle -P INPUT ACCEPT
$IPTABLES -t mangle -P FORWARD ACCEPT
$IPTABLES -t mangle -P OUTPUT ACCEPT
$IPTABLES -t mangle -P POSTROUTING ACCEPT
# Cleanup.
#———
# Delete all
$IPTABLES -F
$IPTABLES -t nat -F
$IPTABLES -t mangle -F
# Delete all
$IPTABLES -X
$IPTABLES -t nat -X
$IPTABLES -t mangle -X
# Zero all packets and counters.
$IPTABLES -Z
$IPTABLES -t nat -Z
$IPTABLES -t mangle -Z
# Completely disable IPv6.
#———
# Block all IPv6 traffic
# If the ip6tables command is available, try to block all IPv6 traffic.
if test -x $IP6TABLES; then
# Set the default policies
# drop everything
$IP6TABLES -P INPUT DROP 2>/dev/null
$IP6TABLES -P FORWARD DROP 2>/dev/null
$IP6TABLES -P OUTPUT DROP 2>/dev/null
# The mangle table can pass everything
$IP6TABLES -t mangle -P PREROUTING ACCEPT 2>/dev/null
$IP6TABLES -t mangle -P INPUT ACCEPT 2>/dev/null
$IP6TABLES -t mangle -P FORWARD ACCEPT 2>/dev/null
$IP6TABLES -t mangle -P OUTPUT ACCEPT 2>/dev/null
$IP6TABLES -t mangle -P POSTROUTING ACCEPT 2>/dev/null
# Delete all rules.
$IP6TABLES -F 2>/dev/null
$IP6TABLES -t mangle -F 2>/dev/null
# Delete all chains.
$IP6TABLES -X 2>/dev/null
$IP6TABLES -t mangle -X 2>/dev/null
# Zero all packets and counters.
$IP6TABLES -Z 2>/dev/null
$IP6TABLES -t mangle -Z 2>/dev/null
fi
# Custom user-defined chains.
#———
# LOG packets, then ACCEPT.
$IPTABLES -N ACCEPTLOG
$IPTABLES -A ACCEPTLOG -j $LOG $RLIMIT –log-prefix “ACCEPT “
$IPTABLES -A ACCEPTLOG -j ACCEPT
# LOG packets, then DROP.
$IPTABLES -N DROPLOG
$IPTABLES -A DROPLOG -j $LOG $RLIMIT –log-prefix “DROP “
$IPTABLES -A DROPLOG -j DROP
# LOG packets, then REJECT.
# TCP packets are rejected with a TCP reset.
$IPTABLES -N REJECTLOG
$IPTABLES -A REJECTLOG -j $LOG $RLIMIT –log-prefix “REJECT “
$IPTABLES -A REJECTLOG -p tcp -j REJECT –reject-with tcp-reset
$IPTABLES -A REJECTLOG -j REJECT
# Only allows RELATED ICMP types
# (destination-unreachable, time-exceeded, and parameter-problem).
# TODO: Rate-limit this traffic?
# TODO: Allow fragmentation-needed?
# TODO: Test.
$IPTABLES -N RELATED_ICMP
$IPTABLES -A RELATED_ICMP -p icmp –icmp-type destination-unreachable -j ACCEPT
$IPTABLES -A RELATED_ICMP -p icmp –icmp-type time-exceeded -j ACCEPT
$IPTABLES -A RELATED_ICMP -p icmp –icmp-type parameter-problem -j ACCEPT
$IPTABLES -A RELATED_ICMP -j DROPLOG
# Make It Even Harder To Multi-PING
$IPTABLES -A INPUT -p icmp -m limit –limit 1/s –limit-burst 2 -j ACCEPT
$IPTABLES -A INPUT -p icmp -m limit –limit 1/s –limit-burst 2 -j LOG –log-prefix PING-DROP:
$IPTABLES -A INPUT -p icmp -j DROP
$IPTABLES -A OUTPUT -p icmp -j ACCEPT
# Only allow the minimally required/recommended parts of ICMP. Block the rest.
#———
# TODO: This section needs a lot of testing!
# First, drop all fragmented ICMP packets (almost always malicious).
$IPTABLES -A INPUT -p icmp –fragment -j DROPLOG
$IPTABLES -A OUTPUT -p icmp –fragment -j DROPLOG
$IPTABLES -A FORWARD -p icmp –fragment -j DROPLOG
# Allow all ESTABLISHED ICMP traffic.
$IPTABLES -A INPUT -p icmp -m state –state ESTABLISHED -j ACCEPT $RLIMIT
$IPTABLES -A OUTPUT -p icmp -m state –state ESTABLISHED -j ACCEPT $RLIMIT
# Allow some parts of the RELATED ICMP traffic, block the rest.
$IPTABLES -A INPUT -p icmp -m state –state RELATED -j RELATED_ICMP $RLIMIT
$IPTABLES -A OUTPUT -p icmp -m state –state RELATED -j RELATED_ICMP $RLIMIT
# Allow incoming ICMP echo requests (ping), but only rate-limited.
$IPTABLES -A INPUT -p icmp –icmp-type echo-request -j ACCEPT $RLIMIT
# Allow outgoing ICMP echo requests (ping), but only rate-limited.
$IPTABLES -A OUTPUT -p icmp –icmp-type echo-request -j ACCEPT $RLIMIT
# Drop any other ICMP traffic.
$IPTABLES -A INPUT -p icmp -j DROPLOG
$IPTABLES -A OUTPUT -p icmp -j DROPLOG
$IPTABLES -A FORWARD -p icmp -j DROPLOG
# Selectively allow certain special types of traffic.
#———
# Allow loopback interface to do anything.
$IPTABLES -A INPUT -i lo -j ACCEPT
$IPTABLES -A OUTPUT -o lo -j ACCEPT
# Allow incoming connections related to existing allowed connections.
$IPTABLES -A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT
# Allow outgoing connections EXCEPT invalid
$IPTABLES -A OUTPUT -m state –state NEW,ESTABLISHED,RELATED -j ACCEPT
# Miscellaneous.
#———
# We don’t care about Milkosoft, Drop SMB/CIFS/etc..
$IPTABLES -A INPUT -p tcp -m multiport –dports 135,137,138,139,445,1433,1434 -j DROP
$IPTABLES -A INPUT -p udp -m multiport –dports 135,137,138,139,445,1433,1434 -j DROP
# Explicitly drop invalid incoming traffic
$IPTABLES -A INPUT -m state –state INVALID -j DROP
# Drop invalid outgoing traffic, too.
$IPTABLES -A OUTPUT -m state –state INVALID -j DROP
# If we would use NAT, INVALID packets would pass – BLOCK them anyways
$IPTABLES -A FORWARD -m state –state INVALID -j DROP
# PORT Scanners (stealth also)
$IPTABLES -A INPUT -m state –state NEW -p tcp –tcp-flags ALL ALL -j DROP
$IPTABLES -A INPUT -m state –state NEW -p tcp –tcp-flags ALL NONE -j DROP
# TODO: Some more anti-spoofing rules? For example:
# $IPTABLES -A INPUT -p tcp –tcp-flags ALL FIN,URG,PSH -j DROP
# $IPTABLES -A INPUT -p tcp –tcp-flags SYN,RST SYN,RST -j DROP
# $IPTABLES -A INPUT -p tcp –tcp-flags SYN,FIN SYN,FIN -j DROP
$IPTABLES -N SYN_FLOOD
$IPTABLES -A INPUT -p tcp –syn -j SYN_FLOOD
$IPTABLES -A SYN_FLOOD -m limit –limit 2/s –limit-burst 6 -j RETURN
$IPTABLES -A SYN_FLOOD -j DROP
# TODO: Block known-bad IPs
# $IPTABLES -A INPUT -s INSERT-BAD-IP-HERE -j DROPLOG
# Drop any traffic from IANA-reserved IPs.
#———
$IPTABLES -A INPUT -s 0.0.0.0/7 -j DROP
$IPTABLES -A INPUT -s 2.0.0.0/8 -j DROP
$IPTABLES -A INPUT -s 5.0.0.0/8 -j DROP
$IPTABLES -A INPUT -s 7.0.0.0/8 -j DROP
$IPTABLES -A INPUT -s 10.0.0.0/8 -j DROP
$IPTABLES -A INPUT -s 23.0.0.0/8 -j DROP
$IPTABLES -A INPUT -s 27.0.0.0/8 -j DROP
$IPTABLES -A INPUT -s 31.0.0.0/8 -j DROP
$IPTABLES -A INPUT -s 36.0.0.0/7 -j DROP
$IPTABLES -A INPUT -s 39.0.0.0/8 -j DROP
$IPTABLES -A INPUT -s 42.0.0.0/8 -j DROP
$IPTABLES -A INPUT -s 49.0.0.0/8 -j DROP
$IPTABLES -A INPUT -s 50.0.0.0/8 -j DROP
$IPTABLES -A INPUT -s 77.0.0.0/8 -j DROP
$IPTABLES -A INPUT -s 78.0.0.0/7 -j DROP
$IPTABLES -A INPUT -s 92.0.0.0/6 -j DROP
$IPTABLES -A INPUT -s 96.0.0.0/4 -j DROP
$IPTABLES -A INPUT -s 112.0.0.0/5 -j DROP
$IPTABLES -A INPUT -s 120.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 173.0.0.0/8 -j DROP
$IPTABLES -A INPUT -s 174.0.0.0/7 -j DROP
$IPTABLES -A INPUT -s 176.0.0.0/5 -j DROP
$IPTABLES -A INPUT -s 184.0.0.0/6 -j DROP
$IPTABLES -A INPUT -s 192.0.2.0/24 -j DROP
$IPTABLES -A INPUT -s 197.0.0.0/8 -j DROP
$IPTABLES -A INPUT -s 198.18.0.0/15 -j DROP
$IPTABLES -A INPUT -s 223.0.0.0/8 -j DROP
$IPTABLES -A INPUT -s 224.0.0.0/3 -j DROP
# Selectively allow certain outbound connections, block the rest.
#———
# Allow outgoing DNS requests. Few things will work without this.
$IPTABLES -A OUTPUT -m state –state NEW -p udp –dport 53 -j ACCEPT
$IPTABLES -A OUTPUT -m state –state NEW -p tcp –dport 53 -j ACCEPT
# Allow outgoing HTTP requests. Unencrypted, use with care.
$IPTABLES -A OUTPUT -m state –state NEW -p tcp –dport 80 -j ACCEPT
# Allow outgoing HTTPS requests.
$IPTABLES -A OUTPUT -m state –state NEW -p tcp –dport 443 -j ACCEPT
# Allow outgoing SMTPS requests. Do NOT allow unencrypted SMTP!
# $IPTABLES -A OUTPUT -m state –state NEW -p tcp –dport 465 -j ACCEPT
# Allow outgoing “submission” (RFC 2476) requests.
$IPTABLES -A OUTPUT -m state –state NEW -p tcp –dport 587 -j ACCEPT
# Allow outgoing POP3S requests.
$IPTABLES -A OUTPUT -m state –state NEW -p tcp –dport 995 -j ACCEPT
# Allow outgoing SSH requests.
$IPTABLES -A OUTPUT -m state –state NEW -p tcp –dport 22 -j ACCEPT
# Allow outgoing FTP requests. Unencrypted, use with care.
$IPTABLES -A OUTPUT -m state –state NEW -p tcp –dport 21 -j ACCEPT
# Allow outgoing NNTP requests. Unencrypted, use with care.
# $IPTABLES -A OUTPUT -m state –state NEW -p tcp –dport 119 -j ACCEPT
# Allow outgoing NTP requests. Unencrypted, use with care.
# $IPTABLES -A OUTPUT -m state –state NEW -p udp –dport 123 -j ACCEPT
# Allow outgoing IRC requests. Unencrypted, use with care.
# Note: This usually needs the ip_conntrack_irc kernel module.
# $IPTABLES -A OUTPUT -m state –state NEW -p tcp –dport 6667 -j ACCEPT
# Allow outgoing requests to various proxies. Unencrypted, use with care.
# $IPTABLES -A OUTPUT -m state –state NEW -p tcp –dport 8080 -j ACCEPT
# $IPTABLES -A OUTPUT -m state –state NEW -p tcp –dport 8090 -j ACCEPT
# Allow outgoing DHCP requests. Unencrypted, use with care.
# TODO: This is completely untested, I have no idea whether it works!
# TODO: I think this can be tightened a bit more.
$IPTABLES -A OUTPUT -m state –state NEW -p udp –sport 67:68 –dport 67:68 -j ACCEPT
# Allow outgoing CVS requests. Unencrypted, use with care.
# $IPTABLES -A OUTPUT -m state –state NEW -p tcp –dport 2401 -j ACCEPT
# Allow outgoing MySQL requests. Unencrypted, use with care.
# $IPTABLES -A OUTPUT -m state –state NEW -p tcp –dport 3306 -j ACCEPT
# Allow outgoing SVN requests. Unencrypted, use with care.
# $IPTABLES -A OUTPUT -m state –state NEW -p tcp –dport 3690 -j ACCEPT
# Allow outgoing PLESK requests. Unencrypted, use with care.
# $IPTABLES -A OUTPUT -m state –state NEW -p tcp –dport 8443 -j ACCEPT
# Allow outgoing Tor requests.
# Note: Do _not_ use unencrypted protocols over Tor (sniffing is possible)!
# $IPTABLES -A OUTPUT -m state –state NEW -p tcp –dport 9001 -j ACCEPT
# $IPTABLES -A OUTPUT -m state –state NEW -p tcp –dport 9002 -j ACCEPT
# $IPTABLES -A OUTPUT -m state –state NEW -p tcp –dport 9030 -j ACCEPT
# $IPTABLES -A OUTPUT -m state –state NEW -p tcp –dport 9031 -j ACCEPT
# $IPTABLES -A OUTPUT -m state –state NEW -p tcp –dport 9090 -j ACCEPT
# $IPTABLES -A OUTPUT -m state –state NEW -p tcp –dport 9091 -j ACCEPT
# Allow outgoing OpenVPN requests.
$IPTABLES -A OUTPUT -m state –state NEW -p udp –dport 1194 -j ACCEPT
# TODO: ICQ, MSN, GTalk, Skype, Yahoo, etc…
# Selectively allow certain inbound connections, block the rest.
#———
# Allow incoming DNS requests.
$IPTABLES -A INPUT -m state –state NEW -p udp –dport 53 -j ACCEPT
$IPTABLES -A INPUT -m state –state NEW -p tcp –dport 53 -j ACCEPT
# Allow incoming HTTP requests.
$IPTABLES -A INPUT -m state –state NEW -p tcp –dport 80 -j ACCEPT
# Allow incoming HTTPS requests.
$IPTABLES -A INPUT -m state –state NEW -p tcp –dport 443 -j ACCEPT
# Allow incoming POP3 requests.
$IPTABLES -A INPUT -m state –state NEW -p tcp –dport 110 -j ACCEPT
# Allow incoming IMAP4 requests.
$IPTABLES -A INPUT -m state –state NEW -p tcp –dport 143 -j ACCEPT
# Allow incoming POP3S requests.
$IPTABLES -A INPUT -m state –state NEW -p tcp –dport 995 -j ACCEPT
# Allow incoming SMTP requests.
$IPTABLES -A INPUT -m state –state NEW -p tcp –dport 25 -j ACCEPT
# Allow incoming SSH requests.
$IPTABLES -A INPUT -m state –state NEW -p tcp –dport 22 -j ACCEPT
# Allow incoming FTP requests.
$IPTABLES -A INPUT -m state –state NEW -p tcp –dport 21 -j ACCEPT
# Allow incoming NNTP requests.
# $IPTABLES -A INPUT -m state –state NEW -p tcp –dport 119 -j ACCEPT
# Allow incoming MySQL requests.
# $IPTABLES -A INPUT -m state –state NEW -p tcp –dport 3306 -j ACCEPT
# Allow incoming PLESK requests.
# $IPTABLES -A INPUT -m state –state NEW -p tcp –dport 8843 -j ACCEPT
# Allow incoming BitTorrent requests.
# TODO: Are these already handled by ACCEPTing established/related traffic?
# $IPTABLES -A INPUT -m state –state NEW -p tcp –dport 6881 -j ACCEPT
# $IPTABLES -A INPUT -m state –state NEW -p udp –dport 6881 -j ACCEPT
# Allow incoming nc requests.
# $IPTABLES -A INPUT -m state –state NEW -p tcp –dport 2030 -j ACCEPT
# $IPTABLES -A INPUT -m state –state NEW -p udp –dport 2030 -j ACCEPT
# Explicitly log and reject everything else.
#———
# Use REJECT instead of REJECTLOG if you don’t need/want logging.
$IPTABLES -A INPUT -j REJECTLOG
$IPTABLES -A OUTPUT -j REJECTLOG
$IPTABLES -A FORWARD -j REJECTLOG
#———
# Testing the firewall.
#———
# You should check/test that the firewall really works, using
# iptables -vnL, nmap, ping, telnet, …
# Exit gracefully.
#———
exit 0