| 1 |
#!/bin/sh |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
myhome="" |
|---|
| 56 |
bigbwhost="" |
|---|
| 57 |
bighostuser="" |
|---|
| 58 |
|
|---|
| 59 |
tuniphome=10.0.0.2 |
|---|
| 60 |
tuniplocal=10.0.0.3 |
|---|
| 61 |
tunipnet="10.0.0.0/8" |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
die() |
|---|
| 66 |
{ |
|---|
| 67 |
echo "$@" |
|---|
| 68 |
rm -f /tmp/prayishtar_default_gw |
|---|
| 69 |
exit 1 |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
if test "$1" = "clean" -o "$1" = "stop" |
|---|
| 73 |
then |
|---|
| 74 |
ip rule del lookup 213 |
|---|
| 75 |
iptables -D POSTROUTING -t nat -j MASQUERADE -o ! lo |
|---|
| 76 |
iptables -D OUTPUT -t mangle -p tcp --dport 22 -j MARK --set-mark 0x71 |
|---|
| 77 |
ip route del $tunipnet dev tun2 proto kernel scope link src $tuniplocal |
|---|
| 78 |
ip route del default |
|---|
| 79 |
ip route del default table 213 |
|---|
| 80 |
ip route replace $(< /tmp/prayishtar_default_gw) |
|---|
| 81 |
exit |
|---|
| 82 |
fi |
|---|
| 83 |
|
|---|
| 84 |
if ! test -z "$1" |
|---|
| 85 |
then |
|---|
| 86 |
myhome="$1" |
|---|
| 87 |
fi |
|---|
| 88 |
|
|---|
| 89 |
if ! test -z "$2" |
|---|
| 90 |
then |
|---|
| 91 |
bigbwhost="$2" |
|---|
| 92 |
fi |
|---|
| 93 |
if ! test -z "$3" |
|---|
| 94 |
then |
|---|
| 95 |
bighostuser="$3" |
|---|
| 96 |
fi |
|---|
| 97 |
|
|---|
| 98 |
if test $(id -u) != 0 |
|---|
| 99 |
then |
|---|
| 100 |
echo "Root privileges needed." |
|---|
| 101 |
echo "Get them." |
|---|
| 102 |
exit 1 |
|---|
| 103 |
fi |
|---|
| 104 |
|
|---|
| 105 |
echo "Using myhome=$myhome and bigbwhost=$bighostuser@$bigbwhost" |
|---|
| 106 |
if test -z "$myhome" -o -z "$bigbwhost" -o -z "$bighostuser" |
|---|
| 107 |
then |
|---|
| 108 |
echo "" |
|---|
| 109 |
echo "[!] Please configure this script ( \"$0\" )" |
|---|
| 110 |
exit 1 |
|---|
| 111 |
fi |
|---|
| 112 |
|
|---|
| 113 |
homeip=`host $myhome | cut -d ' ' -f 4` |
|---|
| 114 |
bigbwhostip=`host $bigbwhost | cut -d ' ' -f 4` |
|---|
| 115 |
myinsecuregw=`ip route | grep default | cut -d ' ' -f 3` |
|---|
| 116 |
|
|---|
| 117 |
echo "" |
|---|
| 118 |
|
|---|
| 119 |
echo "Opening ssh tunnel" |
|---|
| 120 |
modprobe tun |
|---|
| 121 |
remotecmd="modprobe tun; sleep 1; ifconfig tun2 $tuniphome" |
|---|
| 122 |
remotecmd="$remotecmd; iptables -A POSTROUTING -t nat -j MASQUERADE -o ! lo" |
|---|
| 123 |
remotecmd="$remotecmd; ip route replace $tunipnet dev tun2 proto kernel scope link src $tuniphome" |
|---|
| 124 |
ssh -fnw 2:2 $myhome "$remotecmd" || die "Could not set up the SSH vpn" |
|---|
| 125 |
sleep 1 |
|---|
| 126 |
ifconfig tun2 $tuniplocal |
|---|
| 127 |
|
|---|
| 128 |
echo "Setting routes" |
|---|
| 129 |
ip route replace $tunipnet dev tun2 proto kernel scope link src $tuniplocal |
|---|
| 130 |
ip route | grep default | line > /tmp/prayishtar_default_gw |
|---|
| 131 |
ip route del default |
|---|
| 132 |
ip route replace default via $tuniphome dev tun2 |
|---|
| 133 |
|
|---|
| 134 |
echo "Setting iptables" |
|---|
| 135 |
|
|---|
| 136 |
iptables -A POSTROUTING -t nat -j MASQUERADE -o ! lo |
|---|
| 137 |
iptables -A OUTPUT -t mangle -p tcp --dport 22 -j MARK --set-mark 0x71 |
|---|
| 138 |
ip rule add from all fwmark 0x71 lookup 213 |
|---|
| 139 |
ip route replace default via $myinsecuregw table 213 || die "Could not set default route" |
|---|
| 140 |
|
|---|
| 141 |
echo "Remember to set the localhost:8080 SOCKS proxy in your browser" |
|---|
| 142 |
echo "" |
|---|
| 143 |
echo "Creating SOCKS to $bighostuser@$bigbwhost" |
|---|
| 144 |
ssh -fnN $bigbwhostip -D 8080 -l $bighostuser || die "Could not set up the SOCKS proxy" |
|---|
| 145 |
|
|---|
| 146 |
echo "" |
|---|
| 147 |
echo "All done!" |
|---|