免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1126 | 回复: 0
打印 上一主题 下一主题

一防火牆,很好!!!! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-12-15 14:49 |只看该作者 |倒序浏览
#!/bin/bash
#
# ========================================================
# 程式說明:
# 歡迎使用 iptables.rule 這個 script 來建立您的防火牆!
# 這支 script 還需要您的額外設定方可適合您的主機環境!
# 基本規則定義為『拒絕所有,開放特定』的模式!
#
# 強烈建議:
# 不了解 Linux 防火牆機制 iptables 的朋友使用這支 script
# 可能會不太瞭解每個指令列的意義,果真如此的話,
# 歡迎參考底下幾個網頁:
# http://www.study-area.org/linux/servers/linux_nat.htm
# http://linux.vbird.org/linux_server/0240network-secure-1.php
# http://linux.vbird.org/linux_server/0250simple_firewall.php
#
# 使用說明:
# 請先將這個 scripts 的權限更改為可執行:
#        chmod 755 iptables.rule
# 在將這個程式放置在 /usr/local/virus/iptables 目錄下:
#        mkdir -p /usr/local/virus/iptables
#        mv /完整的路徑/iptables.rule /usr/local/virus/iptables
# 執行測試:
#        /usr/local/virus/iptables/iptables.rule
#        iptables -L -n   (這個動作在檢查防火牆規則)
# 將底下這一行加入 /etc/rc.d/rc.local 當中
#        /usr/local/virus/iptables/iptables.rule
# 取消防火牆:
#        iptables -F
#        iptables -X
#        iptables -t nat -F
#        iptables -t nat -X
#
# ========================================================
# 版權宣告:
# 這支程式為 GPL 授權,任何人皆可使用,
# 然,若使用本 scripts 發生問題時,
# 本人不負任何責任
# VBird <vbird@tsai.adsldns.org>
# ========================================================
# 歷史紀錄:
# 2002/08/20        VBird        首次釋出
# 2003/04/26        VBird        加入砍站軟體的相關執行檔案!
# 2003/08/25        VBird        修改 INPUT 的 Policy 成為 DROP
# ========================================================

# 0.0 Please key in your parameters
# 這個 EXTIF 後面接的為『對外可連上 Internet 的網路介面』,
# 一般來說,如果是撥接的 ADSL ,那麼底下就是 ppp0 ,
# 如果是固定制的 IP ,那麼應該就是 eth0 囉!
# The interface that connect Internet
  EXTIF="ppp0"

# 底下這個 INIF 為對內的網路卡介面,
# 如果你的 Linux 並沒有對內連接的網路卡,那麼底下請改成
# INIF=""
# 另外,如果INIF 不是 "" 的時候,請務必將你的內部網域
# 填入 INNET 中!因為 INNET 為內部網域的設定值!
# the inside interface. if you don't have this one
# and you must let this be black ex> INIF=""
  INIF="eth0"
  INNET="192.168.1.0/24"        # This is for NAT's network

# 1.0 測試你的核心版本與防火牆模組
  kver=`uname -r | cut -c 1-3`
  if [ "$kver" != "2.4" ] && [ "$kver" != "2.5" ]; then
        echo "Your Linux Kernel Version may not be suported by this script!"
        echo "This scripts will not be runing"
        exit
  fi
  ipchains=`lsmod | grep ipchains`
  if [ "$ipchains" != "" ]; then
        echo "unload ipchains in your system"
        rmmod ipchains 2> /dev/null
  fi

# 2.0 載入適當的模組
  PATH=/sbin:/bin:/usr/sbin:/usr/bin
  export PATH EXTIF INIF INNET
  modprobe ip_tables                > /dev/null 2>&1
  modprobe iptable_nat                > /dev/null 2>&1
  modprobe ip_nat_ftp                > /dev/null 2>&1
  modprobe ip_nat_irc                > /dev/null 2>&1
  modprobe ip_conntrack                > /dev/null 2>&1
  modprobe ip_conntrack_ftp        > /dev/null 2>&1
  modprobe ip_conntrack_irc        > /dev/null 2>&1

# 3.0 先清除所有的防火牆規則
  /sbin/iptables -F
  /sbin/iptables -X
  /sbin/iptables -Z
  /sbin/iptables -F -t nat
  /sbin/iptables -X -t nat
  /sbin/iptables -Z -t nat
  /sbin/iptables -P INPUT   DROP
  /sbin/iptables -P OUTPUT  ACCEPT
  /sbin/iptables -P FORWARD ACCEPT
  /sbin/iptables -t nat -P PREROUTING  ACCEPT
  /sbin/iptables -t nat -P POSTROUTING ACCEPT
  /sbin/iptables -t nat -P OUTPUT      ACCEPT

# 4.0 先允許信任網域,這包含 lo 這個內部迴圈介面,
#     以及剛剛指定的內部介面網域!
#     當然,重點是可以啟動你的 Linux 成為 NAT 主機的啦!
  /sbin/iptables -A INPUT -i lo          -j ACCEPT
  if [ "$INIF" != "" ]; then
        /sbin/iptables -A INPUT -i $INIF -j ACCEPT
        echo "1" > /proc/sys/net/ipv4/ip_forward
        /sbin/iptables -t nat -A POSTROUTING -s $INNET -o $EXTIF -j MASQUERADE
  fi

# 5.0 開始載入信任與拒絕的網域設定的檔案,
#     底下兩個檔案可以自行建立喔!
  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

# 6.0 底下這個檔案若存在,則執行!請注意,
#     這個檔案與杜絕砍站的軟體有關喔!
  if [ -f /usr/local/virus/httpd-err/iptables.http ]; then
        sh /usr/local/virus/httpd-err/iptables.http
  fi
  
# 7.0 允許 ICMP 封包與允許已建立的連線通過!
  /sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  AICMP="0 3 3/4 4 11 12 14 16 18"
  for tyicmp in $AICMP
  do
        /sbin/iptables -A INPUT -i $EXTIF -p icmp --icmp-type $tyicmp -j ACCEPT
  done

# 8.0 Allow services特別留意底下的服務,將您主機沒有開放的服務關閉吧!
  /sbin/iptables -A INPUT -p TCP -i $EXTIF --dport  22 -j ACCEPT        # SSH
  /sbin/iptables -A INPUT -p TCP -i $EXTIF --dport  25 -j ACCEPT        # SMTP
  /sbin/iptables -A INPUT -p UDP -i $EXTIF --dport  53 -j ACCEPT        # DNS
  /sbin/iptables -A INPUT -p TCP -i $EXTIF --dport  53 -j ACCEPT        # DNS
  /sbin/iptables -A INPUT -p TCP -i $EXTIF --dport  80 -j ACCEPT        # WWW
  /sbin/iptables -A INPUT -p TCP -i $EXTIF --dport 110 -j ACCEPT        # POP3
  /sbin/iptables -A INPUT -p TCP -i $EXTIF --dport 113 -j ACCEPT        # auth
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP