-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwsshuttle
executable file
·241 lines (213 loc) · 6.33 KB
/
wsshuttle
1
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/bin/bash
usage(){
echo -n "${0##*/}"
echo -n ' [--delete] [--dry] [--help] [--upgrade] [--version]'
echo -n ' <sshuttle_args...>'
echo
}
#usage;exit
help(){
echo USAGE
echo -n ' '
usage
cat<<EOF
DESCRIPTION
It runs sshuttle from WSL2, and sets up the routing table of Windows.
Since it modifies the routing table of Windows,
the admin privilege of Windows is required.
(Open WSL2 terminal with "Run as Administrator")
OPTIONS
--delete: Delete the entries of routing table.
--dry: Just print commands to run, don't make any changes.
--help: Show this help.
--upgrade: Update this program.
--version: Show version.
EXAMPLES
wsshuttle -r ssh-server -x 157.0.0.0/8 0/0
Route all (0/0) packets through ssh-server except destination is
157.0.0.0/8. And it excludes 'IP address of ssh-server' too by
specifying -x 'IP address of ssh-server' automatically.
wsshuttle -r ssh-server -x 157.0.0.0/8 0/0 --delete
Deletes the entries of the routing table.
wsshuttle -r ssh-server -x 157.0.0.0/8 0/0 --dry
Dry-run, just print commands to run, doesn't make any changes.
EOF
}
#help;exit
show(){
for rxs in "$@"; do
eval echo '${'$rxs'[@]}' |
xargs -n1 echo $rxs: |
cat
done
exit 0
}
#r=aa; x=(bb cc); s=(dd ee); show r x s; exit
parse_args(){
args=()
for i in "$@"; do
if [[ $i = --delete ]]; then
IS_DELETE=1
elif [[ $i = --dry ]]; then
IS_DRY=1
elif [[ $i = --noresolve ]]; then
IS_NORESOLVE=1
else
args+=("$i")
fi
done
#show IS_DELETE IS_DRY IS_NORESOLVE args
IS_NORESOLVE=1
[[ $IS_NORESOLVE = 1 ]] && return
for((i = 1; i < ${#args[@]}; i++)); do
if [[ ${args[$i-1]} =~ ^-[^-]*r$ ]]; then # -r
ssh=$(ssh -v ${args[$i]} true 2>&1 | tr -d '\r' | tr -s ' ')
break
fi
done
#echo "$ssh";exit
[[ $i = ${#args[@]} ]] && return
while true; do
pc=$(
echo "$ssh" |
grep 'Executing proxy command:' |
cut -d' ' -f6- |
perl -pe 's/^(ssh.* -[^- ]*)W [^ ]+/\1v/' | # -W %h:%p -> -v
cat
)
#echo ,$pc,
[[ -z $pc ]] && break
ssh=$($pc true 2>&1 | tr -d '\r' | tr -s ' ') ||
{ echo error: $pc failed; exit 1; }
done
rhost=$(echo "$ssh" | awk -F'[][]' '/Authenticated/{print $2}')
rip=$(dig +short $rhost | head -n1)
#show rhost rip
}
set_xs(){
x=($rip); s=()
for((i = 1; i < ${#args[@]}; i++)); do
if [[ ${args[$i-1]} =~ ^-[^-]*x$ ]]; then # -x
x+=("${args[$i]}")
elif [[ ! ${args[$i-1]} =~ ^-[^-]*[elr] ]]; then # not -e -l -r
if [[ ${args[$i]} =~ ^[0-9] ]]; then
s+=("${args[$i]}")
fi
fi
done
#show x s # 8.8.8.8[/24]
f() {
for i in "$@"; do # 8.8.8.8[/24]
ipcalc "$i" 32 | # 8.8.8.8[/24] 32
awk '
$1=="Netmask:"{mask=$2}
$1=="Address:"{addr=$2}
$1=="Network:"{addr=substr($2, 0, index($2, "/") - 1)}
$0==""{exit}
END{print addr "/" mask}
' |
cat
done
}
x=( $(f "${x[@]}") )
s=( $(f "${s[@]}") )
#show x s # 8.8.8.0/255.255.255.0
s=($(
echo "${s[@]}" |
sed 's,0.0.0.0/0.0.0.0,0.0.0.0/128.0.0.0 128.0.0.0/128.0.0.0,g'
))
#show s
x=( $(echo "${x[@]}" | xargs -n1 | sort -u) )
s=( $(echo "${s[@]}" | xargs -n1 | sort -u) )
#show x s # uniq
#show x s
}
set_env(){
WSL2_IP=$(hostname -I | cut -d' ' -f1)
WIN_VIFIP=$(ip r s default | cut -d' ' -f3)
WIN_VIFID=$(arp.exe -a -N $WIN_VIFIP | grep -aoPm1 '0x[0-9a-f]+' |
awk '$0=strtonum($0)')
GW_IP=$(route.exe print -4 | awk '$1=="0.0.0.0"{print $3}')
WIN_IFIP=$(route.exe print -4 | awk '$1=="0.0.0.0"{print $4}')
WIN_IFID=$(arp.exe -a -N $WIN_IFIP | grep -aoPm1 '0x[0-9a-f]+' |
awk '$0=strtonum($0)')
#show WSL2_IP WIN_VIFID GW_IP WIN_IFID
}
route_add(){
for i in "${x[@]}"; do
addr=${i%%/*}
mask=${i##*/}
echo route.exe add $addr mask $mask $GW_IP metric 1 if $WIN_IFID
done
for i in "${s[@]}"; do
addr=${i%%/*}
mask=${i##*/}
echo route.exe add $addr mask $mask $WSL2_IP metric 1 if $WIN_VIFID
done
}
route_del(){
for i in "${x[@]}"; do
addr=${i%%/*}
mask=${i##*/}
echo route.exe delete $addr mask $mask
done
for i in "${s[@]}"; do
addr=${i%%/*}
mask=${i##*/}
echo route.exe delete $addr mask $mask
done
}
sshuttle_cmd(){
printf '%q ' sshuttle -l 0.0.0.0:0 "${args[@]}" ${rip:+-x $rip}
echo
}
main(){
if [[ "$*" =~ --help ]]; then
help
exit 0
elif [[ "$*" =~ --version ]]; then
echo 0.0.8
exit 0
elif [[ "$*" =~ --upgrade ]]; then
url=https://raw.githubusercontent.com/yabeenico/wsshuttle/main/wsshuttle
(echo wget $url -O "'$0'"; echo chmod +x "'$0'") |
([[ "$*" =~ --dry ]] && cat || bash)
exit 0
fi
set_env
parse_args "$@" # args, rip, IS_DELETE, IS_DRY
set_xs # x, s
if [[ $IS_DELETE = 1 ]]; then
if [[ $IS_DRY = 1 ]]; then
route_del | column -t
else
route_del | powershell.exe -NonInteractive -NoProfile -
fi
else
if [[ $IS_DRY = 1 ]]; then
(route_del; route_add) | column -t
sshuttle_cmd
route_del | column -t
else
route_del | powershell.exe -NonInteractive -NoProfile - &>/dev/null
route_add | powershell.exe -NonInteractive -NoProfile - |
grep -av OK
if [[ $? != 0 ]]; then # if route.exe add success
trap '
route_del |
powershell.exe -NonInteractive -NoProfile - &>/dev/null &
exit
' 2
trap '
route_del |
powershell.exe -NonInteractive -NoProfile - &>/dev/null &
exit 1
' 1 3 15
sshuttle_cmd
sshuttle_cmd | bash
kill $$
fi
fi
fi
}
main "$@"