@cto.af/pcap-ng-parser 2.1.0 → 3.0.1
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/lib/blocks.mjs +1 -0
- package/lib/events.d.mts +97 -0
- package/lib/events.mjs +1 -0
- package/lib/index.d.mts +119 -0
- package/lib/index.mjs +1 -0
- package/lib/linkTypes.mjs +1 -0
- package/lib/options.d.mts +5 -0
- package/lib/options.mjs +1 -0
- package/lib/utils.mjs +1 -0
- package/package.json +6 -7
- package/src/BlockConfig.js +0 -141
- package/src/PCAPNGParser.js +0 -1109
- package/src/linkTypes.js +0 -223
- package/src/options.js +0 -105
- 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/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/lib/blocks.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const sectionHeaderBlock={byteOrderMagic:{read:`u32`},majorVersion:{read:`u16`},minorVersion:{read:`u16`},sectionLength:{read:`i64`}},interfaceDescriptionBlockFormat={linkType:{read:`u16`},reserved:{read:`u16`},snapLen:{read:`u32`}},nameResolutionFormat={recordType:{read:`u16`},recordValueLength:{read:`u16`}},enhancedPacketBlockFormat={interfaceId:{read:`u32`},timestampHigh:{read:`u32`},timestampLow:{read:`u32`},capturedPacketLength:{read:`u32`},originalPacketLength:{read:`u32`}},optionBlock={optionType:{read:`u16`},dataLength:{read:`u16`}},interfaceStatisticsFormat={interfaceId:{read:`u32`},timestampHigh:{read:`u32`},timestampLow:{read:`u32`}},decryptionSecretsFormat={secretsType:{read:`u32`},secretsLength:{read:`u32`}},timestampFormat={timestampHigh:{read:`u32`},timestampLow:{read:`u32`}},pcapHeaderFormat={byteOrderMagic:{read:`u32`},majorVersion:{read:`u16`},minorVersion:{read:`u16`},_reserved1:{read:`u32`},_reserved2:{read:`u32`},snapLen:{read:`u32`},linkType:{read:`u32`}},pcapPacketFormat={timestampHigh:{read:`u32`},timestampLow:{read:`u32`},capturedPacketLength:{read:`u32`},originalPacketLength:{read:`u32`}};export{decryptionSecretsFormat,enhancedPacketBlockFormat,interfaceDescriptionBlockFormat,interfaceStatisticsFormat,nameResolutionFormat,optionBlock,pcapHeaderFormat,pcapPacketFormat,sectionHeaderBlock,timestampFormat};
|
package/lib/events.d.mts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
//#region src/events.d.ts
|
|
2
|
+
declare const EPB_FLAGS_DIRECTIONS: readonly ["notAvailable", "inbound", "outbound", "invalid"];
|
|
3
|
+
declare const EPB_FLAGS_RECEPTION: readonly ["notSpecified", "unicast", "multicast", "broadcast", "promiscuous", "invalid", "invalid", "invalid"];
|
|
4
|
+
declare const EPB_FLAGS_LINK_LAYER_ERRORS: readonly ["symbol", "preamble", "startFrameDelimiter", "unalignedFrame", "wrongInterFrameGap", "packetTooShort", "packetTooLong", "CRC", "invalid", "invalid", "invalid", "invalid", "invalid", "invalid", "invalid", "invalid"];
|
|
5
|
+
interface GenericOption {
|
|
6
|
+
optionType: number;
|
|
7
|
+
dataLength?: number;
|
|
8
|
+
data?: Uint8Array;
|
|
9
|
+
str?: string;
|
|
10
|
+
bigint?: bigint;
|
|
11
|
+
date?: Date;
|
|
12
|
+
name?: string;
|
|
13
|
+
private?: boolean;
|
|
14
|
+
}
|
|
15
|
+
interface PrivateEnterpriseNumber {
|
|
16
|
+
pen?: number;
|
|
17
|
+
}
|
|
18
|
+
type Option = GenericOption & PrivateEnterpriseNumber;
|
|
19
|
+
interface PacketFlags {
|
|
20
|
+
direction?: typeof EPB_FLAGS_DIRECTIONS[number];
|
|
21
|
+
reception?: typeof EPB_FLAGS_RECEPTION[number];
|
|
22
|
+
FCSlen?: number;
|
|
23
|
+
noChecksum?: boolean;
|
|
24
|
+
checksumValid?: boolean;
|
|
25
|
+
TCPsegmentationOffload?: boolean;
|
|
26
|
+
linkLayerErrors?: typeof EPB_FLAGS_LINK_LAYER_ERRORS[number][];
|
|
27
|
+
}
|
|
28
|
+
interface Packet {
|
|
29
|
+
interfaceId: number;
|
|
30
|
+
timestamp?: Date;
|
|
31
|
+
flags?: PacketFlags;
|
|
32
|
+
originalPacketLength: number;
|
|
33
|
+
data: Uint8Array;
|
|
34
|
+
options: Option[];
|
|
35
|
+
}
|
|
36
|
+
interface Interface {
|
|
37
|
+
/**
|
|
38
|
+
* @see See https://datatracker.ietf.org/doc/html/draft-ietf-opsawg-pcaplinktype for info.
|
|
39
|
+
*/
|
|
40
|
+
linkType: number;
|
|
41
|
+
linkTypeName?: string;
|
|
42
|
+
snapLen: number;
|
|
43
|
+
name?: string;
|
|
44
|
+
tsoffset: bigint;
|
|
45
|
+
tsresol: bigint;
|
|
46
|
+
options: Option[];
|
|
47
|
+
}
|
|
48
|
+
type Endianess = 'BE' | 'LE';
|
|
49
|
+
interface SectionHeader {
|
|
50
|
+
endianess: Endianess;
|
|
51
|
+
majorVersion: number;
|
|
52
|
+
minorVersion: number;
|
|
53
|
+
sectionLength: bigint;
|
|
54
|
+
options: Option[];
|
|
55
|
+
}
|
|
56
|
+
interface IPv4ResolutionRecord {
|
|
57
|
+
name: 'nrb_record_ipv4';
|
|
58
|
+
ipv4: string;
|
|
59
|
+
entries: string[];
|
|
60
|
+
}
|
|
61
|
+
interface IPv6ResolutionRecord {
|
|
62
|
+
name: 'nrb_record_ipv6';
|
|
63
|
+
ipv6: string;
|
|
64
|
+
entries: string[];
|
|
65
|
+
}
|
|
66
|
+
interface EUI48ResolutionRecord {
|
|
67
|
+
name: 'nrb_record_eui48';
|
|
68
|
+
eui48: string;
|
|
69
|
+
entries: string[];
|
|
70
|
+
}
|
|
71
|
+
interface EUI64ResolutionRecord {
|
|
72
|
+
name: 'nrb_record_eui64';
|
|
73
|
+
eui64: string;
|
|
74
|
+
entries: string[];
|
|
75
|
+
}
|
|
76
|
+
type NameResolutionRecord = IPv4ResolutionRecord | IPv6ResolutionRecord | EUI48ResolutionRecord | EUI64ResolutionRecord;
|
|
77
|
+
interface NameResolution {
|
|
78
|
+
records: NameResolutionRecord[];
|
|
79
|
+
options: Option[];
|
|
80
|
+
}
|
|
81
|
+
interface InterfaceStatistics {
|
|
82
|
+
interfaceId: number;
|
|
83
|
+
timestamp: Date;
|
|
84
|
+
options: Option[];
|
|
85
|
+
}
|
|
86
|
+
interface DecryptionSecrets {
|
|
87
|
+
secretsType: number;
|
|
88
|
+
data: Uint8Array;
|
|
89
|
+
options: Option[];
|
|
90
|
+
}
|
|
91
|
+
interface CustomBlock {
|
|
92
|
+
pen: number;
|
|
93
|
+
data: Uint8Array;
|
|
94
|
+
copy: boolean;
|
|
95
|
+
}
|
|
96
|
+
//#endregion
|
|
97
|
+
export { CustomBlock, DecryptionSecrets, EUI48ResolutionRecord, EUI64ResolutionRecord, Endianess, GenericOption, IPv4ResolutionRecord, IPv6ResolutionRecord, Interface, InterfaceStatistics, NameResolution, NameResolutionRecord, Option, Packet, PacketFlags, PrivateEnterpriseNumber, SectionHeader };
|
package/lib/events.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const EPB_FLAGS_DIRECTIONS=[`notAvailable`,`inbound`,`outbound`,`invalid`],EPB_FLAGS_RECEPTION=[`notSpecified`,`unicast`,`multicast`,`broadcast`,`promiscuous`,`invalid`,`invalid`,`invalid`],EPB_FLAGS_LINK_LAYER_ERRORS=[`symbol`,`preamble`,`startFrameDelimiter`,`unalignedFrame`,`wrongInterFrameGap`,`packetTooShort`,`packetTooLong`,`CRC`,`invalid`,`invalid`,`invalid`,`invalid`,`invalid`,`invalid`,`invalid`,`invalid`];var ErrorEventPolyfill=class extends Event{#eventInit;constructor(type,eventInitDict={}){super(type,eventInitDict),this.#eventInit=eventInitDict}get message(){return this.#eventInit.message??``}get filename(){return this.#eventInit.filename??``}get lineno(){return this.#eventInit.lineno??0}get colno(){return this.#eventInit.colno??0}get error(){return this.#eventInit.error}};const LocalErrorEvent=globalThis.ErrorEvent??ErrorEventPolyfill;export{EPB_FLAGS_DIRECTIONS,EPB_FLAGS_LINK_LAYER_ERRORS,EPB_FLAGS_RECEPTION,LocalErrorEvent};
|
package/lib/index.d.mts
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { OptionType } from "./options.mjs";
|
|
2
|
+
import { CustomBlock, DecryptionSecrets, EUI48ResolutionRecord, EUI64ResolutionRecord, Endianess, GenericOption, IPv4ResolutionRecord, IPv6ResolutionRecord, Interface, InterfaceStatistics, NameResolution, NameResolutionRecord, Option, Packet, PacketFlags, PrivateEnterpriseNumber, SectionHeader } from "./events.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/index.d.ts
|
|
5
|
+
type EventCallback<T> = ((evt: T) => void) | {
|
|
6
|
+
handleEvent(evt: T): void;
|
|
7
|
+
} | null;
|
|
8
|
+
/**
|
|
9
|
+
* An error has occured when parsing inside a block. This error can be
|
|
10
|
+
* ignored and parsing can continue within the next block safely.
|
|
11
|
+
*/
|
|
12
|
+
declare class RecoverableError extends Error {
|
|
13
|
+
constructor(err: unknown);
|
|
14
|
+
}
|
|
15
|
+
interface ParseEvents {
|
|
16
|
+
close: Event;
|
|
17
|
+
custom: CustomEvent<CustomBlock>;
|
|
18
|
+
error: ErrorEvent;
|
|
19
|
+
interface: CustomEvent<Interface>;
|
|
20
|
+
names: CustomEvent<NameResolution>;
|
|
21
|
+
data: CustomEvent<Packet>;
|
|
22
|
+
section: CustomEvent<SectionHeader>;
|
|
23
|
+
secrets: CustomEvent<DecryptionSecrets>;
|
|
24
|
+
stats: CustomEvent<InterfaceStatistics>;
|
|
25
|
+
unknown: CustomEvent<number>;
|
|
26
|
+
}
|
|
27
|
+
type ParseEventsDetail<K extends keyof ParseEvents> = ParseEvents[K] extends CustomEvent<infer T> ? T : ParseEvents[K] extends ErrorEvent ? any : void;
|
|
28
|
+
interface ParserOptions {
|
|
29
|
+
/** If true, don't allow old PCAP format. */
|
|
30
|
+
rejectOld?: boolean;
|
|
31
|
+
/** If true, don't allow new PCAPng format. */
|
|
32
|
+
rejectNG?: boolean;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Parse a PCAPng or old-style PCAP file from a stream.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```js
|
|
39
|
+
* import fs from 'node:fs/promises';
|
|
40
|
+
* import {PCAPNGParser} from '@cto.af/pcap-ng-parser';
|
|
41
|
+
*
|
|
42
|
+
* const parser = new PCAPNGParser();
|
|
43
|
+
* parser.on('data', pkt => console.log(pkt.data)); // Uint8Array
|
|
44
|
+
* const file = await fs.open('examples/res/myfile.pcapng');
|
|
45
|
+
* file.readableWebStream().pipeTo(parser);
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
declare class PCAPNGParser extends WritableStream<Uint8Array> implements EventTarget {
|
|
49
|
+
#private;
|
|
50
|
+
/**
|
|
51
|
+
* Interface information receieved for the current section, in order of
|
|
52
|
+
* reception.
|
|
53
|
+
*/
|
|
54
|
+
interfaces: Interface[];
|
|
55
|
+
constructor(opts?: ParserOptions);
|
|
56
|
+
/**
|
|
57
|
+
* Is this file in the pcap-ng format?
|
|
58
|
+
*/
|
|
59
|
+
get ng(): boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Endianness of the current section.
|
|
62
|
+
* Little-endian => LE.
|
|
63
|
+
* Big-endian => BE.
|
|
64
|
+
*/
|
|
65
|
+
get endianess(): Endianess;
|
|
66
|
+
/**
|
|
67
|
+
* The **`addEventListener()`** method of the EventTarget interface sets up
|
|
68
|
+
* a function that will be called whenever the specified event is delivered
|
|
69
|
+
* to the target.
|
|
70
|
+
*
|
|
71
|
+
* @see https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener
|
|
72
|
+
*/
|
|
73
|
+
addEventListener<E extends keyof ParseEvents>(type: E, callback: EventCallback<ParseEvents[E]>, options?: AddEventListenerOptions | boolean): void;
|
|
74
|
+
/**
|
|
75
|
+
* The **`dispatchEvent()`** method of the EventTarget sends an Event to the
|
|
76
|
+
* object, (synchronously) invoking the affected event listeners in the
|
|
77
|
+
* appropriate order.
|
|
78
|
+
*
|
|
79
|
+
* @see https://developer.mozilla.org/docs/Web/API/EventTarget/dispatchEvent
|
|
80
|
+
*/
|
|
81
|
+
dispatchEvent(event: ParseEvents[keyof ParseEvents]): boolean;
|
|
82
|
+
/**
|
|
83
|
+
* The **`removeEventListener()`** method of the EventTarget interface
|
|
84
|
+
* removes an event listener previously registered with
|
|
85
|
+
* EventTarget.addEventListener() from the target.
|
|
86
|
+
*
|
|
87
|
+
* @see https://developer.mozilla.org/docs/Web/API/EventTarget/removeEventListener
|
|
88
|
+
*/
|
|
89
|
+
removeEventListener<E extends keyof ParseEvents>(event: E, calback: EventCallback<ParseEvents[E]>, options?: EventListenerOptions | boolean): void;
|
|
90
|
+
/**
|
|
91
|
+
* Listen for an event, unwrapping the detailor error in the event so that
|
|
92
|
+
* it is easier to process.
|
|
93
|
+
*
|
|
94
|
+
* @param eventName Event name.
|
|
95
|
+
* @param listener Handler.
|
|
96
|
+
* @returns This, for chaining.
|
|
97
|
+
*/
|
|
98
|
+
on<E extends keyof ParseEvents>(eventName: E, listener: (detail: ParseEventsDetail<E>) => void): this;
|
|
99
|
+
/**
|
|
100
|
+
* Listen for the first occurence of a given event. Does the same
|
|
101
|
+
* unwrapping as `on`.
|
|
102
|
+
*
|
|
103
|
+
* @param eventName Event name.
|
|
104
|
+
* @param listener Handler.
|
|
105
|
+
* @returns This, for chaining.
|
|
106
|
+
*/
|
|
107
|
+
once<E extends keyof ParseEvents>(eventName: E, listener: (detail: ParseEventsDetail<E>) => void): this;
|
|
108
|
+
/**
|
|
109
|
+
* Stop listening for the given event.
|
|
110
|
+
*
|
|
111
|
+
* @param eventName Event name.
|
|
112
|
+
* @param listener Handler to remove.
|
|
113
|
+
* @returns This, for chaining.
|
|
114
|
+
* @throws On unknown listener.
|
|
115
|
+
*/
|
|
116
|
+
off<E extends keyof ParseEvents>(eventName: E, listener: (detail: ParseEventsDetail<E>) => void): this;
|
|
117
|
+
}
|
|
118
|
+
//#endregion
|
|
119
|
+
export { type CustomBlock, type DecryptionSecrets, type EUI48ResolutionRecord, type EUI64ResolutionRecord, type Endianess, EventCallback, type GenericOption, type IPv4ResolutionRecord, type IPv6ResolutionRecord, type Interface, type InterfaceStatistics, type NameResolution, type NameResolutionRecord, type Option, type OptionType, PCAPNGParser, PCAPNGParser as default, type Packet, type PacketFlags, ParseEvents, ParseEventsDetail, ParserOptions, type PrivateEnterpriseNumber, RecoverableError, type SectionHeader };
|
package/lib/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{decryptionSecretsFormat,enhancedPacketBlockFormat,interfaceDescriptionBlockFormat,interfaceStatisticsFormat,nameResolutionFormat,optionBlock,pcapHeaderFormat,pcapPacketFormat,sectionHeaderBlock,timestampFormat}from"./blocks.mjs";import{CUSTOM_COPY,CUSTOM_NOCOPY,DECRYPTION_SECRETS,ENHANCED_PACKET,INTERFACE_DESCRIPTION,INTERFACE_STATISTICS,NAME_RESOLUTION,OPTION_NAMES,SECTION_HEADER,SIMPLE_PACKET}from"./options.mjs";import{EPB_FLAGS_DIRECTIONS,EPB_FLAGS_LINK_LAYER_ERRORS,EPB_FLAGS_RECEPTION,LocalErrorEvent}from"./events.mjs";import{colons,pad4}from"./utils.mjs";import{LINK_TYPE_NAMES}from"./linkTypes.mjs";import{DataViewReader,DataViewWritableStream,TruncationError}from"dataview-stream";import{assert}from"@cto.af/utils";import{decode}from"@leichtgewicht/ip-codec";const TD=new TextDecoder;var RecoverableError=class extends Error{constructor(err){super(err instanceof Error?err.message:`Recoverable Error`,{cause:err})}},PCAPNGParser=class extends WritableStream{interfaces=[];#listeners=new WeakMap;#et=new EventTarget;#dv;#ng=!0;#opts;constructor(opts={}){let dv=new DataViewWritableStream,writer=dv.getWriter(),self;super({write(chunk){return writer.write(chunk)},async abort(reason){await writer.abort(reason),self?.dispatchEvent(new LocalErrorEvent(`error`,{error:reason}))},async close(){await writer.close()}}),self=this,this.#opts={rejectOld:!1,rejectNG:!1,...opts},this.#dv=dv,this.#readFile()}get ng(){return this.#ng}get endianess(){return this.#dv.littleEndian?`LE`:`BE`}addEventListener(type,callback,options){this.#et.addEventListener(type,callback,options)}dispatchEvent(event){return this.#et.dispatchEvent(event)}removeEventListener(type,callback,options){this.#et.removeEventListener(type,callback,options)}on(eventName,listener){let wrapper=ev=>{eventName===`close`&&listener(),ev instanceof CustomEvent?listener(ev.detail):ev instanceof LocalErrorEvent&&listener(ev.error)};return this.#listeners.set(listener,wrapper),this.addEventListener(eventName,wrapper),this}once(eventName,listener){let wrapper=ev=>{this.#listeners.delete(listener),eventName===`close`&&listener(),ev instanceof CustomEvent?listener(ev.detail):ev instanceof LocalErrorEvent&&listener(ev.error)};return this.#listeners.set(listener,wrapper),this.addEventListener(eventName,wrapper,{once:!0}),this}off(eventName,listener){let wrapper=this.#listeners.get(listener);if(!wrapper)throw Error(`Unknown listener`);return this.removeEventListener(eventName,wrapper),this.#listeners.delete(listener),this}#timestamp(stampHigh,stampLow,interfaceId){let stamp=BigInt(stampHigh)<<32n|BigInt(stampLow),int=this.interfaces[interfaceId];assert(int,`Interface always checked before calling`);let off=int.tsoffset+stamp/int.tsresol;return new Date(Number(off))}#dvr(bytes){return new DataViewReader(bytes,{littleEndian:this.#dv.littleEndian})}async#readFile(){try{let magicBuf=await this.#dv.peek(8),magic=this.#dvr(magicBuf).u32();if(magic===SECTION_HEADER)await this.#readSections();else if(magic===2712847316)await this.#readPCAP(!1,!1);else if(magic===3569595041)await this.#readPCAP(!0,!1);else if(magic===2712812621)await this.#readPCAP(!1,!0);else if(magic===1295823521)await this.#readPCAP(!0,!0);else throw Error(`Invalid file format: magic = ${magic}`)}catch(error){this.dispatchEvent(new LocalErrorEvent(`error`,{error}))}this.dispatchEvent(new Event(`close`))}async#readPCAP(littleEndian,nano){if(this.#opts.rejectOld)throw Error(`Old PCAP format detected and rejected`);littleEndian&&(this.#dv.littleEndian=!this.#dv.littleEndian),this.#ng=!1;let{linkType,snapLen}=await this.#dv.struct(pcapHeaderFormat),tsresol=nano?1000000n:1000n,int={linkType:linkType&65535,snapLen,tsresol,tsoffset:0n,options:[]},ltName=LINK_TYPE_NAMES.get(int.linkType);if(ltName&&(int.linkTypeName=ltName),linkType&67108864){let FCSlen=linkType>>28;int.options.push({optionType:13,name:`if_fcslen`,data:new Uint8Array([FCSlen*16])})}for(this.interfaces.push(int),this.dispatchEvent(new CustomEvent(`interface`,{detail:int}));;){try{await this.#dv.waitFor(4)}catch(er){if(er instanceof TruncationError&&er.requested===4)return;throw er}let{timestampHigh,timestampLow,capturedPacketLength,originalPacketLength}=await this.#dv.struct(pcapPacketFormat),data=await this.#dv.bytes(capturedPacketLength),pkt={interfaceId:0,timestamp:new Date(timestampHigh*1e3+timestampLow/Number(tsresol)),originalPacketLength,data,options:[]};this.dispatchEvent(new CustomEvent(`data`,{detail:pkt}))}}async#readSections(){if(this.#opts.rejectNG)throw Error(`PCAPng format detected and rejected`);for(;;){let block=await this.#readBlock();if(!block)break;try{switch(block.blockType){case SECTION_HEADER:await this.#processSectionHeader(block);break;case INTERFACE_DESCRIPTION:await this.#processInterface(block);break;case SIMPLE_PACKET:await this.#processSimplePacket(block);break;case NAME_RESOLUTION:await this.#processNameResolution(block);break;case INTERFACE_STATISTICS:await this.#processInterfaceStatistics(block);break;case ENHANCED_PACKET:await this.#processEnhancedPacket(block);break;case DECRYPTION_SECRETS:await this.#processDecryptionSecrets(block);break;case CUSTOM_COPY:case CUSTOM_NOCOPY:await this.#processCustom(block);break;default:this.dispatchEvent(new CustomEvent(`unknown`,{detail:block.blockType}));break}}catch(error){this.dispatchEvent(new LocalErrorEvent(`error`,{error:new RecoverableError(error)}))}}}async#readBlock(){let blockType;try{blockType=await this.#dv.u32()}catch(er){assert(er instanceof TruncationError),assert(er.requested===4);return}if(blockType===SECTION_HEADER){let prelude=await this.#dv.peek(8),dv=this.#dvr(prelude);dv.skip(4);let bom=dv.u32();if(bom!==439041101)if(bom===1295788826)this.#dv.littleEndian=!this.#dv.littleEndian;else throw Error(`Unable to determine endian from 0x${bom.toString(16)}`)}let blockTotalLength=await this.#dv.u32(),dataLen=blockTotalLength-12,data=await this.#dv.bytes(dataLen),block={blockType,blockTotalLength,data:this.#dvr(data)};await this.#dv.bytes(pad4(dataLen));let endTotalLength=await this.#dv.u32();if(endTotalLength!==blockTotalLength)throw Error(`Length mismatch, ${endTotalLength} != ${blockTotalLength}`);return block}#readOptions(block,interfaceId){let foundEndOption=!1,options=[];for(;!block.data.finished&&!foundEndOption;){let rb=block.data.struct(optionBlock);if(rb.optionType===0)foundEndOption=!0;else{let pen,name,optLen=rb.dataLength,typ,desc=OPTION_NAMES.get(block.blockType)?.get(rb.optionType);desc&&(name={name:desc[0]},typ=desc[1],desc[2]&&(pen={pen:block.data.u32()},optLen-=4));let opt={optionType:rb.optionType,...name,...pen};switch(rb.optionType&32768&&(opt.private=!0),typ){case`string`:{let left=block.data.bytes(optLen);opt.str=TD.decode(left).replaceAll(`\0`,``).trim();break}case`ipv4`:case`ipv6`:opt.str=decode(block.data.bytes(rb.dataLength));break;case`ipv4mask`:if(rb.dataLength!==8)throw Error(`Invalid ipv4mask option`);opt.str=`${decode(block.data.bytes(4))}/${decode(block.data.bytes(4))}`;break;case`ipv6prefix`:if(rb.dataLength!==17)throw Error(`Invalid ipv6prefix option`);opt.str=`${decode(block.data.bytes(16))}/${block.data.u8()}`;break;case`eui`:opt.str=colons(block.data.bytes(rb.dataLength));break;case`u32`:opt.bigint=BigInt(block.data.u32());break;case`u64`:opt.bigint=block.data.u64();break;case`timestamp`:{let{timestampHigh,timestampLow}=block.data.struct(timestampFormat);opt.date=this.#timestamp(timestampHigh,timestampLow,interfaceId);break}default:opt.data=block.data.bytes(rb.dataLength);break}block.data.skip(pad4(rb.dataLength)),options.push(opt)}}return options}#processSectionHeader(block){let detail={...block.data.struct(sectionHeaderBlock),endianess:this.endianess,options:this.#readOptions(block)};this.interfaces=[],this.dispatchEvent(new CustomEvent(`section`,{detail}))}#processInterface(block){let idRes=block.data.struct(interfaceDescriptionBlockFormat),iData={linkType:idRes.linkType,snapLen:idRes.snapLen,tsoffset:0n,tsresol:1000n,options:this.#readOptions(block)};for(let opt of iData.options)switch(opt.optionType){case 2:iData.name=opt.str;break;case 14:iData.tsoffset=opt.bigint*1000n;break;case 9:{let tsresol=opt.data[0];tsresol&128?iData.tsresol=(1n<<BigInt(tsresol&127))/1000n:iData.tsresol=10n**(BigInt(tsresol)-3n);break}}this.interfaces.push(iData),this.dispatchEvent(new CustomEvent(`interface`,{detail:iData}))}#processSimplePacket(block){let[int]=this.interfaces;if(!int)throw Error(`No interface for simple packet`);let originalPacketLength=block.data.u32(),len=Math.min(originalPacketLength,int.snapLen),pkt={interfaceId:0,originalPacketLength,data:block.data.bytes(len),options:[]};this.dispatchEvent(new CustomEvent(`data`,{detail:pkt}))}#processNameResolution(block){let res={records:[],options:[]};for(;;){let{recordType,recordValueLength}=block.data.struct(nameResolutionFormat);if(recordType===0)break;let val=block.data.bytes(recordValueLength);switch(block.data.skip(pad4(recordValueLength)),recordType){case 1:if(recordValueLength<6)throw Error(`Invalid nrb_record_ipv4 record`);res.records.push({name:`nrb_record_ipv4`,ipv4:decode(val.subarray(0,4)),entries:TD.decode(val.subarray(4)).split(`\0`).slice(0,-1)});break;case 2:if(recordValueLength<18)throw Error(`Invalid nrb_record_ipv6 record`);res.records.push({name:`nrb_record_ipv6`,ipv6:decode(val.subarray(0,16)),entries:TD.decode(val.subarray(16)).split(`\0`).slice(0,-1)});break;case 3:if(recordValueLength<8)throw Error(`Invalid nrb_record_eui48 record`);res.records.push({name:`nrb_record_eui48`,eui48:colons(val.subarray(0,6)),entries:TD.decode(val.subarray(6)).split(`\0`).slice(0,-1)});break;case 4:if(recordValueLength<10)throw Error(`Invalid nrb_record_eui64 record`);res.records.push({name:`nrb_record_eui64`,eui64:colons(val.subarray(0,8)),entries:TD.decode(val.subarray(8)).split(`\0`).slice(0,-1)});break}}res.options=this.#readOptions(block),this.dispatchEvent(new CustomEvent(`names`,{detail:res}))}#processInterfaceStatistics(block){let{interfaceId,timestampHigh,timestampLow}=block.data.struct(interfaceStatisticsFormat);if(interfaceId>=this.interfaces.length)throw Error(`Invalid interface id: ${interfaceId}`);let stats={interfaceId,timestamp:this.#timestamp(timestampHigh,timestampLow,interfaceId),options:this.#readOptions(block,interfaceId)};this.dispatchEvent(new CustomEvent(`stats`,{detail:stats}))}#processEnhancedPacket(block){let{interfaceId,timestampHigh,timestampLow,capturedPacketLength,originalPacketLength}=block.data.struct(enhancedPacketBlockFormat);if(interfaceId>=this.interfaces.length)throw Error(`Invalid interface ID: ${interfaceId} >= ${this.interfaces.length}`);let data=block.data.bytes(capturedPacketLength),pkt={interfaceId,timestamp:this.#timestamp(timestampHigh,timestampLow,interfaceId),originalPacketLength,data,options:[]};block.data.skip(pad4(capturedPacketLength)),pkt.options=this.#readOptions(block,interfaceId);for(let o of pkt.options)if(o.optionType===2){assert(o.data);let flags=this.#dvr(o.data).u32();pkt.flags={direction:EPB_FLAGS_DIRECTIONS[flags&3],reception:EPB_FLAGS_RECEPTION[flags>>2&7],FCSlen:flags>>5&15,noChecksum:!!(flags>>9&1),checksumValid:!!(flags>>10&1),TCPsegmentationOffload:!!(flags>>11&1),linkLayerErrors:[]},flags>>=16;for(let i=0;i<8;i++)flags&1&&pkt.flags.linkLayerErrors?.push(EPB_FLAGS_LINK_LAYER_ERRORS[i]),flags>>=1}this.dispatchEvent(new CustomEvent(`data`,{detail:pkt}))}#processDecryptionSecrets(block){let{secretsType,secretsLength}=block.data.struct(decryptionSecretsFormat),detail={secretsType,data:block.data.bytes(secretsLength),options:[]};block.data.skip(pad4(secretsLength)),detail.options=this.#readOptions(block),this.dispatchEvent(new CustomEvent(`secrets`,{detail}))}#processCustom(block){let detail={pen:block.data.u32(),data:block.data.unused(),copy:block.blockType===CUSTOM_COPY};this.dispatchEvent(new CustomEvent(`custom`,{detail}))}},src_default=PCAPNGParser;export{PCAPNGParser,RecoverableError,src_default as default};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const LINK_TYPE_NAMES=new Map([[0,`NULL`],[1,`ETHERNET`],[2,`EXP_ETHERNET`],[3,`AX25`],[4,`PRONET`],[5,`CHAOS`],[6,`IEEE802_5`],[7,`ARCNET_BSD`],[8,`SLIP`],[9,`PPP`],[10,`FDDI`],[50,`PPP_HDLC`],[51,`PPP_ETHER`],[99,`SYMANTEC_FIREWALL`],[100,`ATM_RFC1483`],[101,`RAW`],[102,`SLIP_BSDOS`],[103,`PPP_BSDOS`],[104,`C_HDLC`],[105,`IEEE802_11`],[106,`ATM_CLIP`],[107,`FRELAY`],[108,`LOOP`],[109,`ENC`],[110,`LANE8023`],[111,`HIPPI`],[112,`HDLC`],[113,`LINUX_SLL`],[114,`LTALK`],[115,`ECONET`],[116,`IPFILTER`],[117,`PFLOG`],[118,`CISCO_IOS`],[119,`IEEE802_11_PRISM`],[120,`IEEE802_11_AIRONET`],[121,`HHDLC`],[122,`IP_OVER_FC`],[123,`SUNATM`],[124,`RIO`],[125,`PCI_EXP`],[126,`AURORA`],[127,`IEEE802_11_RADIOTAP`],[128,`TZSP`],[129,`ARCNET_LINUX`],[130,`JUNIPER_MLPPP`],[131,`JUNIPER_MLFR`],[132,`JUNIPER_ES`],[133,`JUNIPER_GGSN`],[134,`JUNIPER_MFR`],[135,`JUNIPER_ATM2`],[136,`JUNIPER_SERVICES`],[137,`JUNIPER_ATM1`],[138,`APPLE_IP_OVER_IEEE1394`],[139,`MTP2_WITH_PHDR`],[140,`MTP2`],[141,`MTP3`],[142,`SCCP`],[143,`DOCSIS`],[144,`LINUX_IRDA`],[145,`IBM_SP`],[146,`IBM_SN`],[147,`RESERVED_01`],[148,`RESERVED_02`],[149,`RESERVED_03`],[150,`RESERVED_04`],[151,`RESERVED_05`],[152,`RESERVED_06`],[153,`RESERVED_07`],[154,`RESERVED_08`],[155,`RESERVED_09`],[156,`RESERVED_10`],[157,`RESERVED_11`],[158,`RESERVED_12`],[159,`RESERVED_13`],[160,`RESERVED_14`],[161,`RESERVED_15`],[162,`RESERVED_16`],[163,`IEEE802_11_AVS`],[164,`JUNIPER_MONITOR`],[165,`BACNET_MS_TP`],[166,`PPP_PPPD`],[167,`JUNIPER_PPPOE`],[168,`JUNIPER_PPPOE_ATM`],[169,`GPRS_LLC`],[170,`GPF_T`],[171,`GPF_F`],[172,`GCOM_T1E1`],[173,`GCOM_SERIAL`],[174,`JUNIPER_PIC_PEER`],[175,`ERF_ETH`],[176,`ERF_POS`],[177,`LINUX_LAPD`],[178,`JUNIPER_ETHER`],[179,`JUNIPER_PPP`],[180,`JUNIPER_FRELAY`],[181,`JUNIPER_CHDLC`],[182,`MFR`],[183,`JUNIPER_VP`],[184,`A429`],[185,`A653_ICM`],[186,`USB_FREEBSD`],[187,`BLUETOOTH_HCI_H4`],[188,`IEEE802_16_MAC_CPS`],[189,`USB_LINUX`],[190,`CAN20B`],[191,`IEEE802_15_4_LINUX`],[192,`PPI`],[193,`IEEE802_16_MAC_CPS_RADIO`],[194,`JUNIPER_ISM`],[195,`IEEE802_15_4_WITHFCS`],[196,`SITA`],[197,`ERF`],[198,`RAIF1`],[199,`IPMB_KONTRON`],[200,`JUNIPER_ST`],[201,`BLUETOOTH_HCI_H4_WITH_PHDR`],[202,`AX25_KISS`],[203,`LAPD`],[204,`PPP_WITH_DIR`],[205,`C_HDLC_WITH_DIR`],[206,`FRELAY_WITH_DIR`],[207,`LAPB_WITH_DIR`],[208,`WillBarker-Proprietary`],[209,`I2C_LINUX`],[210,`FLEXRAY`],[211,`MOST`],[212,`LIN`],[213,`X2E_SERIAL`],[214,`X2E_XORAYA`],[215,`IEEE802_15_4_NONASK_PHY`],[216,`LINUX_EVDEV`],[217,`GSMTAP_UM`],[218,`GSMTAP_ABIS`],[219,`MPLS`],[220,`USB_LINUX_MMAPPED`],[221,`DECT`],[222,`AOS`],[223,`WIHART`],[224,`FC_2`],[225,`FC_2_WITH_FRAME_DELIMS`],[226,`IPNET`],[227,`CAN_SOCKETCAN`],[228,`IPV4`],[229,`IPV6`],[230,`IEEE802_15_4_NOFCS`],[231,`DBUS`],[232,`JUNIPER_VS`],[233,`JUNIPER_SRX_E2E`],[234,`JUNIPER_FIBRECHANNEL`],[235,`DVB_CI`],[236,`MUX27010`],[237,`STANAG_5066_D_PDU`],[238,`JUNIPER_ATM_CEMIC`],[239,`NFLOG`],[240,`NETANALYZER`],[241,`NETANALYZER_TRANSPARENT`],[242,`IPOIB`],[243,`MPEG_2_TS`],[244,`NG40`],[245,`NFC_LLCP`],[246,`PFSYNC`],[247,`INFINIBAND`],[248,`SCTP`],[249,`USBPCAP`],[250,`RTAC_SERIAL`],[251,`BLUETOOTH_LE_LL`],[252,`WIRESHARK_UPPER_PDU`],[253,`NETLINK`],[254,`BLUETOOTH_LINUX_MONITOR`],[255,`BLUETOOTH_BREDR_BB`],[256,`BLUETOOTH_LE_LL_WITH_PHDR`],[257,`PROFIBUS_DL`],[258,`PKTAP`],[259,`EPON`],[260,`IPMI_HPM_2`],[261,`ZWAVE_R1_R2`],[262,`ZWAVE_R3`],[263,`WATTSTOPPER_DLM`],[264,`ISO_14443`],[265,`RDS`],[266,`USB_DARWIN`],[267,`OPENFLOW`],[268,`SDLC`],[269,`TI_LLN_SNIFFER`],[270,`LORATAP`],[271,`VSOCK`],[272,`NORDIC_BLE`],[273,`DOCSIS31_XRA31`],[274,`ETHERNET_MPACKET`],[275,`DISPLAYPORT_AUX`],[276,`LINUX_SLL2`],[277,`SERCOS_MONITOR`],[278,`OPENVIZSLA`],[279,`EBHSCR`],[280,`VPP_DISPATCH`],[281,`DSA_TAG_BRCM`],[282,`DSA_TAG_BRCM_PREPEND`],[283,`IEEE802_15_4_TAP`],[284,`DSA_TAG_DSA`],[285,`DSA_TAG_EDSA`],[286,`ELEE`],[287,`Z_WAVE_SERIAL`],[288,`USB_2_0`],[289,`ATSC_ALP`],[290,`ETW`],[291,`NETANALYZER_NG`],[292,`ZBOSS_NCP`],[293,`USB_2_0_LOW_SPEED`],[294,`USB_2_0_FULL_SPEED`],[295,`USB_2_0_HIGH_SPEED`],[296,`AUERSWALD_LOG`],[297,`ZWAVE_TAP`],[298,`SILABS_DEBUG_CHANNEL`],[299,`FIRA_UCI`],[300,`MDB`],[301,`DECT_NR`]]);export{LINK_TYPE_NAMES};
|
package/lib/options.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const SECTION_HEADER=168627466,INTERFACE_DESCRIPTION=1,SIMPLE_PACKET=3,NAME_RESOLUTION=4,INTERFACE_STATISTICS=5,ENHANCED_PACKET=6,DECRYPTION_SECRETS=10,CUSTOM_COPY=2989,CUSTOM_NOCOPY=1073744813,OPTION_NAMES=new Map([[SECTION_HEADER,new Map([[1,[`opt_comment`,`string`]],[2,[`shb_hardware`,`string`]],[3,[`shb_os`,`string`]],[4,[`shb_userappl`,`string`]],[2988,[`opt_custom`,`string`,!0]],[2989,[`opt_custom`,void 0,!0]],[19372,[`opt_custom`,`string`,!0]],[19373,[`opt_custom`,void 0,!0]]])],[1,new Map([[1,[`opt_comment`,`string`]],[2,[`if_name`,`string`]],[3,[`if_description`,`string`]],[4,[`if_IPv4addr`,`ipv4mask`]],[5,[`if_IPv6addr`,`ipv6prefix`]],[6,[`if_MACaddr`,`eui`]],[7,[`if_EUIaddr`,`eui`]],[8,[`if_speed`]],[9,[`if_tsresol`]],[10,[`if_tzone`]],[11,[`if_filter`]],[12,[`if_os`,`string`]],[13,[`if_fcslen`]],[14,[`if_tsoffset`,`u64`]],[15,[`if_hardware`,`string`]],[16,[`if_txspeed`,`u64`]],[17,[`if_rxspeed`,`u64`]],[18,[`if_iana_tzname`,`string`]],[2988,[`opt_custom`,`string`,!0]],[2989,[`opt_custom`,void 0,!0]],[19372,[`opt_custom`,`string`,!0]],[19373,[`opt_custom`,void 0,!0]]])],[4,new Map([[1,[`opt_comment`,`string`]],[2,[`ns_dnsname`,`string`]],[3,[`ns_dnsIP4addr`,`ipv4`]],[4,[`ns_dnsIP6addr`,`ipv6`]],[2988,[`opt_custom`,`string`,!0]],[2989,[`opt_custom`,void 0,!0]],[19372,[`opt_custom`,`string`,!0]],[19373,[`opt_custom`,void 0,!0]]])],[5,new Map([[1,[`opt_comment`,`string`]],[2,[`isb_starttime`,`timestamp`]],[3,[`isb_endtime`,`timestamp`]],[4,[`isb_ifrecv`,`u64`]],[5,[`isb_ifdrop`,`u64`]],[6,[`isb_filteraccept`,`u64`]],[7,[`isb_osdrop`,`u64`]],[8,[`isb_usrdeliv`,`u64`]],[2988,[`opt_custom`,`string`,!0]],[2989,[`opt_custom`,void 0,!0]],[19372,[`opt_custom`,`string`,!0]],[19373,[`opt_custom`,void 0,!0]]])],[6,new Map([[1,[`opt_comment`,`string`]],[2,[`epb_flags`]],[3,[`epb_hash`]],[4,[`epb_dropcount`,`u64`]],[5,[`epb_packetid`,`u64`]],[6,[`epb_queue`,`u32`]],[7,[`epb_verdict`]],[8,[`epb_processid_threadid`]],[2988,[`opt_custom`,`string`,!0]],[2989,[`opt_custom`,void 0,!0]],[19372,[`opt_custom`,`string`,!0]],[19373,[`opt_custom`,void 0,!0]]])],[10,new Map([[1,[`opt_comment`,`string`]],[2988,[`opt_custom`,`string`,!0]],[2989,[`opt_custom`,void 0,!0]],[19372,[`opt_custom`,`string`,!0]],[19373,[`opt_custom`,void 0,!0]]])]]);export{CUSTOM_COPY,CUSTOM_NOCOPY,DECRYPTION_SECRETS,ENHANCED_PACKET,INTERFACE_DESCRIPTION,INTERFACE_STATISTICS,NAME_RESOLUTION,OPTION_NAMES,SECTION_HEADER,SIMPLE_PACKET};
|
package/lib/utils.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function pad4(n){return Math.ceil(n/4)*4-n}function colons(buf){return Array.from(buf).map(x=>x.toString(16).padStart(2,`0`)).join(`:`)}export{colons,pad4};
|
package/package.json
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cto.af/pcap-ng-parser",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.1",
|
|
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
|
-
"
|
|
10
|
-
"types",
|
|
8
|
+
"lib",
|
|
11
9
|
"docs/LICENSE.md"
|
|
12
10
|
],
|
|
13
11
|
"repository": {
|
|
@@ -37,10 +35,11 @@
|
|
|
37
35
|
],
|
|
38
36
|
"license": "BSD-3",
|
|
39
37
|
"dependencies": {
|
|
38
|
+
"@cto.af/utils": "1.3.6",
|
|
40
39
|
"@leichtgewicht/ip-codec": "2.0.5",
|
|
41
|
-
"
|
|
40
|
+
"dataview-stream": "^2.0.2"
|
|
42
41
|
},
|
|
43
|
-
"packageManager": "pnpm@10.
|
|
42
|
+
"packageManager": "pnpm@10.25.0",
|
|
44
43
|
"engines": {
|
|
45
44
|
"node": ">=20"
|
|
46
45
|
}
|
package/src/BlockConfig.js
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @typedef {object} BlockProperty
|
|
3
|
-
* @property {number} size Bytes.
|
|
4
|
-
* @property {boolean} [signed] If true, read signed.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* @typedef {Record<string, BlockProperty>} BlockDescriptor
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* @see documentation at https://www.ietf.org/archive/id/draft-ietf-opsawg-pcapng-04.html#name-section-header-block
|
|
13
|
-
*/
|
|
14
|
-
export const sectionHeaderBlock = {
|
|
15
|
-
byteOrderMagic: {size: 4},
|
|
16
|
-
majorVersion: {size: 2},
|
|
17
|
-
minorVersion: {size: 2},
|
|
18
|
-
sectionLength: {
|
|
19
|
-
size: 8,
|
|
20
|
-
signed: true,
|
|
21
|
-
},
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* @see definition at https://www.ietf.org/archive/id/draft-ietf-opsawg-pcapng-04.html#name-enhanced-packet-block
|
|
26
|
-
*/
|
|
27
|
-
export const blockType = {
|
|
28
|
-
blockType: {
|
|
29
|
-
size: 4,
|
|
30
|
-
signed: true,
|
|
31
|
-
},
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
export const blockTotalLength = {
|
|
35
|
-
blockTotalLength: {size: 4},
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* @see definition at https://www.ietf.org/archive/id/draft-ietf-opsawg-pcapng-04.html#name-interface-description-block
|
|
40
|
-
*/
|
|
41
|
-
export const interfaceDescriptionBlockFormat = {
|
|
42
|
-
linkType: {size: 2},
|
|
43
|
-
reserved: {size: 2},
|
|
44
|
-
snapLen: {size: 4},
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
export const simplePacketFormat = {
|
|
48
|
-
originalPacketLength: {size: 4},
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
export const nameResolutionFormat = {
|
|
52
|
-
recordType: {size: 2},
|
|
53
|
-
recordValueLength: {size: 2},
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* @see definition at https://www.ietf.org/archive/id/draft-ietf-opsawg-pcapng-04.html#name-enhanced-packet-block
|
|
58
|
-
*/
|
|
59
|
-
export const enhancedPacketBlockFormat = {
|
|
60
|
-
interfaceId: {size: 4},
|
|
61
|
-
timestampHigh: {size: 4},
|
|
62
|
-
timestampLow: {size: 4},
|
|
63
|
-
capturedPacketLength: {size: 4},
|
|
64
|
-
originalPacketLength: {size: 4},
|
|
65
|
-
|
|
66
|
-
/* Packet Data */
|
|
67
|
-
/* Options */
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* @see definition at https://www.ietf.org/archive/id/draft-ietf-opsawg-pcapng-04.html#name-options
|
|
72
|
-
*/
|
|
73
|
-
export const optionBlock = {
|
|
74
|
-
optionType: {size: 2},
|
|
75
|
-
dataLength: {size: 2},
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
export const endLength = {
|
|
79
|
-
endTotalLength: {size: 4},
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
export const privateEnterpriseNumber = {
|
|
83
|
-
pen: {size: 4},
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
export const interfaceStatisticsFormat = {
|
|
87
|
-
interfaceId: {size: 4},
|
|
88
|
-
timestampHigh: {size: 4},
|
|
89
|
-
timestampLow: {size: 4},
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
export const decryptionSecretsFormat = {
|
|
93
|
-
secretsType: {size: 4},
|
|
94
|
-
secretsLength: {size: 4},
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
export const epbFlagsFormat = {
|
|
98
|
-
flags: {size: 4},
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
export const ifTsOffsetFormat = {
|
|
102
|
-
tsoffset: {
|
|
103
|
-
size: 8,
|
|
104
|
-
sign: true,
|
|
105
|
-
},
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
export const ipv4MaskFormat = {
|
|
109
|
-
ipv4: {size: 4},
|
|
110
|
-
mask: {size: 4},
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
export const u32Format = {
|
|
114
|
-
u32: {size: 4},
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
export const u64Format = {
|
|
118
|
-
u64: {size: 8},
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
export const timestampFormat = {
|
|
122
|
-
timestampHigh: {size: 4},
|
|
123
|
-
timestampLow: {size: 4},
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
export const pcapHeaderFormat = {
|
|
127
|
-
byteOrderMagic: {size: 4},
|
|
128
|
-
majorVersion: {size: 2},
|
|
129
|
-
minorVersion: {size: 2},
|
|
130
|
-
reserved1: {size: 4},
|
|
131
|
-
reserved2: {size: 4},
|
|
132
|
-
snapLen: {size: 4},
|
|
133
|
-
linkType: {size: 4},
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
export const pcapPacketFormat = {
|
|
137
|
-
timestampHigh: {size: 4},
|
|
138
|
-
timestampLow: {size: 4},
|
|
139
|
-
capturedPacketLength: {size: 4},
|
|
140
|
-
originalPacketLength: {size: 4},
|
|
141
|
-
};
|