-
Notifications
You must be signed in to change notification settings - Fork 1
/
wireless_monitor
281 lines (245 loc) · 6.26 KB
/
wireless_monitor
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
#!/usr/bin/env bash
################################################################################
#
# wifi-monitor
# This is part of the con-test framework
#
# Copyright (C) 2019 Stefan Venz
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
################################################################################
source ./commons
LOG_PATH="con-test_logs"
SUDO=''
# set interface to monitor mode to collect traffic with tcpdump
#
# Aguments:
# interface - interface to use for monitor mode
#
set_interface_to_monitor()
{
interface=$1
ret=$SUCCESS
$SUDO ifconfig $interface down
$SUDO ifconfig $interface mode Monitor
$SUDO ifconfig $interface up
}
stop_tcpdump()
{
side=$1
auth=$(check_auth $side)
if (( $side == $CLIENT )); then
ip=$CLIENT_IP
user=$CLIENT_USER
passwd=$CLIENT_PASSWORD
cert=$CLIENT_CERTIFICATE
else
ip=$SERVER_IP
user=$SERVER_USER
passwd=$SERVER_PASSWORD
cert=$SERVER_CERTIFICATE
fi
if [[ "$auth" == "$PASSWD" ]]; then
tcpdump_pid=$(sshpass -p $passwd \
ssh ${user}@${ip} \
'ps' | awk '/[t]cpdump/{ print $1 }')
if [ ! -z $tcpdump_pid ]; then
ret=$(sshpass -p $passwd \
ssh ${user}@${ip} \
"kill $tcpdump_pid")
check_ret_val $? "Could not kill tcpdump on $ip: $ret"
fi
else
if [ ! -z $tcpdump_pid ]; then
tcpdump_pid=$(ssh -i $cert ${user}@${ip} \
'ps' | awk '/[t]cpdump/{ print $1 }')
ret=$(ssh -i $cert ${user}@${ip} \
"kill $tcpdump_pid")
check_ret_val $? "Could not kill tcpdump on $ip: $ret"
fi
fi
}
stop_monitoring()
{
while sleep 1; do
if [ -f '/tmp/stop_mon' ]; then
stop_tcpdump $CLIENT
stop_tcpdump $SERVER
exit $SUCCESS
fi
done
}
check_monitor()
{
interface=$1
passwd=$2
cert=$3
user=$4
ip=$5
auth=$6
if (( $auth == $PASSWD )); then
if [[ "$(sshpass -p "$passwd" ssh ${user}@${ip} \
iw dev | awk "/${interface}/{nr[NR+5]}; NR in nr" | \
awk '{print $2}')" == "monitor" ]]; then
ret=$SUCCESS
else
printf "${WARNING} $interface is not in monitor mode\n"
ret=$FAILURE
fi
else
if [[ "$(ssh -i ${cert} ${user}@${ip} \
iw dev | awk "/${interface}/{nr[NR+5]}; NR in nr" | \
awk '{print $2}')" == "monitor" ]]; then
ret=$SUCCESS
else
printf "${WARNING} $interface is not in monitor mode\n"
ret=$FAILURE
fi
fi
return $ret
}
# start tcp dump
#
# Arguemnts:
# interface - use this interface for tcpdump
# time - amount of seconds tcpdump runs
# side - Client or server side to start tcpdump
#
start_tcpdump()
{
interface=$1
time=$2
side=$3
auth=$(check_auth $side)
if (( $side == $CLIENT )); then
file_name="client_con-test-tcpdump-$(date +%F-%H-%M-%S).pcap"
passwd=$CLIENT_PASSWORD
cert=$CLIENT_CERTIFICATE
ip=$CLIENT_IP
user=$CLIENT_USER
else
file_name="server_con-test-tcpdump-$(date +%F-%H-%M-%S).pcap"
passwd=$SERVER_PASSWORD
cert=$SERVER_CERTIFICATE
ip=$SERVER_IP
user=$SERVER_USER
fi
if (( $auth == $PASSWD )); then
# check_monitor $interface $passwd $cert $user $ip $auth
check_ret_val $? "Please set the interface to monitor mode"
ret=$(nohup sshpass -p "$passwd" ssh ${user}@${ip} \
"tcpdump \
-i $interface -s 256\
-U -w -" > ${LOG_PATH}/${file_name} &)
check_ret_val $? "something went wrong starting tcpdump on $ip"
else
# check_monitor $interface $passwd $cert $user $ip $auth
ret=$(nohup ssh -i ${cert} ${user}@${ip} \
"tcpdump \
-i $interface -s 256\
-U -w -" > ${LOG_PATH}/${file_name} &)
check_ret_val $? "something went wrong starting tcpdump on $ip"
fi
echo $ret
}
# print information about this script
#
call_help()
{
printf "con-test help:\n\n"
printf "\t -c, --config:\t provide path to conn-test.conf, default ./conn-test.conf \n\n"
printf "\t -h, --help:\t call this overview\n\n"
printf "\t -o, --output\t path to log file to store output, default ./conn-test.log \n\n"
printf "\t -t, --time\t time to run tcpdump\n"
printf "\t -V, --version\t print con-test version\n"
}
# main function
#
# Arguments:
# args - command line arguments
#
main()
{
args=$@
time=0
interface=''
test_get_opt
options=c:ho:t:V
loptions=config:,help,output:,time:,version
config_path="con-test.conf"
! parsed=$(getopt --options=$options --longoptions=$loptions --name "$0" -- $args)
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
exit $FAILURE
fi
eval set -- "$parsed"
while true; do
case "$1" in
-c | --config)
config_path=$2
shift 2
;;
-h | --help)
call_help
exit $SUCCESS
;;
-o | --output)
LOG_PATH=$2
shift 2
;;
-t | --time)
TIME=$2
shift 2
;;
-V | --version)
print_version
exit $SUCCESS
;;
--)
shift
break
;;
*)
break
;;
esac
done
printf "${INFO} Starting wireless monitor\n"
printf "\t\tUsing config ${config_path}\n"
printf "\t\tUsing logpath ${LOG_PATH}\n"
export $LOG_PATH
if [ ! -f "${config_path}" ]; then
printf "${ERROR} ${config_path} does not exist or can not be accessed"
exit $FAILURE
fi
source $config_path
stop_tcpdump $CLIENT
stop_tcpdump $SERVER
if [ -f '/tmp/stop_mon' ]; then
rm -f /tmp/stop_mon
fi
check_dependecies
mkdir -p $LOG_PATH
# check_monitor $interface
printf "${INFO} starting monitoring $CLIENT_INTERFACE on $CLIENT_IP\n"
start_tcpdump $CLIENT_INTERFACE $time $CLIENT
check_ret_val $? "Failed to start tcpdump on $CLIENT_IP $CLIENT_INTERFACE"
printf "${INFO} starting monitoring $SERVER_INTERFACE on $SERVER_IP\n"
start_tcpdump $SERVER_INTERFACE $time $SERVER
check_ret_val $? "Failed to start tcpdump on $SERVER_IP $SERVER_INTERFACE"
stop_monitoring
printf "${INFO} stopped monitoring on $CLIENT_IP and $SERVER_IP"
}
main $@