@cto.af/pcap-ng-parser 2.1.0 → 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 +53 -12
- package/package.json +5 -5
- package/src/blocks.ts +89 -0
- package/src/events.ts +182 -0
- package/src/index.ts +931 -0
- package/src/{options.js → options.ts} +5 -12
- package/src/utils.ts +21 -0
- package/src/BlockConfig.js +0 -141
- package/src/PCAPNGParser.js +0 -1109
- package/types/BlockConfig.d.ts +0 -249
- package/types/PCAPNGParser.d.ts +0 -426
- package/types/linkTypes.d.ts +0 -6
- package/types/options.d.ts +0 -24
- /package/src/{linkTypes.js → linkTypes.ts} +0 -0
package/README.md
CHANGED
|
@@ -18,42 +18,83 @@ $ npm install @cto.af/pcap-ng-parser
|
|
|
18
18
|
|
|
19
19
|
# Usage
|
|
20
20
|
|
|
21
|
-
|
|
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
|
|
22
26
|
Here is a quick example of how to log out packets to the console from a valid .pcapng file named `myfile.pcapng`.
|
|
23
27
|
|
|
24
28
|
```javascript
|
|
25
|
-
import PCAPNGParser from '@cto.af/pcap-ng-parser';
|
|
26
|
-
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');
|
|
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
|
+
```
|
|
27
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';
|
|
28
51
|
const pcapNgParser = new PCAPNGParser();
|
|
29
|
-
const myFileStream = fs.createReadStream('./myfile.pcapng');
|
|
30
52
|
|
|
31
|
-
|
|
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
|
|
32
69
|
.on('data', parsedPacket => {
|
|
33
70
|
console.log(parsedPacket);
|
|
34
71
|
})
|
|
35
72
|
.on('interface', interfaceInfo => {
|
|
36
73
|
console.log(interfaceInfo);
|
|
37
74
|
});
|
|
38
|
-
```
|
|
39
75
|
|
|
40
|
-
|
|
76
|
+
const f = Bun.file('examples/res/myfile.pcapng');
|
|
77
|
+
f.stream().pipeTo(pcapNgParser);
|
|
78
|
+
```
|
|
41
79
|
|
|
42
|
-
## Via TCPDump
|
|
80
|
+
## Via TCPDump in Node.JS
|
|
43
81
|
|
|
44
82
|
You can also pipe from TCPDump using `process.stdin` for a command line interaction.
|
|
45
83
|
|
|
46
84
|
```javascript
|
|
47
|
-
import PCAPNGParser from '@cto.af/pcap-ng-parser';
|
|
85
|
+
import {PCAPNGParser} from '@cto.af/pcap-ng-parser';
|
|
86
|
+
import {Readable} from 'node:stream';
|
|
48
87
|
const pcapNgParser = new PCAPNGParser();
|
|
49
88
|
|
|
50
|
-
|
|
89
|
+
pcapNgParser
|
|
51
90
|
.on('data', parsedPacket => {
|
|
52
91
|
console.log(parsedPacket);
|
|
53
92
|
})
|
|
54
93
|
.on('interface', interfaceInfo => {
|
|
55
94
|
console.log(interfaceInfo);
|
|
56
95
|
});
|
|
96
|
+
|
|
97
|
+
Readable.toWeb(process.stdin).pipeTo(pcapNgParser);
|
|
57
98
|
```
|
|
58
99
|
|
|
59
100
|
```bash
|
|
@@ -62,8 +103,7 @@ $ sudo tcpdump -w - | node exampleAbove.js
|
|
|
62
103
|
|
|
63
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.
|
|
64
105
|
|
|
65
|
-
Further note: If you specify an interface to listen on with "-i", tcpdump
|
|
66
|
-
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.
|
|
67
107
|
|
|
68
108
|
## Other Examples
|
|
69
109
|
|
|
@@ -136,6 +176,7 @@ The following things have been added:
|
|
|
136
176
|
- Added generate API documentation.
|
|
137
177
|
- Added implementation of old .pcap format, with auto-detection.
|
|
138
178
|
- Added decoding of linkType names.
|
|
179
|
+
- Switched to web streams and Uint8Array, for portability. (BREAKING)
|
|
139
180
|
|
|
140
181
|
[](https://github.com/cto-af/pcap-ng-parser/actions/workflows/node.js.yml)
|
|
141
182
|
[](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": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Node.js Module for parsing .pcapng files and tcpdump output.",
|
|
5
|
-
"main": "./
|
|
6
|
-
"types": "./types/PCAPNGParser.d.ts",
|
|
5
|
+
"main": "./lib/index.mjs",
|
|
7
6
|
"type": "module",
|
|
8
7
|
"files": [
|
|
9
8
|
"src",
|
|
@@ -37,10 +36,11 @@
|
|
|
37
36
|
],
|
|
38
37
|
"license": "BSD-3",
|
|
39
38
|
"dependencies": {
|
|
39
|
+
"@cto.af/utils": "1.3.6",
|
|
40
40
|
"@leichtgewicht/ip-codec": "2.0.5",
|
|
41
|
-
"
|
|
41
|
+
"dataview-stream": "^2.0.2"
|
|
42
42
|
},
|
|
43
|
-
"packageManager": "pnpm@10.
|
|
43
|
+
"packageManager": "pnpm@10.25.0",
|
|
44
44
|
"engines": {
|
|
45
45
|
"node": ">=20"
|
|
46
46
|
}
|
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
|
+
};
|