<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## 9.4 單機防火墻的一個實例 介紹了這么多的防火墻語法與相關的注意事項后,終于要來架設防火墻了。鳥哥還是比較偏好使用腳本來撰寫防火墻, 然后透過最終的 /etc/init.d/iptables save 來將結果儲存到 /etc/sysconfig/iptables 去! 而且此一特色還可以用在呼叫其他的 scripts ,可以讓防火墻規則具有較為靈活的使用方式。 好了,那就來談談如何設定咱們的防火墻規則吧! * * * ### 9.4.1 規則草擬 鳥哥底下介紹的這個防火墻,其實可以用來作為路由器上的防火墻,也可以用來作為本機的防火墻。 假設硬件聯機如同下圖所示, Linux 主機本身也是內部 LAN 的路由器!亦即是一個簡單的 IP 分享器的功能啦!依據第三章的[圖 3.2-1](http://linux.vbird.org/linux_server/0120intranet.php#fig3.2-1) 假設鳥哥網絡接口有底下這些: * 外部網絡使用 eth0 (如果是撥接,有可能是 ppp0,請針對你的環境來設定); * 內部網絡使用 eth1 ,且內部使用 192.168.100.0/24 這個 Class ; * 主機默認開放的服務有 WWW, SSH, https 等等; ![](https://box.kancloud.cn/2016-05-13_5735da52646d7.gif) 圖 9.4-1、一個局域網絡的路由器架構示意圖 由于希望將信任網域 (LAN) 與不信任網域 (Internet) 整個分開的完整一點, 所以希望你可以在 Linux 上面安裝兩塊以上的實體網卡,將兩塊網卡接在不同的網域,這樣可以避免很多問題。 至于最重要的防火墻政策是:『關閉所有的聯機,僅開放特定的服務』模式。 而且假設內部使用者已經受過良好的訓練,因此在 filter table 的三條鏈個預設政策是: * INPUT 為 DROP * OUTPUT 及 FORWARD 為 ACCEPT 鳥哥底下預計提供的防火墻流程是這樣的: ![](https://box.kancloud.cn/2016-05-13_5735da5279ae3.png) 圖 9.4-2、本機的防火墻規則流程示意圖 原則上,內部 LAN 主機與主機本身的開放度很高,因為 Output 與 Forward 是完全開放不理的!對于小家庭的主機是可以接受的,因為我們內部的計算機數量不多,而且人員都是熟悉的, 所以不需要特別加以控管!但是:『在大企業的內部,這樣的規劃是很不合格的, 因為你不能保證內部所有的人都可以按照你的規定來使用 Network !』也就是說『家賊難防』呀! 因此,那樣的環境連 Output 與 Forward 都需要特別加以管理才行! * * * ### 9.4.2 實際設定 事實上,我們在設定防火墻的時候,不太可能會一個一個指令的輸入,通常是利用 shell scripts 來幫我們達成這樣的功能吶!底下是利用上面的流程圖所規劃出來的防火墻腳本,你可以參考看看, 但是你需要將環境修改成適合你自己的環境才行喔!此外,為了未來修改維護的方便,鳥哥將整個 script 拆成三部分,分別是: * iptables.rule:設定最基本的規則,包括清除防火墻規則、加載模塊、設定服務可接受等; * iptables.deny:設定抵擋某些惡意主機的進入; * iptables.allow:設定允許某些自定義的后門來源主機! 鳥哥個人習慣是將這個腳本放置到 /usr/local/virus/iptables 目錄下,你也可以自行放置到自己習慣的位置去。 那底下就來瞧瞧這支腳本是怎么寫的吧! ``` [root@www ~]# mkdir -p /usr/local/virus/iptables [root@www ~]# cd /usr/local/virus/iptables [root@www iptables]# vim iptables.rule #!/bin/bash # 請先輸入您的相關參數,不要輸入錯誤了! EXTIF="eth0" # 這個是可以連上 Public IP 的網絡接口 INIF="eth1" # 內部 LAN 的連接接口;若無則寫成 INIF="" INNET="192.168.100.0/24" # 若無內部網域接口,請填寫成 INNET="" export EXTIF INIF INNET # 第一部份,針對本機的防火墻設定!########################################## # 1\. 先設定好核心的網絡功能: echo "1" &gt; /proc/sys/net/ipv4/tcp_syncookies echo "1" &gt; /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts for i in /proc/sys/net/ipv4/conf/*/{rp_filter,log_martians}; do echo "1" &gt; $i done for i in /proc/sys/net/ipv4/conf/*/{accept_source_route,accept_redirects,\ send_redirects}; do echo "0" &gt; $i done # 2\. 清除規則、設定默認政策及開放 lo 與相關的設定值 PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/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,ESTABLISHED -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 # 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 --sport 1024:65534 -j ACCEPT # FTP # iptables -A INPUT -p TCP -i $EXTIF --dport 22 --sport 1024:65534 -j ACCEPT # SSH # iptables -A INPUT -p TCP -i $EXTIF --dport 25 --sport 1024:65534 -j ACCEPT # SMTP # iptables -A INPUT -p UDP -i $EXTIF --dport 53 --sport 1024:65534 -j ACCEPT # DNS # iptables -A INPUT -p TCP -i $EXTIF --dport 53 --sport 1024:65534 -j ACCEPT # DNS # iptables -A INPUT -p TCP -i $EXTIF --dport 80 --sport 1024:65534 -j ACCEPT # WWW # iptables -A INPUT -p TCP -i $EXTIF --dport 110 --sport 1024:65534 -j ACCEPT # POP3 # iptables -A INPUT -p TCP -i $EXTIF --dport 443 --sport 1024:65534 -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 &#124; grep "^${mod} " &#124; awk '{print $1}'` 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" &gt; /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 # 如果你的 MSN 一直無法聯機,或者是某些網站 OK 某些網站不 OK, # 可能是 MTU 的問題,那你可以將底下這一行給他取消批注來啟動 MTU 限制范圍 # iptables -A FORWARD -p tcp -m tcp --tcp-flags SYN,RST SYN -m tcpmss \ # --mss 1400:1536 -j TCPMSS --clamp-mss-to-pmtu # 4\. NAT 服務器后端的 LAN 內對外之服務器設定 # iptables -t nat -A PREROUTING -p tcp -i $EXTIF --dport 80 \ # -j DNAT --to-destination 192.168.1.210:80 # WWW # 5\. 特殊的功能,包括 Windows 遠程桌面所產生的規則,假設桌面主機為 1.2.3.4 # iptables -t nat -A PREROUTING -p tcp -s 1.2.3.4 --dport 6000 \ # -j DNAT --to-destination 192.168.100.10 # iptables -t nat -A PREROUTING -p tcp -s 1.2.3.4 --sport 3389 \ # -j DNAT --to-destination 192.168.100.20 # 6\. 最終將這些功能儲存下來吧! /etc/init.d/iptables save ``` 特別留意上面程序代碼的特殊字體部分,基本上,你只要修改一下最上方的接口部分, 應該就能夠運作這個防火墻了。不過因為每個人的環境都不相同, 因此你在設定完成后,依舊需要測試一下才行喔!不然,出了問題不要怪我啊!.... 再來看一下關于 iptables.allow 的內容是如何?假如我要讓一個 140.116.44.0/24 這個網域的所有主機來源可以進入我的主機的話,那么這個檔案的內容可以寫成這樣: ``` [root@www iptables]# vim iptables.allow #!/bin/bash # 底下則填寫你允許進入本機的其他網域或主機啊! iptables -A INPUT -i $EXTIF -s 140.116.44.0/24 -j ACCEPT # 底下則是關于抵擋的檔案設定法! [root@www iptables]# vim iptables.deny #!/bin/bash # 底下填寫的是『你要抵擋的那個咚咚!』 iptables -A INPUT -i $EXTIF -s 140.116.44.254 -j DROP [root@www iptables]# chmod 700 iptables.* ``` 將這三個檔案的權限設定為 700 且只屬于 root 的權限后,就能夠直接執行 iptables.rule 啰! 不過要注意的是,在上面的案例當中,鳥哥預設將所有的服務的通道都是關閉的! 所以你必須要到[本機防火墻的第 5 步驟](#script_daemon)處將一些批注符號 (#) 解開才行。 同樣的,如果有其他更多的 port 想要開啟時,一樣需要增加額外的規則才行喔! 不過,還是如同前面我們所說的,這個 firewall 僅能提供基本的安全防護,其他的相關問題還需要再測試測試呢! 此外,如果你希望一開機就自動執行這個 script 的話,請將這個檔案的完整檔名寫入 /etc/rc.d/rc.local 當中,有點像底下這樣: ``` [root@www ~]# vim /etc/rc.d/rc.local ....(其他省略).... # 1\. Firewall /usr/local/virus/iptables/iptables.rule ``` 事實上,這個腳本的最底下已經加入寫入防火墻默認規則文件的功能,所以你只要執行一次,就擁有最正確的規則了! 上述的 rc.local 僅是預防萬一而已。 ^_^!上述三個檔案請你不要在 Windows 系統上面編輯后才傳送到 Linux 上運作,因為 Windows 系統的斷行字符問題,將可能導致該檔案無法執行。建議你直接到底下去下載,傳送到 Linux 后可以利用 [dos2unix](http://linux.vbird.org/linux_basic/0310vi.php#dos2unix) 指令去轉換斷行字符!就不會有問題! * [http://linux.vbird.org/download/index.php?action=detail&fileid=43](http://linux.vbird.org/download/index.php?action=detail&fileid=43) 這就是一個最簡單、陽春的防火墻。同時,這個防火墻還可以具有最陽春的 IP 分享器的功能呢! 也就是在 [iptables.rule](#iptables.rule) 這個檔案當中的第二部分了。 這部分我們在下一節會再繼續介紹的。 * * *
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看