@cto.af/pcap-ng-parser 2.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 +127 -0
- package/docs/LICENSE.md +13 -0
- package/package.json +71 -0
- package/src/BlockConfig.js +186 -0
- package/src/PCAPNGParser.js +1001 -0
- package/src/options.js +105 -0
- package/types/BlockConfig.d.ts +192 -0
- package/types/PCAPNGParser.d.ts +417 -0
- package/types/options.d.ts +24 -0
package/README.md
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
|
|
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.
|
|
4
|
+
|
|
5
|
+
# Installation
|
|
6
|
+
|
|
7
|
+
This module is available through the [npm registry](https://www.npmjs.com/).
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
$ npm install @cto.af/pcap-ng-parser
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
# Usage
|
|
14
|
+
|
|
15
|
+
## Via .pcapng File
|
|
16
|
+
Here is a quick example of how to log out packets to the console from a valid .pcapng file named `myfile.pcapng`.
|
|
17
|
+
|
|
18
|
+
```javascript
|
|
19
|
+
import PCAPNGParser from '@cto.af/pcap-ng-parser';
|
|
20
|
+
import fs from 'node:fs';
|
|
21
|
+
|
|
22
|
+
const pcapNgParser = new PCAPNGParser();
|
|
23
|
+
const myFileStream = fs.createReadStream('./myfile.pcapng');
|
|
24
|
+
|
|
25
|
+
myFileStream.pipe(pcapNgParser)
|
|
26
|
+
.on('data', parsedPacket => {
|
|
27
|
+
console.log(parsedPacket);
|
|
28
|
+
})
|
|
29
|
+
.on('interface', interfaceInfo => {
|
|
30
|
+
console.log(interfaceInfo);
|
|
31
|
+
});
|
|
32
|
+
```
|
|
33
|
+
|
|
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.
|
|
35
|
+
|
|
36
|
+
## Via TCPDump
|
|
37
|
+
|
|
38
|
+
You can also pipe from TCPDump using `process.stdin` for a command line interaction.
|
|
39
|
+
|
|
40
|
+
```javascript
|
|
41
|
+
import PCAPNGParser from '@cto.af/pcap-ng-parser';
|
|
42
|
+
const pcapNgParser = new PCAPNGParser();
|
|
43
|
+
|
|
44
|
+
process.stdin.pipe(pcapNgParser)
|
|
45
|
+
.on('data', parsedPacket => {
|
|
46
|
+
console.log(parsedPacket);
|
|
47
|
+
})
|
|
48
|
+
.on('interface', interfaceInfo => {
|
|
49
|
+
console.log(interfaceInfo);
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
$ sudo tcpdump -w - | node exampleAbove.js
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
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
|
+
|
|
59
|
+
Further note: If you specify an interface to listen on with "-i", tcpdump no
|
|
60
|
+
longer uses the pcapng format.
|
|
61
|
+
|
|
62
|
+
## Other Examples
|
|
63
|
+
|
|
64
|
+
Additional examples can be found in the [examples directory](/examples/).
|
|
65
|
+
|
|
66
|
+
# Class PCAPNGParser
|
|
67
|
+
|
|
68
|
+
PCAPNGParser is an extension of the [stream.Transform
|
|
69
|
+
class](https://nodejs.org/api/stream.html#stream_class_stream_transform). The
|
|
70
|
+
PCAPNGParser class fires the following events:
|
|
71
|
+
|
|
72
|
+
- `data`: An Enhanced Packet or Simple block was parsed.
|
|
73
|
+
- `section`: A Section Header block was parsed.
|
|
74
|
+
- `interface`: An Interface block was parsed.
|
|
75
|
+
- `names`: A Name Resolution block was parsed.
|
|
76
|
+
- `secrets`: A Decryption Secrets block was parsed.
|
|
77
|
+
- `stats`: An Interface Statistics block was parsed.
|
|
78
|
+
- `custom`: A Custom block was parsed.
|
|
79
|
+
- `blockType`: An unknown block type has been received.
|
|
80
|
+
|
|
81
|
+
See the full API documentation for the types of the event parameters.
|
|
82
|
+
|
|
83
|
+
# Contribution
|
|
84
|
+
|
|
85
|
+
Refer to the the [Contribution Guide](/docs/CONTRIBUTING.md) for details on how to contribute.
|
|
86
|
+
|
|
87
|
+
# License
|
|
88
|
+
|
|
89
|
+
This module is covered under the BSD-3 Open Software License. Review the [License Documention](/docs/LICENSE.md) for more information.
|
|
90
|
+
|
|
91
|
+
# Provenance
|
|
92
|
+
|
|
93
|
+
This code was forked from https://github.com/CollinearGroup/pcap-ng-parser due
|
|
94
|
+
to lack of maintenance. To simplify matters, copyright remains with the
|
|
95
|
+
original authors, including all changes made in this repository. If the
|
|
96
|
+
original authors contact me (easiest would be to file an issue here), all of
|
|
97
|
+
this can be changed in any way that suits them.
|
|
98
|
+
|
|
99
|
+
The following things have been added:
|
|
100
|
+
|
|
101
|
+
- Fixed critical RangeError bug
|
|
102
|
+
- Made 'end' and 'finish' events fire correctly by removing broken _finish code. Note that you have to add an event for 'data' to get these events or 'close'
|
|
103
|
+
to fire, as with all Node Transform streams.
|
|
104
|
+
- Converted to ES6 (BREAKING)
|
|
105
|
+
- Added TypeScript types
|
|
106
|
+
- Brought dependencies up to date
|
|
107
|
+
- Added linting in @cto.af house style (to make maintenance easier)
|
|
108
|
+
- Added test coverage
|
|
109
|
+
- Renamed 'master' branch to 'main'
|
|
110
|
+
- Added Github Actions for testing and publishing
|
|
111
|
+
- Fixed broken links in readme and code
|
|
112
|
+
- Fixed issues with padding for options
|
|
113
|
+
- Now reads all options for supported block types, giving them names when known
|
|
114
|
+
- Fixed issues with reading unexpected sizes in input stream
|
|
115
|
+
- Added support for AbortSignals
|
|
116
|
+
- Added support for multiple Section Header blocks in a stream, including
|
|
117
|
+
changes of endian-ness.
|
|
118
|
+
- Added support for Simple Packet blocks.
|
|
119
|
+
- Added support for Name Resolution blocks.
|
|
120
|
+
- Added support for Interface Statistics blocks.
|
|
121
|
+
- Added initial Support for Decryption Secrets blocks.
|
|
122
|
+
- Added support for Custom blocks.
|
|
123
|
+
- Added Enhanced Packet flag decoding.
|
|
124
|
+
- Added converting timestamps to JS Dates. Does not handle timezones yet. (BREAKING)
|
|
125
|
+
|
|
126
|
+
[](https://github.com/cto-af/pcap-ng-parser/actions/workflows/node.js.yml)
|
|
127
|
+
[](https://codecov.io/gh/cto-af/pcap-ng-parser)
|
package/docs/LICENSE.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# LICENSE - BSD-3
|
|
2
|
+
|
|
3
|
+
Copyright 2018 Collinear Group, Inc.
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
6
|
+
|
|
7
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
8
|
+
|
|
9
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
10
|
+
|
|
11
|
+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
12
|
+
|
|
13
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/package.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cto.af/pcap-ng-parser",
|
|
3
|
+
"version": "2.0.1",
|
|
4
|
+
"description": "Node.js Module for parsing .pcapng files and tcpdump output.",
|
|
5
|
+
"main": "./src/PCAPNGParser.js",
|
|
6
|
+
"types": "./types/PCAPNGParser.d.ts",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"files": [
|
|
9
|
+
"src",
|
|
10
|
+
"types",
|
|
11
|
+
"docs/LICENSE.md"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"clean": "rimraf coverage doc types",
|
|
15
|
+
"docs": "typedoc",
|
|
16
|
+
"example": "node examples/index.js",
|
|
17
|
+
"test": "c8 mocha",
|
|
18
|
+
"ts": "tsc",
|
|
19
|
+
"lint": "eslint .",
|
|
20
|
+
"build": "npm run test && npm run ts && npm run lint && npm run docs"
|
|
21
|
+
},
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/cto-af/pcap-ng-parser.git"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"pcap-ng",
|
|
28
|
+
"tcpdump",
|
|
29
|
+
"parser",
|
|
30
|
+
"network",
|
|
31
|
+
"packets",
|
|
32
|
+
"decode",
|
|
33
|
+
"draft-ietf-opsawg-pcapng"
|
|
34
|
+
],
|
|
35
|
+
"author": "Collinear Group <support@collineargroup.com>",
|
|
36
|
+
"contributors": [
|
|
37
|
+
{
|
|
38
|
+
"name": "George Barta"
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
"name": "S. Everett Abbott"
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
"name": "Joe Hildebrand <joe-github@cursive.net>"
|
|
45
|
+
}
|
|
46
|
+
],
|
|
47
|
+
"license": "BSD-3",
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@cto.af/eslint-config": "6.0.15",
|
|
50
|
+
"@eslint/markdown": "7.5.0",
|
|
51
|
+
"@types/node": "24.9.1",
|
|
52
|
+
"c8": "10.1.3",
|
|
53
|
+
"chai": "^6.2.0",
|
|
54
|
+
"eslint": "9.38.0",
|
|
55
|
+
"eslint-plugin-jsdoc": "61.1.5",
|
|
56
|
+
"eslint-plugin-mocha": "11.2.0",
|
|
57
|
+
"ether-frame": "^0.2.0",
|
|
58
|
+
"mocha": "^11.7.4",
|
|
59
|
+
"rimraf": "6.0.1",
|
|
60
|
+
"typedoc": "0.28.14",
|
|
61
|
+
"typescript": "5.9.3"
|
|
62
|
+
},
|
|
63
|
+
"packageManager": "pnpm@10.19.0",
|
|
64
|
+
"engines": {
|
|
65
|
+
"node": ">=20"
|
|
66
|
+
},
|
|
67
|
+
"dependencies": {
|
|
68
|
+
"@leichtgewicht/ip-codec": "2.0.5",
|
|
69
|
+
"nofilter": "6.3.0"
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
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: {
|
|
16
|
+
size: 4,
|
|
17
|
+
},
|
|
18
|
+
majorVersion: {
|
|
19
|
+
size: 2,
|
|
20
|
+
},
|
|
21
|
+
minorVersion: {
|
|
22
|
+
size: 2,
|
|
23
|
+
},
|
|
24
|
+
sectionLength: {
|
|
25
|
+
size: 8,
|
|
26
|
+
signed: true,
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @see definition at https://www.ietf.org/archive/id/draft-ietf-opsawg-pcapng-04.html#name-enhanced-packet-block
|
|
32
|
+
*/
|
|
33
|
+
export const blockType = {
|
|
34
|
+
blockType: {
|
|
35
|
+
size: 4,
|
|
36
|
+
signed: true,
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export const blockTotalLength = {
|
|
41
|
+
blockTotalLength: {
|
|
42
|
+
size: 4,
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @see definition at https://www.ietf.org/archive/id/draft-ietf-opsawg-pcapng-04.html#name-interface-description-block
|
|
48
|
+
*/
|
|
49
|
+
export const interfaceDescriptionBlockFormat = {
|
|
50
|
+
linkType: {
|
|
51
|
+
size: 2,
|
|
52
|
+
},
|
|
53
|
+
reserved: {
|
|
54
|
+
size: 2,
|
|
55
|
+
},
|
|
56
|
+
snapLen: {
|
|
57
|
+
size: 4,
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export const simplePacketFormat = {
|
|
62
|
+
originalPacketLength: {
|
|
63
|
+
size: 4,
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export const nameResolutionFormat = {
|
|
68
|
+
recordType: {
|
|
69
|
+
size: 2,
|
|
70
|
+
},
|
|
71
|
+
recordValueLength: {
|
|
72
|
+
size: 2,
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @see definition at https://www.ietf.org/archive/id/draft-ietf-opsawg-pcapng-04.html#name-enhanced-packet-block
|
|
78
|
+
*/
|
|
79
|
+
export const enhancedPacketBlockFormat = {
|
|
80
|
+
interfaceId: {
|
|
81
|
+
size: 4,
|
|
82
|
+
},
|
|
83
|
+
timestampHigh: {
|
|
84
|
+
size: 4,
|
|
85
|
+
},
|
|
86
|
+
timestampLow: {
|
|
87
|
+
size: 4,
|
|
88
|
+
},
|
|
89
|
+
capturedPacketLength: {
|
|
90
|
+
size: 4,
|
|
91
|
+
},
|
|
92
|
+
originalPacketLength: {
|
|
93
|
+
size: 4,
|
|
94
|
+
},
|
|
95
|
+
|
|
96
|
+
/* Packet Data */
|
|
97
|
+
/* Options */
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* @see definition at https://www.ietf.org/archive/id/draft-ietf-opsawg-pcapng-04.html#name-options
|
|
102
|
+
*/
|
|
103
|
+
export const optionBlock = {
|
|
104
|
+
optionType: {
|
|
105
|
+
size: 2,
|
|
106
|
+
},
|
|
107
|
+
dataLength: {
|
|
108
|
+
size: 2,
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
export const endLength = {
|
|
113
|
+
endTotalLength: {
|
|
114
|
+
size: 4,
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
export const privateEnterpriseNumber = {
|
|
119
|
+
pen: {
|
|
120
|
+
size: 4,
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export const interfaceStatisticsFormat = {
|
|
125
|
+
interfaceId: {
|
|
126
|
+
size: 4,
|
|
127
|
+
},
|
|
128
|
+
timestampHigh: {
|
|
129
|
+
size: 4,
|
|
130
|
+
},
|
|
131
|
+
timestampLow: {
|
|
132
|
+
size: 4,
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
export const decryptionSecretsFormat = {
|
|
137
|
+
secretsType: {
|
|
138
|
+
size: 4,
|
|
139
|
+
},
|
|
140
|
+
secretsLength: {
|
|
141
|
+
size: 4,
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
export const epbFlagsFormat = {
|
|
146
|
+
flags: {
|
|
147
|
+
size: 4,
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
export const ifTsOffsetFormat = {
|
|
152
|
+
tsoffset: {
|
|
153
|
+
size: 8,
|
|
154
|
+
sign: true,
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
export const ipv4MaskFormat = {
|
|
159
|
+
ipv4: {
|
|
160
|
+
size: 4,
|
|
161
|
+
},
|
|
162
|
+
mask: {
|
|
163
|
+
size: 4,
|
|
164
|
+
},
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
export const u32Format = {
|
|
168
|
+
u32: {
|
|
169
|
+
size: 4,
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
export const u64Format = {
|
|
174
|
+
u64: {
|
|
175
|
+
size: 8,
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
export const timestampFormat = {
|
|
180
|
+
timestampHigh: {
|
|
181
|
+
size: 4,
|
|
182
|
+
},
|
|
183
|
+
timestampLow: {
|
|
184
|
+
size: 4,
|
|
185
|
+
},
|
|
186
|
+
};
|