This repository has been archived by the owner on Apr 9, 2018. It is now read-only.
forked from node-pcap/node_pcap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcap.js
1810 lines (1603 loc) · 60.6 KB
/
pcap.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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*global process require exports console */
var util, IOWatcher,
dns = require('dns'),
Buffer = require('buffer').Buffer,
events = require('events'),
binding = require('./build/Release/pcap_binding'),
HTTPParser = process.binding('http_parser').HTTPParser,
url = require('url');
if (process.versions && process.versions.node && process.versions.node.split('.')[1] >= 3) {
util = require("util");
IOWatcher = process.binding('io_watcher').IOWatcher;
} else {
util = require("sys");
IOWatcher = process.IOWatcher;
}
function Pcap() {
this.opened = false;
this.fd = null;
events.EventEmitter.call(this);
}
util.inherits(Pcap, events.EventEmitter);
exports.lib_version = binding.lib_version();
Pcap.prototype.findalldevs = function () {
return binding.findalldevs();
};
Pcap.prototype.open = function (live, device, filter, buffer_size, pcap_output_filename) {
var me = this;
if (typeof buffer_size === 'number' && !isNaN(buffer_size)) {
this.buffer_size = Math.round(buffer_size);
} else {
this.buffer_size = 10 * 1024 * 1024; // Default buffer size is 10MB
}
this.live = live;
if (live) {
this.device_name = device || binding.default_device();
this.link_type = binding.open_live(this.device_name, filter || "", this.buffer_size, pcap_output_filename || "");
} else {
this.device_name = device;
this.link_type = binding.open_offline(this.device_name, filter || "", this.buffer_size, pcap_output_filename || "");
}
this.fd = binding.fileno();
this.opened = true;
this.readWatcher = new IOWatcher();
this.empty_reads = 0;
this.buf = new Buffer(65535);
// called for each packet read by pcap
function packet_ready(header) {
header.link_type = me.link_type;
header.time_ms = (header.tv_sec * 1000) + (header.tv_usec / 1000);
me.buf.pcap_header = header;
me.emit('packet', me.buf);
}
// readWatcher gets a callback when pcap has data to read. multiple packets may be readable.
this.readWatcher.callback = function pcap_read_callback() {
var packets_read = binding.dispatch(me.buf, packet_ready);
if (packets_read < 1) {
// according to pcap_dispatch documentation if 0 is returned when reading
// from a savefile there will be no more packets left. this check ensures
// we stop reading. Under certain circumstances IOWatcher will get caught
// in a loop and continue to signal us causing the program to be flooded
// with events.
if(!me.live) {
me.readWatcher.stop();
me.emit('complete');
}
// TODO - figure out what is causing this, and if it is bad.
me.empty_reads += 1;
}
};
this.readWatcher.set(this.fd, true, false);
this.readWatcher.start();
};
Pcap.prototype.close = function () {
this.opened = false;
binding.close();
// TODO - remove listeners so program will exit I guess?
};
Pcap.prototype.stats = function () {
return binding.stats();
};
exports.Pcap = Pcap;
exports.createSession = function (device, filter, buffer_size) {
var session = new Pcap();
session.open(true, device, filter, buffer_size);
return session;
};
exports.createOfflineSession = function (path, filter) {
var session = new Pcap();
session.open(false, path, filter, 0);
return session;
};
//
// Decoding functions
//
function lpad(str, len) {
while (str.length < len) {
str = "0" + str;
}
return str;
}
function dump_bytes(raw_packet, offset) {
for (var i = offset; i < raw_packet.pcap_header.caplen ; i += 1) {
console.log(i + ": " + raw_packet[i]);
}
}
var unpack = {
ethernet_addr: function (raw_packet, offset) {
return [
lpad(raw_packet[offset].toString(16), 2),
lpad(raw_packet[offset + 1].toString(16), 2),
lpad(raw_packet[offset + 2].toString(16), 2),
lpad(raw_packet[offset + 3].toString(16), 2),
lpad(raw_packet[offset + 4].toString(16), 2),
lpad(raw_packet[offset + 5].toString(16), 2)
].join(":");
},
sll_addr: function (raw_packet, offset, len) {
var res = []
for (i=0; i<len; i++){
res.push(lpad(raw_packet[offset+i].toString(16), 2));
}
return res.join(":");
},
uint16: function (raw_packet, offset) {
return ((raw_packet[offset] * 256) + raw_packet[offset + 1]);
},
uint16_be: function (raw_packet, offset) {
return ((raw_packet[offset+1] * 256) + raw_packet[offset]);
},
uint32: function (raw_packet, offset) {
return (
(raw_packet[offset] * 16777216) +
(raw_packet[offset + 1] * 65536) +
(raw_packet[offset + 2] * 256) +
raw_packet[offset + 3]
);
},
uint64: function (raw_packet, offset) {
return (
(raw_packet[offset] * 72057594037927936) +
(raw_packet[offset + 1] * 281474976710656) +
(raw_packet[offset + 2] * 1099511627776) +
(raw_packet[offset + 3] * 4294967296) +
(raw_packet[offset + 4] * 16777216) +
(raw_packet[offset + 5] * 65536) +
(raw_packet[offset + 6] * 256) +
raw_packet[offset + 7]
);
},
ipv4_addr: function (raw_packet, offset) {
return [
raw_packet[offset],
raw_packet[offset + 1],
raw_packet[offset + 2],
raw_packet[offset + 3]
].join('.');
},
ipv6_addr: function (raw_packet, offset) {
var ret = '';
var octets = [];
for (var i=offset; i<offset+16; i+=2) {
octets.push(unpack.uint16(raw_packet,i).toString(16));
}
var curr_start, curr_len = undefined;
var max_start, max_len = undefined;
for(var i = 0; i < 8; i++){
if(octets[i] == "0"){
if(curr_start === undefined){
curr_len = 1;
curr_start = i;
}else{
curr_len++;
if(!max_start || curr_len > max_len){
max_start = curr_start;
max_len = curr_len;
}
}
}else{
curr_start = undefined;
}
}
if(max_start !== undefined){
var tosplice = max_start == 0 || (max_start + max_len > 7) ? ":" : "";
octets.splice(max_start, max_len,tosplice);
if(max_len == 8){octets.push("");}
}
ret = octets.join(":");
return ret;
}
};
exports.unpack = unpack;
var decode = {}; // convert raw packet data into JavaScript objects with friendly names
decode.packet = function (raw_packet) {
var packet = {};
packet.link_type = raw_packet.pcap_header.link_type;
switch (packet.link_type) {
case "LINKTYPE_ETHERNET":
packet.link = decode.ethernet(raw_packet, 0);
break;
case "LINKTYPE_NULL":
packet.link = decode.nulltype(raw_packet, 0);
break;
case "LINKTYPE_RAW":
packet.link = decode.rawtype(raw_packet, 0);
break;
case "LINKTYPE_IEEE802_11_RADIO":
packet.link = decode.ieee802_11_radio(raw_packet, 0);
break;
case "LINKTYPE_LINUX_SLL":
packet.link = decode.linux_sll(raw_packet, 0);
break;
default:
console.log("pcap.js: decode.packet() - Don't yet know how to decode link type " + raw_packet.pcap_header.link_type);
}
packet.pcap_header = raw_packet.pcap_header; // TODO - merge values here instead of putting ref on packet buffer
return packet;
};
decode.rawtype = function (raw_packet, offset) {
var ret = {};
ret.ip = decode.ip(raw_packet, 0);
return ret;
};
decode.nulltype = function (raw_packet, offset) {
var ret = {};
// an oddity about nulltype is that it starts with a 4 byte header, but I can't find a
// way to tell which byte order is used. The good news is that all address family
// values are 8 bits or less.
if (raw_packet[0] === 0 && raw_packet[1] === 0) { // must be one of the endians
ret.pftype = raw_packet[3];
} else { // and this is the other one
ret.pftype = raw_packet[0];
}
if (ret.pftype === 2) { // AF_INET, at least on my Linux and OSX machines right now
ret.ip = decode.ip(raw_packet, 4);
} else if (ret.pftype === 30) { // AF_INET6, often
ret.ip = decode.ip6(raw_packet, 4);
} else {
console.log("pcap.js: decode.nulltype() - Don't know how to decode protocol family " + ret.pftype);
}
return ret;
};
decode.ethernet = function (raw_packet, offset) {
var ret = {};
ret.dhost = unpack.ethernet_addr(raw_packet, 0);
ret.shost = unpack.ethernet_addr(raw_packet, 6);
ret.ethertype = unpack.uint16(raw_packet, 12);
offset = 14;
// Check for a tagged frame
switch (ret.ethertype) {
case 0x8100: // VLAN-tagged (802.1Q)
ret.vlan = decode.vlan(raw_packet, 14);
// Update the ethertype
ret.ethertype = unpack.uint16(raw_packet, 16);
offset = 18;
break;
}
if (ret.ethertype < 1536) {
// this packet is actually some 802.3 type without an ethertype
ret.ethertype = 0;
} else {
// http://en.wikipedia.org/wiki/EtherType
switch (ret.ethertype) {
case 0x800: // IPv4
ret.ip = decode.ip(raw_packet, offset);
break;
case 0x806: // ARP
ret.arp = decode.arp(raw_packet, offset);
break;
case 0x86dd: // IPv6 - http://en.wikipedia.org/wiki/IPv6
ret.ipv6 = decode.ip6(raw_packet, offset);
break;
case 0x88cc: // LLDP - http://en.wikipedia.org/wiki/Link_Layer_Discovery_Protocol
ret.lldp = "need to implement LLDP";
break;
default:
console.log("pcap.js: decode.ethernet() - Don't know how to decode ethertype " + ret.ethertype);
}
}
return ret;
};
decode.linux_sll = function (raw_packet, offset) {
var ret = {};
var types = {0:"HOST", 1:"BROADCAST", 2:"MULTICAST", 3:"OTHERHOST", 4:"OUTGOING"};
ret.sllPacketType = unpack.uint16(raw_packet, offset); offset+=2;
ret.sllAddressType = types[unpack.uint16(raw_packet, offset)]; offset+=2;
var sllAddressLength = unpack.uint16(raw_packet, offset); offset+=2;
ret.sllSource = unpack.sll_addr(raw_packet, offset, sllAddressLength);
offset+=8; //address field is fixed to 8 bytes from witch addresslength bytes are used
ret.sllProtocol = unpack.uint16(raw_packet, offset); offset+=2;
switch (ret.sllProtocol) {
case 0x800: // IPv4
ret.ip = decode.ip(raw_packet, offset);
break;
case 0x806: // ARP
ret.arp = decode.arp(raw_packet, offset);
break;
case 0x86dd: // IPv6 - http://en.wikipedia.org/wiki/IPv6
ret.ipv6 = decode.ip6(raw_packet, offset);
break;
case 0x88cc: // LLDP - http://en.wikipedia.org/wiki/Link_Layer_Discovery_Protocol
ret.lldp = "need to implement LLDP";
break;
default:
console.log("pcap.js: decode.linux_sll() - Don't know how to decode ethertype " + ret.sllProtocol);
}
return ret;
};
decode.ieee802_11_radio = function (raw_packet, offset) {
var ret = {};
var original_offset = offset;
ret.headerRevision = raw_packet[offset++];
ret.headerPad = raw_packet[offset++];
ret.headerLength = unpack.uint16_be(raw_packet, offset); offset += 2;
offset = original_offset + ret.headerLength;
ret.ieee802_11Frame = decode.ieee802_11_frame(raw_packet, offset);
if(ret.ieee802_11Frame && ret.ieee802_11Frame.llc && ret.ieee802_11Frame.llc.ip) {
ret.ip = ret.ieee802_11Frame.llc.ip;
delete ret.ieee802_11Frame.llc.ip;
ret.shost = ret.ieee802_11Frame.shost;
delete ret.ieee802_11Frame.shost;
ret.dhost = ret.ieee802_11Frame.dhost;
delete ret.ieee802_11Frame.dhost
}
return ret;
};
decode.ieee802_11_frame = function (raw_packet, offset) {
var ret = {};
ret.frameControl = unpack.uint16_be(raw_packet, offset); offset += 2;
ret.type = (ret.frameControl >> 2) & 0x0003;
ret.subType = (ret.frameControl >> 4) & 0x000f;
ret.flags = (ret.frameControl >> 8) & 0xff;
ret.duration = unpack.uint16_be(raw_packet, offset); offset += 2;
ret.bssid = unpack.ethernet_addr(raw_packet, offset); offset += 6;
ret.shost = unpack.ethernet_addr(raw_packet, offset); offset += 6;
ret.dhost = unpack.ethernet_addr(raw_packet, offset); offset += 6;
ret.fragSeq = unpack.uint16_be(raw_packet, offset); offset += 2;
switch(ret.subType) {
case 8: // QoS Data
ret.qosPriority = raw_packet[offset++];
ret.txop = raw_packet[offset++];
break;
}
if(ret.type == 2 && ret.subType == 4) {
// skip this is Null function (No data)
}
else if(ret.type == 2 && ret.subType == 12) {
// skip this is QoS Null function (No data)
}
else if(ret.type == 2 && ret.subType == 7) {
// skip this is CF-Ack/Poll
}
else if(ret.type == 2 && ret.subType == 6) {
// skip this is CF-Poll (No data)
}
else if(ret.type == 2) { // data
ret.llc = decode.logicalLinkControl(raw_packet, offset);
}
return ret;
};
decode.logicalLinkControl = function (raw_packet, offset) {
var ret = {};
ret.dsap = raw_packet[offset++];
ret.ssap = raw_packet[offset++];
if(((ret.dsap == 0xaa) && (ret.ssap == 0xaa))
|| ((ret.dsap == 0x00) && (ret.ssap == 0x00))) {
ret.controlField = raw_packet[offset++];
ret.orgCode = [
raw_packet[offset++],
raw_packet[offset++],
raw_packet[offset++]
];
ret.type = unpack.uint16(raw_packet, offset); offset += 2;
switch(ret.type) {
case 0x0800: // ip
ret.ip = decode.ip(raw_packet, offset);
break;
}
} else {
throw new Error("Unknown LLC types: DSAP: " + ret.dsap + ", SSAP: " + ret.ssap);
}
return ret;
}
decode.vlan = function (raw_packet, offset) {
var ret = {};
// http://en.wikipedia.org/wiki/IEEE_802.1Q
ret.priority = (raw_packet[offset] & 0xE0) >> 5;
ret.canonical_format = (raw_packet[offset] & 0x10) >> 4;
ret.id = ((raw_packet[offset] & 0x0F) << 8) | raw_packet[offset + 1];
return ret;
};
decode.arp = function (raw_packet, offset) {
var ret = {};
// http://en.wikipedia.org/wiki/Address_Resolution_Protocol
ret.htype = unpack.uint16(raw_packet, offset); // 0, 1
ret.ptype = unpack.uint16(raw_packet, offset + 2); // 2, 3
ret.hlen = raw_packet[offset + 4];
ret.plen = raw_packet[offset + 5];
ret.operation = unpack.uint16(raw_packet, offset + 6); // 6, 7
if (ret.operation === 1) {
ret.operation = "request";
}
else if (ret.operation === 2) {
ret.operation = "reply";
}
else {
ret.operation = "unknown";
}
if (ret.hlen === 6 && ret.plen === 4) { // ethernet + IPv4
ret.sender_ha = unpack.ethernet_addr(raw_packet, offset + 8); // 8, 9, 10, 11, 12, 13
ret.sender_pa = unpack.ipv4_addr(raw_packet, offset + 14); // 14, 15, 16, 17
ret.target_ha = unpack.ethernet_addr(raw_packet, offset + 18); // 18, 19, 20, 21, 22, 23
ret.target_pa = unpack.ipv4_addr(raw_packet, offset + 24); // 24, 25, 26, 27
}
// don't know how to decode more exotic ARP types
return ret;
};
decode.ip = function (raw_packet, offset) {
var ret = {};
// http://en.wikipedia.org/wiki/IPv4
ret.version = (raw_packet[offset] & 240) >> 4; // first 4 bits
ret.header_length = raw_packet[offset] & 15; // second 4 bits
ret.header_bytes = ret.header_length * 4;
ret.diffserv = raw_packet[offset + 1];
ret.total_length = unpack.uint16(raw_packet, offset + 2); // 2, 3
ret.identification = unpack.uint16(raw_packet, offset + 4); // 4, 5
ret.flags = {};
ret.flags.reserved = (raw_packet[offset + 6] & 128) >> 7;
ret.flags.df = (raw_packet[offset + 6] & 64) >> 6;
ret.flags.mf = (raw_packet[offset + 6] & 32) >> 5;
ret.fragment_offset = ((raw_packet[offset + 6] & 31) * 256) + raw_packet[offset + 7]; // 13-bits from 6, 7
ret.ttl = raw_packet[offset + 8];
ret.protocol = raw_packet[offset + 9];
ret.header_checksum = unpack.uint16(raw_packet, offset + 10); // 10, 11
ret.saddr = unpack.ipv4_addr(raw_packet, offset + 12); // 12, 13, 14, 15
ret.daddr = unpack.ipv4_addr(raw_packet, offset + 16); // 16, 17, 18, 19
// TODO - parse IP "options" if header_length > 5
switch (ret.protocol) {
case 1:
ret.protocol_name = "ICMP";
ret.icmp = decode.icmp(raw_packet, offset + (ret.header_length * 4));
break;
case 2:
ret.protocol_name = "IGMP";
ret.igmp = decode.igmp(raw_packet, offset + (ret.header_length * 4));
break;
case 6:
ret.protocol_name = "TCP";
ret.tcp = decode.tcp(raw_packet, offset + (ret.header_length * 4), ret);
break;
case 17:
ret.protocol_name = "UDP";
ret.udp = decode.udp(raw_packet, offset + (ret.header_length * 4));
break;
default:
ret.protocol_name = "Unknown";
}
return ret;
};
decode.ip6_header = function(raw_packet, next_header, ip, offset) {
switch (next_header) {
case 1:
ip.protocol_name = "ICMP";
ip.icmp = decode.icmp(raw_packet, offset);
break;
case 2:
ip.protocol_name = "IGMP";
ip.igmp = decode.igmp(raw_packet, offset);
break;
case 6:
ip.protocol_name = "TCP";
ip.tcp = decode.tcp(raw_packet, offset, ip);
break;
case 17:
ip.protocol_name = "UDP";
ip.udp = decode.udp(raw_packet, offset);
break;
default:
// TODO: capture the extensions
//decode.ip6_header(raw_packet, raw_packet[offset], offset + raw_packet[offset+1]);
}
};
decode.ip6 = function (raw_packet, offset) {
var ret = {};
// http://en.wikipedia.org/wiki/IPv6
ret.version = (raw_packet[offset] & 240) >> 4; // first 4 bits
ret.traffic_class = ((raw_packet[offset] & 15) << 4) + ((raw_packet[offset+1] & 240) >> 4);
ret.flow_label = ((raw_packet[offset + 1] & 15) << 16) +
(raw_packet[offset + 2] << 8) +
raw_packet[offset + 3];
ret.payload_length = unpack.uint16(raw_packet, offset+4);
ret.total_length = ret.payload_length + 40;
ret.next_header = raw_packet[offset+6];
ret.hop_limit = raw_packet[offset+7];
ret.saddr = unpack.ipv6_addr(raw_packet, offset+8);
ret.daddr = unpack.ipv6_addr(raw_packet, offset+24);
ret.header_bytes = 40;
decode.ip6_header(raw_packet, ret.next_header, ret, offset+40);
return ret;
};
decode.icmp = function (raw_packet, offset) {
var ret = {};
// http://en.wikipedia.org/wiki/Internet_Control_Message_Protocol
ret.type = raw_packet[offset];
ret.code = raw_packet[offset + 1];
ret.checksum = unpack.uint16(raw_packet, offset + 2); // 2, 3
ret.id = unpack.uint16(raw_packet, offset + 4); // 4, 5
ret.sequence = unpack.uint16(raw_packet, offset + 6); // 6, 7
switch (ret.type) {
case 0:
ret.type_desc = "Echo Reply";
break;
case 1:
case 2:
ret.type_desc = "Reserved";
break;
case 3:
switch (ret.code) {
case 0:
ret.type_desc = "Destination Network Unreachable";
break;
case 1:
ret.type_desc = "Destination Host Unreachable";
break;
case 2:
ret.type_desc = "Destination Protocol Unreachable";
break;
case 3:
ret.type_desc = "Destination Port Unreachable";
break;
case 4:
ret.type_desc = "Fragmentation required, and DF flag set";
break;
case 5:
ret.type_desc = "Source route failed";
break;
case 6:
ret.type_desc = "Destination network unknown";
break;
case 7:
ret.type_desc = "Destination host unknown";
break;
case 8:
ret.type_desc = "Source host isolated";
break;
case 9:
ret.type_desc = "Network administratively prohibited";
break;
case 10:
ret.type_desc = "Host administratively prohibited";
break;
case 11:
ret.type_desc = "Network unreachable for TOS";
break;
case 12:
ret.type_desc = "Host unreachable for TOS";
break;
case 13:
ret.type_desc = "Communication administratively prohibited";
break;
default:
ret.type_desc = "Destination Unreachable (unknown code " + ret.code + ")";
}
break;
case 4:
ret.type_desc = "Source Quench";
break;
case 5:
switch (ret.code) {
case 0:
ret.type_desc = "Redirect Network";
break;
case 1:
ret.type_desc = "Redirect Host";
break;
case 2:
ret.type_desc = "Redirect TOS and Network";
break;
case 3:
ret.type_desc = "Redirect TOS and Host";
break;
default:
ret.type_desc = "Redirect (unknown code " + ret.code + ")";
break;
}
break;
case 6:
ret.type_desc = "Alternate Host Address";
break;
case 7:
ret.type_desc = "Reserved";
break;
case 8:
ret.type_desc = "Echo Request";
break;
case 9:
ret.type_desc = "Router Advertisement";
break;
case 10:
ret.type_desc = "Router Solicitation";
break;
case 11:
switch (ret.code) {
case 0:
ret.type_desc = "TTL expired in transit";
break;
case 1:
ret.type_desc = "Fragment reassembly time exceeded";
break;
default:
ret.type_desc = "Time Exceeded (unknown code " + ret.code + ")";
}
break;
// TODO - decode the rest of the well-known ICMP messages
default:
ret.type_desc = "type " + ret.type + " code " + ret.code;
}
// There are usually more exciting things hiding in ICMP packets after the headers
return ret;
};
decode.igmp = function (raw_packet, offset) {
var ret = {};
// http://en.wikipedia.org/wiki/Internet_Group_Management_Protocol
ret.type = raw_packet[offset];
ret.max_response_time = raw_packet[offset + 1];
ret.checksum = unpack.uint16(raw_packet, offset + 2); // 2, 3
ret.group_address = unpack.ipv4_addr(raw_packet, offset + 4); // 4, 5, 6, 7
switch (ret.type) {
case 0x11:
ret.version = ret.max_response_time > 0 ? 2 : 1;
ret.type_desc = "Membership Query"
break;
case 0x12:
ret.version = 1;
ret.type_desc = "Membership Report"
break;
case 0x16:
ret.version = 2;
ret.type_desc = "Membership Report"
break;
case 0x17:
ret.version = 2;
ret.type_desc = "Leave Group"
break;
case 0x22:
ret.version = 3;
ret.type_desc = "Membership Report"
// TODO: Decode v3 message
break;
default:
ret.type_desc = "type " + ret.type;
break;
}
return ret;
}
decode.udp = function (raw_packet, offset) {
var ret = {};
// http://en.wikipedia.org/wiki/User_Datagram_Protocol
ret.sport = unpack.uint16(raw_packet, offset); // 0, 1
ret.dport = unpack.uint16(raw_packet, offset + 2); // 2, 3
ret.length = unpack.uint16(raw_packet, offset + 4); // 4, 5
ret.checksum = unpack.uint16(raw_packet, offset + 6); // 6, 7
ret.data_offset = offset + 8;
ret.data_end = ret.length + ret.data_offset - 8;
ret.data_bytes = ret.data_end - ret.data_offset;
// Follow tcp pattern and don't make a copy of the data payload
// Therefore its only valid for this pass throught the capture loop
if (ret.data_bytes > 0) {
ret.data = raw_packet.slice(ret.data_offset, ret.data_end);
ret.data.length = ret.data_bytes;
}
if (ret.sport === 53 || ret.dport === 53) {
ret.dns = decode.dns(raw_packet, offset + 8);
}
return ret;
};
decode.tcp = function (raw_packet, offset, ip) {
var ret = {}, option_offset, options_end;
// http://en.wikipedia.org/wiki/Transmission_Control_Protocol
ret.sport = unpack.uint16(raw_packet, offset); // 0, 1
ret.dport = unpack.uint16(raw_packet, offset + 2); // 2, 3
ret.seqno = unpack.uint32(raw_packet, offset + 4); // 4, 5, 6, 7
ret.ackno = unpack.uint32(raw_packet, offset + 8); // 8, 9, 10, 11
ret.data_offset = (raw_packet[offset + 12] & 0xf0) >> 4; // first 4 bits of 12
ret.header_bytes = ret.data_offset * 4; // convenience for using data_offset
ret.reserved = raw_packet[offset + 12] & 15; // second 4 bits of 12
ret.flags = {};
ret.flags.cwr = (raw_packet[offset + 13] & 128) >> 7; // all flags packed into 13
ret.flags.ece = (raw_packet[offset + 13] & 64) >> 6;
ret.flags.urg = (raw_packet[offset + 13] & 32) >> 5;
ret.flags.ack = (raw_packet[offset + 13] & 16) >> 4;
ret.flags.psh = (raw_packet[offset + 13] & 8) >> 3;
ret.flags.rst = (raw_packet[offset + 13] & 4) >> 2;
ret.flags.syn = (raw_packet[offset + 13] & 2) >> 1;
ret.flags.fin = raw_packet[offset + 13] & 1;
ret.window_size = unpack.uint16(raw_packet, offset + 14); // 14, 15
ret.checksum = unpack.uint16(raw_packet, offset + 16); // 16, 17
ret.urgent_pointer = unpack.uint16(raw_packet, offset + 18); // 18, 19
ret.options = {};
option_offset = offset + 20;
options_end = offset + (ret.data_offset * 4);
while (option_offset < options_end) {
switch (raw_packet[option_offset]) {
case 0:
option_offset += 1;
break;
case 1:
option_offset += 1;
break;
case 2:
ret.options.mss = unpack.uint16(raw_packet, option_offset + 2);
option_offset += 4;
break;
case 3:
ret.options.window_scale = Math.pow(2, (raw_packet[option_offset + 2]));
option_offset += 3;
break;
case 4:
ret.options.sack_ok = true;
option_offset += 2;
break;
case 5:
ret.options.sack = [];
switch (raw_packet[option_offset + 1]) {
case 10:
ret.options.sack.push([unpack.uint32(raw_packet, option_offset + 2), unpack.uint32(raw_packet, option_offset + 6)]);
option_offset += 10;
break;
case 18:
ret.options.sack.push([unpack.uint32(raw_packet, option_offset + 2), unpack.uint32(raw_packet, option_offset + 6)]);
ret.options.sack.push([unpack.uint32(raw_packet, option_offset + 10), unpack.uint32(raw_packet, option_offset + 14)]);
option_offset += 18;
break;
case 26:
ret.options.sack.push([unpack.uint32(raw_packet, option_offset + 2), unpack.uint32(raw_packet, option_offset + 6)]);
ret.options.sack.push([unpack.uint32(raw_packet, option_offset + 10), unpack.uint32(raw_packet, option_offset + 14)]);
ret.options.sack.push([unpack.uint32(raw_packet, option_offset + 18), unpack.uint32(raw_packet, option_offset + 22)]);
option_offset += 26;
break;
case 34:
ret.options.sack.push([unpack.uint32(raw_packet, option_offset + 2), unpack.uint32(raw_packet, option_offset + 6)]);
ret.options.sack.push([unpack.uint32(raw_packet, option_offset + 10), unpack.uint32(raw_packet, option_offset + 14)]);
ret.options.sack.push([unpack.uint32(raw_packet, option_offset + 18), unpack.uint32(raw_packet, option_offset + 22)]);
ret.options.sack.push([unpack.uint32(raw_packet, option_offset + 26), unpack.uint32(raw_packet, option_offset + 30)]);
option_offset += 34;
break;
default:
console.log("Invalid TCP SACK option length " + raw_packet[option_offset + 1]);
option_offset = options_end;
}
break;
case 8:
ret.options.timestamp = unpack.uint32(raw_packet, option_offset + 2);
ret.options.echo = unpack.uint32(raw_packet, option_offset + 6);
option_offset += 10;
break;
default:
throw new Error("Don't know how to process TCP option " + raw_packet[option_offset]);
}
}
ret.data_offset = offset + ret.header_bytes;
ret.data_end = offset + ip.total_length - ip.header_bytes;
ret.data_bytes = ret.data_end - ret.data_offset;
if (ret.data_bytes > 0) {
// add a buffer slice pointing to the data area of this TCP packet.
// Note that this does not make a copy, so ret.data is only valid for this current
// trip through the capture loop.
ret.data = raw_packet.slice(ret.data_offset, ret.data_end);
ret.data.length = ret.data_bytes;
}
// automatic protocol decode ends here. Higher level protocols can be decoded by using payload.
return ret;
};
// helpers for DNS decoder
var dns_util = {
type_to_string: function (type_num) {
switch (type_num) {
case 1:
return "A";
case 2:
return "NS";
case 3:
return "MD";
case 4:
return "MF";
case 5:
return "CNAME";
case 6:
return "SOA";
case 7:
return "MB";
case 8:
return "MG";
case 9:
return "MR";
case 10:
return "NULL";
case 11:
return "WKS";
case 12:
return "PTR";
case 13:
return "HINFO";
case 14:
return "MINFO";
case 15:
return "MX";
case 16:
return "TXT";
default:
return ("Unknown (" + type_num + ")");
}
},
qtype_to_string: function (qtype_num) {
switch (qtype_num) {
case 252:
return "AXFR";
case 253:
return "MAILB";
case 254:
return "MAILA";
case 255:
return "*";
default:
return dns_util.type_to_string(qtype_num);
}
},
class_to_string: function (class_num) {
switch (class_num) {
case 1:
return "IN";
case 2:
return "CS";
case 3:
return "CH";
case 4:
return "HS";
default:
return "Unknown (" + class_num + ")";
}
},
qclass_to_string: function (qclass_num) {
if (qclass_num === 255) {
return "*";
} else {
return dns_util.class_to_string(qclass_num);
}
},
expandRRData: function(raw_packet, offset, rrRecord) {
if(rrRecord.rrtype == 'A' && rrRecord.rrclass == 'IN' && rrRecord.rdlength == 4) {
var data = {};
data.ipAddress = raw_packet[offset] + '.' + raw_packet[offset+1] + '.' + raw_packet[offset+2] + '.' + raw_packet[offset+3];
return data;
}
return null;
},
readName: function(raw_packet, offset, internal_offset, result) {
if(offset + internal_offset > raw_packet.pcap_header.len) {
throw new Error("Malformed DNS RR. Offset is larger than the size of the packet (readName).");
}
var lenOrPtr = raw_packet[offset + internal_offset];
internal_offset++;
if(lenOrPtr == 0x00) {
return result;
}
if((lenOrPtr & 0xC0) == 0xC0) {
var nameOffset = ((lenOrPtr & ~0xC0) << 8) | raw_packet[offset + internal_offset];
internal_offset++;
return dns_util.readName(raw_packet, offset, nameOffset, result);
}
for(var i=0; i<lenOrPtr; i++) {
var ch = raw_packet[offset + internal_offset];
internal_offset++;
result += String.fromCharCode(ch);
}
result += '.';
return dns_util.readName(raw_packet, offset, internal_offset, result);
},
decodeRR: function(raw_packet, offset, internal_offset, result) {
if(internal_offset > raw_packet.pcap_header.len) {
throw new Error("Malformed DNS RR. Offset is larger than the size of the packet (decodeRR). offset: " + offset + ", internal_offset: " + internal_offset + ", packet length: " + raw_packet.pcap_header.len);
}
var compressedName = raw_packet[internal_offset];
if((compressedName & 0xC0) == 0xC0) {
result.name = "";
result.name = dns_util.readName(raw_packet, offset, internal_offset - offset, result.name);
result.name = result.name.replace(/\.$/, '');
internal_offset += 2;
} else {
result.name = "";
var ch;
while((ch = raw_packet[internal_offset++]) != 0x00) {
result.name += String.fromCharCode(ch);
}
}
result.rrtype = dns_util.qtype_to_string(unpack.uint16(raw_packet, internal_offset));