#!/bin/bash # 請先輸入您的相關參數,不要輸入錯誤了! EXTIF="eth1" # 這個是可以連上 Public IP 的網路介面 INIF="eth0" # 內部 LAN 的連接介面;若無請填 "" INNET="192.168.0.0/24" # 內部 LAN 的網域,若沒有內部 LAN 請設定為 "" export EXTIF INIF INNET # 第一部份,針對本機的防火牆設定!########################### # 1. 先設定好核心的網路功能: echo "1" > /proc/sys/net/ipv4/tcp_syncookies echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts for i in /proc/sys/net/ipv4/conf/*/rp_filter; do echo "1" > $i done for i in /proc/sys/net/ipv4/conf/*/log_martians; do echo "1" > $i done for i in /proc/sys/net/ipv4/conf/*/accept_source_route; do echo "0" > $i done 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 # 2. 清除規則、設定預設政策及開放 lo 與相關的設定值 PATH=/sbin:/usr/sbin:/bin:/usr/bin; export PATH iptables -F iptables -X iptables -Z iptables -P INPUT DROP iptables -P OUTPUT ACCEPT iptables -P FORWARD ACCEPT iptables -A INPUT -i lo -j ACCEPT iptables -A INPUT -m state --state RELATED -j ACCEPT # 3. 啟動額外的防火牆 script 模組 if [ -f /usr/local/virus/iptables/iptables.deny ]; then sh /usr/local/virus/iptables/iptables.deny fi if [ -f /usr/local/virus/iptables/iptables.allow ]; then sh /usr/local/virus/iptables/iptables.allow fi if [ -f /usr/local/virus/httpd-err/iptables.http ]; then sh /usr/local/virus/httpd-err/iptables.http fi iptables -A INPUT -m state --state ESTABLISHED -j ACCEPT # 4. 允許某些類型的 ICMP 封包進入 AICMP="0 3 3/4 4 11 12 14 16 18" for tyicmp in $AICMP do iptables -A INPUT -i $EXTIF -p icmp --icmp-type $tyicmp -j ACCEPT done # 5. 允許某些服務的進入,請依照您自己的環境開啟 iptables -A INPUT -p TCP -i $EXTIF --dport 21 -j ACCEPT # FTP iptables -A INPUT -p TCP -i $EXTIF --dport 22 -j ACCEPT # SSH # iptables -A INPUT -p TCP -i $EXTIF --dport 25 -j ACCEPT # SMTP # iptables -A INPUT -p UDP -i $EXTIF --sport 53 -j ACCEPT # DNS # iptables -A INPUT -p TCP -i $EXTIF --sport 53 -j ACCEPT # DNS # iptables -A INPUT -p TCP -i $EXTIF --dport 80 -j ACCEPT # WWW # iptables -A INPUT -p TCP -i $EXTIF --dport 110 -j ACCEPT # POP3 # iptables -A INPUT -p TCP -i $EXTIF --dport 443 -j ACCEPT # HTTPS # 第二部份,針對後端主機的防火牆設定!############################## # 1. 先載入一些有用的模組 modules="ip_tables iptable_nat ip_nat_ftp ip_nat_irc ip_conntrack ip_conntrack_ftp ip_conntrack_irc" for mod in $modules do testmod=`lsmod | grep "${mod} "` if [ "$testmod" == "" ]; then modprobe $mod fi done # 2. 清除 NAT table 的規則吧! iptables -F -t nat iptables -X -t nat iptables -Z -t nat iptables -t nat -P PREROUTING ACCEPT iptables -t nat -P POSTROUTING ACCEPT iptables -t nat -P OUTPUT ACCEPT # 3. 開放成為路由器,且為 IP 分享器! if [ "$INIF" != "" ]; then iptables -A INPUT -i $INIF -j ACCEPT echo "1" > /proc/sys/net/ipv4/ip_forward if [ "$INNET" != "" ]; then for innet in $INNET do iptables -t nat -A POSTROUTING -s $innet -o $EXTIF -j MASQUERADE done fi fi