LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 770|回复: 0

Linux动态伪装防火墙的自编脚本

[复制链接]
发表于 2005-5-30 11:20:37 | 显示全部楼层 |阅读模式
#!/bin/bash
#Our complete stateful firewall script.  This firewall can be customized for
#a laptop, workstation, router or even a server.
#这是一个完美的防火墙脚本,它不是一个基于用户顶端的工作组、路由或相等的防火墙

#change this to the name of the interface that provides your "uplink"
#(connection to the Internet)指定网络端口(即是你的设备的位置)

UPLINK="eth1"
#if you're a router (and thus should forward IP packets between interfaces), 如果你有一个静态的路由或IP地址,你需在下面的ROUTER选项使用yse,否则如使DHCP动态分码则用No这一值。
#you want ROUTER="yes"; otherwise, ROUTER="no"

ROUTER="yes"

#change this next line to the static IP of your uplink interface for static SNAT, or
#"dynamic" if you have a dynamic IP.  If you don't need any NAT, set NAT to "" to
#disable it.
#以上改变将会在下一次与IP所关联,但必需是一个不受冲突的地址、路由,如果你不确定>将会使用动态分址,如果你不知道那NAT最好将NAT字义为“dynamic”。

NAT="192.168.1.1"

#change this next line so it lists all your network interfaces, including lo在下一次上线时它将监听你所有的静态网络,包括lo中断。

INTERFACES="all"

#change this line so that it lists the assigned numbers or symbolic names (from
#/etc/services) of all the services that you'd like to provide to the general
#public.  If you don't want any services enabled, set it to ""
#同时在下一次上线时则监听包括动态的/etc/services下的所有服务,如果你不想将所有服务启动于防火墙下,以下选项即给出一个空值"".

SERVICES="http ftp smtp ssh rsync"
if [ "$1" = "start" ] #如果变量值为1,那将启动它。
then
        echo "Starting firewall..." #显示一个有效状态
        iptables -P INPUT DROP
        iptables -A INPUT -i ! ${UPLINK} -j ACCEPT
        iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

        #enable public access to certain services 允许公众账户激活服务

        for x in ${SERVICES}
        do
                iptables -A INPUT -p tcp --dport ${x} -m state --state NEW -j ACCEPT
        done

        iptables -A INPUT -p tcp -i ${UPLINK} -j REJECT --reject-with tcp-reset
        iptables -A INPUT -p udp -i ${UPLINK} -j REJECT --reject-with icmp-port-unreachable

        #explicitly disable ECN 中断ECN
        if [ -e /proc/sys/net/ipv4/tcp_ecn ]
        then
                echo 0 > /proc/sys/net/ipv4/tcp_ecn
        fi
        #disable spoofing on all interfaces 欺骗全部静态
        for x in ${INTERFACES}
        do      
                echo 1 > /proc/sys/net/ipv4/conf/${x}/rp_filter
        done
        if [ "$ROUTER" = "yes" ]
        then
                #we're a router of some kind, enable IP forwarding 参照主路由相关类型,实现IP传输
                echo 1 > /proc/sys/net/ipv4/ip_forward
                if [ "$NAT" = "dynamic" ]
                then
                        #dynamic IP address, use masquerading   
                        echo "Enabling masquerading (dynamic ip)..."
                        iptables -t nat -A POSTROUTING -o ${UPLINK} -j MASQUERADE
                elif [ "$NAT" != "" ]
                then
                        #static IP, use SNAT
                        echo "Enabling SNAT (static ip)..."
                        iptables -t nat -A POSTROUTING -o ${UPLINK} -j SNAT --to ${UPIP}192.168.1.100
                fi
        fi

elif [ "$1" = "stop" ]
then
        echo "Stopping firewall..."
        iptables -F INPUT
        iptables -P INPUT ACCEPT
        #turn off NAT/masquerading, if any
        iptables -t nat -F POSTROUTING
fi
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表