linux

IPTABLES TCP Proxy

Quick and easy way to make a TCP Proxy using IPTABLES. Found on the Internet:

#!/bin/bash
YourIP=192.168.1.100
YourPort=1234
TargetIP=192.168.1.200
TargetPort=2345

iptables -t nat -A PREROUTING --dst $YourIP -p tcp --dport $YourPort -j DNAT \
        --to-destination $TargetIP:$TargetPort
iptables -t nat -A POSTROUTING -p tcp --dst $TargetIP --dport $TargetPort -j SNAT \
        --to-source $YourIP
iptables -t nat -A OUTPUT --dst $YourIP -p tcp --dport $YourPort -j DNAT \
        --to-destination $TargetIP:$TargetPort

And, of course:

sysctl -w net.ipv4.ip_forward=1

I wanted to use balance but for whatever reason it is not available on Ubuntu 20.04. There is a simpleproxy package which I haven’t tried. And, don’t forget, there’s always socat.

Published:
comments powered by Disqus