使用iptable实现动态防火墙
时间:2007-05-29 05:44:51 来源:站长资讯收集整理 作者:佚名
# 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 that's causing problems
#attacker's 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行被执行时特定的规则将被适当的插入或删除。
现在我们看看这些规则本身的功能,它们能和任何类型的防火墙一起发挥作用,甚至在没有部署防火墙的系统上。需要的条件仅仅是支持iptables的Linux2.4版本的内核。我们阻塞来自恶意IP的攻击数据报(第一条iptables语句),阻塞发向恶意攻击IP的数据报(第二条iptables语句),并且对该IP关闭任意方向的数据转发(最后两条iptables工具)。一旦这些规则发挥作用系统将丢弃满足这些条件的任何数据报。


















文章评论
共有 位CH网友发表了评论 查看完整内容