现在的位置: 首页 > 综合 > 正文

(转)iptables 防火墙

2011年09月10日 ⁄ 综合 ⁄ 共 7462字 ⁄ 字号 评论关闭

转自: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  

 

抱歉!评论已关闭.