├── .github └── workflows │ └── ci.yml ├── .gitignore ├── .npmrc ├── LICENSE ├── README.md ├── package.json ├── src └── index.ts ├── test ├── fixtures │ ├── chrome-tls-connect.bin │ ├── server.crt │ └── server.key ├── test-util.ts ├── test.spec.ts └── tsconfig.json ├── tsconfig.json └── wallaby.js /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: [push, pull_request] 3 | jobs: 4 | build: 5 | name: Build & test 6 | runs-on: ubuntu-latest 7 | 8 | strategy: 9 | matrix: 10 | node-version: [12.x, 14.x, v16.x, v18.x, v18.16, '*'] 11 | 12 | steps: 13 | - uses: actions/checkout@v3 14 | 15 | - uses: actions/setup-node@v3 16 | with: 17 | node-version: ${{ matrix.node-version }} 18 | 19 | - run: npm install 20 | - run: npm test -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | 9 | # Diagnostic reports (https://nodejs.org/api/report.html) 10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | *.lcov 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | build/Release 39 | 40 | # Dependency directories 41 | node_modules/ 42 | jspm_packages/ 43 | 44 | # TypeScript v1 declaration files 45 | typings/ 46 | 47 | # TypeScript cache 48 | *.tsbuildinfo 49 | 50 | # Optional npm cache directory 51 | .npm 52 | 53 | # Optional eslint cache 54 | .eslintcache 55 | 56 | # Microbundle cache 57 | .rpt2_cache/ 58 | .rts2_cache_cjs/ 59 | .rts2_cache_es/ 60 | .rts2_cache_umd/ 61 | 62 | # Optional REPL history 63 | .node_repl_history 64 | 65 | # Output of 'npm pack' 66 | *.tgz 67 | 68 | # Yarn Integrity file 69 | .yarn-integrity 70 | 71 | # dotenv environment variables file 72 | .env 73 | .env.test 74 | 75 | # parcel-bundler cache (https://parceljs.org/) 76 | .cache 77 | 78 | # Next.js build output 79 | .next 80 | 81 | # Nuxt.js build / generate output 82 | .nuxt 83 | dist 84 | 85 | # Gatsby files 86 | .cache/ 87 | # Comment in the public line in if your project uses Gatsby and *not* Next.js 88 | # https://nextjs.org/blog/next-9-1#public-directory-support 89 | # public 90 | 91 | # vuepress build output 92 | .vuepress/dist 93 | 94 | # Serverless directories 95 | .serverless/ 96 | 97 | # FuseBox cache 98 | .fusebox/ 99 | 100 | # DynamoDB Local files 101 | .dynamodb/ 102 | 103 | # TernJS port file 104 | .tern-port 105 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Read-TLS-Client-Hello [![Build Status](https://github.com/httptoolkit/read-tls-client-hello/workflows/CI/badge.svg)](https://github.com/httptoolkit/read-tls-client-hello/actions) [![Available on NPM](https://img.shields.io/npm/v/read-tls-client-hello.svg)](https://npmjs.com/package/read-tls-client-hello) 2 | 3 | > _Part of [HTTP Toolkit](https://httptoolkit.tech): powerful tools for building, testing & debugging HTTP(S)_ 4 | 5 | A pure-JS module to read TLS client hello data and calculate TLS fingerprints from an incoming socket connection. Tiny, with zero runtime dependencies. 6 | 7 | Using this, you can analyze incoming TLS connections before you start a full handshake, and using their fingerprints you can recognize certain TLS clients - e.g. specific browser, cURL, or even the specific versions of a specific programming language a client is using - regardless of the content of the request they send. 8 | 9 | See https://httptoolkit.com/blog/tls-fingerprinting-node-js/#how-does-tls-fingerprinting-work for more background on how TLS fingerprinting works. 10 | 11 | Be aware that fingerprinting is _not_ a 100% reliable test. Most clients can modify their TLS fingerprint with a bit of work (though few do). In many cases, it's even possible to mimic another arbitrary fingerprint on demand (e.g. using libraries like [CycleTLS](https://www.npmjs.com/package/cycletls)). Most of the time though, for clients that aren't actively messing with you, the fingerprint will tell you what kind of client is making the connection. 12 | 13 | ## Docs 14 | 15 | ### TLS server helper 16 | 17 | The easiest way to use this is to use the built-in `trackClientHellos` helper, which can be applied to any `tls.TLSServer` instance, including `https.Server` instances, like so: 18 | 19 | ```javascript 20 | const https = require('https'); 21 | const { trackClientHellos } = require('read-tls-client-hello'); 22 | 23 | const server = new https.Server({ /* your TLS options etc */ }); 24 | 25 | trackClientHellos(server); // <-- Automatically track everything on this server 26 | 27 | server.on('request', (request, response) => { 28 | // In your normal request handler, check `tlsClientHello` on the request's socket: 29 | console.log('Received request with TLS client hello:', request.socket.tlsClientHello); 30 | }); 31 | ``` 32 | 33 | A `tlsClientHello` property will be attached to all sockets, containing the parsed data returned by `readTlsClientHello` (see below), a `ja3` property with the JA3 TLS fingerprint for the client hello, e.g. `cd08e31494f9531f560d64c695473da9` and a `ja4` property with the JA4 TLS fingerprint for the client hello, e.g. `t13d591000_a33745022dd6_1f22a2ca17c4`. 34 | 35 | ### Reading a TLS client hello 36 | 37 | To read all available data from a TLS client hello manually, pass a stream (e.g. a `net.Socket`) to the exported `readTlsClientHello(stream)`, before the TLS handshake (or any other processing) starts. This returns a promise containing all data parsed from the client hello. 38 | 39 | This method reads the initial data from the socket, parses it, and then unshifts it back into the socket, so that once the returned promise resolves the stream can be used like new, to start a normal TLS session using the same client hello. 40 | 41 | If parsing fails, this method will throw an error, but will still ensure all data is returned to the socket first, so that non-TLS streams can also be processed as normal. 42 | 43 | The returned promise resolves to an object, containing: 44 | 45 | * `serverName` - The server name requested in the client hello (or undefined if SNI was not used) 46 | * `alpnProtocols` - A array of ALPN protcol names requested in the client hello (or undefined if ALPN was not used) 47 | * `fingerprintData` - An array containing the raw components used for JA3 TLS fingerprinting: 48 | 1. The TLS version number as a Uint16 (771 for TLS 1.2+) 49 | 2. An array of cipher ids (excluding GREASE) 50 | 3. An array of extension ids (excluding GREASE) 51 | 4. An array of supported group ids (excluding GREASE) 52 | 5. An array of supported elliptic curve ids 53 | 6. An array of signature algorithms (TLS 1.3) 54 | 55 | ### TLS fingerprinting 56 | 57 | To calculate TLS fingerprints manually, there are a few options exported from this module: 58 | 59 | * `getTlsFingerprintAsJa3` - Reads from a stream, just like `readTlsClientHello` above, but returns a promise for the JA3 hash string, e.g. `cd08e31494f9531f560d64c695473da9`, instead of the raw hello components. 60 | * `getTlsFingerprintAsJa4` - Reads from a stream, just like `readTlsClientHello` above, but returns a promise for the JA4 hash string, e.g. `t13d591000_a33745022dd6_1f22a2ca17c4`, instead of the raw hello components. 61 | * `readTlsClientHello(stream)` - Reads the entire hello (see above). In the returned object, you can read the raw data components used for JA3 fingerprinting from the `fingerprintData` property. 62 | * `calculateJa3FromFingerprintData(data)` - Takes raw TLS fingerprint data, and returns the corresponding JA3 hash. 63 | * `calculateJa4FromHelloData(data)` - Takes the full hello data, including the serverName, alpnProtocols & fingerprinting parameters returned by `readTlsClientHello`, and returns the corresponding JA4 hash, eg. `t13d591000_a33745022dd6_1f22a2ca17c4`. -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "read-tls-client-hello", 3 | "version": "1.1.0", 4 | "author": "Tim Perry ", 5 | "description": "A pure-JS module to read TLS client hello data and fingerprints from an incoming socket connection", 6 | "main": "./dist/index.js", 7 | "types": "./dist/index.d.ts", 8 | "files": [ 9 | "dist/", 10 | "src/" 11 | ], 12 | "scripts": { 13 | "prebuild": "rimraf dist/*", 14 | "build": "tsc", 15 | "prepack": "npm run build", 16 | "pretest": "npm run build", 17 | "test": "mocha -r ts-node/register 'test/**/*.spec.ts'" 18 | }, 19 | "engines": { 20 | "node": ">=12.0.0" 21 | }, 22 | "keywords": [ 23 | "tls", 24 | "fingerprint", 25 | "fingerprinting", 26 | "client-hello", 27 | "alpn", 28 | "sni", 29 | "https", 30 | "ja3", 31 | "ja4" 32 | ], 33 | "licenses": [ 34 | { 35 | "type": "Apache-2.0", 36 | "url": "http://github.com/httptoolkit/read-tls-client-hello/raw/main/LICENSE" 37 | } 38 | ], 39 | "repository": { 40 | "type": "git", 41 | "url": "http://github.com/httptoolkit/read-tls-client-hello.git" 42 | }, 43 | "dependencies": { 44 | "@types/node": "*" 45 | }, 46 | "devDependencies": { 47 | "@types/chai": "^4.2.21", 48 | "@types/mocha": "^9.0.0", 49 | "chai": "^4.3.4", 50 | "destroyable-server": "^1.0.0", 51 | "mocha": "^10.0.0", 52 | "rimraf": "^3.0.2", 53 | "ts-node": "^10.2.1", 54 | "typescript": "^4.8.4" 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import * as stream from 'stream'; 2 | import * as crypto from 'crypto'; 3 | import * as tls from 'tls'; 4 | import * as net from 'net'; 5 | 6 | type ErrorWithConsumedData = Error & { 7 | consumedData: Buffer 8 | }; 9 | 10 | const collectBytes = (stream: stream.Readable, byteLength: number) => { 11 | if (byteLength === 0) return Buffer.from([]); 12 | 13 | return new Promise(async (resolve, reject) => { 14 | const closeReject = () => reject(new Error('Stream closed before expected data could be read')); 15 | 16 | const data: Buffer[] = []; 17 | 18 | try { 19 | stream.on('error', reject); 20 | stream.on('close', closeReject); 21 | let dataLength = 0; 22 | let readNull = false; 23 | do { 24 | if (!stream.readable || readNull) await new Promise((resolve) => stream.once('readable', resolve)); 25 | 26 | const nextData = stream.read(byteLength - dataLength) 27 | ?? stream.read(); // If less than wanted data is available, at least read what we can get 28 | 29 | if (nextData === null) { 30 | // Still null => tried to read, not enough data 31 | readNull = true; 32 | continue; 33 | } 34 | 35 | data.push(nextData); 36 | dataLength += nextData.byteLength; 37 | } while (dataLength < byteLength) 38 | 39 | return resolve(Buffer.concat(data, byteLength)); 40 | } catch (e) { 41 | Object.assign(e as ErrorWithConsumedData, { consumedData: data }); 42 | reject(e); 43 | } finally { 44 | stream.removeListener('error', reject); 45 | stream.removeListener('close', closeReject); 46 | } 47 | }); 48 | }; 49 | 50 | const getUint16BE = (buffer: Buffer, offset: number) => 51 | (buffer[offset] << 8) + buffer[offset+1]; 52 | 53 | // https://datatracker.ietf.org/doc/html/draft-davidben-tls-grease-01 defines GREASE values for various 54 | // TLS fields, reserving 0a0a, 1a1a, 2a2a, etc for ciphers, extension ids & supported groups. 55 | const isGREASE = (value: number) => (value & 0x0f0f) == 0x0a0a; 56 | 57 | export type TlsHelloData = { 58 | serverName: string | undefined; 59 | alpnProtocols: string[] | undefined; 60 | fingerprintData: TlsFingerprintData; 61 | }; 62 | 63 | export type TlsFingerprintData = [ 64 | tlsVersion: number, 65 | ciphers: number[], 66 | extensions: number[], 67 | groups: number[], 68 | curveFormats: number[], 69 | sigAlgorithms: number[] 70 | ]; 71 | 72 | /** 73 | * Seperate error class. If you want to detect TLS parsing errors, but ignore TLS fingerprint 74 | * issues from definitely-not-TLS traffic, you can ignore all instances of this error. 75 | */ 76 | export class NonTlsError extends Error { 77 | constructor(message: string) { 78 | super(message); 79 | 80 | // Fix prototypes (required for custom error types): 81 | const actualProto = new.target.prototype; 82 | Object.setPrototypeOf(this, actualProto); 83 | } 84 | } 85 | 86 | async function extractTlsHello(inputStream: stream.Readable): Promise { 87 | const consumedData = []; 88 | try { 89 | consumedData.push(await collectBytes(inputStream, 1)); 90 | const [recordType] = consumedData[0]; 91 | if (recordType !== 0x16) throw new Error("Can't calculate TLS fingerprint - not a TLS stream"); 92 | 93 | consumedData.push(await collectBytes(inputStream, 2)); 94 | const recordLengthBytes = await collectBytes(inputStream, 2); 95 | consumedData.push(recordLengthBytes); 96 | const recordLength = recordLengthBytes.readUint16BE(); 97 | 98 | consumedData.push(await collectBytes(inputStream, recordLength)); 99 | 100 | // Put all the bytes back, so that this stream can still be used to create a real TLS session 101 | return Buffer.concat(consumedData); 102 | } catch (error: any) { 103 | if (error.consumedData) { 104 | // This happens if there's an error inside collectBytes with a partial read. 105 | (error.consumedData as ErrorWithConsumedData).consumedData = Buffer.concat([ 106 | ...consumedData, 107 | error.consumedData as Buffer 108 | ]) 109 | } else { 110 | Object.assign(error, { consumedData: Buffer.concat(consumedData) }); 111 | } 112 | 113 | throw error; 114 | } 115 | } 116 | 117 | function parseSniData(data: Buffer) { 118 | // SNI is almost always just one value - and is arguably required to be, since there's only one type 119 | // in the RFC and you're only allowed one name per type, but it's still structured as a list: 120 | let offset = 0; 121 | while (offset < data.byteLength) { 122 | const entryLength = data.readUInt16BE(offset); 123 | offset += 2; 124 | const entryType = data[offset]; 125 | offset += 1; 126 | const nameLength = data.readUInt16BE(offset); 127 | offset += 2; 128 | 129 | if (nameLength !== entryLength - 3) { 130 | throw new Error('Invalid length in SNI entry'); 131 | } 132 | 133 | const name = data.slice(offset, offset + nameLength).toString('ascii'); 134 | offset += nameLength; 135 | 136 | if (entryType === 0x0) return name; 137 | } 138 | 139 | // No data, or no names with DNS hostname type. 140 | return undefined; 141 | } 142 | 143 | function parseAlpnData(data: Buffer) { 144 | const protocols: string[] = []; 145 | 146 | const listLength = data.readUInt16BE(); 147 | if (listLength !== data.byteLength - 2) { 148 | throw new Error('Invalid length for ALPN list'); 149 | } 150 | 151 | let offset = 2; 152 | while (offset < data.byteLength) { 153 | const nameLength = data[offset]; 154 | offset += 1; 155 | const name = data.slice(offset, offset + nameLength).toString('ascii'); 156 | offset += nameLength; 157 | protocols.push(name); 158 | } 159 | 160 | return protocols; 161 | } 162 | 163 | export async function readTlsClientHello(inputStream: stream.Readable): Promise { 164 | const wasFlowing = inputStream.readableFlowing; 165 | if (wasFlowing) inputStream.pause(); // Pause other readers, so we have time to precisely get the data we need. 166 | 167 | let clientHelloRecordData: Buffer; 168 | try { 169 | clientHelloRecordData = await extractTlsHello(inputStream); 170 | } catch (error: any) { 171 | if ('consumedData' in error) { 172 | inputStream.unshift(error.consumedData as Buffer); 173 | } 174 | if (wasFlowing) inputStream.resume(); // If there were other readers, resume and let them continue 175 | throw new NonTlsError(error.message); 176 | } 177 | 178 | // Put all the bytes back, so that this stream can still be used to create a real TLS session 179 | inputStream.unshift(clientHelloRecordData); 180 | if (wasFlowing) inputStream.resume(); // If there were other readers, resume and let them continue 181 | 182 | // Collect all the hello bytes, and then give us a stream of exactly only those bytes, so we can 183 | // still process them step by step in order: 184 | const clientHello = clientHelloRecordData.slice(5); // Strip TLS record prefix 185 | const helloDataStream = stream.Readable.from(clientHello, { objectMode: false }); 186 | 187 | const [helloType] = (await collectBytes(helloDataStream, 1)); 188 | if (helloType !== 0x1) throw new Error("Can't calculate TLS fingerprint - not a TLS client hello"); 189 | 190 | const helloLength = (await collectBytes(helloDataStream, 3)).readIntBE(0, 3); 191 | if (helloLength !== clientHello.byteLength - 4) throw new Error( 192 | `Unexpected client hello length: ${helloLength} (of ${clientHello.byteLength})` 193 | ); 194 | 195 | const clientTlsVersion = await collectBytes(helloDataStream, 2); 196 | const clientRandom = await collectBytes(helloDataStream, 32); 197 | 198 | const [sessionIdLength] = await collectBytes(helloDataStream, 1); 199 | const sessionId = await collectBytes(helloDataStream, sessionIdLength); 200 | 201 | const cipherSuitesLength = (await collectBytes(helloDataStream, 2)).readUint16BE(); 202 | const cipherSuites = await collectBytes(helloDataStream, cipherSuitesLength); 203 | 204 | const [compressionMethodsLength] = await collectBytes(helloDataStream, 1); 205 | const compressionMethods = await collectBytes(helloDataStream, compressionMethodsLength); 206 | 207 | const extensionsLength = (await collectBytes(helloDataStream, 2)).readUint16BE(); 208 | let readExtensionsDataLength = 0; 209 | const extensions: Array<{ id: Buffer, data: Buffer }> = []; 210 | let signatureAlgorithms: number[] = []; 211 | 212 | while (readExtensionsDataLength < extensionsLength) { 213 | const extensionId = await collectBytes(helloDataStream, 2); 214 | const extensionLength = (await collectBytes(helloDataStream, 2)).readUint16BE(); 215 | const extensionData = await collectBytes(helloDataStream, extensionLength); 216 | 217 | if (extensionId.readUInt16BE() === 13) { 218 | const sigAlgsLength = extensionData.readUInt16BE(0); 219 | const sigAlgs: number[] = []; 220 | for (let i = 2; i < sigAlgsLength + 2; i += 2) { 221 | sigAlgs.push(extensionData.readUInt16BE(i)); 222 | } 223 | signatureAlgorithms = sigAlgs; 224 | } 225 | 226 | extensions.push({ id: extensionId, data: extensionData }); 227 | readExtensionsDataLength += 4 + extensionLength; 228 | } 229 | 230 | // All data received & parsed! Now turn it into the fingerprint format: 231 | //SSLVersion,Cipher,SSLExtension,EllipticCurve,EllipticCurvePointFormat 232 | 233 | const tlsVersionFingerprint = clientTlsVersion.readUint16BE() 234 | 235 | const cipherFingerprint: number[] = []; 236 | for (let i = 0; i < cipherSuites.length; i += 2) { 237 | const cipherId = getUint16BE(cipherSuites, i); 238 | if (isGREASE(cipherId)) continue; 239 | cipherFingerprint.push(cipherId); 240 | } 241 | 242 | const extensionsFingerprint: number[] = extensions 243 | .map(({ id }) => getUint16BE(id, 0)) 244 | .filter(id => !isGREASE(id)); 245 | 246 | const supportedGroupsData = ( 247 | extensions.find(({ id }) => id.equals(Buffer.from([0x0, 0x0a])))?.data 248 | ?? Buffer.from([]) 249 | ).slice(2) // Drop the length prefix 250 | 251 | const groupsFingerprint: number[] = []; 252 | for (let i = 0; i < supportedGroupsData.length; i += 2) { 253 | const groupId = getUint16BE(supportedGroupsData, i) 254 | if (isGREASE(groupId)) continue; 255 | groupsFingerprint.push(groupId); 256 | } 257 | 258 | const curveFormatsData = extensions.find(({ id }) => id.equals(Buffer.from([0x0, 0x0b])))?.data 259 | ?? Buffer.from([]); 260 | const curveFormatsFingerprint: number[] = Array.from(curveFormatsData.slice(1)); // Drop length prefix 261 | 262 | const fingerprintData = [ 263 | tlsVersionFingerprint, 264 | cipherFingerprint, 265 | extensionsFingerprint, 266 | groupsFingerprint, 267 | curveFormatsFingerprint, 268 | signatureAlgorithms 269 | ] as TlsFingerprintData; 270 | 271 | // And capture other client hello data that might be interesting: 272 | const sniExtensionData = extensions.find(({ id }) => id.equals(Buffer.from([0x0, 0x0])))?.data; 273 | const serverName = sniExtensionData 274 | ? parseSniData(sniExtensionData) 275 | : undefined; 276 | 277 | const alpnExtensionData = extensions.find(({ id }) => id.equals(Buffer.from([0x0, 0x10])))?.data; 278 | const alpnProtocols = alpnExtensionData 279 | ? parseAlpnData(alpnExtensionData) 280 | : undefined; 281 | 282 | return { 283 | serverName, 284 | alpnProtocols, 285 | fingerprintData 286 | }; 287 | } 288 | 289 | export function calculateJa3FromFingerprintData(fingerprintData: TlsFingerprintData) { 290 | const fingerprintString = [ 291 | fingerprintData[0], 292 | fingerprintData[1].join('-'), 293 | fingerprintData[2].join('-'), 294 | fingerprintData[3].join('-'), 295 | fingerprintData[4].join('-') 296 | ].join(','); 297 | 298 | return crypto.createHash('md5').update(fingerprintString).digest('hex'); 299 | } 300 | 301 | export async function getTlsFingerprintAsJa3(rawStream: stream.Readable) { 302 | return calculateJa3FromFingerprintData( 303 | (await readTlsClientHello(rawStream)).fingerprintData 304 | ); 305 | } 306 | 307 | export interface Ja4Data { 308 | protocol: 't' | 'q' | 'd'; // TLS, QUIC, DTLS (only TLS supported for now) 309 | version: '10' | ' 11' | '12' | '13'; // TLS version 310 | sni: 'd' | 'i'; // 'd' if a domain was provided via SNI, 'i' otherwise (for IP) 311 | cipherCount: number; 312 | extensionCount: number; 313 | alpn: string; // First and last character of the ALPN value or '00' if none 314 | cipherSuites: number[]; 315 | extensions: number[]; 316 | sigAlgorithms: number[]; 317 | } 318 | 319 | export function calculateJa4FromHelloData( 320 | { serverName, alpnProtocols, fingerprintData }: TlsHelloData 321 | ): string { 322 | const [tlsVersion, ciphers, extensions, , , sigAlgorithms] = fingerprintData; 323 | 324 | // Part A: Protocol info 325 | const protocol = 't'; // We only handle TCP for now 326 | 327 | const version = extensions.includes(0x002B) 328 | ? '13' // TLS 1.3 uses the supported versions extension, and 1.4+ doesn't exist (yet) 329 | : { // Previous TLS sets the version in the handshake up front: 330 | 0x0303: '12', 331 | 0x0302: '11', 332 | 0x0301: '10' 333 | }[tlsVersion] 334 | ?? '00'; // Other unknown version 335 | 336 | const sni = !serverName ? 'i' : 'd'; // 'i' for IP (no SNI), 'd' for domain 337 | 338 | // Handle different ALPN protocols 339 | let alpn = '00'; 340 | const firstProtocol = alpnProtocols?.[0]; 341 | if (firstProtocol && firstProtocol.length >= 1) { 342 | // Take first and last character of the protocol string 343 | alpn = firstProtocol.length >= 2 344 | ? `${firstProtocol[0]}${firstProtocol[firstProtocol.length - 1]}` 345 | : `${firstProtocol[0]}${firstProtocol[0]}`; 346 | } 347 | 348 | // Format numbers as fixed-width hex 349 | const cipherCount = ciphers.length.toString().padStart(2, '0'); 350 | const extensionCount = extensions.length.toString().padStart(2, '0'); 351 | 352 | const ja4_a = `${protocol}${version}${sni}${cipherCount}${extensionCount}${alpn}`; 353 | 354 | // Part B: Truncated SHA256 of cipher suites 355 | // First collect all hex values and sort them 356 | const cipherHexValues = ciphers 357 | .filter(c => !isGREASE(c)) 358 | .map(c => c.toString(16).padStart(4, '0')); 359 | const sortedCiphers = [...cipherHexValues].sort().join(','); 360 | const cipherHash = ciphers.length 361 | ? crypto.createHash('sha256') 362 | .update(sortedCiphers) 363 | .digest('hex') 364 | .slice(0, 12) 365 | : '000000000000'; // No ciphers provided 366 | 367 | // Part C: Truncated SHA256 of extensions + sig algorithms 368 | // Get extensions (excluding SNI and ALPN) 369 | const extensionsStr = extensions 370 | .filter(e => { 371 | if (e === 0x0 || e === 0x10) return false; // Filter SNI and ALPN 372 | return !isGREASE(e); 373 | }) 374 | .sort((a, b) => a - b) 375 | .map(e => e.toString(16).padStart(4, '0')) 376 | .join(','); 377 | 378 | // Get signature algorithms from the actual TLS hello data 379 | const signatureAlgorithmsStr = sigAlgorithms 380 | ? sigAlgorithms 381 | .filter(s => !isGREASE(s)) 382 | .map(s => s.toString(16).padStart(4, '0')) 383 | .join(',') 384 | : ''; 385 | 386 | // Add separator only if we have signature algorithms 387 | const separator = signatureAlgorithmsStr ? '_' : ''; 388 | 389 | // Combine and hash 390 | const ja4_c_raw = `${extensionsStr}${separator}${signatureAlgorithmsStr}`; 391 | 392 | const extensionHash = crypto.createHash('sha256') 393 | .update(ja4_c_raw) 394 | .digest('hex') 395 | .slice(0, 12); 396 | 397 | return `${ja4_a}_${cipherHash}_${extensionHash}`; 398 | } 399 | 400 | export async function getTlsFingerprintAsJa4(rawStream: stream.Readable) { 401 | return calculateJa4FromHelloData( 402 | (await readTlsClientHello(rawStream)) 403 | ); 404 | } 405 | 406 | interface SocketWithHello extends net.Socket { 407 | tlsClientHello?: TlsHelloData & { 408 | ja3: string; 409 | ja4: string; 410 | } 411 | } 412 | 413 | declare module 'tls' { 414 | interface TLSSocket { 415 | /** 416 | * This module extends the global TLS types so that all TLS sockets may include 417 | * TLS fingerprint data. 418 | * 419 | * This is only set if the socket came from a TLS server where fingerprinting 420 | * has been enabled with `trackClientHellos`. 421 | */ 422 | tlsClientHello?: TlsHelloData & { 423 | ja3: string; 424 | ja4: string; 425 | } 426 | } 427 | } 428 | 429 | /** 430 | * Modify a TLS server, so that the TLS client hello is always parsed and the result is 431 | * attached to all sockets at the point when the 'secureConnection' event fires. 432 | * 433 | * This method mutates and returns the TLS server provided. TLS client hello data is 434 | * available from all TLS sockets afterwards in the `socket.tlsClientHello` property. 435 | * 436 | * This will work for all standard uses of a TLS server or similar (e.g. an HTTPS server) 437 | * but may behave unpredictably for advanced use cases, e.g. if you are already 438 | * manually injecting connections, hooking methods or events or otherwise doing something 439 | * funky & complicated. In those cases you probably want to use the fingerprint 440 | * calculation methods directly inside your funky logic instead. 441 | */ 442 | export function trackClientHellos(tlsServer: tls.Server) { 443 | // Disable the normal TLS 'connection' event listener that triggers TLS setup: 444 | const tlsConnectionListener = tlsServer.listeners('connection')[0] as (socket: net.Socket) => {}; 445 | if (!tlsConnectionListener) throw new Error('TLS server is not listening for connection events'); 446 | tlsServer.removeListener('connection', tlsConnectionListener); 447 | 448 | // Listen ourselves for connections, get the fingerprint first, then let TLS setup resume: 449 | tlsServer.on('connection', async (socket: SocketWithHello) => { 450 | try { 451 | const helloData = await readTlsClientHello(socket); 452 | 453 | socket.tlsClientHello = { 454 | ...helloData, 455 | ja3: calculateJa3FromFingerprintData(helloData.fingerprintData), 456 | ja4: calculateJa4FromHelloData(helloData) 457 | }; 458 | } catch (e) { 459 | if (!(e instanceof NonTlsError)) { // Ignore totally non-TLS traffic 460 | console.warn(`TLS client hello data not available for TLS connection from ${ 461 | socket.remoteAddress ?? 'unknown address' 462 | }: ${(e as Error).message ?? e}`); 463 | } 464 | } 465 | 466 | // Once we have a fingerprint, TLS handshakes can continue as normal: 467 | tlsConnectionListener.call(tlsServer, socket); 468 | }); 469 | 470 | tlsServer.prependListener('secureConnection', (tlsSocket: tls.TLSSocket) => { 471 | const fingerprint = (tlsSocket as unknown as { 472 | _parent?: SocketWithHello, // Private TLS socket field which points to the source 473 | })._parent?.tlsClientHello; 474 | 475 | tlsSocket.tlsClientHello = fingerprint; 476 | }); 477 | 478 | return tlsServer; 479 | } -------------------------------------------------------------------------------- /test/fixtures/chrome-tls-connect.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/httptoolkit/read-tls-client-hello/e1574e8e78ed1ecfcd7a624047315df4dce6d5cc/test/fixtures/chrome-tls-connect.bin -------------------------------------------------------------------------------- /test/fixtures/server.crt: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIDJzCCAg+gAwIBAgIUfxc7qPwEIlAJQtS+67g2MhKq9zkwDQYJKoZIhvcNAQEL 3 | BQAwFDESMBAGA1UEAwwJbG9jYWxob3N0MCAXDTIxMDgzMDE1MjUxNFoYDzIxMjEw 4 | ODA2MTUyNTE0WjAUMRIwEAYDVQQDDAlsb2NhbGhvc3QwggEiMA0GCSqGSIb3DQEB 5 | AQUAA4IBDwAwggEKAoIBAQC4Cf55DdM7CE40miw2k2B1m+gihcHl+GkEu5ulrX9i 6 | NbgqsWC3FixNMejDk0DdyEi2QGdQNFha7w/jlQ/u4HGJTQBmKsqVFaV03N/dnnCV 7 | 3yll7tjDJoA7yxohGSs2ouOhvytM/w2JflJ/bV5OWhsTrbV8V7omPGm83F/3RXer 8 | r76VUvhNbtpwc/QEV+zZ9HWDA9p+bjtsusFb+HD4XIn7lYfBBXQgUfGFJR1VCftQ 9 | 7b8VCoMZTGj6+NL1+k+EQvgdlwi2u7BfXYI1q765gUk5XS3/eennvOKi8Jn5kzuh 10 | gQRyhErzYHwKv+KUYvJvOctIjHL6egNUqeVXYB3Q9AphAgMBAAGjbzBtMB0GA1Ud 11 | DgQWBBRWMwFWKMABMs5WSuxEnFoYPwlAUjAfBgNVHSMEGDAWgBRWMwFWKMABMs5W 12 | SuxEnFoYPwlAUjAPBgNVHRMBAf8EBTADAQH/MBoGA1UdEQQTMBGCCWxvY2FsaG9z 13 | dIcEfwAAATANBgkqhkiG9w0BAQsFAAOCAQEAdjlHqfdKUdEdGofuL5CLMOZtMgmU 14 | 7BYRuBVkBBRb+aysRf+vb8OvmCHK+j4HzWe5hlsrJkyDhvTWQ6IMxuufEdK9NwOX 15 | 8zLut+k7N7LRkJvqC+RX8bbD+2a56n+TJjdvCqsyBFyuYdlxw07s7So2gW818ooK 16 | qTJSAjBcSkrdgZhYOEkG/AWKkelZRw2R9WLQv5wB2b+R7q0wrGtB2b9IXOs6JTaS 17 | HA5dFMDW8YaGiUQLm53eEZeTeZg+l5+izl4mTi4ytV4xa5KDyQTzzTttkLHK4cb3 18 | kVgGmAHsFu5/W1u/1u/WG/JnUW1XeEml7HVRuyA2E+GWCJc2fvTd4A0DaQ== 19 | -----END CERTIFICATE----- 20 | -------------------------------------------------------------------------------- /test/fixtures/server.key: -------------------------------------------------------------------------------- 1 | -----BEGIN PRIVATE KEY----- 2 | MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQC4Cf55DdM7CE40 3 | miw2k2B1m+gihcHl+GkEu5ulrX9iNbgqsWC3FixNMejDk0DdyEi2QGdQNFha7w/j 4 | lQ/u4HGJTQBmKsqVFaV03N/dnnCV3yll7tjDJoA7yxohGSs2ouOhvytM/w2JflJ/ 5 | bV5OWhsTrbV8V7omPGm83F/3RXerr76VUvhNbtpwc/QEV+zZ9HWDA9p+bjtsusFb 6 | +HD4XIn7lYfBBXQgUfGFJR1VCftQ7b8VCoMZTGj6+NL1+k+EQvgdlwi2u7BfXYI1 7 | q765gUk5XS3/eennvOKi8Jn5kzuhgQRyhErzYHwKv+KUYvJvOctIjHL6egNUqeVX 8 | YB3Q9AphAgMBAAECggEAYCeQlizb/Q7U1XTrvsP3dNs1SLw712yXagqfQsvIL0bD 9 | 50JvtpjWIqr94xkPnhCjtN0nXWdL9o7K7WwXPAZ2K3dYywh2ebgqj0lLiZ3bUuKa 10 | 3ZASHrwB6buu9jYRNuWaKwsXk436w6iFb+BzklpPpVNv6/xl3M5ZrHwzg5z+7mrs 11 | LQ83IgjFe6kVnkLYFPWLrFEsoiS3jOoILiDCUO5k4j0ZNYjRlmPpG7Iq+oPv+0rf 12 | JHiOV1Q9L1SMeubYSZ7NSiKPAqbi93BjJOpbNS2BrSL5YPt5pp8tKhwLj0gB8hux 13 | +XBwpn8GwwUypNy0YdbtYbLs/CbPcg1t5NEvhkrysQKBgQDfnBiQck9Xd8nqvwkb 14 | N0nw5UBXiPoKqHOxfQC5fsYjNdwKEvUj4OiGwAFtUwTSMqyqdmrj1Sy9SUuYfm3w 15 | ehOdQnabVyxYZj5Z1nLrP+wtXdCWv66ezq2S4dRsygs2fuy3uiL+ryVMojlBYj6M 16 | s53ipMN3qjDKt2NmwGWt33DJrQKBgQDSsokjbg7OMDebiWoimViE+eWRJ3YEXAtk 17 | WnDttCcG0qEGVi2gqZh6NjnCpiK3+dC07Sh24wfW6TS9Z9SZeNPrQ3BTnY4e0EDC 18 | hLo4MXeSEeXa+ndlTzpNcw/q63RNlKg7L+FJOV7Q3HaHKZvmC+kw2Y2dI9zYlK+X 19 | RH9ymffCBQKBgHdJHS2JXVwK0hNBX8k+AFra4S0RLFodLMKlLYrG30oPRFe3b0B5 20 | jXG84cYBQJQlZkj1LOZnZRuBCyvJXjqn1OjSeNU7drOdr2tbZCitC//TiR+yF6Qu 21 | Gxg9EoYKblre8Ma+LEbzBhHQhHylvTpv4yzxujiO+MJbfFJnFpbfmJptAoGARxx4 22 | yptvpcmCSx1y0+Cbjq3k/DusSkZileksah2+ekAGluPpHGuBCeZZUkfOOfe3qAjO 23 | +mkfkTo+UZrEl1O/eozVUXNAr0esQ7qWOzb+2y7tPB4CxA+cZt1pxujW5QRCT0+W 24 | oqcZSDbQTkgN1PO6LYGPmTSsafCs3soAlcY/Z50CgYBRhbOo9mq+hNXrKYifJwWZ 25 | dVzvlV1hJZ8ViXCzJIFwkcq9yokUwsppF/K+5c/fKN+6IWAwep7I7035GEAwtLTc 26 | J4oxpxDBYgHAON7OqHuns/xQ1Lvvg6fzAIQzR3NTXoPBSOyT2TbZJLDUf9jQ/TN/ 27 | ts7xHGcDnvSUE/8RItnM5w== 28 | -----END PRIVATE KEY----- 29 | -------------------------------------------------------------------------------- /test/test-util.ts: -------------------------------------------------------------------------------- 1 | import * as stream from 'stream'; 2 | import * as fs from 'fs'; 3 | 4 | export const testKey = fs.readFileSync(__dirname + '/fixtures/server.key'); 5 | export const testCert = fs.readFileSync(__dirname + '/fixtures/server.crt'); 6 | 7 | export type Deferred = Promise & { 8 | resolve(value: T): void, 9 | reject(e: Error): void 10 | } 11 | 12 | export function getDeferred(): Deferred { 13 | let resolveCallback: (value: T) => void; 14 | let rejectCallback: (e: Error) => void; 15 | let result = > new Promise((resolve, reject) => { 16 | resolveCallback = resolve; 17 | rejectCallback = reject; 18 | }); 19 | result.resolve = resolveCallback!; 20 | result.reject = rejectCallback!; 21 | 22 | return result; 23 | } 24 | 25 | export async function streamToBuffer(stream: stream.Readable): Promise { 26 | const data: Buffer[] = []; 27 | stream.on('data', (d) => data.push(d)); 28 | 29 | return new Promise((resolve, reject) => { 30 | stream.on('end', () => resolve(Buffer.concat(data))); 31 | stream.on('error', reject); 32 | }); 33 | } -------------------------------------------------------------------------------- /test/test.spec.ts: -------------------------------------------------------------------------------- 1 | import * as path from 'path'; 2 | import * as fs from 'fs'; 3 | import * as net from 'net'; 4 | import * as tls from 'tls'; 5 | import * as http from 'http'; 6 | import * as https from 'https'; 7 | import { makeDestroyable, DestroyableServer } from 'destroyable-server'; 8 | 9 | import { expect } from 'chai'; 10 | import { 11 | getDeferred, 12 | streamToBuffer, 13 | testKey, 14 | testCert 15 | } from './test-util'; 16 | 17 | import { 18 | readTlsClientHello, 19 | getTlsFingerprintAsJa3, 20 | getTlsFingerprintAsJa4, 21 | calculateJa3FromFingerprintData, 22 | trackClientHellos, 23 | calculateJa4FromHelloData 24 | } from '../src/index'; 25 | 26 | const nodeMajorVersion = parseInt(process.version.slice(1).split('.')[0], 10); 27 | 28 | interface EchoResponse { 29 | tls: { 30 | ja3: { 31 | hash: string; 32 | }; 33 | ja4: { 34 | hash: string; 35 | raw: string; 36 | }; 37 | }; 38 | } 39 | 40 | describe("Read-TLS-Client-Hello", () => { 41 | 42 | let server: DestroyableServer; 43 | 44 | afterEach(() => server?.destroy().catch(() => {})); 45 | 46 | it("can read Node's fingerprint data", async () => { 47 | server = makeDestroyable(new net.Server()); 48 | 49 | server.listen(); 50 | await new Promise((resolve) => server.on('listening', resolve)); 51 | 52 | let incomingSocketPromise = getDeferred(); 53 | server.on('connection', (socket) => incomingSocketPromise.resolve(socket)); 54 | 55 | const port = (server.address() as net.AddressInfo).port; 56 | tls.connect({ 57 | host: 'localhost', 58 | port 59 | }).on('error', () => {}); // Socket will fail, since server never responds, that's OK 60 | 61 | const incomingSocket = await incomingSocketPromise; 62 | const { fingerprintData } = await readTlsClientHello(incomingSocket); 63 | 64 | const [ 65 | tlsVersion, 66 | ciphers, 67 | extension, 68 | groups, 69 | curveFormats 70 | ] = fingerprintData; 71 | 72 | expect(tlsVersion).to.equal(771); // TLS 1.2 - now set even for TLS 1.3 for backward compat 73 | expect(ciphers.slice(0, 3)).to.deep.equal([4866, 4867, 4865]); 74 | expect(extension).to.deep.equal([ 75 | 11, 76 | 10, 77 | 35, 78 | 22, 79 | 23, 80 | 13, 81 | 43, 82 | 45, 83 | 51 84 | ]); 85 | expect(groups).to.deep.equal([ 86 | 29, 23, 30, 25, 24, 87 | ...(nodeMajorVersion >= 17 ? [256, 257, 258, 259, 260] : []) 88 | ]); 89 | expect(curveFormats).to.deep.equal([0, 1, 2]); 90 | }); 91 | 92 | it("can read Node's client hello data", async () => { 93 | server = makeDestroyable(new net.Server()); 94 | 95 | server.listen(); 96 | await new Promise((resolve) => server.on('listening', resolve)); 97 | 98 | let incomingSocketPromise = getDeferred(); 99 | server.on('connection', (socket) => incomingSocketPromise.resolve(socket)); 100 | 101 | const port = (server.address() as net.AddressInfo).port; 102 | tls.connect({ 103 | host: 'localhost', 104 | port 105 | }).on('error', () => {}); // Socket will fail, since server never responds, that's OK 106 | 107 | const incomingSocket = await incomingSocketPromise; 108 | const { serverName, alpnProtocols } = await readTlsClientHello(incomingSocket); 109 | 110 | expect(serverName).to.equal(undefined); // No SNI set for pure TLS like this 111 | expect(alpnProtocols).to.equal(undefined); // No SNI set for pure TLS like this 112 | }); 113 | 114 | it("can read Node's JA3 fingerprint", async () => { 115 | server = makeDestroyable(new net.Server()); 116 | 117 | server.listen(); 118 | await new Promise((resolve) => server.on('listening', resolve)); 119 | 120 | let incomingSocketPromise = getDeferred(); 121 | server.on('connection', (socket) => incomingSocketPromise.resolve(socket)); 122 | 123 | const port = (server.address() as net.AddressInfo).port; 124 | https.request({ 125 | host: 'localhost', 126 | port 127 | }).on('error', () => {}); // Socket will fail, since server never responds, that's OK 128 | 129 | const incomingSocket = await incomingSocketPromise; 130 | const fingerprint = await getTlsFingerprintAsJa3(incomingSocket); 131 | 132 | expect(fingerprint).to.be.oneOf([ 133 | '398430069e0a8ecfbc8db0778d658d77', // Node 12 - 16 134 | '0cce74b0d9b7f8528fb2181588d23793' // Node 17+ 135 | ]); 136 | }); 137 | 138 | it("can read Node's JA4 fingerprint", async () => { 139 | server = makeDestroyable(new net.Server()); 140 | 141 | server.listen(); 142 | await new Promise((resolve) => server.on('listening', resolve)); 143 | 144 | let incomingSocketPromise = getDeferred(); 145 | server.on('connection', (socket) => incomingSocketPromise.resolve(socket)); 146 | 147 | const port = (server.address() as net.AddressInfo).port; 148 | https.request({ 149 | host: 'localhost', 150 | port 151 | }).on('error', () => {}); // Socket will fail, since server never responds, that's OK 152 | 153 | const incomingSocket = await incomingSocketPromise; 154 | const fingerprint = await getTlsFingerprintAsJa4(incomingSocket); 155 | 156 | expect(fingerprint).to.be.oneOf([ 157 | 't13d591000_a33745022dd6_5ac7197df9d2', // Node 12 - 16 158 | 't13d591000_a33745022dd6_1f22a2ca17c4' // Node 17+ 159 | ]); 160 | }); 161 | 162 | it("calculates the same fingerprint as echo.ramaproxy.org", async () => { 163 | server = makeDestroyable(new net.Server()); 164 | 165 | server.listen(); 166 | await new Promise((resolve) => server.on('listening', resolve)); 167 | 168 | let incomingSocketPromise = getDeferred(); 169 | server.on('connection', (socket) => incomingSocketPromise.resolve(socket)); 170 | 171 | const port = (server.address() as net.AddressInfo).port; 172 | https.request({ 173 | host: 'localhost', 174 | port 175 | }).on('error', () => {}); // Socket will fail, since server never responds, that's OK 176 | 177 | const incomingSocket = await incomingSocketPromise; 178 | const helloData = await readTlsClientHello(incomingSocket); 179 | const ourJa3 = await getTlsFingerprintAsJa3(incomingSocket); 180 | const ourJa4 = calculateJa4FromHelloData(helloData); 181 | 182 | const remoteFingerprints = await new Promise((resolve, reject) => { 183 | const response = https.get('https://echo.ramaproxy.org/'); 184 | response.on('response', async (resp) => { 185 | if (resp.statusCode !== 200) reject(new Error(`Unexpected ${resp.statusCode} from echo.ramaproxy.org`)); 186 | 187 | try { 188 | const rawData = await streamToBuffer(resp); 189 | const data = JSON.parse(rawData.toString()) as EchoResponse; 190 | resolve(data); 191 | } catch (e) { 192 | reject(e); 193 | } 194 | }); 195 | response.on('error', reject); 196 | }); 197 | 198 | // Check both JA3 and JA4 hashes 199 | expect(ourJa3).to.equal(remoteFingerprints.tls.ja3.hash); 200 | expect(ourJa4).to.equal(remoteFingerprints.tls.ja4.hash); 201 | }); 202 | 203 | it("can capture the server name from a Chrome request", async () => { 204 | const incomingData = fs.createReadStream(path.join(__dirname, 'fixtures', 'chrome-tls-connect.bin')); 205 | 206 | const { serverName } = await readTlsClientHello(incomingData); 207 | expect(serverName).to.equal('localhost'); 208 | }); 209 | 210 | it("can capture ALPN protocols from a Chrome request", async () => { 211 | const incomingData = fs.createReadStream(path.join(__dirname, 'fixtures', 'chrome-tls-connect.bin')); 212 | 213 | const { alpnProtocols } = await readTlsClientHello(incomingData); 214 | expect(alpnProtocols).to.deep.equal([ 215 | 'h2', 216 | 'http/1.1' 217 | ]); 218 | }); 219 | 220 | it("can calculate the correct TLS fingerprint from a Chrome request", async () => { 221 | const incomingData = fs.createReadStream(path.join(__dirname, 'fixtures', 'chrome-tls-connect.bin')); 222 | 223 | const { fingerprintData } = await readTlsClientHello(incomingData); 224 | 225 | const [ 226 | tlsVersion, 227 | ciphers, 228 | extension, 229 | groups, 230 | curveFormats 231 | ] = fingerprintData; 232 | 233 | expect(tlsVersion).to.equal(771); // TLS 1.2 - now set even for TLS 1.3 for backward compat 234 | expect(ciphers.slice(0, 3)).to.deep.equal([4865, 4866, 4867]); 235 | expect(ciphers.length).to.equal(15); 236 | expect(extension).to.deep.equal([ 237 | 0, 238 | 23, 239 | 65281, 240 | 10, 241 | 11, 242 | 35, 243 | 16, 244 | 5, 245 | 13, 246 | 18, 247 | 51, 248 | 45, 249 | 43, 250 | 27, 251 | 17513, 252 | 21 253 | ]); 254 | expect(groups).to.deep.equal([29, 23, 24]); 255 | expect(curveFormats).to.deep.equal([0]); 256 | 257 | const fingerprint = calculateJa3FromFingerprintData(fingerprintData); 258 | expect(fingerprint).to.equal('cd08e31494f9531f560d64c695473da9'); 259 | }); 260 | 261 | it("can be manually calculate the fingerprint alongside a real TLS session", async () => { 262 | const tlsServer = tls.createServer({ key: testKey, cert: testCert }) 263 | server = makeDestroyable(new net.Server()); 264 | 265 | server.on('connection', async (socket: any) => { 266 | socket.tlsFingerprint = await getTlsFingerprintAsJa3(socket); 267 | tlsServer.emit('connection', socket); 268 | }); 269 | 270 | const tlsSocketPromise = new Promise((resolve) => 271 | tlsServer.on('secureConnection', (tlsSocket: any) => { 272 | tlsSocket.tlsFingerprint = tlsSocket._parent.tlsFingerprint; 273 | resolve(tlsSocket); 274 | }) 275 | ); 276 | 277 | server.listen(); 278 | await new Promise((resolve) => server.on('listening', resolve)); 279 | 280 | const port = (server.address() as net.AddressInfo).port; 281 | tls.connect({ 282 | host: 'localhost', 283 | ca: [testCert], 284 | port 285 | }); 286 | 287 | const tlsSocket: any = await tlsSocketPromise; 288 | const fingerprint = tlsSocket.tlsFingerprint; 289 | expect(fingerprint).to.be.oneOf([ 290 | '76cd17e0dc73c98badbb6ee3752dcf4c', // Node 12 - 16 291 | '6521bd74aad3476cdb3daa827288ec35' // Node 17+ 292 | ]); 293 | }); 294 | 295 | it("can be parsed automatically with the provided helper", async () => { 296 | const httpsServer = makeDestroyable( 297 | trackClientHellos( 298 | https.createServer({ key: testKey, cert: testCert }) 299 | ) 300 | ); 301 | server = httpsServer; 302 | 303 | const tlsSocketPromise = new Promise((resolve) => 304 | httpsServer.on('request', (request: http.IncomingMessage) => 305 | resolve(request.socket as tls.TLSSocket) 306 | ) 307 | ); 308 | 309 | httpsServer.listen(); 310 | await new Promise((resolve) => httpsServer.on('listening', resolve)); 311 | 312 | const port = (httpsServer.address() as net.AddressInfo).port; 313 | https.get({ 314 | host: 'localhost', 315 | ca: [testCert], 316 | port 317 | }).on('error', () => {}); // No response, we don't care 318 | 319 | const tlsSocket = await tlsSocketPromise; 320 | 321 | const [ 322 | tlsVersion, 323 | ciphers, 324 | extension, 325 | groups, 326 | curveFormats, 327 | sigAlgorithms 328 | ] = tlsSocket.tlsClientHello!.fingerprintData; 329 | 330 | expect(tlsSocket.tlsClientHello!.fingerprintData.length).to.equal(6); 331 | expect(tlsVersion).to.equal(771); 332 | expect(ciphers.length).to.be.greaterThan(0); 333 | expect(extension.length).to.be.greaterThan(0); 334 | expect(groups.length).to.be.greaterThan(0); 335 | expect(curveFormats.length).to.be.greaterThan(0); 336 | expect(sigAlgorithms.length).to.be.greaterThan(0); 337 | 338 | }); 339 | 340 | it("doesn't break non-TLS connections", async () => { 341 | const httpServer = new http.Server(); 342 | server = makeDestroyable(new net.Server()); 343 | 344 | server.on('connection', async (socket: any) => { 345 | socket.tlsFingerprint = await getTlsFingerprintAsJa3(socket) 346 | .catch(e => ({ error: e })); 347 | httpServer.emit('connection', socket); 348 | }); 349 | 350 | httpServer.on('request', (request, response) => { 351 | expect(request.method).to.equal('GET'); 352 | expect(request.url).to.equal('/test-request-path'); 353 | 354 | const fingerprint = (request.socket as any).tlsFingerprint; 355 | expect(fingerprint.error.message).to.equal( 356 | "Can't calculate TLS fingerprint - not a TLS stream" 357 | ); 358 | 359 | response.writeHead(200).end(); 360 | }); 361 | 362 | server.listen(); 363 | await new Promise((resolve) => server.on('listening', resolve)); 364 | 365 | const port = (server.address() as net.AddressInfo).port; 366 | const req = http.get({ host: 'localhost', port, path: '/test-request-path' }); 367 | 368 | const response = await new Promise((resolve) => 369 | req.on('response', resolve) 370 | ); 371 | 372 | expect(response.statusCode).to.equal(200); 373 | }); 374 | 375 | it("can read a TLS v1 fingerprint", async function () { 376 | if (nodeMajorVersion >= 17) this.skip(); // New Node doesn't support this 377 | 378 | server = makeDestroyable(new net.Server()); 379 | 380 | server.listen(); 381 | await new Promise((resolve) => server.on('listening', resolve)); 382 | 383 | let incomingSocketPromise = getDeferred(); 384 | server.on('connection', (socket) => incomingSocketPromise.resolve(socket)); 385 | 386 | const port = (server.address() as net.AddressInfo).port; 387 | tls.connect({ 388 | host: 'localhost', 389 | port, 390 | maxVersion: 'TLSv1', // <-- Force old TLS 391 | minVersion: 'TLSv1' 392 | }).on('error', () => {}); // Socket will fail, since server never responds, that's OK 393 | 394 | const incomingSocket = await incomingSocketPromise; 395 | const { fingerprintData } = await readTlsClientHello(incomingSocket); 396 | 397 | const [ 398 | tlsVersion, 399 | ciphers, 400 | extension, 401 | groups, 402 | curveFormats 403 | ] = fingerprintData; 404 | 405 | expect(tlsVersion).to.equal(769); // TLS 1! 406 | expect(ciphers.slice(0, 3)).to.deep.equal([49162, 49172, 57]); 407 | expect(extension).to.deep.equal([ 408 | 11, 409 | 10, 410 | 35, 411 | 22, 412 | 23 413 | ]); 414 | expect(groups).to.deep.equal([29, 23, 30, 25, 24]); 415 | expect(curveFormats).to.deep.equal([0, 1, 2]); 416 | }); 417 | 418 | }); -------------------------------------------------------------------------------- /test/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "include": [ 4 | "../src/**/*.ts", 5 | "../dist/**/*.d.ts", 6 | "./**/*.ts", 7 | "../package.json" 8 | ] 9 | } -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es6", 4 | "module": "commonjs", 5 | "outDir": "./dist", 6 | "declaration": true, 7 | "sourceMap": true, 8 | "strict": true 9 | }, 10 | "include": [ 11 | "src/**/*.ts" 12 | ] 13 | } 14 | -------------------------------------------------------------------------------- /wallaby.js: -------------------------------------------------------------------------------- 1 | module.exports = (wallaby) => { 2 | return { 3 | files: [ 4 | 'package.json', 5 | 'src/**/*.ts', 6 | 'test/**/*.ts', 7 | { pattern: 'test/fixtures/**/*', load: false, binary: true }, 8 | '!test/**/*.spec.ts' 9 | ], 10 | tests: [ 11 | 'test/**/*.spec.ts' 12 | ], 13 | 14 | preprocessors: { 15 | // Package.json points `main` to the built output. We use this a lot in the tests, but we 16 | // want wallaby to run on raw source. This is a simple remap of paths to lets us do that. 17 | 'test/**/*.ts': file => { 18 | return file.content.replace( 19 | /("|')\.\.("|')/g, 20 | '"../src/"' 21 | ); 22 | } 23 | }, 24 | 25 | workers: { 26 | initial: 1, 27 | regular: 1, 28 | restart: true 29 | }, 30 | 31 | testFramework: 'mocha', 32 | env: { 33 | type: 'node' 34 | }, 35 | debug: true 36 | }; 37 | }; --------------------------------------------------------------------------------