-
Notifications
You must be signed in to change notification settings - Fork 15
/
start-mon.sh
executable file
·351 lines (307 loc) · 12 KB
/
start-mon.sh
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/bin/bash
SCRIPT_NAME="start-mon.sh"
SCRIPT_VERSION="20230305"
# Purpose: Start and configure monitor mode on the provided wifi interface
# Usage: $ sudo ./start-mon.sh [interface]
clear
# check to ensure sudo was used to start the script
if [ "$(id -u)" -ne 0 ]; then
echo "You must run this script with superuser (root) privileges."
echo "Try: \"sudo ./${SCRIPT_NAME}\""
exit 1
fi
# check to ensure iw is installed
if ! command -v iw >/dev/null 2>&1; then
echo "A required package is not installed."
echo "Please install the following package: iw"
echo "Once the package is installed, please run \"sudo ./${SCRIPT_NAME}\""
exit 1
fi
# check to ensure ip is installed
if ! command -v ip >/dev/null 2>&1; then
echo "A required package is not installed."
echo "Please install the following package: ip"
echo "Once the package is installed, please run \"sudo ./${SCRIPT_NAME}\""
exit 1
fi
# select option to set automatic (1) or manual (2) interface name capture mode
#
# option 1: if you only have one wlan interface (automatic detection)
#iface0=$(iw dev | grep 'Interface' | sed 's/Interface //' | sed -e 's/^[ \t]*//')
#
# option 2: if you have more than one wlan interface
iface0=${1}
# assign default channel
chan="6"
# set iface0 down
ip link set dev "$iface0" down
RESULT=$?
# if interface was successfully taken down, continue
if [ "$RESULT" = "0" ]; then
# disable interfering processes
PROCESSES="wpa_action\|wpa_supplicant\|wpa_cli\|dhclient\|ifplugd\|dhcdbd\|dhcpcd\|udhcpc\|NetworkManager\|knetworkmanager\|avahi-autoipd\|avahi-daemon\|wlassistant\|wifibox\|net_applet\|wicd-daemon\|wicd-client\|iwd"
# unset match
# match="$(ps -A -o comm= | grep ${PROCESSES} | grep -v grep | wc -l)"
# shellcheck disable=SC2009
badProcs=$(ps -A -o pid=PID -o comm=Name | grep "${PROCESSES}\|PID")
# shellcheck disable=SC2009
for pid in $(ps -A -o pid= -o comm= | grep ${PROCESSES} | awk '{print $1}'); do
command kill -19 "${pid}"
# (-19 = STOP)
done
clear
echo
echo ' The following processes have been stopped:'
echo
echo "${badProcs}"
echo
echo ' Note: The above processes can be returned'
echo ' to a normal state at the end of this script.'
echo
read -p " Press any key to continue... " -n 1 -r
# display interface settings
clear
echo
echo ' --------------------------------'
echo -e " ${SCRIPT_NAME} ${SCRIPT_VERSION}"
echo ' --------------------------------'
echo ' WiFi Interface:'
echo ' '"$iface0"
echo ' --------------------------------'
iface_name=$(iw dev "$iface0" info | grep 'Interface' | sed 's/Interface //' | sed -e 's/^[ \t]*//')
echo ' name - ' "$iface_name"
iface_type=$(iw dev "$iface0" info | grep 'type' | sed 's/type //' | sed -e 's/^[ \t]*//')
echo ' type - ' "$iface_type"
iface_state=$(ip addr show "$iface0" | grep 'state' | sed 's/.*state \([^ ]*\)[ ]*.*/\1/')
echo ' state - ' "$iface_state"
iface_addr=$(iw dev "$iface0" info | grep 'addr' | sed 's/addr //' | sed -e 's/^[ \t]*//')
echo ' addr - ' "$iface_addr"
echo ' --------------------------------'
echo
# set addr (has to be done before renaming the interface)
iface_addr_orig=$iface_addr
read -p " Do you want to set a new addr? [y/N] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
read -p " What addr do you want? ( e.g. 12:34:56:78:90:ab ) " -r iface_addr
re="^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$"
while [[ ! "$iface_addr" =~ $re ]]
do
read -p " Invalid addr format, try again: " -r iface_addr
done
ip link set dev "$iface0" address "$iface_addr"
# shellcheck disable=SC2181
while [[ $? -ne 0 ]]
do
read -p " Setting addr failed, try again: " -r iface_addr
while [[ ! "$iface_addr" =~ $re ]]
do
read -p " Invalid addr format, try again: " -r iface_addr
done
ip link set dev "$iface0" address "$iface_addr"
done
fi
# set monitor mode
# iw dev <devname> set monitor <flag>
# Valid monitor flags are:
# none: no special flags
# fcsfail: show frames with FCS errors
# control: show control frames
# otherbss: show frames from other BSSes
# cook: use cooked mode
# active: use active mode (ACK incoming unicast packets)
# mumimo-groupid <GROUP_ID>: use MUMIMO according to a group id
# mumimo-follow-mac <MAC_ADDRESS>: use MUMIMO according to a MAC address
iw dev "$iface0" set monitor none
# assign monitor mode interface name
# info: Realtek out-of-kernel drivers have problems when trying to rename
# interfaces on some systems.
# info: The above may be corrected by turning off the following:
# PREDICTABLE NETWORK INTERFACE NAMES
# more info: https://wiki.debian.org/NetworkInterfaceNames#initrd
# info: Realtek out-of-kernel drivers do not handle deleting or adding
# interface names. There is no virtual interface support.
# info: In-kernel drivers do not have the above problems.
#
iface0mon="$iface0"
if [ "$iface0mon" = "wlan0" ]; then
iface0mon="wlan0mon"
fi
if [ "$iface0mon" = "wlan1" ]; then
iface0mon="wlan1mon"
fi
ip link set dev "$iface0" name $iface0mon
# bring the interface up
ip link set dev "$iface0mon" up
# set channel to default
iw dev "$iface0mon" set channel "$chan"
# display interface settings
clear
echo
echo ' --------------------------------'
echo -e " ${SCRIPT_NAME} ${SCRIPT_VERSION}"
echo ' --------------------------------'
echo ' WiFi Interface:'
echo ' '"$iface0"
echo ' --------------------------------'
iface_name=$(iw dev "$iface0mon" info | grep 'Interface' | sed 's/Interface //' | sed -e 's/^[ \t]*//')
echo ' name - ' "$iface_name"
iface_type=$(iw dev "$iface0mon" info | grep 'type' | sed 's/type //' | sed -e 's/^[ \t]*//')
echo ' type - ' "$iface_type"
iface_state=$(ip addr show "$iface0mon" | grep 'state' | sed 's/.*state \([^ ]*\)[ ]*.*/\1/')
echo ' state - ' "$iface_state"
iface_addr=$(iw dev "$iface0mon" info | grep 'addr' | sed 's/addr //' | sed -e 's/^[ \t]*//')
echo ' addr - ' "$iface_addr"
iface_chan=$(iw dev "$iface0mon" info | grep 'channel' | sed 's/channel //' | sed -e 's/^[ \t]*//')
echo ' chan - ' "$iface_chan"
echo ' --------------------------------'
echo
# set channel
# Documentation:
# iw dev <devname> set channel <channel> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz]
# iw dev <devname> set freq <freq> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz]
# iw dev <devname> set freq <control freq> [5|10|20|40|80|80+80|160] [<center1_freq> [<center2_freq>]]
read -p " Do you want to set the channel? [y/N] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
read -p " What channel do you want to set? " -r chan
# Select one or modify as required:
iw dev "$iface0mon" set channel "$chan" HT20
# iw dev "$iface0mon" set channel "$chan" HT40-
# iw dev "$iface0mon" set channel "$chan" 80MHz
fi
# display interface settings
clear
echo
echo ' --------------------------------'
echo -e " ${SCRIPT_NAME} ${SCRIPT_VERSION}"
echo ' --------------------------------'
echo ' WiFi Interface:'
echo ' '"$iface0"
echo ' --------------------------------'
iface_name=$(iw dev "$iface0mon" info | grep 'Interface' | sed 's/Interface //' | sed -e 's/^[ \t]*//')
echo ' name - ' "$iface_name"
iface_type=$(iw dev "$iface0mon" info | grep 'type' | sed 's/type //' | sed -e 's/^[ \t]*//')
echo ' type - ' "$iface_type"
iface_state=$(ip addr show "$iface0mon" | grep 'state' | sed 's/.*state \([^ ]*\)[ ]*.*/\1/')
echo ' state - ' "$iface_state"
iface_addr=$(iw dev "$iface0mon" info | grep 'addr' | sed 's/addr //' | sed -e 's/^[ \t]*//')
echo ' addr - ' "$iface_addr"
iface_chan=$(iw dev "$iface0mon" info | grep 'channel' | sed 's/channel //' | sed -e 's/^[ \t]*//')
echo ' chan - ' "$iface_chan"
iface_txpw=$(iw dev "$iface0mon" info | grep 'txpower' | sed 's/txpower //' | sed -e 's/^[ \t]*//')
echo ' txpw - ' "$iface_txpw"
echo ' --------------------------------'
echo
# set txpw
read -p " Do you want to set the txpower? [y/N] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo " Notes: Some USB WiFi adapters will not allow the txpw to be set."
echo " Be careful to not increase power to the point that you overheat the adapter."
echo
read -p " What txpw setting do you want to attempt to set? ( e.g. 2300 = 23 dBm ) " -r iface_txpw
iw dev "$iface0mon" set txpower fixed "$iface_txpw"
fi
# display interface settings
clear
echo
echo ' --------------------------------'
echo -e " ${SCRIPT_NAME} ${SCRIPT_VERSION}"
echo ' --------------------------------'
echo ' WiFi Interface:'
echo ' '"$iface0"
echo ' --------------------------------'
iface_name=$(iw dev "$iface0mon" info | grep 'Interface' | sed 's/Interface //' | sed -e 's/^[ \t]*//')
echo ' name - ' "$iface_name"
iface_type=$(iw dev "$iface0mon" info | grep 'type' | sed 's/type //' | sed -e 's/^[ \t]*//')
echo ' type - ' "$iface_type"
iface_state=$(ip addr show "$iface0mon" | grep 'state' | sed 's/.*state \([^ ]*\)[ ]*.*/\1/')
echo ' state - ' "$iface_state"
iface_addr=$(iw dev "$iface0mon" info | grep 'addr' | sed 's/addr //' | sed -e 's/^[ \t]*//')
echo ' addr - ' "$iface_addr"
iface_chan=$(iw dev "$iface0mon" info | grep 'channel' | sed 's/channel //' | sed -e 's/^[ \t]*//')
echo ' chan - ' "$iface_chan"
iface_txpw=$(iw dev "$iface0mon" info | grep 'txpower' | sed 's/txpower //' | sed -e 's/^[ \t]*//')
echo ' txpw - ' "$iface_txpw"
echo ' --------------------------------'
echo
echo ' DORMANT = up but inactive.'
echo
# interface ready
echo " Ready for Monitor Mode use."
echo
echo ' You can place this terminal in'
echo ' the background while you run any'
echo ' applications you wish to run.'
echo
read -p " Press any key to exit... " -n 1 -r
echo
# return the adapter to original settings or not
read -p " Do you want to return the adapter to original settings? [Y/n] " -n 1 -r
if [[ $REPLY =~ ^[Nn]$ ]]
then
# display interface settings
clear
echo
echo ' --------------------------------'
echo -e " ${SCRIPT_NAME} ${SCRIPT_VERSION}"
echo ' --------------------------------'
echo ' WiFi Interface:'
echo ' '"$iface0"
echo ' --------------------------------'
iface_name=$(iw dev "$iface0mon" info | grep 'Interface' | sed 's/Interface //' | sed -e 's/^[ \t]*//')
echo ' name - ' "$iface_name"
iface_type=$(iw dev "$iface0mon" info | grep 'type' | sed 's/type //' | sed -e 's/^[ \t]*//')
echo ' type - ' "$iface_type"
iface_state=$(ip addr show "$iface0mon" | grep 'state' | sed 's/.*state \([^ ]*\)[ ]*.*/\1/')
echo ' state - ' "$iface_state"
iface_addr=$(iw dev "$iface0mon" info | grep 'addr' | sed 's/addr //' | sed -e 's/^[ \t]*//')
echo ' addr - ' "$iface_addr"
echo ' --------------------------------'
echo
exit 0
else
ip link set dev "$iface0mon" down
ip link set dev "$iface0mon" address "$iface_addr_orig"
iw "$iface0mon" set type managed
ip link set dev "$iface0mon" name "$iface0"
ip link set dev "$iface0" up
# enable interfering processes
# shellcheck disable=SC2009
for pid in $(ps -A -o pid= -o comm= | grep ${PROCESSES} | awk '{print $1}'); do
command kill -18 "${pid}" # -18 = CONT
done
# display interface settings
clear
echo
echo ' --------------------------------'
echo -e " ${SCRIPT_NAME} ${SCRIPT_VERSION}"
echo ' --------------------------------'
echo ' WiFi Interface:'
echo ' '"$iface0"
echo ' --------------------------------'
iface_name=$(iw dev "$iface0" info | grep 'Interface' | sed 's/Interface //' | sed -e 's/^[ \t]*//')
echo ' name - ' "$iface_name"
iface_type=$(iw dev "$iface0" info | grep 'type' | sed 's/type //' | sed -e 's/^[ \t]*//')
echo ' type - ' "$iface_type"
iface_state=$(ip addr show "$iface0" | grep 'state' | sed 's/.*state \([^ ]*\)[ ]*.*/\1/')
echo ' state - ' "$iface_state"
iface_addr=$(iw dev "$iface0" info | grep 'addr' | sed 's/addr //' | sed -e 's/^[ \t]*//')
echo ' addr - ' "$iface_addr"
echo ' --------------------------------'
echo
exit 0
fi
else
clear
echo
echo " ERROR: Please provide an existing interface as parameter!"
echo -e " Usage: $ sudo ./$SCRIPT_NAME [interface]"
echo " Tip: $ iw dev"
echo
exit 1
fi