-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtyr-analizer.js
875 lines (751 loc) · 43.7 KB
/
tyr-analizer.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
/*/
* @author Nehuen Prados <nehuensd@gmail.com>
* @date 07/06/2017
* @version 1.1
* @licence Public Domain
/*/
const TyrAnalizer = (function(window, undefined) {
function TyrAnalizer(capture, options) {
const analizer = this;
analizer.protocols = options.protocols;
analizer.history = options.history;
analizer.graph = options.graph;
this.loadCapture(capture);
}
/*/ Load a Wireshark .json capture file. /*/
TyrAnalizer.prototype.loadCapture = function(path) {
const analizer = this;
analizer.capture = {
path: path,
data: null,
info: null
}
var loadData = new Promise(function(resolve, reject) {
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState == 4) {
if (request.status == 200) {
var data = null;
try {
data = JSON.parse(request.responseText);
resolve(data);
} catch (err) {
console.log(request.responseText);
reject(err);
}
} else {
reject(request);
}
}
}
request.open("GET", path, true);
request.send();
});
loadData
.then(function(data) {
analizer.capture.data = data;
analizer.initAnalizer();
})
.catch(function(error) {
console.log(error);
});
}
/*/ Init a pseudo-heuristic analizer. /*/
TyrAnalizer.prototype.initAnalizer = function() {
const analizer = this;
// Init table history with headers.
analizer.history.innerHTML = '<thead><tr><th>#</th><th>Protocolo</th><th>Origen</th><th>Destino</th><th>Mensaje</th></tr></thead>';
analizer.protocols.innerHTML = "<thead>" +
"<tr>" +
"<th>Capa 1<br><strong>Fisica</strong></th>" +
"<th>Capa 2<br><strong>Enlace</strong></th>" +
"<th>Capa 3<br><strong>Red</strong></th>" +
"<th>Capa 4<br><strong>Transporte</strong></th>" +
"<th>Capa 5<br><strong>Sesion</strong></th>" +
"<th>Capa 6<br><strong>Presentacion</strong></th>" +
"<th>Capa 7<br><strong>Aplicacion</strong></th>" +
"</tr>" +
"</thead>" +
"<tbody>" +
"<tr>" +
"<td></td>" +
"<td></td>" +
"<td></td>" +
"<td></td>" +
"<td></td>" +
"<td></td>" +
"<td></td>" +
"</tr>" +
"</tbody>";
// Parse raw data.
analizer.capture.traffic = TyrAnalizer.parseTraffic(analizer.capture.data.map(TyrAnalizer.parsePacket));
// Analize topology
var nodes = analizer.graph.querySelectorAll(".node"),
networks = analizer.graph.querySelectorAll(".network[data-broad]");
analizer.topologyTable = {
indexMac: {},
indexIp: {},
interfaces: {}
}
// Autoparse ARP
analizer.capture.traffic
.filter(function(packet) {
return packet.protocol.lastIndexOf("arp") === packet.protocol.length - 3;
})
.forEach(function(packet) {
analizer.topologyTable.indexMac[packet.sourceMac] = packet.sourceIp;
analizer.topologyTable.indexIp[packet.sourceIp] = packet.sourceMac;
});
// Populate graph data
for (var nro=networks.length-1; nro >= 0; nro--) {
var networkNodes = networks[nro].querySelectorAll(".node");
for (var nNro=networkNodes.length-1; nNro >= 0; nNro--) {
networkNodes[nNro].dataset.broad = (networkNodes[nNro].dataset.broad ? networkNodes[nNro].dataset.broad + "-" : "") +
networks[nro].dataset.broad;
}
}
// Index interfaces
for (var nro=nodes.length-1; nro >= 0; nro--) {
var node = {
id: nodes[nro].querySelector("input").id,
name: nodes[nro].querySelector("label strong").innerHTML,
icon: nodes[nro].querySelector("i").className,
ips: nodes[nro].dataset.ip.split(" "),
broad: (nodes[nro].dataset.broad ? nodes[nro].dataset.broad.split(" ") : [])
}
// Remove duplicates and self-broadcast
node.broad = node.broad.filter(function(ip, pos) {
return node.broad.indexOf(ip) === pos && node.ips.indexOf(ip) === -1;
});
node.ips.forEach(function(ip) {
if (!analizer.topologyTable.indexIp[ip]) {
console.warn("IP INTERFACE: " + ip + " not be present in ARP messages.");
return;
}
analizer.topologyTable.interfaces[analizer.topologyTable.indexIp[ip]] = node;
});
}
// Populate protocol table.
analizer.capture.traffic.reduce(function(index, packet) {
var stack = packet.protocol.split(":");
for (var nro=0; nro<stack.length; nro++) {
if (index[stack[nro]]) {
continue;
}
switch (stack[nro]) {
case "arp":
analizer.protocols.querySelector("tbody td:nth-child(2)")
.innerHTML += '<input type="checkbox" id="protocol-' + stack[nro] + '" checked><label for="protocol-' + stack[nro] + '">' + stack[nro] + '</label>';
index[stack[nro]] = true;
break;
case "ip":
analizer.protocols.querySelector("tbody td:nth-child(3)")
.innerHTML += '<input type="checkbox" id="protocol-' + stack[nro] + '" checked><label for="protocol-' + stack[nro] + '">' + stack[nro] + '</label>';
index[stack[nro]] = true;
break;
case "udp":
case "tcp":
analizer.protocols.querySelector("tbody td:nth-child(4)")
.innerHTML += '<input type="checkbox" id="protocol-' + stack[nro] + '" checked><label for="protocol-' + stack[nro] + '">' + stack[nro] + '</label>';
index[stack[nro]] = true;
break;
case "http":
case "dns":
case "ssl":
analizer.protocols.querySelector("tbody td:nth-child(7)")
.innerHTML += '<input type="checkbox" id="protocol-' + stack[nro] + '" checked><label for="protocol-' + stack[nro] + '">' + stack[nro] + '</label>';
index[stack[nro]] = true;
break;
default:
console.log(stack[nro]);
break;
}
}
return index;
}, {});
// Populate history table.
analizer.history.innerHTML += "<tbody><tr>" +
analizer.capture.traffic.map(function(packet, idx) {
return "<td>" +
[
'<input type="radio" name="current-packet" id="packet-' + (idx + 1) + '" '+(idx === 0 ? 'checked' : '')+' data-idx="'+idx+'"><label for="packet-' + (idx + 1) + '">' + (idx + 1) + '</label><a href="#graph">Grafico</a>',
TyrAnalizer.makeTemplate(packet, "protocol", analizer.topologyTable, analizer.capture.traffic),
TyrAnalizer.makeTemplate(packet, "source", analizer.topologyTable, analizer.capture.traffic),
TyrAnalizer.makeTemplate(packet, "target", analizer.topologyTable, analizer.capture.traffic),
TyrAnalizer.makeTemplate(packet, "message", analizer.topologyTable, analizer.capture.traffic),
]
.join("</td><td>") +
"</td>";
})
.join("</tr><tr>") +
"</tr></tbody>";
// Show current packet data:
function updateCurrentPacket() {
var current = analizer.history.querySelector("input[type='radio'][name='current-packet']:checked"),
packet = analizer.capture.traffic[current.dataset.idx],
sourceZone = analizer.graph.querySelector(".messages .source"),
targetZone = analizer.graph.querySelector(".messages .target"),
protocolZone = analizer.graph.querySelector(".messages .protocol"),
messageZone = analizer.graph.querySelector(".messages .message"),
elements;
elements = analizer.graph.querySelectorAll('.node.source');
for(nro=0; nro<elements.length; nro++) {
elements[nro].classList.remove("source");
}
elements = document.querySelectorAll('.node.target');
for(nro=0; nro<elements.length; nro++) {
elements[nro].classList.remove("target");
}
sourceZone.innerHTML = TyrAnalizer.makeTemplate(packet, "source", analizer.topologyTable, analizer.capture.traffic);
targetZone.innerHTML = TyrAnalizer.makeTemplate(packet, "target", analizer.topologyTable, analizer.capture.traffic);
protocolZone.innerHTML = TyrAnalizer.makeTemplate(packet, "protocol", analizer.topologyTable, analizer.capture.traffic);
messageZone.innerHTML = TyrAnalizer.makeTemplate(packet, "message", analizer.topologyTable, analizer.capture.traffic);
elements = analizer.graph.querySelectorAll("[data-ip*='" + analizer.topologyTable.indexMac[packet.sourceMac] + "']");
for(nro=0; nro<elements.length; nro++) {
elements[nro].classList.add("source");
}
if (packet.targetMac === "ff:ff:ff:ff:ff:ff") {
elements = analizer.graph.querySelectorAll(".node[data-broad*='" + packet.sourceIp + "'], .network[data-broad*='" + packet.sourceIp + "'] .node");
} else {
elements = analizer.graph.querySelectorAll("[data-ip*='" + analizer.topologyTable.indexMac[packet.targetMac] + "']");
}
for(nro=0; nro<elements.length; nro++) {
elements[nro].classList.add("target");
}
updateAutoplay();
}
function updateAutoplay() {
var autoplay = analizer.graph.querySelector("#autoplay").checked;
if (autoplay) {
setTimeout(function(){
var autoplay = analizer.graph.querySelector("#autoplay").checked;
if (!autoplay) {
return;
}
var current = analizer.history.querySelector("input[type='radio'][name='current-packet']:checked"),
currentRow = current.parentNode.parentNode;
do {
currentRow = currentRow.nextElementSibling;
} while (currentRow.nextElementSibling && currentRow.nextElementSibling.classList.contains("hide"));
if (currentRow !== current.parentNode.parentNode) {
currentRow.querySelector("input[type='radio'][name='current-packet']").checked = true;
}
updateCurrentPacket();
}, 3000);
}
}
// Filter history:
function updateFilters() {
var protocols = analizer.protocols.querySelectorAll("input[type='checkbox']"),
nodes = analizer.graph.querySelectorAll(".node input[type='checkbox']"),
history = analizer.history.querySelectorAll("tbody > tr"),
filter = {
protocols: {},
nodes: {}
};
for (var nro=protocols.length-1; nro>=0; nro--) {
if (protocols[nro].checked) {
filter.protocols[protocols[nro].id.replace("protocol-", "")] = true;
}
}
for (var nro=nodes.length-1; nro>=0; nro--) {
if (nodes[nro].checked) {
filter.nodes[nodes[nro].id] = true;
}
}
for (var nro=analizer.capture.traffic.length-1; nro>=0; nro--) {
var packet = analizer.capture.traffic[nro],
protocol = packet.protocol.lastIndexOf(":") !== -1 ? packet.protocol.substr(packet.protocol.lastIndexOf(":") + 1) : packet.protocol;
if (filter.protocols[protocol] && // Has a valid protocol
(
// Has a valid sender node
filter.nodes[analizer.topologyTable.interfaces[packet.sourceMac].id] ||
// Has a valid target node
(packet.targetMac !== "ff:ff:ff:ff:ff:ff" && filter.nodes[analizer.topologyTable.interfaces[packet.targetMac].id]) ||
// Has a target node of broadcast
(packet.targetMac === "ff:ff:ff:ff:ff:ff" && (function(){
for (var mac in analizer.topologyTable.interfaces) {
var iFace = analizer.topologyTable.interfaces[mac];
if (!filter.nodes[iFace.id]) {
continue;
}
if (iFace.broad.map(function(ip) { return analizer.topologyTable.indexIp[ip]; }).indexOf(packet.sourceMac) !== -1) {
return true;
}
}
return false;
})()
)
)
) {
history[nro].classList.remove("hide");
} else {
history[nro].classList.add("hide");
}
}
}
// Add filter behavior to protocol table:
var filters = analizer.protocols.querySelectorAll("input[type='checkbox']");
for (var nro=filters.length-1; nro>=0; nro--) {
filters[nro].addEventListener("change", updateFilters);
}
// Add filter behavior to nodes graph:
filters = analizer.graph.querySelectorAll(".node input[type='checkbox']");
for (var nro=filters.length-1; nro>=0; nro--) {
filters[nro].addEventListener("change", updateFilters);
}
// Add filter behavior to nodes graph:
var packets = analizer.history.querySelectorAll("input[type='radio']");
for (var nro=packets.length-1; nro>=0; nro--) {
packets[nro].addEventListener("change", updateCurrentPacket);
}
analizer.graph.querySelector("#autoplay").addEventListener("change", updateAutoplay);
updateFilters();
updateCurrentPacket();
}
/*/ Parse individual packet. /*/
TyrAnalizer.parsePacket = function parsePacket(packet) {
var info = {},
size = 0;
info.protocol = packet._source.layers.frame["frame.protocols"];
info.size = Number(packet._source.layers.frame["frame.len"]);
info.dataSize = 0;
size = info.size;
info.overhead = {};
// Protocol alias
if (info.protocol.indexOf("http:data-text-lines") !== -1) {
info.protocol = info.protocol.replace("http:data-text-lines", "http");
}
if (info.protocol.indexOf("http:data") !== -1) {
info.protocol = info.protocol.replace("http:data", "http");
}
if (info.protocol.indexOf("eth:ethertype") !== -1) {
info.protocol = info.protocol.replace("eth:ethertype", "eth");
}
// Copy of protocol stack
info.protocolsStack = info.protocol;
if (packet._source.layers.eth) {
// It has Ethernet info
info.sourceMac = packet._source.layers.eth["eth.src"];
info.targetMac = packet._source.layers.eth["eth.dst"];
// Header length in bytes
info.overhead.eth = 14 + (packet._source.layers.eth["eth.padding"] ? packet._source.layers.eth["eth.padding"].split(":").length : 0);
size -= info.overhead.eth;
}
if (packet._source.layers.arp) {
// It has ARP info
info.sourceMac = packet._source.layers.arp["arp.src.hw_mac"];
// Broadcast is "ff:ff:ff:ff:ff:ff" like Ethernet
if (packet._source.layers.arp["arp.dst.hw_mac"] !== "00:00:00:00:00:00") {
info.targetMac = packet._source.layers.arp["arp.dst.hw_mac"];
}
info.sourceIp = packet._source.layers.arp["arp.src.proto_ipv4"];
info.targetIp = packet._source.layers.arp["arp.dst.proto_ipv4"];
info.opcode = packet._source.layers.arp["arp.opcode"];
info.overhead.arp = 28; // Header length in bytes
size -= info.overhead.arp;
}
if (packet._source.layers.ip) {
// It has IP info
info.sourceIp = packet._source.layers.ip["ip.src"];
info.targetIp = packet._source.layers.ip["ip.dst"];
info.overhead.ip = Number(packet._source.layers.ip["ip.hdr_len"]); // Header length in bytes
size -= info.overhead.ip;
}
if (packet._source.layers.tcp) {
// It has TCP info
info.sourcePort = packet._source.layers.tcp["tcp.srcport"];
info.targetPort = packet._source.layers.tcp["tcp.dstport"];
info.ack = Number(packet._source.layers.tcp["tcp.ack"]);
info.seq = Number(packet._source.layers.tcp["tcp.seq"]);
info.flags = {
syn: packet._source.layers.tcp["tcp.flags_tree"]["tcp.flags.syn"] === "1",
ack: packet._source.layers.tcp["tcp.flags_tree"]["tcp.flags.ack"] === "1",
fin: packet._source.layers.tcp["tcp.flags_tree"]["tcp.flags.fin"] === "1"
};
info.overhead.tcp = Number(packet._source.layers.tcp["tcp.hdr_len"]); // Header length in bytes
size -= info.overhead.tcp;
}
if (packet._source.layers.udp) {
// It has UDP info
info.sourcePort = packet._source.layers.udp["udp.srcport"];
info.targetPort = packet._source.layers.udp["udp.dstport"];
info.overhead.udp = 8; // Header length in bytes
size -= info.overhead.udp;
}
if (packet._source.layers.http) {
function byteLength(str) {
// returns the byte length of an utf8 string
var s = str.length;
for (var i=str.length-1; i>=0; i--) {
var code = str.charCodeAt(i);
if (code > 0x7f && code <= 0x7ff) s++;
else if (code > 0x7ff && code <= 0xffff) s+=2;
if (code >= 0xDC00 && code <= 0xDFFF) i--; //trail surrogate
}
return s;
}
// It has HTTP info
info.message = Object.keys(packet._source.layers.http)[0];
if (info.message.indexOf("GET ") === 0 || info.message.indexOf("CONNECT ") === 0) {
info.url = info.message.split(" ")[1];
info.dataSize = byteLength(info.message);
} else if (info.message.indexOf("HTTP") === 0) {
if (info.message.split(" ")[1] === "200") {
info.data = packet._source.layers.http["http.file_data"];
if (info.data) {
info.dataSize = packet._source.layers.http["http.content_length_header"];
}
}
} else if (packet._source.layers.data) {
info.message = "DATA CHUNK";
info.data = packet._source.layers.data["data.data"];
info.dataSize = packet._source.layers.data["data.len"];
} else if (!packet._source.layers.ssl) {
console.warn("HTTP packet not parsed: ", packet);
}
info.overhead.http = size - info.dataSize; // Header length in bytes
size -= info.overhead.http;
}
if (packet._source.layers.dns) {
// It has DNS info
if (packet._source.layers.dns["dns.count.queries"] !== "0") {
info.query = Object.keys(packet._source.layers.dns.Queries)[0];
}
if (packet._source.layers.dns["dns.count.answers"] !== "0") {
info.answer = Object.keys(packet._source.layers.dns.Answers)[0];
}
if (packet._source.layers.dns["dns.count.auth_rr"] !== "0") {
info.aNameservers = Object.keys(packet._source.layers.dns["Authoritative nameservers"])[0];
}
if (packet._source.layers.dns["dns.count.add_rr"] !== "0") {
info.aRecords = Object.keys(packet._source.layers.dns["Additional records"])[0];
}
info.overhead.dns = size; // DNS is all overhead
size -= info.overhead.dns;
}
return info;
}
/*/ Parse global traffic. /*/
TyrAnalizer.parseTraffic = function parseTraffic(traffic) {
// No traffic.
if (traffic.length === 0) {
return traffic;
}
// Clean common protocol prefix
var commonPrefix = traffic[0].protocol;
for (var pos = traffic.length - 1; pos > 0; pos--) {
if (traffic[pos].protocol.indexOf(commonPrefix) === 0) {
continue;
}
// commonPrefix not be here
var lastPartIndex = commonPrefix.lastIndexOf(":");
if (lastPartIndex === -1) {
break;
}
// Start a search of subpart
commonPrefix = commonPrefix.substr(0, commonPrefix.lastIndexOf(":"));
pos = traffic.length - 1;
}
if (pos === 0 && commonPrefix !== "") {
traffic = traffic.map(function(packet) {
packet.protocol = packet.protocol.replace(commonPrefix + ":", "");
return packet;
});
}
return traffic;
}
/*/ Make template of property. /*/
TyrAnalizer.makeTemplate = function makeTemplate(packet, field, topology, traffic) {
switch (field) {
case "source":
var name = "<i class=\"" + topology.interfaces[packet.sourceMac].icon + "\"></i>" +
"<strong>" + topology.interfaces[packet.sourceMac].name + "</strong>" +
(packet.sourceIp ? "<strong>" + packet.sourceIp + (packet.sourcePort ? "<span>" + packet.sourcePort + "</span>" : "") + "</strong>" : "<br>") +
"<em>" + packet.sourceMac + "</em>"
return name;
break;
case "target":
var name;
if (packet.targetMac === "ff:ff:ff:ff:ff:ff") {
name = "<strong>BROADCAST</strong>";
} else {
name = "<i class=\"" + topology.interfaces[packet.targetMac].icon + "\"></i>" +
"<strong>" + topology.interfaces[packet.targetMac].name + "</strong>" +
(packet.targetIp ? "<strong>" + packet.targetIp + (packet.targetPort ? "<span>" + packet.targetPort + "</span>" : "") + "</strong>" : "<br>");
}
name += "<em>" + packet.targetMac + "</em>";
return name;
break;
case "message":
var message = "",
protocol = packet.protocol.lastIndexOf(":") !== -1 ? packet.protocol.substr(packet.protocol.lastIndexOf(":") + 1) : packet.protocol;
switch (protocol) {
case "arp":
if (packet.opcode === "1") {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em> pregunta:<br>¿Quien tiene esta IP? <strong>" + packet.targetIp + "</strong>";
} else {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em> contesta:<br>¡Yo tengo esa IP! <strong>" + packet.sourceIp + "</strong>";
}
break;
case "tcp":
// Esto es una conexion y todo puede pasar:
if (packet.flags.syn) {
// Desde que un nodo quiera abrir una conexion pasando por otro:
if (topology.indexIp[packet.sourceIp] === packet.sourceMac &&
topology.indexIp[packet.targetIp] !== packet.targetMac
) {
if (packet.flags.ack) {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em> acepta la conexion<br>y quiere conectase con <em>" + topology.interfaces[topology.indexIp[packet.targetIp]].name + "</em><br>pasando por <em>" + topology.interfaces[packet.targetMac].name + "</em>:<br>Acepto su conexion, ¿Abrimos esta tambien? <strong>" + packet.sourceIp + "<span>" + packet.sourcePort + "</span> → " + packet.targetIp + "<span>" + packet.targetPort + "</span></strong>";
} else {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em> quiere<br>conectase con <em>" + topology.interfaces[topology.indexIp[packet.targetIp]].name + "</em><br>pasando por <em>" + topology.interfaces[packet.targetMac].name + "</em>:<br>¿Abrimos esta conexion? <strong>" + packet.sourceIp + "<span>" + packet.sourcePort + "</span> → " + packet.targetIp + "<span>" + packet.targetPort + "</span></strong>";
}
}
// Pasando porque un router quiera abrir la conexion contra un nodo en nombre de otro nodo:
else if (topology.indexIp[packet.sourceIp] !== packet.sourceMac &&
topology.indexIp[packet.targetIp] === packet.targetMac
) {
if (packet.flags.ack) {
message = "El <em>" + topology.interfaces[topology.indexIp[packet.sourceIp]].name + "</em> acepta la conexion<br>y quiere conectase con <em>" + topology.interfaces[packet.targetMac].name + "</em><br>pasando por <em>" + topology.interfaces[packet.sourceMac].name + "</em>:<br>Acepto su conexion, ¿Abrimos esta tambien? <strong>" + packet.sourceIp + "<span>" + packet.sourcePort + "</span> → " + packet.targetIp + "<span>" + packet.targetPort + "</span></strong>";
} else {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em> quiere<br>conectar a <em>" + topology.interfaces[topology.indexIp[packet.sourceIp]].name + "</em><br>con <em>" + topology.interfaces[packet.targetMac].name + "</em>:<br>¿Abrimos esta conexion? <strong>" + packet.sourceIp + "<span>" + packet.sourcePort + "</span> → " + packet.targetIp + "<span>" + packet.targetPort + "</span></strong>";
}
}
// O que router quiera abrir la conexion contra otro router en nombre de otro nodo:
else if (topology.indexIp[packet.sourceIp] !== packet.sourceMac &&
topology.indexIp[packet.targetIp] !== packet.targetMac
) {
if (packet.flags.ack) {
message = "TO-DO: ";
} else {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em> quiere<br>conectar a <em>" + topology.interfaces[topology.indexIp[packet.sourceIp]].name + "</em><br>con <em>" + topology.interfaces[topology.indexIp[packet.targetIp]].name + "</em><br>pasando por <em>" + topology.interfaces[packet.targetMac].name + "</em>:<br>¿Abrimos esta conexion? <strong>" + packet.sourceIp + "<span>" + packet.sourcePort + "</span> → " + packet.targetIp + "<span>" + packet.targetPort + "</span></strong>";
}
} else if (topology.indexIp[packet.sourceIp] === packet.sourceMac &&
topology.indexIp[packet.targetIp] === packet.targetMac
) {
if (packet.flags.ack) {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em> acepta la conexion<br>y quiere conectase con <em>" + topology.interfaces[packet.targetMac].name + "</em>:<br>Acepto su conexion, ¿Abrimos esta tambien? <strong>" + packet.sourceIp + "<span>" + packet.sourcePort + "</span> → " + packet.targetIp + "<span>" + packet.targetPort + "</span></strong>";
} else {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em> quiere<br>conectarse con <em>" + topology.interfaces[topology.indexIp[packet.sourceIp]].name + "</em>:<br>¿Abrimos esta conexion? <strong>" + packet.sourceIp + "<span>" + packet.sourcePort + "</span> → " + packet.targetIp + "<span>" + packet.targetPort + "</span></strong>";
}
} else {
console.warn("Syn type not parsed in:", packet);
}
}
// O que se trate de una confirmacion.
else if (packet.flags.ack) {
var ackOfConnections = traffic
.filter(function(tPacket) {
return ((tPacket.protocol.lastIndexOf(":") !== -1 ?
tPacket.protocol.substr(tPacket.protocol.lastIndexOf(":") + 1) :
tPacket.protocol) === "tcp" &&
tPacket.flags.syn
);
})
.map(function(tPacket) {
return tPacket.seq + 1;
});
// Donde un nodo quiere confirmar datos pasando por otro:
if (topology.indexIp[packet.sourceIp] === packet.sourceMac &&
topology.indexIp[packet.targetIp] !== packet.targetMac
) {
// El ack es de una apertura de conexion:
if (ackOfConnections.indexOf(packet.ack) !== -1) {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em> acepta la conexion<br>de <em>" + topology.interfaces[topology.indexIp[packet.targetIp]].name + "</em><br>pasando por <em>" + topology.interfaces[packet.targetMac].name + "</em>:<br>Acepto su conexion.<strong>" + packet.sourceIp + "<span>" + packet.sourcePort + "</span> → " + packet.targetIp + "<span>" + packet.targetPort + "</span></strong>";
}
// El ack es de otra cosa:
else {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em> confirma la recepcion #"+packet.ack+"<br> a <em>" + topology.interfaces[topology.indexIp[packet.targetIp]].name + "</em><br>pasando por <em>" + topology.interfaces[packet.targetMac].name + "</em>:<br>Confirmo con ACK #"+packet.ack+".<strong>" + packet.sourceIp + "<span>" + packet.sourcePort + "</span> → " + packet.targetIp + "<span>" + packet.targetPort + "</span></strong>";
}
}
// Pasando porque un router quiera confirmar datos un nodo en nombre de otro nodo:
else if (topology.indexIp[packet.sourceIp] !== packet.sourceMac &&
topology.indexIp[packet.targetIp] === packet.targetMac
) {
// El ack es de una apertura de conexion:
if (ackOfConnections.indexOf(packet.ack) !== -1) {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em> acepta la conexion<br>de <em>" + topology.interfaces[packet.targetMac].name + "</em><br>en nombre de <em>" + topology.interfaces[topology.indexIp[packet.sourceIp]].name + "</em>:<br>Acepto su conexion.<strong>" + packet.sourceIp + "<span>" + packet.sourcePort + "</span> → " + packet.targetIp + "<span>" + packet.targetPort + "</span></strong>";
}
// El ack es de otra cosa:
else {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em> confirma la recepcion #"+packet.ack+"<br>a <em>" + topology.interfaces[topology.indexIp[packet.targetIp]].name + "</em><br>en nombre de <em>" + topology.interfaces[topology.indexIp[packet.sourceIp]].name + "</em>:<br>Confirmo con ACK #"+packet.ack+".<strong>" + packet.sourceIp + "<span>" + packet.sourcePort + "</span> → " + packet.targetIp + "<span>" + packet.targetPort + "</span></strong>";
}
}
// O que router confirme contenido a otro router en nombre de otro nodo:
else if (topology.indexIp[packet.sourceIp] !== packet.sourceMac &&
topology.indexIp[packet.targetIp] !== packet.targetMac
) {
if (packet.flags.ack) {
message = "TO-DO: ";
} else {
message = "TO-DO: ";
}
} else if (topology.indexIp[packet.sourceIp] === packet.sourceMac &&
topology.indexIp[packet.targetIp] === packet.targetMac
) {
// El ack es de una apertura de conexion:
if (ackOfConnections.indexOf(packet.ack) !== -1) {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em> acepta la conexion<br>de <em>" + topology.interfaces[packet.targetMac].name + "</em>:<br>Acepto su conexion.<strong>" + packet.sourceIp + "<span>" + packet.sourcePort + "</span> → " + packet.targetIp + "<span>" + packet.targetPort + "</span></strong>";
}
// El ack es de otra cosa:
else {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em> confirma la recepcion #"+packet.ack+"<br>a <em>" + topology.interfaces[topology.indexIp[packet.targetIp]].name + "</em>:<br>Confirmo con ACK #"+packet.ack+".<strong>" + packet.sourceIp + "<span>" + packet.sourcePort + "</span> → " + packet.targetIp + "<span>" + packet.targetPort + "</span></strong>";
}
} else {
console.warn("Ack type not parsed in:", packet);
}
} else {
console.warn("TCP type not parsed in:", packet);
}
break;
case "http":
// Es una peticion http y todo puede pasar:
if (packet.message.indexOf("GET ") === 0) {
// Desde que un nodo pida un recurso pasando por otro:
if (topology.indexIp[packet.sourceIp] === packet.sourceMac &&
topology.indexIp[packet.targetIp] !== packet.targetMac
) {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em> pide un recurso<br>a <em>" + topology.interfaces[topology.indexIp[packet.targetIp]].name + "</em><br>pasando por <em>" + topology.interfaces[packet.targetMac].name + "</em>:<br><strong>GET <span>" + packet.url + "</span></strong>";
}
// Pasando porque un router pida un recurso en nombre de otro nodo:
else if (topology.indexIp[packet.sourceIp] !== packet.sourceMac &&
topology.indexIp[packet.targetIp] === packet.targetMac
) {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em> pide un recurso<br>en nombre de <em>" + topology.interfaces[topology.indexIp[packet.sourceIp]].name + "</em><br>a <em>" + topology.interfaces[packet.targetMac].name + "</em>:<br><strong>GET <span>" + packet.url + "</span></strong>";
} else {
console.warn("HTTP GET not parsed in:", packet);
}
} else if (packet.data) {
// Desde que un nodo envie un recurso pasando por otro:
if (topology.indexIp[packet.sourceIp] === packet.sourceMac &&
topology.indexIp[packet.targetIp] !== packet.targetMac
) {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em> envia el recurso<br>a <em>" + topology.interfaces[topology.indexIp[packet.targetIp]].name + "</em> pasando por <em>" + topology.interfaces[packet.targetMac].name + "</em>:";
}
// Pasando porque un router envie un recurso a otro router en nombre de otro nodo:
else if (topology.indexIp[packet.sourceIp] !== packet.sourceMac &&
topology.indexIp[packet.targetIp] !== packet.targetMac
) {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em> envia el recurso<br>a <em>" + topology.interfaces[topology.indexIp[packet.targetIp]].name + "</em> en nombre de <em>" + topology.interfaces[topology.indexIp[packet.sourceIp]].name + "</em><br>pasando por <em>" + topology.interfaces[packet.targetMac].name + "</em>:";
}
// O que un router envie un recurso en nombre de otro nodo:
else if (topology.indexIp[packet.sourceIp] !== packet.sourceMac &&
topology.indexIp[packet.targetIp] === packet.targetMac
) {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em> envia el recurso<br>a <em>" + topology.interfaces[packet.targetMac].name + "</em> en nombre de <em>" + topology.interfaces[topology.indexIp[packet.sourceIp]].name + "</em>:";
}
// O que un nodo envie un recurso a otro nodo:
else if (topology.indexIp[packet.sourceIp] === packet.sourceMac &&
topology.indexIp[packet.targetIp] === packet.targetMac
) {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em> envia el recurso<br>a <em>" + topology.interfaces[packet.targetMac].name + "</em>:";
} else {
console.warn("HTTP data not parsed:", packet);
}
message += "<br><iframe sandbox srcdoc=\"" +
packet.data
.replace(/img src\=\"/gi, 'img data-src="')
.replace(/&/g, '&')
.replace(/>/g, '>')
.replace(/</g, '<')
.replace(/"/g, '"')
.replace(/'/g, ''') +
"\"></iframe>";
} else if (packet.message.indexOf("CONNECT ") === 0) {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em> quiere iniciar una conexion HTTP<br> con <em>" + topology.interfaces[topology.indexIp[packet.targetIp]].name + "</em>:<br><strong><span>" + packet.message + "</span></strong>";
} else if (packet.message.indexOf("200 Connection established") !== -1) {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em> acepta una conexion HTTP<br> con <em>" + topology.interfaces[topology.indexIp[packet.targetIp]].name + "</em>:<br><strong><span>" + packet.message + "</span></strong>";
} else if (packet.message === "DATA TUNNEL") {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em> envia datos<br> a <em>" + topology.interfaces[topology.indexIp[packet.targetIp]].name + "</em>:<br><strong><span>" + packet.message + "</span></strong>";
} else {
console.warn("This packet of http module is not parsed: ", packet);
}
break;
case "dns":
if (packet.answer || packet.aNameservers || packet.aRecords) {
var response = '';
if (packet.answer) {
response += "<br>La respuesta es:<br><strong><span>" + packet.answer + "</span></strong>";
}
if (packet.aNameservers) {
response += "<br>Los servidores de nombres con autoridad:<br><strong><span>" + packet.aNameservers + "</span></strong>";
}
if (packet.aRecords) {
response += "<br>Los registros adicionales:<br><strong><span>" + packet.aRecords + "</span></strong>";
}
// Desde que un nodo responde pasando por otro:
if (topology.indexIp[packet.sourceIp] === packet.sourceMac &&
topology.indexIp[packet.targetIp] !== packet.targetMac
) {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em><br>responde a <em>" + topology.interfaces[topology.indexIp[packet.targetIp]].name + "</em><br>pasando por <em>" + topology.interfaces[packet.targetMac].name + "</em>:" + response;
}
// Pasando porque un router responde a un nodo en nombre de otro nodo:
else if (topology.indexIp[packet.sourceIp] !== packet.sourceMac &&
topology.indexIp[packet.targetIp] === packet.targetMac
) {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em><br>responde en nombre de <em>" + topology.interfaces[topology.indexIp[packet.sourceIp]].name + "</em><br> a <em>" + topology.interfaces[packet.targetMac].name + "</em>:" + response;
}
// O que router quiera preguntar a otro router en nombre de otro nodo:
else if (topology.indexIp[packet.sourceIp] !== packet.sourceMac &&
topology.indexIp[packet.targetIp] !== packet.targetMac
) {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em><br>responde en nombre de <em>" + topology.interfaces[topology.indexIp[packet.sourceIp]].name + "</em><br>a <em>" + topology.interfaces[topology.indexIp[packet.targetIp]].name + "</em><br>pasando por <em>" + topology.interfaces[packet.targetMac].name + "</em>:" + response;
} else if (topology.indexIp[packet.sourceIp] === packet.sourceMac &&
topology.indexIp[packet.targetIp] === packet.targetMac
) {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em><br>responde a <em>" + topology.interfaces[packet.targetMac].name + "</em>:" + response;
}
} else {
// Desde que un nodo pregunta pasando por otro:
if (topology.indexIp[packet.sourceIp] === packet.sourceMac &&
topology.indexIp[packet.targetIp] !== packet.targetMac
) {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em><br>pregunta a <em>" + topology.interfaces[topology.indexIp[packet.targetIp]].name + "</em><br>pasando por <em>" + topology.interfaces[packet.targetMac].name + "</em>:<br>Consulta DNS: <strong><span>" + packet.query + "</span></strong>";
}
// Pasando porque un router pregunta a un nodo en nombre de otro nodo:
else if (topology.indexIp[packet.sourceIp] !== packet.sourceMac &&
topology.indexIp[packet.targetIp] === packet.targetMac
) {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em><br>pregunta en nombre de <em>" + topology.interfaces[topology.indexIp[packet.sourceIp]].name + "</em><br> a <em>" + topology.interfaces[packet.targetMac].name + "</em>:<br>Consulta DNS: <strong><span>" + packet.query + "</span></strong>";
}
// O que router quiera preguntar a otro router en nombre de otro nodo:
else if (topology.indexIp[packet.sourceIp] !== packet.sourceMac &&
topology.indexIp[packet.targetIp] !== packet.targetMac
) {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em><br>pregunta en nombre de <em>" + topology.interfaces[topology.indexIp[packet.sourceIp]].name + "</em><br>a <em>" + topology.interfaces[topology.indexIp[packet.targetIp]].name + "</em><br>pasando por <em>" + topology.interfaces[packet.targetMac].name + "</em>:<br>Consulta DNS: <strong><span>" + packet.query + "</span></strong>";
} else if (topology.indexIp[packet.sourceIp] === packet.sourceMac &&
topology.indexIp[packet.targetIp] === packet.targetMac
) {
message = "El <em>" + topology.interfaces[packet.sourceMac].name + "</em><br>pregunta a <em>" + topology.interfaces[packet.targetMac].name + "</em>:<br>Consulta DNS: <strong><span>" + packet.query + "</span></strong>";
}
}
break;
default:
console.warn("This packet is not parsed: ", packet);
break;
}
var totalPercent = 0,
totalSize = 0,
details = packet.protocolsStack.split(":").map(function(protocol) {
packet.overhead[protocol] || (packet.overhead[protocol] = 0);
packet.overhead[protocol] = Number(packet.overhead[protocol]);
var percent = Math.round(packet.overhead[protocol] / packet.size * 10000) / 100;
totalSize += packet.overhead[protocol];
return "<span class=\"" + protocol + "\">" + protocol + "<br><span>" + packet.overhead[protocol] + "B<br>(" + percent + "%)</span></span>";
})
.join("");
totalPercent = Math.round(totalSize / packet.size * 10000) / 100;
message += "<div class=\"overhead\">" +
"<strong>Overhead de "+totalSize+"B ("+totalPercent+"%) en paquete de " + packet.size + "B:</strong>" +
"<div>" + details + "</div>" +
"</div>";
return message;
break;
default:
return packet[field];
break;
}
}
return TyrAnalizer;
})(window);
/*/ ============================================= /*/