I have two machines: machine A and machine B. Machine A routes all the traffic through the machine B. On machine A I set these iptables rules:
iptables -t nat -A PREROUTING -i wlan0 -j DNAT --to-destination 172.16.250.128
iptables -t nat -A POSTROUTING -o tun0 -j MASQUERADE
Where tun0 is the interface which contains the IP address 172.16.250.128 in its ip range.
Now all the packets received on machine A is routed to the machine B, without changing ports.
On machine B I setup a transparent proxy (mitmproxy, burpsuite, etc) on port 8080 and this iptables rule:
iptables -t nat -A PREROUTING -i ens33 -p tcp -j REDIRECT --to-ports 8080
Where ens33 (ip 172.16.250.128) is the interface connected to the interface tun0 on machine A.
The rule above redirects all tcp packets from that interface to port 8080 - the port on which the transparent proxy listens to.
All works great, all TCP packets is routed through the transparent proxy.
But I'm not sure about how the transparent proxy knows the original destination of the packet (both address and port)?
I found this question: [link][1]
It says that iptables saves the original destination before the destination is modified.
Now I understand how the transparent proxy knows the original port.
But what about the destination address? Since the destination address is modified on the machine A, I think machine B and its internals should not know about the original destination address since the machine B didn't modified it, right? But somehow it's still able to somehow get the original destination address of the packet sent to wlan0 interface that is routed to the transparent proxy. I know it because obviously the transparent proxy routes packets to the original destination correctly (the destination on the Internet).
How the machine B knows the original destination of the DNATed (modified destination) packet? Does the original destination sent over the network as well? I didn't see it in wireshark. [1]: How does a transparent SOCKS proxy know which destination IP to use?