关于limit hashlimit资料整理

时间:2022-10-28 07:51:25

这几天正在捣鼓防火墙,用到了hashlimit模块。Google了一圈发现相关的文档无论英文还

是中文都很少,

所以我就把自己的折腾的心得记录下来吧。

hashlimit是iptables的一个匹配模块,用它结合iptables的其它命令可以实现限速的功能

。(注意,单独hashlimit模块

是无法限速的)。

不过首先必须明确,hashlimit本身只是一个“匹配”模块。我们知道,iptables的基本原

理是“匹配--处理”,hashlimit在

这个工作过程中只能起到匹配的作用,它本身是无法对网络数据包进行任何处理的。我看到

网上有些hashlimit的例子里面说只

用一条包含hashlimit匹配规则的iptables语句就可以实现限速,那是错误的。

实际上,利用hashlimit来限速需要包括两个步骤。

1.对符合hashlimit匹配规则包放行

2.丢弃/拒绝未放行的包

下面是一个简单的例子:

iptables -A INPUT -p tcp --dport 22 -m hashlimit --hashlimit-name ssh

--hashlimit 5/sec --hashlimit-burst 10 --hashlimit-mode srcip

--hashlimit-htable-expire 90000 -j ACCEPT

iptables -A INPUT -p tcp --dport 22 -j DROP

然后,我们来着重讲讲hashlimit模块具体是如何工作的。hashlimit的匹配是基于令牌桶

(Token bucket)模型的。令牌桶是一种网络通讯中常见的缓冲区工作原理,它有两个重要

的参数,令牌桶容量n和令牌产生速率s。我们可以把令牌当成是门票,而令牌桶则是负责制

作和发放门票的管理员,它手里最多有n张令牌。一开始,管理员开始手里有n张令牌。每当

一个数据包到达后,管理员就看看手里是否还有可用的令牌。如果有,就把令牌发给这个数

据包,hashlimit就告诉iptables,这个数据包被匹配了。而当管理员把手上所有的令牌都

发完了,再来的数据包就拿不到令牌了。这时,hashlimit模块就告诉iptables,这个数据

包不能被匹配。

除了发放令牌之外,只要令牌桶中的令牌数量少于n,它就会以速率s来产生新的令牌,直到

令牌数量到达n为止。

通过令牌桶机制,即可以有效的控制单位时间内通过(匹配)的数据包数量,又可以容许短

时间内突发的大量数据包的通过(只要数据包数量不超过令牌桶n)。

hashlimit模块提供了两个参数--hashlimit和--hashlimit-burst,分别对应于令牌产生速

率和令牌桶容量。

除了令牌桶模型外,hashlimit匹配的另外一个重要概念是匹配项。在hashlimit中,每个匹

配项拥有一个单独的令牌桶,执行独立的匹配计算。通过hashlimit的--hashlimit-mode参

数,你可以指定四种匹配项及其组合,即:srcip(每个源地址IP为一个匹配项),dstip(

每个目的地址IP为一个匹配项),srcport(每个源端口为一个匹配项),dstport(每个目

的端口为一个匹配项)

除了前面介绍的三个参数外,hashlimit还有一个必须要用的参数,即--hashlimit-name。

hashlimit会在/proc/net/ipt_hashlimit目录中,为每个调用了hashlimit模块的iptables

命令建立一个文件,其中保存着各匹配项的信息。--hashlimit-name参数即用来指定该文件

的文件名。

好了,以上我们已经介绍了hashlimit的工作原理和相应的参数,下面我们来看几个例子。

首先是前面的那个例子:

iptables -A INPUT -p tcp --dport 22 -m hashlimit --hashlimit-name ssh

--hashlimit 5/sec --hashlimit-burst 10 --hashlimit-mode -j ACCEPT

iptables -A INPUT -p tcp --dport 22 -j DROP

在了解了hashlimit各参数的含义之后,我们现在就可以知道这两条iptables命令的作用。

第一条的作用是,为所有访问本机22端口的不同IP建立一个匹配项,匹配项对应的令牌桶容

量为10,令牌产生速率为5个每秒。放行通过匹配的数据包。

第二条的作用是,丢弃所有其它访问本机22端口的数据包。

通过这两条命令,我们就实现了限制其它机器对本机22端口(ssh服务)频繁访问的功能,

再来我们看一个复杂点的限速。假设我们现在在一台NAT网关上,想限制内部网某个网段

192.168.1.2/24对外的访问频率。(这个的主要作用是限制内部中毒主机对外的flood攻击

那我们可以这么做:

iptables -N DEFLOOD

iptables -A FORWARD -s 192.168.1.2/24 -m state --state NEW -j DEFLOOD

iptables -A DEFLOOD -m hashlimit --hashlimit-name deflood --hashlimit 10/sec

--hashlimit-burst 10 --hashlimit-mode srcip -j ACCEPT

iptables -P DEFLOOD -j DROP

第一条命令建立了一个自定义的处理链

第二条命令,所有来自192.168.1.2/24网段,并且打算新建网络连接的数据包,都进入

DEFLOOD链处理

第三条命令,在DEFLOOD链中,为每个IP建立一个匹配项,对应令牌桶容量为10,产生速率为

10个每秒。放行通过匹配的数据包。

第四条命令,在DEFLOOD链中丢弃所有其它的数据包

以上我们介绍了hashlimit模块的原理和使用。希望能对大家有所帮助:)

用iptables的limit或hashlimit模块,目标是ACCEPT。当你设置300/s时,它大约每3ms发出一个令牌,获得令牌的包可以发出去,没有获得令牌的包只能等待下一个令牌到来,这样不会造成一些包丢失,更不会造成所谓“断线”的。

limit匹配:限制匹配数据包的频率或速率,看清楚了,它是用来限制匹配的数据包的频率和速率的. 这里“limit”这个词经常给别人“限制”的误解,其实准确说,应该是“按一定速率去匹配”

至于“限制”还是“放行”是后面 -j 动作来实现的

limit 仅仅是个 match 模块,他的功能是匹配,匹配方式是按一定速率

以下2条是对icmp的burst限制

iptables -A INPUT -p icmp -m limit --limit 1/sec --limit-burst 10 -j ACCEPT

iptables -A INPUT -p icmp -j DROP

第一条ipables的意思是限制ping包每一秒钟一个,10个后重新开始.

同时可以限制IP碎片,每秒钟只允许100个碎片,用来防止DoS攻击.

iptables -A INPUT -f -m limit --limit 100/sec --limit-burst 100 -j ACCEPT

iptables limit 参数备忘

? 限制特定封包传入速度

? 限制特定端口口连入频率

? iptables Log 记录参数备忘

? 自定 Chain 使用备忘

? 防治 SYN-Flood 碎片攻击

限制 ping (echo-request) 传入的速度

限制前, 可正常每 0.2 秒 ping 一次

ping your.linux.ip -i 0.2

限制每秒只接受一个 icmp echo-request 封包

iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/s --limit-burst 1 -j ACCEPT

iptables -A INPUT -p icmp --icmp-type echo-request -j DROP

--limit 1/s 表示每秒一次; 1/m 则为每分钟一次

--limit-burst 表示允许触发 limit 限制的最大次数 (预设 5)

再以每 0.2 秒 ping 一次, 得到的响应是每秒一次

ping your.linux.ip -i 0.2

限制 ssh 连入频率

建立自订 Chain, 限制 tcp 联机每分钟一次, 超过者触发 Log 记录 (记录在 /var/log/messages)

iptables -N ratelimit

iptables -A ratelimit -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT

iptables -A ratelimit -p tcp --syn -m limit --limit 1/m --limit-burst 1 -j ACCEPT

iptables -A ratelimit -p tcp -j LOG --log-level "NOTICE" --log-prefix "[RATELIMIT]"

iptables -A ratelimit -p tcp -j DROP

引用自订 Chain, 限制 ssh (tcp port 22) 连入频率

iptables -A INPUT -p tcp --dport 22 -s 192.168.0.0/16 -j ACCEPT (特定 IP 来源不受限制)

iptables -A INPUT -p tcp --dport 22 -j ratelimit

参考数据: Mike's Blog - How to limit attack attempts in Linux

sshd_config 设定备忘:

? LoginGraceTime 30 密码输入时限为 30 秒

? MaxAuthTries 2 最多只能输入 3 次密码

同理可证

iptables -N pinglimit

iptables -A pinglimit -m limit --limit 1/s --limit-burst 1 -j ACCEPT

iptables -A pinglimit -j DROP

iptables -A INPUT -p icmp --icmp-type echo-request -j pinglimit

亦可达到每秒只接受一个 echo-request 封包

补充: 清除自订 Chain

iptables -L -n --line-number

iptables -D INPUT n

iptables -F ratelimit

iptables -X ratelimit

防治 SYN-Flood 碎片攻击

iptables -N syn-flood

iptables -A syn-flood -m limit --limit 50/s --limit-burst 10 -j RETURN

iptables -A syn-flood -j DROP

iptables -I INPUT -j syn-flood

模拟攻击

wget http://www.xfocus.net/tools/200102/naptha-1.1.tgz

wget ftp://rpmfind.net/linux/freshrpms/redhat/7.0/libnet/libnet-1.0.1b-1.src.rpm

tar -zxf naptha-1.1.tgz

rpmbuild --recompile libnet-1.0.1b-1.src.rpm

cp -r /var/tmp/libnet-buildroot/usr/* /usr/local/

cd naptha-1.1

make

./synsend your.linux.host.ip 80 local.host.eth0.ip 0.1

若成功抵挡, 不久后会出现 Can't send packet!: Operation not permitted 的讯息

iprange a.b.c.d-a.b.c.d 表示这一段地址还是分别表示每一个包含的地址?

例 iptables -A -m iprange --src-range 172.16.1.10-172.16.16.1 -m limit --limit 300/second -j ACCEPT

表示172.16.1.10-172.16.16.1这段地址每秒一共匹配300个数据包

还是表示172.16.1.10-172.16.16.1地址中的每一个ip 分别匹配300个数据包?-------iprange a.b.c.d-a.b.c.d 表示这一段地址还是分别表示每一个包含的地址?

例 iptables -A -m iprange --src-range 172.16.1.10-172.16.16.1 -m limit --limit 300/second -j ACCEPT

表示172.16.1.10-172.16.16.1这段地址每秒一共匹配300个数据包

还是表示172.16.1.10-172.16.16.1地址中的每一个ip 分别匹配300个数据包?

limit

This module must be explicitly specified with `-m limit' or `--match limit'. It is used to restrict the rate of matches, such as for suppressing log messages. It will only match a given number of times per second (by default 3 matches per hour, with a burst of 5). It takes two optional arguments:

--limit

followed by a number; specifies the maximum average number of matches to allow per second. The number can specify units explicitly, using `/second', `/minute', `/hour' or `/day', or parts of them (so `5/second' is the same as `5/s').

--limit-burst

followed by a number, indicating the maximum burst before the above limit kicks in.

This match can often be used with the LOG target to do rate-limited logging. To understand how it works, let's look at the following rule, which logs packets with the default limit parameters:

# iptables -A FORWARD -m limit -j LOG

The first time this rule is reached, the packet will be logged; in fact, since the default burst is 5, the first five packets will be logged. After this, it will be twenty minutes before a packet will be logged from this rule, regardless of how many packets reach it. Also, every twenty minutes which passes without matching a packet, one of the burst will be regained; if no packets hit the rule for 100 minutes, the burst will be fully recharged; back where we started.

iptables -t filter -A INPUT -p icmp --icmp-type echo-request -m limit --limit 6/minute --limit-burst 6 -j LOG --log-prefix="filter INPUT:"

列:

#!/bin/bash

iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

iptables -N syn-flood

iptables -A INPUT -i eth0 -p tcp -m state --state NEW -j syn-flood

iptables -A syn-flood -m limit --limit 1/s --limit-burst 4 -j RETURN

iptables -A INPUT -i eth0 -p tcp ! --syn -m state --state NEW -j DROP

iptables -A INPUT -i eth0 -p tcp -d 0/0 --dport 80 -j ACCEPT

iptables -A INPUT -i eth0 -p tcp --dport 22 -j ACCEPT

iptables -A INPUT -f -m limit --limit 100/s --limit-burst 100 -j ACCEPT

iptables -A INPUT -p icmp -m limit --limit 1/s --limit-burst 3 -j ACCEPT

iptables -A INPUT -i eth0 -p tcp --dport 21 -j ACCEPT

iptables -A INPUT -i eth0 -p tcp --dport 20 -j ACCEPT

iptables -A INPUT -i eth0 -j DROP

流量控制

tc qdisc del dev eth0 root 2>/dev/null

##定义上传总带宽(用tc语法,这里用的是htb过滤器)

##define root and default rule

tc qdisc add dev eth0 root handle 10: htb default 70

##define uplink max rate

tc class add dev eth0 parent 10: classid 10:1 htb rate 64kbps ceil 64kbps

##对不同的业务进行分类,定义不同的数据流量

##define second leaf

#tc class add dev eth0 parent 10:1 classid 10:10 htb rate 2kbps ceil 4kbps prio 2

#tc class add dev eth0 parent 10:1 classid 10:20 htb rate 2kbps ceil 4kbps prio 2

#tc class add dev eth0 parent 10:1 classid 10:30 htb rate 32kbps ceil 40kbps prio 3

tc class add dev eth0 parent 10:1 classid 10:40 htb rate 3kbps ceil 13kbps prio 0

tc class add dev eth0 parent 10:1 classid 10:50 htb rate 1kbps ceil 11kbps prio 1

tc class add dev eth0 parent 10:1 classid 10:60 htb rate 1kbps ceil 11kbps prio 1

tc class add dev eth0 parent 10:1 classid 10:70 htb rate 2kbps ceil 5kbps prio 1

##定义不同数据传输业务的优先级别和优化数据传输方法

##define rule for second leaf

#tc qdisc add dev eth0 parent 10:10 handle 101: pfifo

#tc qdisc add dev eth0 parent 10:20 handle 102: pfifo

#tc qdisc add dev eth0 parent 10:30 handle 103: pfifo

#tc qdisc add dev eth0 parent 10:40 handle 104: pfifo

#tc qdisc add dev eth0 parent 10:50 handle 105: pfifo

#tc qdisc add dev eth0 parent 10:60 handle 106: pfifo

#tc qdisc add dev eth0 parent 10:70 handle 107: pfifo

##tc qdisc add dev eth0 parent 10:10 handle 101: sfq perturb 10

##tc qdisc add dev eth0 parent 10:20 handle 102: sfq perturb 10

##tc qdisc add dev eth0 parent 10:30 handle 103: sfq perturb 10

tc qdisc add dev eth0 parent 10:40 handle 104: sfq perturb 5

tc qdisc add dev eth0 parent 10:50 handle 105: sfq perturb 10

tc qdisc add dev eth0 parent 10:60 handle 106: sfq perturb 10

tc qdisc add dev eth0 parent 10:70 handle 107: sfq perturb 10

##为netfilter链中的mangle链打标记做好准备(做句柄标示)

##define fw for ipfilter

#tc filter add dev eth0 parent 10: protocol ip prio 100 handle 10 fw classid 10:10

#tc filter add dev eth0 parent 10: protocol ip prio 100 handle 20 fw classid 10:20

#tc filter add dev eth0 parent 10: protocol ip prio 100 handle 30 fw classid 10:30

tc filter add dev eth0 parent 10: protocol ip prio 100 handle 40 fw classid 10:40

tc filter add dev eth0 parent 10: protocol ip prio 100 handle 50 fw classid 10:50

tc filter add dev eth0 parent 10: protocol ip prio 100 handle 60 fw classid 10:60

tc filter add dev eth0 parent 10: protocol ip prio 100 handle 70 fw classid 10:70

###################################################################################

##下载端口配置(方法同上传配置,只是在速率定义上有调整)

echo "Enabling downlink limit"

#downlink limit

##clear dev eth1 rule

tc qdisc del dev eth1 root 2>/dev/null

##define root and default rule

tc qdisc add dev eth1 root handle 10: htb default 70

##define downlink max rate

tc class add dev eth1 parent 10: classid 10:1 htb rate 128kbps ceil 128kbps

##define second leaf

#tc class add dev eth1 parent 10:1 classid 10:10 htb rate 2kbps ceil 32kbps prio 2

#tc class add dev eth1 parent 10:1 classid 10:20 htb rate 2kbps ceil 32kbps prio 2

#tc class add dev eth1 parent 10:1 classid 10:30 htb rate 32kbps ceil 212kbps prio 3

tc class add dev eth1 parent 10:1 classid 10:40 htb rate 5kbps ceil 20kbps prio 0

tc class add dev eth1 parent 10:1 classid 10:50 htb rate 2kbps ceil 17kbps prio 1

tc class add dev eth1 parent 10:1 classid 10:60 htb rate 2kbps ceil 17kbps prio 1

tc class add dev eth1 parent 10:1 classid 10:70 htb rate 3kbps ceil 5kbps prio 1

##define rule for second leaf

#tc qdisc add dev eth1 parent 10:10 handle 101: pfifo

#tc qdisc add dev eth1 parent 10:20 handle 102: pfifo

#tc qdisc add dev eth1 parent 10:30 handle 103: pfifo

#tc qdisc add dev eth1 parent 10:40 handle 104: pfifo

#tc qdisc add dev eth1 parent 10:50 handle 105: pfifo

#tc qdisc add dev eth1 parent 10:60 handle 106: pfifo

#tc qdisc add dev eth1 parent 10:70 handle 107: pfifo

##tc qdisc add dev eth1 parent 10:10 handle 101: sfq perturb 10

##tc qdisc add dev eth1 parent 10:20 handle 102: sfq perturb 10

##tc qdisc add dev eth1 parent 10:30 handle 103: sfq perturb 10

tc qdisc add dev eth1 parent 10:40 handle 104: sfq perturb 5

tc qdisc add dev eth1 parent 10:50 handle 105: sfq perturb 10

tc qdisc add dev eth1 parent 10:60 handle 106: sfq perturb 10

tc qdisc add dev eth1 parent 10:70 handle 107: sfq perturb 10

##define fw for ipfilter

#tc filter add dev eth1 parent 10: protocol ip prio 100 handle 10 fw classid 10:10

#tc filter add dev eth1 parent 10: protocol ip prio 100 handle 20 fw classid 10:20

#tc filter add dev eth1 parent 10: protocol ip prio 100 handle 30 fw classid 10:30

tc filter add dev eth1 parent 10: protocol ip prio 100 handle 40 fw classid 10:40

tc filter add dev eth1 parent 10: protocol ip prio 100 handle 50 fw classid 10:50

tc filter add dev eth1 parent 10: protocol ip prio 100 handle 60 fw classid 10:60

tc filter add dev eth1 parent 10: protocol ip prio 100 handle 70 fw classid 10:70

echo "Enabling mangle "

# uploads

#iptables -t mangle -A PREROUTING -s 192.168.0.6 -m layer7 --l7proto dns -j MARK --set-mark 10

#iptables -t mangle -A PREROUTING -s 192.168.0.6 -m layer7 --l7proto smtp -j MARK --set-mark 20

#iptables -t mangle -A PREROUTING -s 192.168.0.6 -m layer7 --l7proto http -j MARK --set-mark 30

##为ip地址打标记以便进行流量控制--上传

#iptables -t mangle -A PREROUTING -s 192.168.0.52 -j MARK --set-mark 40

#iptables -t mangle -A PREROUTING -s 192.168.0.0/24 -j MARK --set-mark 70

#iptables -t mangle -A PREROUTING -s 192.168.0.3 -j MARK --set-mark 60

# downloads

#iptables -t mangle -A POSTROUTING -d 192.168.0.6 -m layer7 --l7proto dns -j MARK --set-mark 10

#iptables -t mangle -A POSTROUTING -d 192.168.0.6 -m layer7 --l7proto smtp -j MARK --set-mark 20

#iptables -t mangle -A POSTROUTING -d 192.168.0.6 -m layer7 --l7proto http -j MARK --set-mark 30

##为ip地址打标记以便进行流量控制--下载

#iptables -t mangle -A POSTROUTING -d 192.168.0.52 -j MARK --set-mark 40

#iptables -t mangle -A POSTROUTING -d 192.168.0.0/24 -j MARK --set-mark 70

#iptables -t mangle -A POSTROUTING -d 192.168.0.3 -j MARK --set-mark 60

DDOS Protection Script

#!/bin/sh

# Firewall script made by Magarus for verlihubforums.com and adminzone.ro

# Copyright @ 2007 - Saftoiu Mihai, All rights reserved.

# The distribution of this script without Saftoiu Mihai's

# approval is a violation of copyright and will be persued to the

# full extent of the law. You may use it ONLY for non-commercial use,

# except without the author's explicit approval.

# Define constants - Leave them alone

IPTABLES=`which iptables`

MODPROBE=`which modprobe`

$MODPROBE ip_conntrack

$MODPROBE ipt_recent

NR_IP=""

IP_LOOP=""

PORT_LOOP=""

# Modify tcp/ip parameters

# Reduce timeout

echo "15" > /proc/sys/net/ipv4/tcp_fin_timeout

# Increase backlog and max conn

echo "3000" > /proc/sys/net/core/netdev_max_backlog

echo "3000" > /proc/sys/net/core/somaxconn

# Reduce timeouts and retransmissions

echo "300" > /proc/sys/net/ipv4/tcp_keepalive_time

echo "15" > /proc/sys/net/ipv4/tcp_keepalive_intvl

echo "1" > /proc/sys/net/ipv4/tcp_keepalive_probes

echo "1" > /proc/sys/net/ipv4/tcp_syncookies

echo "2" > /proc/sys/net/ipv4/tcp_synack_retries

echo "1" > /proc/sys/net/ipv4/tcp_syn_retries

# Increase SYN backlog

echo "28000" > /proc/sys/net/ipv4/tcp_max_syn_backlog

# Decrease timeouts

echo "10" > /proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_syn_recv

echo "40" > /proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_syn_sent

# Check for spoofing / Use 2 instead of 1 bellow if it doesn't fix it

echo "1" > /proc/sys/net/ipv4/conf/all/rp_filter

# See that conntrack doesn't get overflowed

echo "220000" > /proc/sys/net/ipv4/ip_conntrack_max

# Use scaling

echo "1" > /proc/sys/net/ipv4/tcp_window_scaling

# Remove overhead and unnecessary tcp/icmp params.

echo "0" > /proc/sys/net/ipv4/tcp_sack

echo "0" > /proc/sys/net/ipv4/conf/all/accept_source_route

echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts

echo "1" > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses

echo "1" > /proc/sys/net/ipv4/conf/all/log_martians

echo "0" > /proc/sys/net/ipv4/tcp_timestamps

# Increase available memory

echo "16777216" > /proc/sys/net/core/rmem_max

echo "16777216" > /proc/sys/net/core/wmem_max

echo "4096 87380 16777216" > /proc/sys/net/ipv4/tcp_rmem

echo "4096 87380 16777216" > /proc/sys/net/ipv4/tcp_wmem

echo "1" > /proc/sys/net/ipv4/tcp_no_metrics_save

# Increase number of ports available (this is a must for future apache fix)

echo "1024 65000" > /proc/sys/net/ipv4/ip_local_port_range

# Function for protection/hub/ip

protect_hub(){

$IPTABLES -A OUTPUT -s $IP_LOOP -p tcp --sport $PORT_LOOP --tcp-flags ALL PSH,ACK -m string --algo bm --string Pk=version --to 300 -j RST_LOOP_OUT

$IPTABLES -A INPUT -d $IP_LOOP -p tcp --dport $PORT_LOOP --syn -j SYN_CHECK

$IPTABLES -A INPUT -d $IP_LOOP -p tcp --dport $PORT_LOOP --tcp-flags ALL PSH,ACK -m string --algo bm --string MyNick --to 100 -j REJECT --reject-with tcp-reset

$IPTABLES -A INPUT -d $IP_LOOP -p tcp --dport $PORT_LOOP -m state --state RELATED,ESTABLISHED -j ACCEPT

$IPTABLES -A INPUT -d $IP_LOOP -p tcp --dport $PORT_LOOP -j DROP

$IPTABLES -A INPUT -d $IP_LOOP -p udp --dport $PORT_LOOP -j DROP

$IPTABLES -A SYN_CHECK -d $IP_LOOP -p tcp --dport $PORT_LOOP -m hashlimit --hashlimit 2/min --hashlimit-mode srcip,dstip

--hashlimit-name dcclients --hashlimit-burst 1 --hashlimit-htable-expire 30000 --hashlimit-htable-gcinterval 1000 -j ACCEPT

$IPTABLES -A SYN_CHECK -d $IP_LOOP -p tcp --dport $PORT_LOOP -j REJECT --reject-with tcp-reset

$IPTABLES -A RST_LOOP_OUT -d $IP_LOOP -p tcp --sport $PORT_LOOP --tcp-flags ALL PSH,ACK -m conntrack --ctexpire 1:1000 -j REJECT --reject-with tcp-reset

$IPTABLES -A RST_LOOP_OUT -d $IP_LOOP -p tcp --sport $PORT_LOOP --tcp-flags ALL FIN,PSH,ACK -m conntrack --ctexpire 1:1000 -j REJECT --reject-with tcp-reset

}

# Main()

firewall_run(){

clear

echo -e "n Anti DDOS firewall for verlihub software, Copyright @ 2007 Saftoiu Mihai nn"

echo -e " How many ip addresses do you have allocated for your running hubs? c" && read NR_IP

NR_IP=`expr $NR_IP + 1`

ctl="1"

while [ "$ctl" -lt "$NR_IP" ]; do

echo -e "n Input ip no. $ctl = c"

read IP[$ctl]

let "ctl += 1"

done

echo -e "n"

ctl="1"

# Define custom chains

# Check syn chain frequency drops anyway

$IPTABLES -N SYN_CHECK

# Reset output packets so hub doesn't get locked up on output

$IPTABLES -N RST_LOOP_OUT

# Drop all junk data

$IPTABLES -A INPUT -p tcp ! --syn -m state --state NEW -j DROP

$IPTABLES -A INPUT -p tcp -m state --state INVALID,UNTRACKED -j DROP

# Enter loop

while [ "$ctl" -lt "$NR_IP" ]; do

IP_LOOP="${IP[$ctl]}"

echo -e "n How many hub ports are there on this ip ${IP[$ctl]}? c"

read NR_PORTS && NR_PORTS=`expr $NR_PORTS + 1` && ctlx="1"

while [ "$ctlx" -lt "$NR_PORTS" ]; do

echo -e "n Input port $ctlx for ${IP[$ctl]} : c"

read PORT[$ctlx] && PORT_LOOP="${PORT[$ctlx]}"

protect_hub

let "ctlx += 1"

done

let "ctl += 1"

done

}

# Clear the rules and any i might add

firewall_clear(){

clear

echo -e "nn Firewall rules are now being cleared...n"

$IPTABLES -t mangle -F

$IPTABLES -t filter -F

$IPTABLES -t raw -F

$IPTABLES -Z

$IPTABLES -X

$IPTABLES -P INPUT ACCEPT

$IPTABLES -P OUTPUT ACCEPT

$IPTABLES -P FORWARD ACCEPT

$IPTABLES -L

echo -e "n Firewall CLEARED!"

}

# Run-time options

case "$1" in

'start') firewall_run ;;

'stop') firewall_clear ;;

*) echo -e "nUsage: $0 [OPTION]..."

echo -e "nOPTIONS:"

echo -e " start Run the firewall."

echo -e " stop Stop the firewall."

echo -e "n " ;;

esac