|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Setup iptables for IP relay by creating an interface configured |
| 4 | +# to be the destination of TPROXY rules. |
| 5 | + |
| 6 | +if [ "$EUID" -ne 0 ] |
| 7 | + then echo "Please run as root" |
| 8 | + exit |
| 9 | +fi |
| 10 | + |
| 11 | +if [ "$1" != "setup" ] && [ "$1" != "unsetup" ]; then |
| 12 | + echo -e "Usage: ./vethrelay <setup/unsetup>\n" |
| 13 | + exit 1 |
| 14 | +fi |
| 15 | + |
| 16 | +IFACE="vethrelay" |
| 17 | +IP="2.2.2.2" |
| 18 | + |
| 19 | +# Linux doc about TPROXY and example regarding this: |
| 20 | +# https://www.kernel.org/doc/Documentation/networking/tproxy.txt |
| 21 | +# https://powerdns.org/tproxydoc/tproxy.md.html |
| 22 | + |
| 23 | +function checkSetup() { |
| 24 | + iptables -t mangle -n --list "DIVERT" >/dev/null 2>&1 |
| 25 | + return $? |
| 26 | +} |
| 27 | + |
| 28 | +if [ "$1" == "setup" ]; then |
| 29 | + # Add "DIVERT" chain if it doesn't exist |
| 30 | + checkSetup |
| 31 | + if [ $? -eq 0 ]; then |
| 32 | + echo "vethrelay already setup !" |
| 33 | + exit 1 |
| 34 | + fi |
| 35 | + # Create an interface tcpreplay dedicated to relay |
| 36 | + ip link add dev $IFACE type dummy |
| 37 | + sysctl net.ipv6.conf.$IFACE.disable_ipv6=1 >/dev/null |
| 38 | + ip link set dev $IFACE up |
| 39 | + ip addr add dev $IFACE $IP/32 |
| 40 | + # Create mangle "DIVERT" chain as an optimisation. -m socket matches |
| 41 | + # packets from already established sockets. Those are marked as 1 then |
| 42 | + # accepted directly. |
| 43 | + iptables -t mangle -N DIVERT |
| 44 | + iptables -t mangle -A PREROUTING -p tcp -m socket -j DIVERT |
| 45 | + iptables -t mangle -A DIVERT -j MARK --set-mark 1 |
| 46 | + iptables -t mangle -A DIVERT -j ACCEPT |
| 47 | + # Packets marked with 1 are routed through table 100 instead of the |
| 48 | + # default routing table |
| 49 | + ip rule add fwmark 1 lookup 100 |
| 50 | + # In routing table 100, all IPs are local to 'vethrelay' |
| 51 | + ip route add local 0.0.0.0/0 dev $IFACE table 100 |
| 52 | + echo -e "\x1b[32mInterface $IFACE is now setup with IPv4: $IP !\x1b[0m\n" |
| 53 | + echo -e "Add listening rules as follow:\n" |
| 54 | + echo "# TPROXY incoming TCP packets on port 80 to $IFACE on port 8080" |
| 55 | + echo "iptables -t mangle -A PREROUTING -p tcp --dport 80 -j TPROXY --tproxy-mark 0x1/0x1 --on-port 8080 --on-ip $IP" |
| 56 | + echo |
| 57 | + echo "# Listen on wlp4s0 for incoming packets on port 80 (on the interface where it really comes from)" |
| 58 | + echo "iptables -A INPUT -i wlp4s0 -p tcp --dport 80 -j ACCEPT" |
| 59 | +elif [ "$1" == "unsetup" ]; then |
| 60 | + checkSetup |
| 61 | + if [ $? -ne 0 ]; then |
| 62 | + echo "vethrelay not setup !" |
| 63 | + exit 1 |
| 64 | + fi |
| 65 | + # Remove all setup rules |
| 66 | + sudo ip rule del fwmark 1 lookup 100 |
| 67 | + sudo ip route del local 0.0.0.0/0 dev $IFACE table 100 |
| 68 | + sudo iptables -t mangle -D DIVERT -j ACCEPT |
| 69 | + sudo iptables -t mangle -D DIVERT -j MARK --set-mark 1 |
| 70 | + sudo iptables -t mangle -D PREROUTING -p tcp -m socket -j DIVERT |
| 71 | + sudo iptables -t mangle -X DIVERT |
| 72 | + sudo ip link del dev $IFACE |
| 73 | + echo -e "\x1b[32mInterface $IFACE unsetup !\x1b[0m" |
| 74 | +fi |
0 commit comments