forked from feross/spoof
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·497 lines (437 loc) · 13.5 KB
/
index.js
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
var cp = require('child_process')
var quote = require('shell-quote').quote
var zeroFill = require('zero-fill')
var Winreg = require('winreg')
if (!cp.execSync) {
console.error('Error: Missing child_process.execSync. Please use node 0.12 or iojs.')
process.exit(1)
}
// Path to Airport binary on 10.7, 10.8, and 10.9 (might be different on older OS X)
var PATH_TO_AIRPORT = '/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport'
// Windows registry key for interface MAC. Checked on Windows 7
var WIN_REGISTRY_PATH = '\\SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}'
// Regex to validate a MAC address
// Example: 00-00-00-00-00-00 or 00:00:00:00:00:00 or 000000000000
var MAC_ADDRESS_RE = /([0-9A-F]{1,2})[:-]?([0-9A-F]{1,2})[:-]?([0-9A-F]{1,2})[:-]?([0-9A-F]{1,2})[:-]?([0-9A-F]{1,2})[:-]?([0-9A-F]{1,2})/i
// Regex to validate a MAC address in cisco-style
// Example: 0123.4567.89ab
var CISCO_MAC_ADDRESS_RE = /([0-9A-F]{0,4})\.([0-9A-F]{0,4})\.([0-9A-F]{0,4})/i
// The possible port names for wireless devices as returned by networksetup.
var WIRELESS_PORT_NAMES = ['wi-fi', 'airport']
exports.WIRELESS_PORT_NAMES = WIRELESS_PORT_NAMES
/**
* Returns the list of interfaces found on this machine as reported by the
* `networksetup` command.
* @param {Array.<string>|null} targets
* @return {Array.<Object>)}
*/
exports.findInterfaces = function (targets) {
targets = targets || []
targets = targets.map(function (target) {
return target.toLowerCase()
})
var output, interfaces, details, result, i, port, device, address, it, j, target, lines
if (process.platform === 'darwin') {
// Parse the output of `networksetup -listallhardwareports` which gives
// us 3 fields per port:
// - the port name,
// - the device associated with this port, if any,
// - the MAC address, if any, otherwise 'N/A'
try {
output = cp.execSync('networksetup -listallhardwareports').toString()
} catch (err) {
throw err
}
details = []
while (true) {
result = /(?:Hardware Port|Device|Ethernet Address): (.+)/.exec(output)
if (!result || !result[1]) {
break
}
details.push(result[1])
output = output.slice(result.index + result[1].length)
}
interfaces = [] // to return
// Split the results into chunks of 3 (for our three fields) and yield
// those that match `targets`.
for (i = 0; i < details.length; i += 3) {
port = details[i]
device = details[i + 1]
address = details[i + 2]
address = MAC_ADDRESS_RE.exec(address.toUpperCase())
if (address) {
address = exports.normalize(address[0])
}
it = {
address: address,
currentAddress: exports.getInterfaceMAC(device),
device: device,
port: port
}
if (targets.length === 0) {
// Not trying to match anything in particular, return everything.
interfaces.push(it)
continue
}
for (j = 0; j < targets.length; j++) {
target = targets[j]
if (target === port.toLowerCase() || target === device.toLowerCase()) {
interfaces.push(it)
break
}
}
}
} else if (process.platform === 'linux') {
// Parse the output of `ifconfig` which gives us:
// - the adapter description
// - the adapter name/device associated with this, if any,
// - the MAC address, if any
try {
output = cp.execSync('ifconfig', { stdio: 'pipe' }).toString()
} catch (err) {
throw err
}
details = []
while (true) {
result = /(.*?)HWaddr(.*)/mi.exec(output)
if (!result || !result[1] || !result[2]) {
break
}
details.push(result[1], result[2])
output = output.slice(result.index + result[0].length)
}
interfaces = []
for (i = 0; i < details.length; i += 2) {
var s = details[i].split(':')
if (s.length >= 2) {
device = s[0].split(' ')[0]
port = s[1].trim()
}
address = details[i + 1].trim()
if (address) {
address = exports.normalize(address)
}
it = {
address: address,
currentAddress: exports.getInterfaceMAC(device),
device: device,
port: port
}
if (targets.length === 0) {
// Not trying to match anything in particular, return everything.
interfaces.push(it)
continue
}
for (j = 0; j < targets.length; j++) {
target = targets[j]
if (target === port.toLowerCase() || target === device.toLowerCase()) {
interfaces.push(it)
break
}
}
}
} else if (process.platform === 'win32') {
try {
output = cp.execSync('ipconfig /all', { stdio: 'pipe' }).toString()
} catch (err) {
throw err
}
details = []
interfaces = []
lines = output.split('\n')
it = false
for (i = 0; i < lines.length; i++) {
// Check if new device
if (lines[i].substr(0, 1).match(/[A-Z]/)) {
if (it) {
if (targets.length === 0) {
// Not trying to match anything in particular, return everything.
interfaces.push(it)
} else {
for (j = 0; j < targets.length; j++) {
target = targets[j]
if (target === it.port.toLowerCase() || target === it.device.toLowerCase()) {
interfaces.push(it)
break
}
}
}
}
it = {
port: '',
device: ''
}
result = /adapter (.+?):/.exec(lines[ i ])
if (!result) {
continue
}
it.device = result[1]
}
if (!it) {
continue
}
// Try to find address
result = /Physical Address.+?:(.*)/mi.exec(lines[i])
if (result) {
it.address = exports.normalize(result[1].trim())
it.currentAddress = it.address
continue
}
// Try to find description
result = /description.+?:(.*)/mi.exec(lines[i])
if (result) {
it.description = result[1].trim()
continue
}
}
}
return interfaces
}
/**
* Returns the first interface which matches `target`
* @param {string} target
* @return {Object}
*/
exports.findInterface = function (target) {
var interfaces = exports.findInterfaces([target])
return interfaces && interfaces[0]
}
/**
* Returns currently-set MAC address of given interface. This is distinct from the
* interface's hardware MAC address.
* @return {string}
*/
exports.getInterfaceMAC = function (device) {
var output, address
if (process.platform === 'darwin' || process.platform === 'linux') {
try {
output = cp.execSync(quote(['ifconfig', device]), { stdio: 'pipe' }).toString()
} catch (err) {
return null
}
address = MAC_ADDRESS_RE.exec(output)
return address && exports.normalize(address[0])
} else if (process.platform === 'win32') {
console.error('No windows support for this method yet - PR welcome!')
}
}
/**
* Sets the mac address for given `device` to `mac`.
*
* Device varies by platform:
* OS X, Linux: this is the interface name in ifconfig
* Windows: this is the network adapter name in ipconfig
*
* @param {string} device
* @param {string} mac
* @param {string=} port
*/
exports.setInterfaceMAC = function (device, mac, port) {
if (!MAC_ADDRESS_RE.exec(mac)) {
throw new Error(mac + ' is not a valid MAC address')
}
if (process.platform === 'darwin') {
if (port && port.toLowerCase().indexOf(WIRELESS_PORT_NAMES) >= 0) {
// Turn on the device, assuming it's an airport device.
try {
cp.execSync(quote(['networksetup', '-setairportpower', device, 'on']))
} catch (err) {
throw new Error('Unable to power on wifi device')
}
}
// For some reason this seems to be required even when changing a non-airport device.
try {
cp.execSync(quote([PATH_TO_AIRPORT, '-z']))
} catch (err) {
throw new Error('Unable to disassociate from wifi networks')
}
// Change the MAC.
try {
cp.execSync(quote(['ifconfig', device, 'ether', mac]))
} catch (err) {
throw new Error('Unable to change MAC address')
}
// Associate airport with known network (if any)
// Note: This does not work on OS X 10.9 due to changes in the Airport utility
try {
cp.execSync('networksetup -detectnewhardware')
} catch (err) {
throw new Error('Unable to associate with known networks')
}
} else if (process.platform === 'linux') {
// Set the device's mac address.
// Handles shutting down and starting back up interface.
try {
cp.execSync(quote(['ifconfig', device, 'down', 'hw', 'ether', mac]))
cp.execSync(quote(['ifconfig', device, 'up']))
} catch (err) {
throw new Error('Unable to change MAC address')
}
} else if (process.platform === 'win32') {
// Locate adapter's registry and update network address (mac)
var regKey = new Winreg({
hive: Winreg.HKLM,
key: WIN_REGISTRY_PATH
})
regKey.keys(function (err, keys) {
if (err) {
console.log('ERROR: ' + err)
} else {
// Loop over all available keys and find the right adapter
for (var i = 0; i < keys.length; i++) {
exports.tryWindowsKey(keys[i].key, mac, device)
}
}
})
}
}
/**
* Tries to set the "NetworkAddress" value on the specified registry key for given `device` to `mac`.
*
* @param {string} key
* @param {string} device
* @param {string} mac
*/
exports.tryWindowsKey = function (key, mac, device) {
// Skip the Properties key to avoid problems with permissions
if (key.indexOf('Properties') > -1) {
return false
}
var networkAdapterKeyPath = new Winreg({
hive: Winreg.HKLM,
key: key
})
// we need to format the MAC a bit for Windows
mac = mac.replace(/:/g, '')
networkAdapterKeyPath.values(function (err, values) {
var gotAdapter = false
if (err) {
console.log('ERROR: ' + err)
} else {
for (var x = 0; x < values.length; x++) {
if (values[x].name === 'AdapterModel') {
gotAdapter = true
break
}
}
if (gotAdapter) {
networkAdapterKeyPath.set('NetworkAddress', 'REG_SZ', mac, function () {
try {
cp.execSync('netsh interface set interface "' + device + '" disable')
cp.execSync('netsh interface set interface "' + device + '" enable')
} catch (err) {
throw new Error('Unable to restart device, is the cmd running as admin?')
}
})
}
}
})
}
/**
* Generates and returns a random MAC address.
* @param {boolean} localAdmin locally administered address
* @return {string}
*/
exports.random = function (localAdmin) {
// Randomly assign a VM vendor's MAC address prefix, which should
// decrease chance of colliding with existing device's addresses.
var vendors = [
[ 0x00, 0x05, 0x69 ], // VMware
[ 0x00, 0x50, 0x56 ], // VMware
[ 0x00, 0x0C, 0x29 ], // VMware
[ 0x00, 0x16, 0x3E ], // Xen
[ 0x00, 0x03, 0xFF ], // Microsoft Hyper-V, Virtual Server, Virtual PC
[ 0x00, 0x1C, 0x42 ], // Parallels
[ 0x00, 0x0F, 0x4B ], // Virtual Iron 4
[ 0x08, 0x00, 0x27 ] // Sun Virtual Box
]
// Windows needs specific prefixes sometimes
// http://www.wikihow.com/Change-a-Computer's-Mac-Address-in-Windows
var windowsPrefixes = [
'D2',
'D6',
'DA',
'DE'
]
var vendor = vendors[Math.floor(Math.random() * vendors.length)]
if (process.platform === 'win32') {
vendor[0] = windowsPrefixes[random(0, 3)]
}
var mac = [
vendor[0],
vendor[1],
vendor[2],
random(0x00, 0x7f),
random(0x00, 0xff),
random(0x00, 0xff)
]
if (localAdmin) {
// Universally administered and locally administered addresses are
// distinguished by setting the second least significant bit of the
// most significant byte of the address. If the bit is 0, the address
// is universally administered. If it is 1, the address is locally
// administered. In the example address 02-00-00-00-00-01 the most
// significant byte is 02h. The binary is 00000010 and the second
// least significant bit is 1. Therefore, it is a locally administered
// address.[3] The bit is 0 in all OUIs.
mac[0] |= 2
}
return mac
.map(function (byte) {
return zeroFill(2, byte.toString(16))
})
.join(':').toUpperCase()
}
/**
* Takes a MAC address in various formats:
*
* - 00:00:00:00:00:00,
* - 00-00-00-00-00-00,
* - 0000.0000.0000
*
* ... and returns it in the format 00:00:00:00:00:00.
*
* @param {string} mac
* @return {string}
*/
exports.normalize = function (mac) {
var m = CISCO_MAC_ADDRESS_RE.exec(mac)
if (m) {
var halfwords = m.slice(1)
mac = halfwords.map(function (halfword) {
return zeroFill(4, halfword)
}).join('')
return chunk(mac, 2).join(':').toUpperCase()
}
m = MAC_ADDRESS_RE.exec(mac)
if (m) {
var bytes = m.slice(1)
return bytes
.map(function (byte) {
return zeroFill(2, byte)
})
.join(':')
.toUpperCase()
}
// return None
}
function chunk (str, n) {
var arr = []
for (var i = 0; i < str.length; i += n) {
arr.push(str.slice(i, i + n))
}
return arr
}
/**
* Return a random integer between min and max (inclusive).
* @param {number} min
* @param {number=} max
* @return {number}
*/
function random (min, max) {
if (max == null) {
max = min
min = 0
}
return min + Math.floor(Math.random() * (max - min + 1))
}