2小时玩转iptables
一直以来都特别想学会iptables,但是网上真的没有找到好的学习资料。
最近被大神安利了一个教程,是现在阿里P8白金大佬在2006年左右chinaunix社区做的分享,其标题就是《2小时玩转iptables》。
该分享简直是业界良心,只有深刻透彻理解iptables的大神才能做出如此透彻简单的教程,相信大家看完之后再也不会感觉iptables有多么神秘了,利用iptables可以实现想做的任何事情,比如徒手制作一个软路由。
教程介绍
该教程包括了1个PPT和3段MP3录音(音质较差)。
其内容核心就是3页PPT,其他的都是围绕这些知识点展开讲解。
首先是说明了数据包的流动过程:
然后描述了非常关键的,也是让学习者一目了然的知识,也就是链和表的关系:
最后是说明iptables命令的组成部分,以便后续进行逐一细化讲解:
PPT与音频下载,请加入我的星球,付费获取更多宝贵分享:
简单练习
学习完成后,我尝试做了一个简单练习,也遇到了2个问题,这里记录下来。
期望的效果是:访问树莓派7777端口,流量自动转发给baidu,其效果如下:
1 2 3 |
pi@raspberrypi:~ $ curl http://localhost:7777 -H 'Host:www.baidu.com' <!DOCTYPE html> <!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html> |
解决这个需求需要2个步骤:
- DNAT:本机当然没有7777端口的监听,但是可以通过iptables进行DNAT改写其目标IP地址。
- SNAT:目标IP为baidu的包经过树莓派路由表确认后即将发往默认网关,在离开树莓派前需要改写源IP地址为树莓派的网卡对外地址。
做DNAT可以这样处理:
1 |
sudo iptables -t nat -A PREROUTING -p tcp --dport 7777 -j DNAT --to 103.235.46.39:80 |
任何发往7777端口的包做DNAT到baidu的IP地址,更精确匹配应该在匹配中加一下-d限制一下目标IP,这里就不演示了。
根据之前框架图中的说明,经过PREROUTING改变目标IP地址后,会经过树莓派的路由表确认网卡出口,因为目标地址是baidu所以应该会发往树莓派的默认网关(也就是我家里的路由器)。
在发往默认网关之前,需要做SNAT外部IP转换,即将源IP改为树莓派的网卡IP而不是原先的localhost:
1 |
sudo iptables -t nat -A POSTROUTING -d 103.235.46.39/32 -p tcp --dport 80 -j MASQUERADE |
这一步就是识别出发往baidu的80端口的包,利用MASQUERADE自动做SNAT完成源IP转换即可。
现在telnet localhost 7777拒绝了连接,这种情况一般是去路不通导致的,经过谷歌明确原因:
从本机发往本机的包不通过prerouting链而是过的output链,所以dnat规则需要写到output链上。
1 |
sudo iptables -t nat -A OUTPUT -p tcp --dport 7777 -j DNAT --to-destination 103.235.46.39:80 |
删除PREROUTING链上的dnat规则即可。
现在再次测试,发现这次没有出现连接拒绝,而是telnet hang住了,这种情况一般是数据回路不通导致的一直在等待tcp应答。
按照理论分析,回路数据首先经过prerouting链,因为去路的时候在nat表生成了snat关系,所以经过prerouting链的时候会恢复数据包的IP地址为localhost;此后经过input链,因为去路的时候在nat表生成了dnat关系,所以经过input链的时候会恢复数据包的源IP地址为localhost。
整个分析下来是没有问题的,那么问题在哪呢?咨询了运维专家之后,得知原因如下:
因为在PREROUTING中根据SNAT映射恢复目标IP时,目标地址被改成了localhost,而且接下来要过路由表。
而内核默认对localhost走路由表做了限制,所以需要我们打开一个开关才能走通这个场景:
sysctl -w net.ipv4.conf.eth0.route_localnet=1
再次做测试,请求跑通。
如果文章帮助您解决了工作难题,您可以帮我点击屏幕上的任意广告,或者赞助少量费用来支持我的持续创作,谢谢~

这个知识星球貌似被封禁了。
1
1