|
| 1 | +/** |
| 2 | + * @author n1474335 [[email protected]] |
| 3 | + * @copyright Crown Copyright 2021 |
| 4 | + * @license Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import Operation from "../Operation.mjs"; |
| 8 | +import OperationError from "../errors/OperationError.mjs"; |
| 9 | +import Utils from "../Utils.mjs"; |
| 10 | +import Stream from "../lib/Stream.mjs"; |
| 11 | +import {runHash} from "../lib/Hash.mjs"; |
| 12 | + |
| 13 | +/** |
| 14 | + * TLS JA3 Fingerprint operation |
| 15 | + */ |
| 16 | +class TLSJA3Fingerprint extends Operation { |
| 17 | + |
| 18 | + /** |
| 19 | + * TLSJA3Fingerprint constructor |
| 20 | + */ |
| 21 | + constructor() { |
| 22 | + super(); |
| 23 | + |
| 24 | + this.name = "TLS JA3 Fingerprint"; |
| 25 | + this.module = "Crypto"; |
| 26 | + this.description = "Generates a JA3 fingerprint to help identify TLS clients based on hashing together values from the Client Hello.<br><br>Input: A hex stream of the TLS Client Hello application layer."; |
| 27 | + this.infoURL = "https://engineering.salesforce.com/tls-fingerprinting-with-ja3-and-ja3s-247362855967"; |
| 28 | + this.inputType = "string"; |
| 29 | + this.outputType = "string"; |
| 30 | + this.args = [ |
| 31 | + { |
| 32 | + name: "Input format", |
| 33 | + type: "option", |
| 34 | + value: ["Hex", "Base64", "Raw"] |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "Output format", |
| 38 | + type: "option", |
| 39 | + value: ["Hash digest", "JA3 string", "Full details"] |
| 40 | + } |
| 41 | + ]; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @param {string} input |
| 46 | + * @param {Object[]} args |
| 47 | + * @returns {string} |
| 48 | + */ |
| 49 | + run(input, args) { |
| 50 | + const [inputFormat, outputFormat] = args; |
| 51 | + |
| 52 | + input = Utils.convertToByteArray(input, inputFormat); |
| 53 | + const s = new Stream(new Uint8Array(input)); |
| 54 | + |
| 55 | + const handshake = s.readInt(1); |
| 56 | + if (handshake !== 0x16) |
| 57 | + throw new OperationError("Not handshake data."); |
| 58 | + |
| 59 | + // Version |
| 60 | + s.moveForwardsBy(2); |
| 61 | + |
| 62 | + // Length |
| 63 | + const length = s.readInt(2); |
| 64 | + if (s.length !== length + 5) |
| 65 | + throw new OperationError("Incorrect handshake length."); |
| 66 | + |
| 67 | + // Handshake type |
| 68 | + const handshakeType = s.readInt(1); |
| 69 | + if (handshakeType !== 1) |
| 70 | + throw new OperationError("Not a Client Hello."); |
| 71 | + |
| 72 | + // Handshake length |
| 73 | + const handshakeLength = s.readInt(3); |
| 74 | + if (s.length !== handshakeLength + 9) |
| 75 | + throw new OperationError("Not enough data in Client Hello."); |
| 76 | + |
| 77 | + // Hello version |
| 78 | + const helloVersion = s.readInt(2); |
| 79 | + |
| 80 | + // Random |
| 81 | + s.moveForwardsBy(32); |
| 82 | + |
| 83 | + // Session ID |
| 84 | + const sessionIDLength = s.readInt(1); |
| 85 | + s.moveForwardsBy(sessionIDLength); |
| 86 | + |
| 87 | + // Cipher suites |
| 88 | + const cipherSuitesLength = s.readInt(2); |
| 89 | + const cipherSuites = s.getBytes(cipherSuitesLength); |
| 90 | + const cs = new Stream(cipherSuites); |
| 91 | + const cipherSegment = parseJA3Segment(cs, 2); |
| 92 | + |
| 93 | + // Compression Methods |
| 94 | + const compressionMethodsLength = s.readInt(1); |
| 95 | + s.moveForwardsBy(compressionMethodsLength); |
| 96 | + |
| 97 | + // Extensions |
| 98 | + const extensionsLength = s.readInt(2); |
| 99 | + const extensions = s.getBytes(extensionsLength); |
| 100 | + const es = new Stream(extensions); |
| 101 | + let ecsLen, ecs, ellipticCurves = "", ellipticCurvePointFormats = ""; |
| 102 | + const exts = []; |
| 103 | + while (es.hasMore()) { |
| 104 | + const type = es.readInt(2); |
| 105 | + const length = es.readInt(2); |
| 106 | + switch (type) { |
| 107 | + case 0x0a: // Elliptic curves |
| 108 | + ecsLen = es.readInt(2); |
| 109 | + ecs = new Stream(es.getBytes(ecsLen)); |
| 110 | + ellipticCurves = parseJA3Segment(ecs, 2); |
| 111 | + break; |
| 112 | + case 0x0b: // Elliptic curve point formats |
| 113 | + ecsLen = es.readInt(1); |
| 114 | + ecs = new Stream(es.getBytes(ecsLen)); |
| 115 | + ellipticCurvePointFormats = parseJA3Segment(ecs, 1); |
| 116 | + break; |
| 117 | + default: |
| 118 | + es.moveForwardsBy(length); |
| 119 | + } |
| 120 | + if (!GREASE_CIPHERSUITES.includes(type)) |
| 121 | + exts.push(type); |
| 122 | + } |
| 123 | + |
| 124 | + // Output |
| 125 | + const ja3 = [ |
| 126 | + helloVersion.toString(), |
| 127 | + cipherSegment, |
| 128 | + exts.join("-"), |
| 129 | + ellipticCurves, |
| 130 | + ellipticCurvePointFormats |
| 131 | + ]; |
| 132 | + const ja3Str = ja3.join(","); |
| 133 | + const ja3Hash = runHash("md5", Utils.strToArrayBuffer(ja3Str)); |
| 134 | + |
| 135 | + switch (outputFormat) { |
| 136 | + case "JA3 string": |
| 137 | + return ja3Str; |
| 138 | + case "Full details": |
| 139 | + return `Hash digest: |
| 140 | +${ja3Hash} |
| 141 | +
|
| 142 | +Full JA3 string: |
| 143 | +${ja3Str} |
| 144 | +
|
| 145 | +TLS Version: |
| 146 | +${helloVersion.toString()} |
| 147 | +Cipher Suites: |
| 148 | +${cipherSegment} |
| 149 | +Extensions: |
| 150 | +${exts.join("-")} |
| 151 | +Elliptic Curves: |
| 152 | +${ellipticCurves} |
| 153 | +Elliptic Curve Point Formats: |
| 154 | +${ellipticCurvePointFormats}`; |
| 155 | + case "Hash digest": |
| 156 | + default: |
| 157 | + return ja3Hash; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | +} |
| 162 | + |
| 163 | +/** |
| 164 | + * Parses a JA3 segment, returning a "-" separated list |
| 165 | + * |
| 166 | + * @param {Stream} stream |
| 167 | + * @returns {string} |
| 168 | + */ |
| 169 | +function parseJA3Segment(stream, size=2) { |
| 170 | + const segment = []; |
| 171 | + while (stream.hasMore()) { |
| 172 | + const element = stream.readInt(size); |
| 173 | + if (!GREASE_CIPHERSUITES.includes(element)) |
| 174 | + segment.push(element); |
| 175 | + } |
| 176 | + return segment.join("-"); |
| 177 | +} |
| 178 | + |
| 179 | +const GREASE_CIPHERSUITES = [ |
| 180 | + 0x0a0a, |
| 181 | + 0x1a1a, |
| 182 | + 0x2a2a, |
| 183 | + 0x3a3a, |
| 184 | + 0x4a4a, |
| 185 | + 0x5a5a, |
| 186 | + 0x6a6a, |
| 187 | + 0x7a7a, |
| 188 | + 0x8a8a, |
| 189 | + 0x9a9a, |
| 190 | + 0xaaaa, |
| 191 | + 0xbaba, |
| 192 | + 0xcaca, |
| 193 | + 0xdada, |
| 194 | + 0xeaea, |
| 195 | + 0xfafa |
| 196 | +]; |
| 197 | + |
| 198 | +export default TLSJA3Fingerprint; |
0 commit comments