Skip to content

Instantly share code, notes, and snippets.

@ChronoMonochrome
Last active March 7, 2023 16:00
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ChronoMonochrome/7ce4e0f4eba9c1ee7521774e7b7e504a to your computer and use it in GitHub Desktop.
Save ChronoMonochrome/7ce4e0f4eba9c1ee7521774e7b7e504a to your computer and use it in GitHub Desktop.
RedSocks proxy for Linux
#!/bin/bash
# Create new chain
sudo iptables -t nat -N REDSOCKS
# Ignore LANs and some other reserved addresses.
sudo iptables -t nat -A REDSOCKS -d 0.0.0.0/8 -j RETURN
sudo iptables -t nat -A REDSOCKS -d 10.0.0.0/8 -j RETURN
sudo iptables -t nat -A REDSOCKS -d 127.0.0.0/8 -j RETURN
sudo iptables -t nat -A REDSOCKS -d 169.254.0.0/16 -j RETURN
sudo iptables -t nat -A REDSOCKS -d 172.16.0.0/12 -j RETURN
sudo iptables -t nat -A REDSOCKS -d 192.168.0.0/16 -j RETURN
sudo iptables -t nat -A REDSOCKS -d 224.0.0.0/4 -j RETURN
sudo iptables -t nat -A REDSOCKS -d 240.0.0.0/4 -j RETURN
# Put your ssh server IP here
sudo iptables -t nat -A REDSOCKS -d x.x.x.x/4 -j RETURN
# Anything else should be redirected to port 31338
sudo iptables -t nat -A REDSOCKS -p tcp -j REDIRECT --to-ports 31338
# Any tcp connection made by `user' should be redirected, put your username here.
sudo iptables -t nat -A OUTPUT -p tcp -m owner --uid-owner user -j REDSOCKS
if ! test -f socks.conf ; then
cat << EOF > socks.conf
base{log_debug = on; log_info = on; log = "file:/tmp/reddi.log";
daemon = on; redirector = iptables;}
redsocks { local_ip = 127.0.0.1; local_port = 31338; ip = 127.0.0.1;
port = 31337; type = socks5; }
EOF
fi
./redsocks -c socks.conf
ssh -fND localhost:31337 user@server 2>/dev/null 1>2
sudo iptables -t nat -F
sudo iptables -t mangle -F
sudo iptables -F
sudo iptables -X
killall redsocks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment