關於使用iptables對url的重定向

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.com
23.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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章