iptablestorstunneltransparentproxy

Redirecting from outgoing loopback traffic - is it possible?


I have 2 kinds of proxies in my local machine : stunnel and TOR-VPN.

I want to get web traffic to go to stunnel first and the output traffic of stunnel go to tor-vpn. This needs double redirecting. is it possible to do it with iptables? I mean by using "table nat chain OUTPUT".

Because as far as I know "table nat chain OUTPUT" cant be called twice.

web traffic = browser listening on 127.0.0.1:6666

these are my rules:

iptables -t nat -A OUTPUT -p tcp -j REDIRECT --to-ports 6666

iptables -t nat -A OUTPUT -p tcp -m owner --uid-owner bob -m tcp -j REDIRECT --to-ports 9040

iptables -t nat -A OUTPUT -p udp -m owner --uid-owner bob -m udp --dport 53 -j REDIRECT --to-ports 53

iptables -t filter -A OUTPUT -p tcp --dport 6666 -j ACCEPT

iptables -t filter -A OUTPUT -p tcp -m owner --uid-owner bob -m tcp --dport 9040 -j ACCEPT

iptables -t filter -A OUTPUT -p udp -m owner --uid-owner bob -m udp --dport 53 -j ACCEPT

iptables -t filter -A OUTPUT -m owner --uid-owner bob -j DROP

the above rules make stunnel work independently from TOR/VPN. i mean when browser is set with proxy, no traffic will go through TOR/VPN but if i turn off the proxy in browser, all traffic will go through TOR/VPN.

now i want to let browser have the proxy on and all web traffic go to stunnel first, but outgoing stunnel traffic(outgoing loopback traffic) redirects to TOR/VPN(127.0.0.1:9040)

is it possible ? how can i do that? somehow i mean double redirecting inside system.

Policy of all tables is ACCEPT


Solution

  • I found the answer by myself. in my first post, i said something that was completely wrong and because of that, i could not do double redirecting. i said:

    Because as far as I know "table nat chain OUTPUT" cant be called twice

    it is wrong and "table nat chain OUTPUT" can be called twice. i dont know what exactly i did 2 months ago that thought "table nat chain OUTPUT" cant be called twice.

    this is the tables and chains order when using some services on loopback interface or not:

    Without having any services on loopback:

    Generated packets on local machine -> nat(OUTPUT) -> filter(OUTPUT) -> wlan(ethernet) interface

    With having some services on loopback:

    Generated packets on local machine -> nat(OUTPUT) -> filter(OUTPUT) -> loopback interface -> nat(OUTPUT) -> filter(OUTPUT) -> wlan(ethernet) interface

    these are my rules to solve the problem:

    iptables -t nat -A OUTPUT -p tcp -m tcp --dport 6666 -j REDIRECT --to-ports 6666
    iptables -t nat -A OUTPUT -p tcp -m owner --uid-owner bob -m tcp -j REDIRECT --to-ports 9040
    iptables -t nat -A OUTPUT -p udp -m owner --uid-owner bob -m udp --dport 53 -j REDIRECT --to-ports 53
    iptables -t nat -A OUTPUT -d "StunnelServerIp" -o wlan0 -p tcp -j REDIRECT --to-ports 9040
    iptables -t filter -A OUTPUT -p tcp -m owner --uid-owner bob -m tcp --dport 9040 -j ACCEPT
    iptables -t filter -A OUTPUT -p udp -m owner --uid-owner bob -m udp --dport 53 -j ACCEPT
    iptables -t filter -A OUTPUT -p tcp -m tcp --dport 6666 -j ACCEPT
    iptables -t filter -A OUTPUT -m owner --uid-owner bob -j DROP