MASQUERADE 同样是做源地址转换, 只不过防火墙会根据该策略自动查找可用的公网 IP 地址, 适应变化的情况
? 若接口使用 ppp+, 表示匹配 ppp0,ppp1...... 中任意可用的拨号连接
? 若需要演示操作, 可以针对前一个 SNAT 策略的例子进行改写 (因没有 ADSL 连接, 接口仍然使用 eth0):
先执行 "iptables -t nat -F" 清空 nat 表
再添加规则 "iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE"
? 讲解应用 DNAT 策略的基本步骤, 其中涉及到的相关操作
? 同样可适当强调: 外网测试机并不需要将默认网关地址设为该 Linux 网关主机的 IP 地址
? 确认路由功能状态时, 可以执行 sysctl 命令查看:
- [root@gateway ~]# sysctl -a | grep "ip_forward"
- net.ipv4.ip_forward = 1
执行 "elinks http://218.29.30.29" 或者 "lynx http://218.29.30.29" 均可以进行测试
- [root@localhost ~]# tail -f /var/log/httpd/access_log
- 218.29.30.29 - - [04/Jun/2009:14:35:53 +0800] "GET / HTTP/1.1" 200 15 "-" "ELinks/0.11.1 (textmode; Linux; 80x25-2)"
NAT 转换
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth1 -j SNAT --to-source 218.29.30.31
先执行 "iptables -t nat -F" 清空 nat 表
再添加规则 "iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE"
- [root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o ppp0 -j MASQUERADE
- [root@gateway ~]# sysctl -a | grep "ip_forward" net.ipv4.ip_forward = 1
- iptables -t nat -A PREROUTING -i eth0 -d 218.29.30.31 -p tcp --dport 80 -j DNAT --to-destination 192.168.1.6
- [root@localhost ~]# iptables -t nat -A PREROUTING -i eth0 -d 218.29.30.31 -p tcp --dport 2222 -j DNAT --to-destination
- 192.168.1.5:22
-------------------------- 理解 ESTABLISHED
- vm1
- # iptables -P INPUT DROP
- ping 192.168.56.201
- SSH 192.168.56.201
全部失败
- # iptables -A INPUT -p all -m state --state ESTABLISHED -j ACCEPT
- ping 192.168.56.201
- SSH 192.168.56.201
全部成功
vm2 关闭防火墙
成功穿越反向防火墙.
tracerout 192.168.56.201 失败 , 原因看下面
-------------------------- 理解 RELATE
- vm1
- # iptables -P INPUT DROP
- # iptables -F
- # iptables -A INPUT -p all -m state --state RELATED -j ACCEPT
tracerout 192.168.56.201 成功. 由第一个包产生的其他回包都属于 RELATE 状态.
-------------------------- 理解 NEW
vm1
OUTPUT 默认策略为 DROP. 其他策略为 ACCEPT
- # iptables -P INPUT ACCEPT
- # iptables -P OUTPUT DROP
- # iptables -F
- ping 192.168.56.201
- SSH 192.168.56.201
全部失败
# iptables -A OUTPUT -p all -m state --state ESTABLISHED,NEW -j ACCEPT
-------------------------- 理解 INVALID
他需要特殊的第一条黑客工具产生, 所以这里面紧急下面的用法.
通常应用在 INPUT 表的第一条.
# iptables -A INPUT -p all -m state --state INVALID -j DROP
--------------------------SNAT - 内网访问外网
------- 外网 -------------------- 网关防火墙 ------------------- 内网 PC1 192.168.56.202
- 10.0.100 eth0 eth1
- 10.0.1.200 192.168.56.200
内网: yum install elinks lynx -y
网关防火墙
# iptables -t nat -A POSTROUTING -s 192.168.56.202 -o eth0 -j SNAT --to-source 10.0.1.200
外网: httpd
# tailf /var/log/httpd/access_log
--------------------------DNAT - 对外发布端口
外网: yum install elinks lynx -y
网关防火墙:
# iptables -t nat -A PREROUTING -i eth0 -d 10.0.1.200 -p tcp --dport 2345 -j DNAT --to-destination 192.168.56.202:80
内网: httpd
# tailf /var/log/httpd/access_log
内核模块存放位置
- # ls /lib/modules/2.6.32-431.el6.x86_64/kernel.NET/ipv4/netfilter/ //ipv4 支持
- # ls /lib/modules/2.6.32-431.el6.x86_64/kernel.NET/ipv6/netfilter/ //ipv6 支持
- # ls /lib/modules/2.6.32-431.el6.x86_64/kernel.NET/netfilter/ // 同时支持
lsmod 查看当前的模块
来源: http://www.bubuko.com/infodetail-2788597.html