@cto.af/pcap-ng-parser 2.0.3 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,12 @@
1
1
 
2
2
  # Overview
3
- @cto.af/pcap-ng-parser is a stream-based module to decode, print and analyze network traffic packets. With this module, you can read from an existing .pcapng file or connect it to an active stream.
3
+ @cto.af/pcap-ng-parser is a stream-based module to decode, print and analyze
4
+ network traffic packets. With this module, you can read from an existing
5
+ .pcap or .pcapng file or connect it to an active stream.
6
+
7
+ Implements:
8
+ - [draft-ietf-opsawg-pcap-06](https://www.ietf.org/archive/id/draft-ietf-opsawg-pcap-06.html)
9
+ - [draft-ietf-opsawg-pcapng-04](https://www.ietf.org/archive/id/draft-ietf-opsawg-pcapng-04.html)
4
10
 
5
11
  # Installation
6
12
 
@@ -12,42 +18,83 @@ $ npm install @cto.af/pcap-ng-parser
12
18
 
13
19
  # Usage
14
20
 
15
- ## Via .pcapng File
21
+ Note that the package on its own should work just fine in any modern JS
22
+ runtime (including browsers). However, reading from a file requires some
23
+ runtime-dependent code.
24
+
25
+ ## Via .pcapng File for Node.js
16
26
  Here is a quick example of how to log out packets to the console from a valid .pcapng file named `myfile.pcapng`.
17
27
 
18
28
  ```javascript
19
- import PCAPNGParser from '@cto.af/pcap-ng-parser';
20
- import fs from 'node:fs';
29
+ import {PCAPNGParser} from '@cto.af/pcap-ng-parser';
30
+ import fs from 'node:fs/promises';
31
+ const parser = new PCAPNGParser();
32
+ const file = await fs.open('examples/res/myfile.pcapng');
21
33
 
34
+ parser
35
+ .on('data', parsedPacket => {
36
+ console.log(parsedPacket);
37
+ })
38
+ .on('interface', interfaceInfo => {
39
+ console.log(interfaceInfo);
40
+ });
41
+ file.readableWebStream().pipeTo(parser);
42
+ ```
43
+
44
+ In the example above, we create a new ReadableStream from our file and pipe to
45
+ the instance `parser` which will cause various events to fire.
46
+
47
+ ## Via .pcapng file in Deno
48
+
49
+ ```js
50
+ import {PCAPNGParser} from '@cto.af/pcap-ng-parser';
22
51
  const pcapNgParser = new PCAPNGParser();
23
- const myFileStream = fs.createReadStream('./myfile.pcapng');
24
52
 
25
- myFileStream.pipe(pcapNgParser)
53
+ pcapNgParser
54
+ .on('data', parsedPacket => {
55
+ console.log(parsedPacket);
56
+ });
57
+
58
+ const f = await Deno.open('examples/res/myfile.pcapng');
59
+ f.readable.pipeTo(pcapNgParser);
60
+ ```
61
+
62
+ ## Via .pcapng file in Bun
63
+
64
+ ```js
65
+ import {PCAPNGParser} from './src/index.ts';
66
+ const pcapNgParser = new PCAPNGParser();
67
+
68
+ pcapNgParser
26
69
  .on('data', parsedPacket => {
27
70
  console.log(parsedPacket);
28
71
  })
29
72
  .on('interface', interfaceInfo => {
30
73
  console.log(interfaceInfo);
31
74
  });
32
- ```
33
75
 
34
- In the example above, we create a new Readable stream from our file and pipe the instance `pcapNgParser` which will read our packet data on the `_transform` event.
76
+ const f = Bun.file('examples/res/myfile.pcapng');
77
+ f.stream().pipeTo(pcapNgParser);
78
+ ```
35
79
 
36
- ## Via TCPDump
80
+ ## Via TCPDump in Node.JS
37
81
 
38
82
  You can also pipe from TCPDump using `process.stdin` for a command line interaction.
39
83
 
40
84
  ```javascript
41
- import PCAPNGParser from '@cto.af/pcap-ng-parser';
85
+ import {PCAPNGParser} from '@cto.af/pcap-ng-parser';
86
+ import {Readable} from 'node:stream';
42
87
  const pcapNgParser = new PCAPNGParser();
43
88
 
44
- process.stdin.pipe(pcapNgParser)
89
+ pcapNgParser
45
90
  .on('data', parsedPacket => {
46
91
  console.log(parsedPacket);
47
92
  })
48
93
  .on('interface', interfaceInfo => {
49
94
  console.log(interfaceInfo);
50
95
  });
96
+
97
+ Readable.toWeb(process.stdin).pipeTo(pcapNgParser);
51
98
  ```
52
99
 
53
100
  ```bash
@@ -56,13 +103,16 @@ $ sudo tcpdump -w - | node exampleAbove.js
56
103
 
57
104
  Note that in order to utilize tcpdump you must be a superuser. Refer to [tcpdump documentation](http://www.tcpdump.org/manpages/tcpdump.1.html) for details.
58
105
 
59
- Further note: If you specify an interface to listen on with "-i", tcpdump no
60
- longer uses the pcapng format.
106
+ Further note: If you specify an interface to listen on with "-i", tcpdump on some systems uses the old PCAP format, which PCAPNGParser will read just fine.
61
107
 
62
108
  ## Other Examples
63
109
 
64
110
  Additional examples can be found in the [examples directory](/examples/).
65
111
 
112
+ It allows you to read from a sample capture file (the default), a file (if you
113
+ specify a file name on the command line), or stdin (if you specify "-" as the
114
+ input file).
115
+
66
116
  # Class PCAPNGParser
67
117
 
68
118
  PCAPNGParser is an extension of the [stream.Transform
@@ -124,6 +174,9 @@ The following things have been added:
124
174
  - Added Enhanced Packet flag decoding.
125
175
  - Added converting timestamps to JS Dates. Does not handle timezones yet. (BREAKING)
126
176
  - Added generate API documentation.
177
+ - Added implementation of old .pcap format, with auto-detection.
178
+ - Added decoding of linkType names.
179
+ - Switched to web streams and Uint8Array, for portability. (BREAKING)
127
180
 
128
181
  [![Tests](https://github.com/cto-af/pcap-ng-parser/actions/workflows/node.js.yml/badge.svg)](https://github.com/cto-af/pcap-ng-parser/actions/workflows/node.js.yml)
129
182
  [![codecov](https://codecov.io/gh/cto-af/pcap-ng-parser/graph/badge.svg?token=Akjw67WYcn)](https://codecov.io/gh/cto-af/pcap-ng-parser)
package/package.json CHANGED
@@ -1,9 +1,8 @@
1
1
  {
2
2
  "name": "@cto.af/pcap-ng-parser",
3
- "version": "2.0.3",
3
+ "version": "3.0.0",
4
4
  "description": "Node.js Module for parsing .pcapng files and tcpdump output.",
5
- "main": "./src/PCAPNGParser.js",
6
- "types": "./types/PCAPNGParser.d.ts",
5
+ "main": "./lib/index.mjs",
7
6
  "type": "module",
8
7
  "files": [
9
8
  "src",
@@ -36,12 +35,13 @@
36
35
  }
37
36
  ],
38
37
  "license": "BSD-3",
39
- "packageManager": "pnpm@10.19.0",
40
- "engines": {
41
- "node": ">=20"
42
- },
43
38
  "dependencies": {
39
+ "@cto.af/utils": "1.3.6",
44
40
  "@leichtgewicht/ip-codec": "2.0.5",
45
- "nofilter": "6.3.0"
41
+ "dataview-stream": "^2.0.2"
42
+ },
43
+ "packageManager": "pnpm@10.25.0",
44
+ "engines": {
45
+ "node": ">=20"
46
46
  }
47
47
  }
package/src/blocks.ts ADDED
@@ -0,0 +1,89 @@
1
+ /**
2
+ * @see documentation at https://www.ietf.org/archive/id/draft-ietf-opsawg-pcapng-04.html#name-section-header-block
3
+ */
4
+ export const sectionHeaderBlock = {
5
+ byteOrderMagic: {read: 'u32'},
6
+ majorVersion: {read: 'u16'},
7
+ minorVersion: {read: 'u16'},
8
+ sectionLength: {read: 'i64'},
9
+ } as const;
10
+
11
+ /**
12
+ * @see definition at https://www.ietf.org/archive/id/draft-ietf-opsawg-pcapng-04.html#name-enhanced-packet-block
13
+ */
14
+ export const blockType = {
15
+ blockType: {read: 'i32'},
16
+ } as const;
17
+
18
+ /**
19
+ * @see definition at https://www.ietf.org/archive/id/draft-ietf-opsawg-pcapng-04.html#name-interface-description-block
20
+ */
21
+ export const interfaceDescriptionBlockFormat = {
22
+ linkType: {read: 'u16'},
23
+ reserved: {read: 'u16'},
24
+ snapLen: {read: 'u32'},
25
+ } as const;
26
+
27
+ export const nameResolutionFormat = {
28
+ recordType: {read: 'u16'},
29
+ recordValueLength: {read: 'u16'},
30
+ } as const;
31
+
32
+ /**
33
+ * @see definition at https://www.ietf.org/archive/id/draft-ietf-opsawg-pcapng-04.html#name-enhanced-packet-block
34
+ */
35
+ export const enhancedPacketBlockFormat = {
36
+ interfaceId: {read: 'u32'},
37
+ timestampHigh: {read: 'u32'},
38
+ timestampLow: {read: 'u32'},
39
+ capturedPacketLength: {read: 'u32'},
40
+ originalPacketLength: {read: 'u32'},
41
+
42
+ /* Packet Data */
43
+ /* Options */
44
+ } as const;
45
+
46
+ /**
47
+ * @see definition at https://www.ietf.org/archive/id/draft-ietf-opsawg-pcapng-04.html#name-options
48
+ */
49
+ export const optionBlock = {
50
+ optionType: {read: 'u16'},
51
+ dataLength: {read: 'u16'},
52
+ } as const;
53
+
54
+ export const interfaceStatisticsFormat = {
55
+ interfaceId: {read: 'u32'},
56
+ timestampHigh: {read: 'u32'},
57
+ timestampLow: {read: 'u32'},
58
+ } as const;
59
+
60
+ export const decryptionSecretsFormat = {
61
+ secretsType: {read: 'u32'},
62
+ secretsLength: {read: 'u32'},
63
+ } as const;
64
+
65
+ export const ifTsOffsetFormat = {
66
+ tsoffset: {size: 8, sign: true},
67
+ };
68
+
69
+ export const timestampFormat = {
70
+ timestampHigh: {read: 'u32'},
71
+ timestampLow: {read: 'u32'},
72
+ } as const;
73
+
74
+ export const pcapHeaderFormat = {
75
+ byteOrderMagic: {read: 'u32'},
76
+ majorVersion: {read: 'u16'},
77
+ minorVersion: {read: 'u16'},
78
+ _reserved1: {read: 'u32'},
79
+ _reserved2: {read: 'u32'},
80
+ snapLen: {read: 'u32'},
81
+ linkType: {read: 'u32'},
82
+ } as const;
83
+
84
+ export const pcapPacketFormat = {
85
+ timestampHigh: {read: 'u32'},
86
+ timestampLow: {read: 'u32'},
87
+ capturedPacketLength: {read: 'u32'},
88
+ originalPacketLength: {read: 'u32'},
89
+ } as const;
package/src/events.ts ADDED
@@ -0,0 +1,182 @@
1
+ export const EPB_FLAGS_DIRECTIONS = [
2
+ 'notAvailable',
3
+ 'inbound',
4
+ 'outbound',
5
+ 'invalid',
6
+ ] as const;
7
+
8
+ export const EPB_FLAGS_RECEPTION = [
9
+ 'notSpecified',
10
+ 'unicast',
11
+ 'multicast',
12
+ 'broadcast',
13
+ 'promiscuous',
14
+ 'invalid',
15
+ 'invalid',
16
+ 'invalid',
17
+ ] as const;
18
+
19
+ export const EPB_FLAGS_LINK_LAYER_ERRORS = [
20
+ 'symbol',
21
+ 'preamble',
22
+ 'startFrameDelimiter',
23
+ 'unalignedFrame',
24
+ 'wrongInterFrameGap',
25
+ 'packetTooShort',
26
+ 'packetTooLong',
27
+ 'CRC',
28
+ 'invalid',
29
+ 'invalid',
30
+ 'invalid',
31
+ 'invalid',
32
+ 'invalid',
33
+ 'invalid',
34
+ 'invalid',
35
+ 'invalid',
36
+ ] as const;
37
+
38
+ export interface GenericOption {
39
+ optionType: number;
40
+ dataLength?: number;
41
+ data?: Uint8Array;
42
+ str?: string;
43
+ bigint?: bigint;
44
+ date?: Date;
45
+ name?: string;
46
+ private?: boolean;
47
+ }
48
+
49
+ export interface PrivateEnterpriseNumber {
50
+ pen?: number;
51
+ }
52
+
53
+ export type Option = GenericOption & PrivateEnterpriseNumber;
54
+
55
+ export interface PacketFlags {
56
+ direction?: typeof EPB_FLAGS_DIRECTIONS[number];
57
+ reception?: typeof EPB_FLAGS_RECEPTION[number];
58
+ FCSlen?: number;
59
+ noChecksum?: boolean;
60
+ checksumValid?: boolean;
61
+ TCPsegmentationOffload?: boolean;
62
+ linkLayerErrors?: typeof EPB_FLAGS_LINK_LAYER_ERRORS[number][];
63
+ }
64
+
65
+ export interface Packet {
66
+ interfaceId: number;
67
+ timestamp?: Date; // To be replaced with Temporal.Instant one day.
68
+ flags?: PacketFlags;
69
+ originalPacketLength: number;
70
+ data: Uint8Array;
71
+ options: Option[];
72
+ }
73
+
74
+ export interface Interface {
75
+ /**
76
+ * @see See https://datatracker.ietf.org/doc/html/draft-ietf-opsawg-pcaplinktype for info.
77
+ */
78
+ linkType: number;
79
+ linkTypeName?: string;
80
+ snapLen: number;
81
+ name?: string;
82
+ tsoffset: bigint; // Offset, in MS, applied to each timestamp.
83
+ tsresol: bigint; // Timestamp resolution in MS.
84
+ options: Option[];
85
+ }
86
+
87
+ export type Endianess = 'BE' | 'LE';
88
+
89
+ export interface SectionHeader {
90
+ endianess: Endianess;
91
+ majorVersion: number;
92
+ minorVersion: number;
93
+ sectionLength: bigint;
94
+ options: Option[];
95
+ }
96
+
97
+ export interface IPv4ResolutionRecord {
98
+ name: 'nrb_record_ipv4';
99
+ ipv4: string;
100
+ entries: string[];
101
+ }
102
+
103
+ export interface IPv6ResolutionRecord {
104
+ name: 'nrb_record_ipv6';
105
+ ipv6: string;
106
+ entries: string[];
107
+ }
108
+
109
+ export interface EUI48ResolutionRecord {
110
+ name: 'nrb_record_eui48';
111
+ eui48: string;
112
+ entries: string[];
113
+ }
114
+
115
+ export interface EUI64ResolutionRecord {
116
+ name: 'nrb_record_eui64';
117
+ eui64: string;
118
+ entries: string[];
119
+ }
120
+
121
+ export type NameResolutionRecord =
122
+ IPv4ResolutionRecord | IPv6ResolutionRecord |
123
+ EUI48ResolutionRecord | EUI64ResolutionRecord;
124
+
125
+ export interface NameResolution {
126
+ records: NameResolutionRecord[];
127
+ options: Option[];
128
+ }
129
+
130
+ export interface InterfaceStatistics {
131
+ interfaceId: number;
132
+ timestamp: Date;
133
+ options: Option[];
134
+ }
135
+
136
+ export interface DecryptionSecrets {
137
+ secretsType: number;
138
+ data: Uint8Array;
139
+ options: Option[];
140
+ }
141
+
142
+ export interface CustomBlock {
143
+ pen: number;
144
+ data: Uint8Array;
145
+ copy: boolean;
146
+ }
147
+
148
+ // Needed before node 25.0.0.
149
+ export class ErrorEventPolyfill extends Event implements ErrorEvent {
150
+ #eventInit;
151
+
152
+ public constructor(type: string, eventInitDict: ErrorEventInit = {}) {
153
+ super(type, eventInitDict);
154
+ this.#eventInit = eventInitDict;
155
+ }
156
+
157
+ public get message(): string {
158
+ return this.#eventInit.message ?? '';
159
+ }
160
+
161
+ public get filename(): string {
162
+ return this.#eventInit.filename ?? '';
163
+ }
164
+
165
+ public get lineno(): number {
166
+ return this.#eventInit.lineno ?? 0;
167
+ }
168
+
169
+ public get colno(): number {
170
+ return this.#eventInit.colno ?? 0;
171
+ }
172
+
173
+ public get error(): any {
174
+ return this.#eventInit.error;
175
+ }
176
+ }
177
+
178
+ const LocalErrorEvent = globalThis.ErrorEvent ?? ErrorEventPolyfill;
179
+
180
+ export {
181
+ LocalErrorEvent as ErrorEvent,
182
+ };