)iptables 防火牆

來源:互聯網
上載者:User

轉自:http://blog.csdn.net/jixiuffff/article/details/5879547

[c-sharp] view plaincopy
  1. # 五個檢查點PREROUTING ,FORWARD POSTROUTING INPUT OUTPUT   
  2. #   一個資料包從prerouting 進入我的機器,它有兩個去向,一是經過input 訪問我機器上的應用程式,後經output ,postrouting   
  3. # 流走,另一個去向是:直接經forward postrouting 流向別的機器,也就是說我的機器只是充當路由,資料包經我的機器到其他機器上  
  4. #  
  5. #      PREROUTING ---------------->FORWARD---------------------> POSTROUTING  
  6. #                   |                                ^  
  7. #                   |                                |  
  8. #                   INPUT                           OUTPUT   
  9. #                   |                                |   
  10. #                   v                                |  
  11. #                    --->我機器上的應用程式---------->  
  12. #  
  13. #iptables 的結構由上到下是:表(table),規則鏈(chain),規則(rule) ,表由規則鏈組成,規則鏈由一條條規則群組成  
  14. #iptables 預設有三張表filter ,nat ,mangle ,   使用-t 參數指定對哪張表操作,如果不指定,則預設是對filter 表進行操作  
  15. #  
  16. # filter 預設有三條內建的規則鏈, INPUT FORWARD OUTPUT   
  17. # nat     。。。。。。。。。。。 POSTROUTING ,OUTPUT PREROUTING   
  18. # mangle  .....兩.............    OUTPUT PREROUTING   
  19. #  
  20. #iptables 命令的一般格式 iptables [ -t table]  操作  [ chain] [options ]  
  21. #一個查對完整參數的樣本   
  22. #iptables -t filter      -I    INPUT 2    -i eth0  -s 10.2.1.111 --sport 1234 -d 10.2.1.123 --dport 22   -j ACCEPT   
  23. #iptables -t filter      -I    OUTPUT 2    -i eth0  -d 10.2.1.123  --dport 22 -s 10.2.1.123  --sport 1234   -j ACCEPT   
  24. # 這條命令是:在filter 表中的INPUT 鏈上 插入一條規則在2處(此規則排在第二個位置) ,規則的具體:從我的eth0 網卡聯我,且對方機 的ip 是10.2.1.111 對方連接埠1234,訪問我的ip :10.2.1.123 我的22連接埠 ,時才接受  
  25. # 然後是,從我的ip 10.2.1.123:22 向10.2.1.111:1234 經eth0 網卡發出的包允許存取  
  26. #iptables -F  ,清空filter 的所有規則鏈  
  27. #iptables -t nat -F 清空nat 表的所有規則鏈  
  28. ###################################################################################################  
  29. # 關於INPUT ,OUTPUT 都是相對於“我”這台機器,即INPUT  :表示向我輸入資料,OUTPUT 表示“我”向外輸出資料  
  30. # 而-s -d --sport --dport 分別表示 源ip(source) ,目標ip(destination) ,源ip的連接埠,目標ip的連接埠  
  31. # 當在對INPUT 作處理的時候,-s 指的是對方的機器,-d  指的是我這台機器,因為資料是從對方的機器流向我的,  
  32. # 而對OUTPUT 作處理的時候   -s 指的是我,而-d 指的是對方的機器  
  33. #正確使用防火牆,一般預設設為拒絕所有,然後只開放需要開放的,而不是允放所有,只拒絕需要拒絕的  
  34. #首先啟動iptables 服務/etc/init.d/iptables start   
  35. #我用的是gentoo 系統裝上iptables 後,第一次運行 它它提示我要先運行/etc/init.d/iptables save ,好像是做一些初始化或者儲存一些檔案,  
  36. /etc/init.d/iptables save   
  37. /etc/init.d/iptables start   
  38. #啟動後看一下預設的訪問規則  
  39. iptables -L 或者iptables -L --line-number 顯示行號, -v 詳細資料  
  40. Chain INPUT (policy ACCEPT)  
  41. target     prot opt source               destination           
  42. Chain FORWARD (policy ACCEPT)  
  43. target     prot opt source               destination           
  44. Chain OUTPUT (policy ACCEPT)  
  45. target     prot opt source               destination  
  46. #預設情況下是policy 是ACCEPT   ,等於沒有防火牆,現在修改預設的policy   
  47. #注意千萬不要使用遠程ssh 串連進行這個操作,因為它也會關閉ssh 使用的22 連接埠,  
  48. #使用ssh 串連 ,首先開放了22 連接埠再進行下面三條命令  
  49. # sshd   
  50. # 允許任何機器向我的22 連接埠發出請求  
  51. #  這裡沒用用-t 則預設是-t filter   
  52.                         iptables -A INPUT  -p tcp --dport 22      -j ACCEPT  
  53. # 等價於:iptables -t filter  -A INPUT  -p tcp --dport 22      -j ACCEPT  
  54. #允許我的22連接埠向外輸出資料  
  55.                         iptables -A OUTPUT  -p tcp --sport 22      -j ACCEPT  
  56. #如果只限某些特定ip 的機器訪問我,上面兩條要換成  
  57.                         iptables -A INPUT  -p tcp --dport 22  -s 10.2.1.110     -j ACCEPT  
  58.                         iptables -A OUTPUT  -p tcp --sport 22 -d 10.2.1.110     -j ACCEPT  
  59. #現在只有ip為10.2.1.110的ip 可以訪問我  
  60. #  
  61. iptables -P INPUT  DROP  
  62. iptables -P OUTPUT DROP  
  63. iptables -P FORWARD DROP   
  64. #預設的策略只能是ACCEPT ,DROP ,不能是REJECT   
  65. Chain INPUT (policy DROP)  
  66. target     prot opt source               destination           
  67. Chain FORWARD (policy DROP)  
  68. target     prot opt source               destination           
  69. Chain OUTPUT (policy DROP)  
  70. target     prot opt source               destination  
  71. #現在無論INPUT ,OUTPUT ,FORWARD 預設都是丟包(drop拒絕),而不是accept 接受  
  72. #此時我極度安全,等於沒連網,我不能訪問別人,別人不能訪問我  
  73. #現在我想上網  
  74. # 假 如我想訪問對方的80 連接埠,其實包括了兩個方面,一是我有許可權向對方的80 連接埠發出請求,二是有許可權從對方的80 連接埠取得資料,這裡只規定對方的 80 連接埠,而沒有規定我從哪個連接埠去訪問它的80 ,意味著我可以從任意連接埠訪問對方的80連接埠,這裡連接埠都是tcp 類型的  
  75. #允許我向對方的80 連接埠發出請求  
  76. iptables -A OUTPUT -p tcp --dport 80 -j ACCEPT  
  77. #允許對方的80 連接埠向我返回資料  
  78. iptables -A INPUT -p tcp --sport 80 -j ACCEPT  
  79. # 雖然我們此時可以訪問對方的80 連接埠,但是我們在瀏覽器中輸入www.baidu.com 並不能顯示對方的網頁,但是 http://202.108.22.142/ 確可以。因為在這個過程中還要進行dns網域名稱解析,又要有另一個許可權,那就是允許我向 dns server 的udp 53 連接埠請求,並允許從它返回資料  
  80.  iptables -A OUTPUT -p udp --dport 53 -j ACCEPT  
  81.  iptables -A INPUT -p udp --sport 53 -j ACCEPT  
  82. # 這裡沒有指定dns server 的ip 地址,如果想邊dns server 的ip 也做限定的話  
  83. # 可以這樣寫  
  84.  iptables -A OUTPUT -p udp -d 211.64.208.1 --dport 53 -j ACCEPT  
  85.  iptables -A INPUT -p udp -s 211.64.208.1  --sport 53 -j ACCEPT  
  86. #  
  87. #我校園網用drcom 進行流量計費要開upd 61440 連接埠  
  88. # drcom   
  89. #允許211.64.208.160 從它的61440 (sport) 連接埠串連到我的機器的61440 (dport)  
  90. # -s 表示源,表示從哪台機器向我發送資料  
  91. iptables -A INPUT  -p udp --sport 61440 --dport 61440 -s 211.64.208.160 -j ACCEPT  
  92. #允許 我的機器 從61440(sport) 連接埠 向211.64.208.160 的61440(dport)連接埠發送資料  
  93. # -d 指定對方機器(目標機器)  
  94. iptables -A OUTPUT  -p udp --sport 61440 --dport 61440 -d 211.64.208.160 -j ACCEPT  
  95. #目前為止,都是作為一個客戶去訪問別人,如果我要在我的電腦上架設個伺服器又當如何呢,比如架設sshd 及web 伺服器  
  96. #web伺服器,開放80連接埠  
  97. iptables -A INPUT -p tcp  --dport 80 -j ACCEPT   
  98. iptables -A OUTPUT  -p tcp  --sport 80 -j ACCEPT   
  99.  
  100. #開放ftp 服務  
  101. #iptables -A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT  
  102. #允許串連保持的被動訪問。  
  103. #ftp協議是一個簡單、保密性差(明碼)的tcp協議,它的工作原理是用戶端先連伺服器端的21連接埠,然後經過3步的握手以後建立了一條串連。要注意的是,這條串連只可以用來傳輸ftp的命令,只有這條串連的話是什麼都傳不了的,就算是用“ls”命令來查看檔案也不行。  
  104. # 建立了命令的串連以後,伺服器端就要建立一條資料的串連。資料的串連又分為主動模式(port)和被動模式(passive)。ftp預設是被動模式,主 動和被動之間使用"pass"命令切換。主動模式通過20連接埠與用戶端相連,而被動模式卻使用1024以後的連接埠與用戶端相連。由於1024以後的連接埠是 隨機分配的,所以在被動模式下我們是不知道服務端是使用什麼連接埠與用戶端串連的。也就是說,我們是不知道iptables要開放什麼連接埠。   
  105. #  
  106. #  
  107. #1 在/etc/conf.d/iptables設定檔中 加入 如下語句(不同發行版可能檔案位置不同)  
  108. #IPTABLES_MODULES="ip_conntrack_ftp"  
  109. #  
  110. iptables -A INPUT  -m state --state ESTABLISHED,RELATED -j ACCEPT  
  111. iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT  
  112. iptables -A INPUT -p tcp --dport 21 -j ACCEPT  
  113. iptables -A OUTPUT -p tcp --sport 21 -j ACCEPT  
  114. #主動模式使用20連接埠  
  115. iptables -A OUTPUT -p tcp --sport 20 -j ACCEPT  
  116. iptables -A INPUT -p tcp --dport 20 -j ACCEPT  
  117.  
  118. #對於lo 裝置的資料包都允許存取 ,也就是本機資料 -i 表示輸入,-o 表示輸出  
  119. #表示所有從lo 來的資料accept  
  120. iptables  -t filter  -I  INPUT l -i lo  -j ACCEPT   
  121. #表示流向lo 的資料accept   
  122. iptables  -t filter  -I   OUTPUT 1 -o lo  -j ACCEPT   
  123. #   
  124. #  
  125. #  
  126. #  
  127.   
  128.   
  129.   
  130.   
  131. 完整的指令碼:  
  132. sudo /etc/init.d/iptables save  
  133. sudo /etc/init.d/iptable restart  
  134. #清空表中規則鏈  
  135. iptables -F   
  136. iptables -X  
  137. iptables -t nat -F   
  138. iptables -t nat -X  
  139. #開放sshd服務  
  140. iptables -A INPUT  -p tcp --dport 22      -j ACCEPT  
  141. iptables -A OUTPUT  -p tcp --sport 22      -j ACCEPT  
  142. #預設drop 所有包  
  143. iptables -P INPUT  DROP  
  144. iptables -P OUTPUT DROP  
  145. iptables -P FORWARD DROP   
  146. #本機裝置允許存取  
  147. iptables  -t filter  -I  INPUT 1 -i lo  -j ACCEPT   
  148. iptables  -t filter  -I   OUTPUT 1 -o lo  -j ACCEPT   
  149. #dns   
  150.  iptables -A OUTPUT -p udp   --dport 53 -j ACCEPT  
  151.  iptables -A INPUT -p udp --sport 53  -j ACCEPT  
  152. #上網  
  153. iptables -A OUTPUT -p tcp --dport 80 -j ACCEPT  
  154. iptables -A INPUT -p tcp --sport 80 -j ACCEPT  
  155. #drcom   
  156. iptables -A INPUT  -p udp --sport 61440 --dport 61440 -s 211.64.208.160 -j ACCEPT  
  157. iptables -A OUTPUT  -p udp --sport 61440 --dport 61440 -d 211.64.208.160 -j ACCEPT  
  158. # ftp   
  159. # 在設定檔中加入  IPTABLES_MODULES="ip_conntrack_ftp"  
  160. iptables -I INPUT  2 -m state --state ESTABLISHED,RELATED -j ACCEPT  
  161. iptables -I OUTPUT 2  -m state --state ESTABLISHED,RELATED -j ACCEPT  
  162. iptables -A INPUT -p tcp --dport 21 -j ACCEPT  
  163. iptables -A OUTPUT -p tcp --sport 21 -j ACCEPT  
  164. iptables -A OUTPUT -p tcp --sport 20 -j ACCEPT  
  165. iptables -A INPUT -p tcp --dport 20 -j ACCEPT  
  166. #web 服務  
  167. iptables -A INPUT -p tcp  --dport 80 -j ACCEPT   
  168. iptables -A OUTPUT  -p tcp  --sport 80 -j ACCEPT   
  169. # dhcp ,使用dhcp 獲得ip ,  
  170. # dhcp  
  171. iptables -A INPUT -p udp --sport 67 --dport 68 -j ACCEPT  

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.