关于使用iptables对url的重定向

来源:互联网 发布:淘宝返利用什么 编辑:程序博客网 时间:2024/05/19 00:40

1. 背景

想做对url的重定向,通过iptables的string模块匹配在nat链PREROUTING始终无法实现DNAT,例如:

iptables -t nat -I PREROUTING -m string --string "baidu.com" --algo bm -j DNAT --to-destination 192.168.1.1

但是对其他特征能进行DNAT,例如:

iptables -t nat -I PREROUTING -p tcp --dport 80 -s xx.xx.xx.xx -d xx.xx.xx.xx -j DNAT --to-destination 192.168.1.1



2.  关于PREROUTING
只是最开始进行一次PREROUTING进行路由,之后不再通过PREROUTING,即一个http请求,先tcp握手的时候进入了PREROUTING,而这个握手的过程是匹配不到url的,然后之后包含了url的http请求则直接走FORWARD了。


3.  解决

可以通过dnsmasq来辅助实现。

tcp握手的过程的dip即是之前通过dns获取的,所以通过修改dnsmasq使其匹配到url的dns请求时传出该url的ip,例如:

nslookup baidu.com23.34.56.78

iptables -t nat -I PREROUTING -m string --string "baidu.com" --algo bm -j DNAT --to-destination 192.168.1.1
等价于

iptables -t nat -I PREROUTING -p tcp --dport 80 -d 23.34.56.78 -j DNAT --to-destination 192.168.1.1



0 0