免费注册 查看新帖 |

Chinaunix

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

用ipfilter在动态ip环境下做重定向 v0.02 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-04-04 10:58 |只看该作者 |倒序浏览
这个短文的v0.01我在这里发过,不过现在不能用搜索功能,找不到了  

看到这个帖子里一堆的“关注”,再贴出来,看看能不能有点帮助

http://chinaunix.net/forum/viewtopic.php?t=40859

from: https://bbs.bsdbase.com/index.php?s=34aa56e62f6325ed5494bb7162a683d7&act=ST&f=1&t=152


  1.           用ipfilter在动态ip环境下做重定向
  2.               阿土 tutu@bsdbase.com

  3. v0.01 2002.11.29 初始总结
  4. v0.02 2003.04.04 修正了一些错误;

  5.     在这里,我希望提供的是思路,而不是这个粗浅的script。

  6.     在ipfilter的nat规则中,如果出口ip地址是动态ip,比如PPPoE拨号或DHCP,那么可以使用类似
  7. map tun0 192.168.0.0/24 ->; 0.0.0.0/32 的语法进行地址(端口)映射;可是类似
  8. rdr tun0 0/32 port 80 ->; 192.168.2.100 port 80 tcp/udp这样的语法却是不对的,因为rdr
  9. 规则要求第三个域是ip包的目的地址,通常是该出口网卡的地址,显然,当别人访问你的机器的时候
  10. tcp/udp包的目的地址是你的当前地址,而不是0/32,所以,rdr tun0 0/32 ...这样的规则是不会生效的,
  11. 你必须以该网卡的当前ip地址来做这个规则;ipfilter本身没有提供这样的定义使得我们可以方便的做
  12. 这样的规则,下面是我做的脚本,用来动态的获得当前出口网卡地址,并一次刷新ipfilter规则。

  13. 1。内核中编译ipfilter的支持,不能让ipfilter以模块的形式载入;
  14. 因为我没有在/etc/rc.conf中指定ipfilter_enable="yes";
  15. 2。编制如下shell script;

  16. #!/bin/sh
  17. #vi /usr/local/sbin/ipf.sh
  18. #此脚本用来刷新ipf规则;
  19. #注意/etc/ipf.rules是根据/etc/ipf.rules.template这个模板自动生成的;
  20. #所以,如果你要改规则,应该改/etc/ipf.rules.template这个模板;
  21. #For dynamic ip ipf rules
  22. #$EXT_NIC is the internet connected NIC
  23. #$EXT_ADDR is the internet connected NIC ADDRESS.

  24. #get dynamic nic.
  25. #获得默认路由经过的网卡,即所谓的外网网卡;
  26. EXT_NIC=`netstat -arn | grep "default\>;" | awk '{print $6 }'`
  27. if [ -z $EXT_NIC ];
  28. then
  29.         echo "ERROR default gateway NO found !!!"
  30.         exit 1
  31. else
  32.         export EXT_NIC
  33.         #get dynamic nic and ip.
  34.         #获得外网卡地址;
  35.         EXT_ADDR=`ifconfig $EXT_NIC | grep "inet\>;" | awk '{print $2}'`
  36.         if [ -z $EXT_ADDR ];
  37.         then
  38.                 echo "ERROR:EXT_ADDR NO found !!!"
  39.                 exit 1
  40.         else
  41.                 export EXT_ADDR
  42.                 #根据上述信息参照模板生成规则;
  43.                 echo "#NOTE:" >;/etc/ipf.rules
  44.                 echo "#DON'T modify /etc/ipf.rules for your ipf rules ,Just modify /etc/ipf.rules.template instance !!!" >;>;/etc/ipf.rules
  45.                 echo "#Read /usr/local/sbin/ipf.sh for detail." >;>;/etc/ipf.rules
  46.                 echo "#." >;>;/etc/ipf.rules  
  47.                 echo "#Reflashed date:`date`." >;>;/etc/ipf.rules
  48.                 sed s/\$EXT_NIC/$EXT_NIC/g /etc/ipf.rules.template >;/etc/ipf.rules.nic
  49.                 sed s/\$EXT_ADDR/$EXT_ADDR/g /etc/ipf.rules.nic >;>;/etc/ipf.rules
  50.                 #刷新规则;
  51.                 /sbin/ipf -Fa
  52.                 /sbin/ipf -y -f /etc/ipf.rules
  53. fi
  54. fi
  55. #end /usr/local/sbin/ipf.sh

  56. #!/bin/sh
  57. #vi /usr/local/sbin/ipnat.sh
  58. #此脚本用来刷新ipnat规则;
  59. #注意/etc/ipnat.rules是根据/etc/ipnat.rules.template这个模板自动生成的;
  60. #所以,如果你要改规则,应该改/etc/ipnat.rules.template这个模板;
  61. #For dynamic ip ipnat rules
  62. #$EXT_NIC is the internet connected NIC
  63. #$EXT_ADDR is the internet connected NIC ADDRESS.

  64. #get dynamic nic.
  65. #获得默认路由经过的网卡,即所谓的外网网卡;
  66. EXT_NIC=`netstat -arn | grep "default\>;" | awk '{print $6 }'`
  67. if [ -z $EXT_NIC ];
  68. then
  69.         echo "ERROR default gateway NO found !!!"
  70.         exit 1
  71. else
  72.         export EXT_NIC
  73.         #get dynamic nic and ip.
  74.         #获得外网卡地址;
  75.         EXT_ADDR=`ifconfig $EXT_NIC | grep "inet\>;" | awk '{print $2}'`
  76.         if [ -z $EXT_ADDR ];
  77.         then
  78.                 echo "ERROR:EXT_ADDR NO found !!!"
  79.                 exit 1
  80.         else
  81.                 export EXT_ADDR
  82.                 #根据上述信息参照模板生成规则;
  83.                 echo "#NOTE:" >;/etc/ipnat.rules
  84.                 echo "#DON'T modify /etc/ipnat.rules for your nat rules ,Just modify /etc/ipnat.rules.template instance !!!" >;>;/etc/ipnat.rules
  85.                 echo "#Read /usr/local/sbin/ipnat.sh for detail." >;>;/etc/ipnat.rules
  86.                 echo "#." >;>;/etc/ipnat.rules  
  87.                 echo "#Reflashed date:`date`." >;>;/etc/ipnat.rules               
  88.                 sed s/\$EXT_NIC/$EXT_NIC/g /etc/ipnat.rules.template >;/etc/ipnat.rules.nic
  89.                 sed s/\$EXT_ADDR/$EXT_ADDR/g /etc/ipnat.rules.nic >;>;/etc/ipnat.rules
  90.                 #刷新规则;
  91.                 /sbin/ipnat -C
  92.                 /sbin/ipnat -v -f /etc/ipnat.rules
  93.         fi
  94. fi
  95. #end /usr/local/sbin/ipnat.sh

  96. #!/bin/sh
  97. #vi /usr/local/sbin/ipfrenew
  98. #调用预先编制的脚本刷新ipf以及ipnat规则;
  99. /usr/local/sbin/ipf.sh
  100. /usr/local/sbin/ipnat.sh
  101. #显示当前状况;
  102. /sbin/ipnat -l |grep -v '<- ->; '
  103. echo List of active sessions have been cutted.
  104. /sbin/ipfstat -if
  105. /sbin/ipfstat -of
  106. #end of /usr/local/sbin/ipfrenew

  107. #设置可执行;
  108. chmod 700 /usr/local/sbin/*

  109. 3。在会更换ip的程序中调用/usr/local/sbin/ipfrenew
  110. PPPoE:

  111. #vi /etc/ppp/ppp.linkup
  112. default:
  113. pppoe:
  114.   shell "/usr/local/sbin/ipfrenew"
  115. #end of /etc/ppp/ppp.linkup
  116.   
  117. #假设你的PPPoE配置名称叫pppoe;

  118. DHCP(Cable modem):

  119. #!/bin/sh
  120. #vi /etc/dhclient-exit-hooks
  121. /usr/local/sbin/ipfrenew
  122. #end of /etc/dhclient-exit-hooks

  123. #至于说调用的语法,自己查man,都说的很清楚了;

  124. #begin of /etc/ipnat.rules.template
  125. #这里使用$EXT_ADDR是必须的,因为每次获得的ip不同;
  126. rdr $EXT_NIC $EXT_ADDR/32 port 80 ->; 192.168.0.82 port 80
  127. #当然,如果你想好用,还得配合动态dns client,另文说明;



  128. #下面是一些常用的map规则;
  129. # For 192.168.0.0/24
  130. # ------------------------------------------------------------
  131. # Use ipfilter FTP proxy for hosts behind NAT doing transfer
  132. # mode active.
  133. # ------------------------------------------------------------
  134. map $EXT_NIC 192.168.0.0/24 ->; 0/32 proxy port ftp ftp/tcp


  135. # -----------------------------------------------------------
  136. # Use ipfilter IKE proxy for ESP packets for hosts behind NAT
  137. # IP Filter 3.4.21 and beyond only.
  138. # -----------------------------------------------------------
  139. map $EXT_NIC 192.168.0.0/24 ->; 0/32 proxy port 500 ipsec/udp


  140. # -----------------------------------------------------------
  141. # Use ipfilter RealAudio proxy for hosts behind NAT
  142. # -----------------------------------------------------------
  143. map $EXT_NIC 192.168.0.0/24 ->; 0/32 proxy port 7070 raudio/tcp

  144. # -----------------------------------------------------------
  145. # Use ipfilter H323 proxy for hosts behind NAT
  146. # -----------------------------------------------------------
  147. map $EXT_NIC 192.168.0.0/24 ->; 0/32 proxy port 1720 h323/tcp


  148. # -----------------------------------------------------------
  149. # Map all internal UDP and TCP traffic to the external IP address
  150. # -----------------------------------------------------------
  151. map $EXT_NIC 192.168.0.0/24 ->; 0/32 portmap tcp/udp 40000:60000


  152. # -----------------------------------------------------------
  153. # Map all other traffic e.g. ICMP to the external IP address
  154. # -----------------------------------------------------------
  155. map $EXT_NIC 192.168.0.0/24 ->; 0/32
  156. #end of /etc/ipnat.rules.template



  157. #begin of /etc/ipf.rules.template
  158. #一个简单的规则,注意没有对pptp提供支持;
  159. #ipfilter default to pass;
  160. block in quick all with ipopts
  161. block in quick all with short
  162. #小心拦截碎片,一些程序就是会产生碎片,ipfilter的碎片拦截不会对碎片进行合法性检查。
  163. #block in quick all with frag  

  164. block in on $EXT_NIC all
  165. block out on $EXT_NIC all

  166. block in quick on $EXT_NIC from 10.0.0.0/8 to any
  167. block in log quick on $EXT_NIC from 192.168.0.0/16 to any
  168. block in log quick on $EXT_NIC from 172.16.0.0/12 to any
  169. block in log quick on $EXT_NIC from 127.0.0.0/8 to any
  170. block in log quick on $EXT_NIC from 169.254.0.0/16 to any

  171. pass in on $EXT_NIC proto icmp from any to any icmp-type echo
  172. pass in on $EXT_NIC proto icmp from any to any icmp-type echorep

  173. #for http and https
  174. pass in quick on $EXT_NIC proto tcp from any to any port = 80 keep state
  175. pass in quick on $EXT_NIC proto tcp from any to any port = 443 keep state

  176. #for mail
  177. pass in quick on $EXT_NIC proto tcp from any to any port = 25 keep state
  178. pass in quick on $EXT_NIC proto tcp from any to any port = 110 keep state

  179. pass out quick on $EXT_NIC proto tcp/udp from any to any keep state
  180. pass out quick on $EXT_NIC proto icmp from any to any keep state

  181. block return-rst in log on $EXT_NIC proto tcp from any to any
  182. block return-icmp-as-dest(port-unr) in log on $EXT_NIC proto udp from any to any
  183. #end of /etc/ipf.rules.template
复制代码

论坛徽章:
0
2 [报告]
发表于 2003-04-05 04:18 |只看该作者

用ipfilter在动态ip环境下做重定向 v0.02

...

bsdbase = 阿土?  

论坛徽章:
0
3 [报告]
发表于 2003-04-05 10:07 |只看该作者

用ipfilter在动态ip环境下做重定向 v0.02

原帖由 "红袖添香" 发表:
...

bsdbase = 阿土?  


sure. 假设你说的是 tutux .
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP