-
Notifications
You must be signed in to change notification settings - Fork 4
/
osx-randmac.sh
executable file
·340 lines (249 loc) · 7.04 KB
/
osx-randmac.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
#! /bin/ksh
#
# Setup OS X with a random mac address for WiFi or a specific
# interface. The MAC address on the specified iface is randomized
# every day (24 hours). This script uses a systemwide LaunchAgent to
# trigger the periodic MAC refresh.
#
# Author: Sudhi Herle <sudhi-at-herle-net>
# (c) Sudhi Herle 2013-2016
# License: GPLv2
#
# Order of doing things
# - disassociate from any network
# - update airport interface with random mac address
# - restart network
# name for launchd
FNAME=net.spoof.mac
airport=/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport
net=/usr/sbin/networksetup
ipconfig=/usr/sbin/ipconfig
Z=`basename $0`
DATE=/bin/date
STAT=/usr/bin/stat
TOUCH=/usr/bin/touch
PROG=$0
Me=`id -u`
# Debug hook
#e=echo
Installdir=/Library/LaunchDaemons
Bindir=/usr/local/bin
PATH=/sbin:/usr/sbin:/bin:/usr/bin:$PATH
export PATH
function die {
echo "$Z: $@" 1>&2
exit 1
}
if [ $Me -ne 0 ]; then
echo "$Z: Need root privilege to run; switching to dry-run mode .."
Installdir=/tmp/LaunchAgents
Bindir=/tmp/bin
e=echo
mkdir -p $Installdir $Bindir
fi
#set -x
# First dump existing config to stderr
#ifconfig -a 1>&2
function usage {
cat 1>&2 <<EOF1
$Z - Randomize MAC address of OS X WiFi (or any given interface).
Usage: $Z start|update|stop|restart|install [IFACE]
If no interface is specified, the script automatically detects and
uses the WiFi interface. Description of actions:
start, update: Update MAC Address
stop: No Op
restart: No op
install: Creates a launchd file in /Library/LaunchDaemons
EOF1
exit 1
}
uname=`uname`
case $uname in
Darwin);;
*) die "This script is meant for Mac OS X only."
;;
esac
action=$1
test -n "$action" || usage
shift
function writelog {
#$e logger -p "daemon.notice" -t "${FNAME}" "$@"
echo "`date`: $@" >> /tmp/mac.log
}
function grok_wifi_darwin {
typeset iface=$1
if [ -n "$iface" ]; then
n=$(ifconfig $iface 2>/dev/null | wc -l)
test $n -gt 0 || die "$iface is not a network interface?"
else
# grok the wifi interface
# Run scutil to get interface state which has AirPort in it
typeset xx=`echo list | scutil | grep State: | grep -i AirPort | cut -d= -f2`
if [ -z "$xx" ]; then
echo list | scutil 1>&2
die "Whoa. Can't find any WiFi interface!"
fi
typeset dx=`dirname $xx`
iface=`basename $dx`
test -n "$iface" || die "Can't grok WiFi Interface. Are you connected via WiFi?"
fi
# Now, lets make sure this is really a wireless interface
xx=`$net -getairportnetwork $iface`
test $? -eq 0 || die "$iface doesn't look like a WiFi interface.."
echo $iface
}
# Ugly hack to wait for an interface to come up
# The problem seems to have started with Yosemite!
function wait_wifi_darwin {
typeset max=10
typeset i=0
typeset iface=
while [ $i -lt 10 ]; do
i=$(( i + 1 ))
# Run scutil to get interface state which has AirPort in it
typeset xx=`echo list | scutil | grep State: | grep -i AirPort | cut -d= -f2`
if [ -z "$xx" ]; then
sleep 2
continue
fi
typeset dx=`dirname $xx`
iface=`basename $dx`
if [ -z "$iface" ]; then
sleep 2
continue
fi
# Now, lets make sure this is really a wireless interface
xx=`$net -getairportnetwork $iface`
test $? -eq 0 || die "$iface doesn't look like a WiFi interface.."
echo $iface
return 0
done
die "Can't grok WiFi interface after 10 tries!"
}
# Verify that a given interface is really a wifi interface.
function verify_wifi_Darwin {
typeset iface=$1
typeset x=`echo list | scutil | grep "State:/Network/Interface/$iface/AirPort"`
test -n "$x" || die "$iface is not a WiFi interface"
$e ifconfig $iface down
return 0
}
# Generate a random mac address
function _randmac {
# Generate random bytes
typeset randstr=$(dd if=/dev/urandom bs=4 count=1 2>/dev/null| od -t xC)
# These are VMware, Xen and Parallels OUIs
typeset -a vendors=("00:05:69" "00:0c:29" "00:1c:14" "00:50:56" \
"00:1c:42" "00:16:3e" "00:bb:3a" "e0:cb:1d")
typeset -a rand=($randstr)
# Number of vendors (length of array)
typeset n=${#vendors[@]}
# rand[0] and rand[5] are offsets; we can ignore them.
typeset a1=${rand[1]}
typeset a2=${rand[2]}
typeset a3=${rand[3]}
typeset a4=$(( 0 + 0x${rand[4]} )) # We want this to be an integer
# pick a random prefix from the list
typeset pref=${vendors[$a4 % $n]}
echo "$pref:$a1:$a2:$a3"
}
function update_mac {
typeset iface=$1
typeset mac=$2
# Updating MAC requires us to turn on the WiFi iface
# First disassociate from any connected WiFi
#$e $ipconfig set $iface NONE
$e $airport -z || return $?
$e ifconfig $iface down
#$e $net -setairportpower $iface off; sleep 2;
$e $net -setairportpower $iface on
$e ifconfig $iface ether $mac up || return $?
$e $net -detectnewhardware
#writelog "$iface: MAC updated to $mac"
writelog "$iface: $mac"
return 0
}
# Installs a startup service
function install_service {
typeset iface=$1
typeset fname=${2}.$iface
typeset bn=`basename $Z`
typeset prog=$Bindir/$bn
typeset file=$Installdir/${fname}.plist
cat > $file <<EOF2
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>$fname</string>
<key>Disabled</key>
<false/>
<key>UserName</key>
<string>root</string>
<key>GroupName</key>
<string>wheel</string>
<key>KeepAlive</key>
<false/>
<key>Debug</key>
<false/>
<key>RunAtLoad</key>
<true/>
<key>AbandonProcessGroup</key>
<true/>
<!-- Randomize the MAC at 0600 every day -->
<key>StartCalendarInterval</key>
<dict>
<key>Minute</key>
<integer>0</integer>
<key>Hour</key>
<integer>6</integer>
</dict>
<key>ProgramArguments</key>
<array>
<string>$prog</string>
<string>update</string>
<string>$iface</string>
</array>
</dict>
</plist>
EOF2
test -d $Bindir || $e mkdir -p $Bindir
$e cp $PROG $prog
$e chmod a+rx,og-w $prog
$e launchctl load $file
$e launchctl enable system/$fname
echo "Installed to $file and $prog .."
return 0
}
# -- start of main() --
iface=$1
if [ -z "$iface" ]; then
iface=`wait_wifi_darwin`
test -n "$iface" || die "Can't figure out the WiFi interface. Aborting..."
else
verify_wifi_Darwin $iface
test $? -eq 0 || exit 1
fi
mac=$2
if [ -z "$mac" ]; then
mac=$(_randmac)
fi
r=0
case $action in
start|update)
update_mac $iface $mac
r=$?
;;
install)
install_service ${iface} ${FNAME}
r=$?
;;
stop|restart)
;;
help|*)
usage
;;
esac
exit $r
# EOF