Chinaunix首页 | 论坛 | 博客
  • 博客访问: 18137908
  • 博文数量: 7460
  • 博客积分: 10434
  • 博客等级: 上将
  • 技术积分: 78178
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-02 22:54
文章分类

全部博文(7460)

文章存档

2011年(1)

2009年(669)

2008年(6790)

分类: BSD

2008-05-05 08:23:28


    1. 緣起:
      • Network Address Translation
        • 本校於擴大內需方案執行之初分配到64個IP(四分之一個Class C),目前已明顯不夠使用。
        • 另外,有網友提到他申請的ADSL只配給一個IP,那麼公司內其他的機器怎麼上網呢?
      • IP-FireWall
        • 近日主機常常受到特定IP的騷擾。
      以下的筆記是我在家裡以HiNet的ADS-L(1 IP)環境操作的。

    • private ip
      • RFC 1918中有分配一般私人的ip (private ip):
        According to RFC 1918, you can use the following IP networks for private nets which will never be connected to the Internet:
        Class A		10.0.0.0    - 10.255.255.255	255.0.0.0
        Class B		172.16.0.0  - 172.31.255.255	255.255.0.0
        Class C		192.168.0.0 - 192.168.255.255	255.255.255.0
      • 我挑選 Clacc C 其中一段 192.168.1.0/255.255.255.0

    • 背景說明:
      • 主機:FreeBSD 4.4-RELEASE。
      • 目前只有安裝一塊網路卡(D-Link 530TX verB):IP:211.75.215.107    NetMask:255.255.255.0    Gateway:211.75.215.254
        這塊俗稱超級機車的網路卡(相對於RedHat),自 FreeBSD 4.2-RELEASE 起就已內建支援。
      • 為了建立NAT功能,同一塊網卡新增一虛擬IP:192.168.1.254    NetMask:255.255.255.0,詳如 /etc/rc.conf 設定。
      • 目的:讓其他的Win98能透過192.168.1.254為通訊閘來存取網路上的資源。
      • 以下的說明文件中之"vr0",是我實際對外連上Internet的網卡代號(當您使用兩塊網卡做NAT時,要特別留意這一點。),你的主機網卡代號可能不同,用 dmesg 或 ifconfig -l 來查一查。
      • 您也可以使用二塊網卡的方式,只要對照於本文件中的網卡代號,分清楚哪一塊是對外的、而哪一塊又是對內的。兩塊網卡千萬不要接在同一個HUB上,除非使用SWITCH-HUB,並切割VLAN。


    1. kernel
      • 修改kernel:
        cd /usr/src/sys/i386/conf
        vi GENERIC
        options   IPFIREWALL
        options   IPFIREWALL_DEFAULT_TO_ACCEPT
        options   IPFIREWALL_VERBOSE
        options   IPFIREWALL_VERBOSE_LIMIT=10
        options   IPDIVERT
        請留意,options後面是用TAB鍵做間隔,不是用空白鍵(spacebar)。

      • 編譯kernel:
        config   GENERIC
        cd   ../../compile/GENERIC
        make   depend   all   install

    • 在 /etc/rc.conf 檔案中加入下列設定:
      # 第一片網卡固有的設定:
      ifconfig_vr0="inet   211.75.215.107   media   100baseTX   netmask   255.255.255.0"

      # 只用一片網卡時,將第一片網卡虛擬出另一個IP(如果使用兩片網卡,就不要設這一行,或者註解起來也可)。
      ifconfig_vr0_alias0="inet   192.168.1.254   media   100baseTX   netmask   255.255.255.0"

      # 如果你有第二片網卡時,將此網卡設定如下(當然啦,這一行的註解就應該取消,第二塊網卡才會有作用)。
      # ifconfig_vr1="inet   192.168.1.254   media   100baseTX   netmask   255.255.255.0"

      # 宣告本主機可做為gateway(通訊閘)
      gateway_enable="YES"

      # 宣告防火牆(IP-FIREWALL)
      firewall_enable="YES"
      firewall_type="simple"
      firewall_quiet="YES"
      tcp_extensions="YES"

      # 定義 NATD 的網路卡介面,應定義在設定 public IP 的網卡代號上。
      natd_interface="vr0"
      natd_enable="YES"
      # 將真實 IP 上 port:80 轉向至防火牆內。這樣的轉向為必要時才設定,NAT實際運作只要上述兩行即可。
      natd_flags="-redirect_port tcp 192.168.1.220:80 211.75.215.107:80"

      vr0:真實IP的網卡。

      開機後,若要更改NAT的設定:
      kill -KILL `cat /var/run/natd.pid`
      natd -redirect_port tcp 192.168.1.220:80 211.75.215.107:80 -interface vr0

    • /etc/services
      確認 /etc/services 中是否有這一行,如果沒有請加上去:
      natd   8668/divert

    • /etc/rc.firewall
      修改 /etc/rc.firewall 檔:(先將原檔案備份)
      #!/bin/sh
      # ================
      # 清除所有防火牆過濾的規則(歸零),ipfw詳細語法請:man   ipfw
      /sbin/ipfw   -f   flush
      
      # ================
      # 先定義 deny,由此處開始定義防火牆過濾的規則
      # 我是比較狠一點,要擋就全部都擋住,滴水不漏。
      # ================
      
      # 這裡的 all ,是指 /etc/services 檔案中所記載的各項服務名稱。
      # 如此設定防火牆規則後,他連 ping 我的主機都別想了。
      
      # 以下的 IP 或 Class C ,不是有入侵動作、就是亂寄廣告信,我不歡迎這種人,所以拒絕提供任何服務。
      # ----- spam ----- #
      /sbin/ipfw   add   deny   all   from   211.22.166.45    to   any
      /sbin/ipfw   add   deny   all   from   216.153.141.44   to   any
      /sbin/ipfw   add   deny   all   from   192.72.80.7      to   any
      /sbin/ipfw   add   deny   all   from   61.220.214.251   to   any
      /sbin/ipfw   add   deny   all   from   61.154.244.0/24  to   any
      
      /sbin/ipfw   add   deny   all   from   140.113.75.248   to   any
      /sbin/ipfw   add   deny   all   from   61.16.11.0/24    to   any
      /sbin/ipfw   add   deny   all   from   61.217.135.209   to   any
      /sbin/ipfw   add   deny   all   from   61.225.169.0/24  to   any
      /sbin/ipfw   add   deny   all   from   61.227.50.0/24   to   any
      /sbin/ipfw   add   deny   all   from   61.228.0.0/24    to   any
      /sbin/ipfw   add   deny   all   from   63.119.26.216    to   any
      /sbin/ipfw   add   deny   all   from   64.94.217.0/24   to   any
      /sbin/ipfw   add   deny   all   from   64.114.31.2      to   any
      /sbin/ipfw   add   deny   all   from   65.30.9.44       to   any
      /sbin/ipfw   add   deny   all   from   65.32.169.173    to   any
      /sbin/ipfw   add   deny   all   from   139.175.252.20   to   any
      /sbin/ipfw   add   deny   all   from   163.29.255.0/24  to   any
      /sbin/ipfw   add   deny   all   from   192.72.81.0/24   to   any
      /sbin/ipfw   add   deny   all   from   193.126.14.83    to   any
      /sbin/ipfw   add   deny   all   from   195.190.94.200   to   any
      /sbin/ipfw   add   deny   all   from   203.79.166.137   to   any
      /sbin/ipfw   add   deny   all   from   203.198.160.118  to   any
      /sbin/ipfw   add   deny   all   from   203.146.235.0/24 to   any
      /sbin/ipfw   add   deny   all   from   203.204.139.129  to   any
      /sbin/ipfw   add   deny   all   from   206.154.48.203   to   any
      /sbin/ipfw   add   deny   all   from   207.254.20.124   to   any
      /sbin/ipfw   add   deny   all   from   210.85.75.0/24   to   any
      /sbin/ipfw   add   deny   all   from   210.208.48.108   to   any
      /sbin/ipfw   add   deny   all   from   211.20.175.110   to   any
      /sbin/ipfw   add   deny   all   from   211.21.140.133   to   any
      /sbin/ipfw   add   deny   all   from   211.21.191.123   to   any
      /sbin/ipfw   add   deny   all   from   211.75.204.163   to   any
      /sbin/ipfw   add   deny   all   from   211.75.220.228   to   any
      /sbin/ipfw   add   deny   all   from   211.78.1.3       to   any
      /sbin/ipfw   add   deny   all   from   211.114.30.1     to   any
      /sbin/ipfw   add   deny   all   from   212.67.193.231   to   any
      /sbin/ipfw   add   deny   all   from   216.4.172.254    to   any
      /sbin/ipfw   add   deny   all   from   217.11.131.182   to   any
      /sbin/ipfw   add   deny   all   from   217.115.144.0/24 to   any
      
      # ================
      # 這一行是定義NAT的通行,如果只是設定 firewall 的話,不需要設定這一行。
      /sbin/ipfw   add   divert   natd   all   from   any   to   any   via   vr0
      
      # ================
      # 其餘的(all)都放行了,NAT 和 FireWall 都需要設定這一行。
      /sbin/ipfw   add   pass   all   from   any   to   any
      
      vr0:真實IP的網卡。

    • 您可以在任何時間對 /etc/rc.firewall 檔案做防火牆之規則的修改,存檔後,記得做 sh /etc/rc.firewall ,就能立即使新的規則生效。
      sh /etc/rc.firewall
      Flushed all rules.
      00100 deny ip from 140.113.75.248 to any
      00200 deny ip from 211.78.1.3 to any
      00300 deny ip from 211.21.191.123 to any
      00400 deny ip from 64.114.31.2 to any
      00500 divert 8668 ip from any to any via vr0
      00600 allow ip from any to any


    1. 更改過kernel需重新啟動FreeBSD主機,新的設定值才會生效。請確定主機就在你身邊,萬一操作不當,會因為防火牆的關係,而無法從遠端登入,不可不慎。

    • 將Win98與這臺FreeBSD接在同一組集線器(hub)上,其網路組態設定為:假設Win98的IP是在 192.168.1.1 ~ 192.168.1.255 的這段Class C上,NetMask為 255.255.255.0 ,通訊閘設為 192.168.1.254 ( 就是 FreeBSD虛擬出來的 IP ),其他的部份你應該會設定吧。
      建議再使用 ,讓192.168.1.0/24 這個網段的各win98或工作站自動取得網路組態。

    • 我嘗試著從這個NAT-Server底下的Win98以Outlook Express透過這臺NAT-Server的SMTP來寄信出去時,在/var/log/maillog留下了紀錄:
      Jul  9 23:54:18 www sendmail[4378]: f69FsHY04378: 
      from=, 
      size=1161, class=0, nrcpts=1, msgid=<000f01c1088f$69195d20$6601a8c0@bsdlab.idv.tw>,
      proto=SMTP, daemon=MTA, 
      relay=pc102.priv.bsdlab.idv.tw [192.168.1.102]
  • 阅读(1344) | 评论(0) | 转发(0) |
    给主人留下些什么吧!~~