-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCaptureManager.js
359 lines (319 loc) · 11.1 KB
/
CaptureManager.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
const EventEmitter = require('events');
const ChildProcess = require('child_process');
const MacAddress = require('macaddress');
const PCap = require('pcap');
const DeviceInstanceManager = require('./DeviceInstanceManager');
const TopologyManager = require('./TopologyManager');
const ClientManager = require('./ClientManager');
const ConfigDB = require('./Config');
const Apps = require('./utils/HelperApps');
const Log = require('debug')('capture');
const CAPTURE_BUFFER_SIZE = 1024 * 1024; // 1MB
const CAPTURE_BUFFER_TIMEOUT = 0; // Immediate delivery
const CAPTURE_DEFAULT_SNAP_SIZE = 2000; // Reasonable default size if we can't work this out
const ACTIVATE_INTERVAL = 60 * 1000; // 1 minute
PCap.warningHandler = (msg) => Log(`PCap warning: ${msg}`);
class CaptureManager extends EventEmitter {
constructor() {
super();
this.mirrors = [];
this.restores = [];
this.mymacs = [];
this.session = null;
this.running = false;
this._onPacket = this._onPacket.bind(this);
}
async start() {
this.activateCapturePoint();
}
stop() {
}
getAttachmentPoint() {
if (!this.attach) {
return null;
}
return {
device: this.attach.entryDevice,
portnr: this.attach.entryPortnr
};
}
async startCapture(config) {
Log('startCapture:');
try {
this.running = true;
if (this.session) {
throw new Error('capture already started');
}
if (!this.attach) {
throw new Error('no attachment point found');
}
const snapsize = parseInt(this.attach.entryDevice.readKV(`network.physical.port.${this.attach.entryPortnr}.framesize`) || CAPTURE_DEFAULT_SNAP_SIZE);
const filter = this._buildFilter(config.filter);
Log('startCapture: filter: ', filter);
this._calculateMirrors(config);
await this._activateMirrors();
Log('startCapture: createSession:');
this.session = PCap.createSession(this.attach.captureDevice, {
filter: filter,
promiscuous: true,
monitor: false,
buffer_size: CAPTURE_BUFFER_SIZE,
buffer_timeout: CAPTURE_BUFFER_TIMEOUT,
snap_length: snapsize
});
this.session.on('packet', this._onPacket);
}
catch (e) {
this.running = false;
throw e;
}
}
async stopCapture() {
Log('stopCapture:');
if (!this.running) {
return;
}
try {
if (this.session) {
this.session.off('packet', this._onPacket);
this.session.close();
this.session = null;
}
await this._deactivateMirrors();
}
finally {
this.running = false;
}
}
_buildFilter(config) {
const filter = [];
if (config.ignoreBroadcast) {
filter.push('(not ether broadcast)');
}
if (config.ignoreMulticast) {
filter.push('(not ether multicast)');
}
if (config.ignoreHost) {
this.mymacs.forEach(mac => {
filter.push(`(not ether host ${mac})`);
});
}
if (config.host) {
switch (config.hostType) {
case 'S':
filter.push(`(src host ${config.host})`);
break;
case 'D':
filter.push(`(dst host ${config.host})`);
break;
case 'SD':
filter.push(`(host ${config.host})`);
break;
default:
break;
}
}
switch (config.proto) {
case 'ICMP':
filter.push(`(ip proto \\icmp)`);
break;
case 'UDP':
filter.push(`(ip proto \\udp)`);
break;
case 'TCP':
filter.push(`(ip proto \\tcp)`);
break;
case 'ARP':
filter.push(`(ether proto \\arp)`);
break;
}
if (config.port) {
switch (config.proto) {
case '':
case 'UDP':
case 'TCP':
switch (config.portType) {
case 'S':
filter.push(`(src port ${config.port})`);
break;
case 'D':
filter.push(`(dst port ${config.port})`);
break;
case 'SD':
filter.push(`(port ${config.port})`);
break;
default:
break;
}
break;
default:
break;
}
}
if (config.freeform) {
filter.push(`(${config.freeform})`);
}
// Filter traffic from this application
this.mymacs.forEach(mac => {
filter.push(`(not (ether host ${mac} and ip proto \\tcp and port ${global.WEBPORT}))`);
});
return filter.join(' and ');
}
_onPacket(raw) {
if (raw.link_type === 'LINKTYPE_ETHERNET') {
this.emit('packet', raw);
}
}
_calculateMirrors(config) {
if (!this.attach) {
Log('_calculateMirrors: no attachment port:');
throw new Error('no attachment port');
}
if (!config.points || !config.points.length) {
Log('_calculateMirrors: no target points');
throw new Error('no points');
}
config.points.forEach(point => {
if (point.device === this.attach.entryDevice && point.portnr === this.attach.entryPortnr) {
throw new Error('cannot capture attachment point');
}
});
const mirrors = {};
config.points.forEach(point => {
// Find the path between the attachment point and the port we want to capture.
const path = TopologyManager.findPath(point.device, this.attach.entryDevice);
if (!path) {
Log('_calculateMirrors: cannot build mirrors');
throw new Error('no mirrors possible');
}
// Add the attach point to the end of the path
path.push([ { device: this.attach.entryDevice, port: this.attach.entryPortnr }, {} ]);
// Convert path into a set of mirrors
let current = { device: point.device, port: point.portnr };
for (let i = 0; i < path.length; i++) {
const mirror = mirrors[current.device._id] || (mirrors[current.device._id] = { device: current.device, sources: [], target: null });
if (mirror.target === null || mirror.target === path[i][0].port) {
mirror.target = path[i][0].port;
mirror.sources.push(current.port);
}
else {
throw new Error('impossible mirrors');
}
current = path[i][1];
}
});
this.mirrors = Object.values(mirrors);
Log('_calculateMirrors: mirrors:', this.mirrors.map(m => [{ device: m.device.name, sources: m.sources, target: m.target }][0]));
}
async _activateMirrors() {
// Create chain of mirrors, keeping a record of what the there before so we can restore it later
this.restores = [];
// If we're mirroring traffic through multiple switches, we can only capture the ingress traffic (the traffic entering
// the port at the end of the mirror chain). While we can set the end mirror to also mirror the egress traffic, when this
// enters the next switch in the chain, it will look at the source ethernet address and think the associated device has moved
// somewhere else in the network (it can't know the traffic is *special*). This will cause all kinds of problems resulting in
// the network failing.
this.mirrors.forEach(mirror => {
this.restores.push({ device: mirror.device, mirror: mirror.device.readKV(`network.mirror.0`) });
const ports = {};
mirror.sources.forEach(portnr => ports[portnr] = { ingress: true });
mirror.device.writeKV('network.mirror.0',
{
enable: true,
target: mirror.target,
port: ports
}, { replace: true });
});
// Activate mirrors from furthest to nearest. Some devices might become difficult to access when mirroring
// is happening on nodes between outselves and them (I'm not sure that it should, as tcp should deal with duplicate
// packets but some switches clearly have issues with this).
await DeviceInstanceManager.commit({ direction: 'far-to-near', preconnect: true });
}
async _deactivateMirrors() {
if (!this.restores.length) {
return;
}
for (let i = 0; i < this.restores.length; i++) {
const restore = this.restores[i];
restore.device.writeKV('network.mirror.0', restore.mirror, { replace: true });
}
this.restores = [];
// Tear down mirrors starting with the nearest first. We don't preconnect because mirrors can
// effect our ability to contact some switches (I'm not sure why).
await DeviceInstanceManager.commit({ direction: 'near-to-far', preconnect: false });
}
async activateCapturePoint() {
for (;;) {
Log('activateCapturePoint:');
const device = ConfigDB.read('network.capture.device');
if (device) {
try {
ChildProcess.execSync(`${Apps.ip} link set ${device} up`);
const dmac = await MacAddress.one(device);
// Look for the capture point in the network
const client = ClientManager.getClientByMac(dmac);
if (client && client.connected && client.connected.portnr !== null) {
this.attach = {
captureDevice: device,
entryDevice: client.connected.device,
entryPortnr: client.connected.portnr
};
Log('activateCapturePoint: attach:', this.attach.captureDevice, this.attach.entryDevice.name, this.attach.entryPortnr);
}
else {
this.attach = null;
Log('activateCapturePoint: no capture point:', device);
}
// Update list of my mac addresses
this.mymacs = Object.values(await MacAddress.all()).map(entry => entry.mac);
// Send a packet out of the capture point so we can locate it in the network.
// We do this periodically to keep point alive in the network.
const mac = dmac.split(':').map(v => parseInt(v, 16));
const pkt = Buffer.from([
0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], // My mac
0x08, 0x06, // ARP
0x08, 0x00, // Ethernet,
0x00, 0x04, // IPv4
6, // HW len
4, // Protocol len
0, 1, // Request
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], // My mac
0, 0, 0, 0, // My IP - 0.0.0.0 as placeholder
0, 0, 0, 0, 0, 0, // Target HW
0, 0, 0, 0 // Target IP
]);
let session;
try {
session = PCap.createSession(device, {
promiscuous: false,
monitor: false,
buffer_size: CAPTURE_BUFFER_SIZE,
snap_length: CAPTURE_DEFAULT_SNAP_SIZE
});
session.inject(pkt);
await new Promise(resolve => setTimeout(resolve, 100));
}
finally {
if (session) {
session.close();
}
}
// If we don't have the attachment point, sync the state from our capture-able devices
// so we can find it.
if (!this.attach) {
TopologyManager.getCaptureDevices().forEach(dev => {
dev.watch();
dev.unwatch();
});
}
}
catch (_) {
Log(_);
}
}
await new Promise(resolve => setTimeout(resolve, ACTIVATE_INTERVAL));
}
}
}
module.exports = new CaptureManager();