如果能创建一个特殊的"ipdrop"脚本,其被设计为能方便地插入一个规则来阻塞指定的IP,那么将上面的工作将非常容易。通过该脚本阻塞某个IP将
是非常容易的工作,只需要几秒钟就可以实现。而且通过该脚本还可以防止手工加入规则时容易出现的错误。因此阻塞Bob的攻击将变为确定其攻击源地址。然后
通过如下命令:
# ipdrop 129.24.8.1 on
IP 129.24.8.1 drop on.
ipdrop脚本将立即阻塞129.24.8.1。通过使用该脚本能显著地提高你的防卫能力。下面就是ipdrop脚本的实现:
The ipdrop bash script
#!/bin/bash
source /usr/local/share/dynfw.sh
args 2 $# "${0} IPADDR {on/off}" "Drops packets to/from IPADDR. Good for obnoxious networks/hosts/DoS"
if [ "$2" == "on" ]
then
#rules will be appended or inserted as normal
APPEND="-A"
INSERT="-I"
rec_check ipdrop $1 "$1 already blocked" on
record ipdrop $1
elif [ "$2" == "off" ]
then
#rules will be deleted instead
APPEND="-D"
INSERT="-D"
rec_check ipdrop $1 "$1 not currently blocked" off
unrecord ipdrop $1
else
echo "Error: "off" or "on" expected as second argument"
exit 1
fi
#block outside IP address thats causing problems
#attackers incoming TCP connections will take a minute or so to time out,
#reducing DoS effectiveness.
iptables $INSERT INPUT -s $1 -j DROP
iptables $INSERT OUTPUT -d $1 -j DROP
iptables $INSERT FORWARD -d $1 -j DROP
iptables $INSERT FORWARD -s $1 -j DROP
echo "IP ${1} drop ${2}."
ipdrop:解释
从上面的脚本源代码中最后四行内容可以看到实际的命令是在防火墙表中插入适当的规则。可以看到$INSERT变量的值取决于在命令行参数中是使用"on"还是"off"模式。当iptables行被执行时特定的规则将被适当的插入或删除。
tcplimit提供了一个限制对非关键服务的使用的非常好的方法-这样大量到非关键服务的数据不会破坏服务器。在上面的例子中使用tcplimit来
设置使用rsync的限制,以防止tsync数据占用了Internet连接的所有带宽。其中连接服务限制信息记录在文件/root/.dynfw-
tcplimit中。若想关闭该限制只需要键入如下命令:
# tcplimit 873 5 minute off
Port 873 new connection limit off.
host-tcplimit和tcplimit非常类似,但是它是限制来自一个特定的IP的到服务器上某个特定端口的TCP连接数量。host-
tcplimit在防止某个特定的人滥用你的网络资源时非常有用处。例如你维护有一个CVS服务器,有一天突然发现一个特殊的新开发者出现了,他好像建立
了一个脚本每十分钟更新它的资源。占用了大量的网络资源。然后你就给他发送信件说明他的行为的错误之处。但是你收到他如下的回信:
Hi guys!
Im really excited to be part of your development project. I just set up a
script to update my local copy of the code every ten minutes. Im about to
leave on a two-week cruise, but when I get back, my sources will be totally
up-to-date and Ill be ready to help out! Im heading out the door now...see
you in two weeks!
Sincerely,
Mr. Newbie
对于这种情况,使用host-tcplimit可以非常容易的解决问题:
# host-tcplimit 1.1.1.1 2401 1 day on
现在Newbie先生(IP地址为1.1.1.1)被限制为每天只能进行一次CVS连接从而节省了网络带宽。
user-outblock
要进行安装只需要解压缩包,运行其中的install.sh文件。该脚本将安装一个共享bash脚本为
/usr/local/share/dynfw.sh,并且安装动态防火墙脚本到/usr/local/sbin目录下。若希望安装在其他脚本中,则只需
要在执行install.sh以前执行:
# export PREFIX=/usr
还可以在dynamic firewall scripts section to the Gentoo Linux Web site下载dynfw的最新版本。
* tcpdump是一个非常重要的探测底层的IP报交换的工具,使用它可以验证防火墙工作是否正常。 Linux联盟
收集整理