-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathja4plus_test.go
More file actions
207 lines (199 loc) · 6.33 KB
/
ja4plus_test.go
File metadata and controls
207 lines (199 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package ja4plus
import (
"crypto/tls"
"encoding/hex"
"math"
"testing"
)
func TestJA4(t *testing.T) {
tests := []struct {
name string
hello *tls.ClientHelloInfo
expected string
}{
{
name: "Basic ClientHelloInfo",
hello: &tls.ClientHelloInfo{
SupportedVersions: []uint16{tls.VersionTLS13},
SupportedProtos: []string{"http/1.1"},
},
expected: "t13i0000h1_000000000000_000000000000",
},
{
name: "ClientHelloInfo with multiple versions and protocols",
hello: &tls.ClientHelloInfo{
SupportedVersions: []uint16{tls.VersionTLS12, tls.VersionTLS13},
SupportedProtos: []string{"h2", "http/1.1"},
},
expected: "t13i0000h2_000000000000_000000000000",
},
{
name: "ClientHelloInfo with SNI",
hello: &tls.ClientHelloInfo{
SupportedVersions: []uint16{tls.VersionTLS13},
SupportedProtos: []string{"http/1.1"},
ServerName: "example.com",
},
expected: "t13d0000h1_000000000000_000000000000",
},
{
name: "ClientHelloInfo with cipher suites",
hello: &tls.ClientHelloInfo{
SupportedVersions: []uint16{tls.VersionTLS13},
CipherSuites: []uint16{tls.TLS_AES_128_GCM_SHA256, tls.TLS_AES_256_GCM_SHA384},
SupportedProtos: []string{"http/1.1"},
},
expected: "t13i0200h1_62ed6f6ca7ad_000000000000",
},
{
name: "ClientHelloInfo with only ignored extensions",
hello: &tls.ClientHelloInfo{
SupportedVersions: []uint16{tls.VersionTLS13},
SupportedProtos: []string{"http/1.1"},
Extensions: []uint16{0x0000 /* SNI */, 0x0010 /* ALPN */, 0x1A1A /* GREASE */},
},
expected: "t13i0002h1_000000000000_000000000000",
},
{
name: "ClientHelloInfo with extensions and signature schemes",
hello: &tls.ClientHelloInfo{
SupportedVersions: []uint16{tls.VersionTLS13},
SupportedProtos: []string{"http/1.1"},
Extensions: []uint16{0x0000 /* SNI */, 0x1A1A /* GREASE */, 0x0042 /* "early data" */},
SignatureSchemes: []tls.SignatureScheme{tls.PKCS1WithSHA256, tls.ECDSAWithP256AndSHA256},
SupportedCurves: []tls.CurveID{tls.CurveP256},
},
expected: "t13i0002h1_000000000000_5b56ea7744b1",
},
{
name: "ClientHelloInfo with grease values in SupportedVersions",
hello: &tls.ClientHelloInfo{
SupportedVersions: []uint16{tls.VersionTLS13, 0x1A1A /* GREASE */, tls.VersionTLS12, 0x2A2A /* GREASE */},
SupportedProtos: []string{"http/1.1"},
},
expected: "t13i0000h1_000000000000_000000000000",
},
{
name: "ClientHelloInfo with cipher suites, extensions and signature schemes, all greased",
hello: &tls.ClientHelloInfo{
SupportedVersions: []uint16{0x1A1A /* GREASE */, tls.VersionTLS13},
SupportedProtos: []string{string([]byte{0x1A, 0x1A}) /* GREASE */, "http/1.1"},
CipherSuites: []uint16{tls.TLS_AES_128_GCM_SHA256, tls.TLS_AES_256_GCM_SHA384},
Extensions: []uint16{0x0000 /* SNI */, 0x1A1A /* GREASE */, 0x0042 /* "early data" */},
SignatureSchemes: []tls.SignatureScheme{0x1A1A /* GREASE */, tls.PKCS1WithSHA256, tls.ECDSAWithP256AndSHA256},
SupportedCurves: []tls.CurveID{tls.CurveP256},
},
expected: "t13i0202h1_62ed6f6ca7ad_5b56ea7744b1",
},
{
// the TLS stack should not allow this, but we ensure we're defensive.
name: "Do not panic on invalid proto version",
hello: &tls.ClientHelloInfo{
SupportedVersions: []uint16{tls.VersionTLS13},
SupportedProtos: []string{"a"}, // invalid!
},
expected: "t13i000000_000000000000_000000000000",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fingerprint := JA4(tt.hello)
if fingerprint != tt.expected {
t.Errorf("Expected %s, but got %s", tt.expected, fingerprint)
}
})
}
}
func BenchmarkJA4(b *testing.B) {
hello := &tls.ClientHelloInfo{
SupportedVersions: []uint16{tls.VersionTLS13},
CipherSuites: []uint16{tls.TLS_AES_128_GCM_SHA256, tls.TLS_AES_256_GCM_SHA384},
SupportedProtos: []string{"h2", "http/1.1"},
Extensions: []uint16{0x0000 /* SNI */, 0x1A1A /* GREASE */, 0x0042 /* "early data" */},
}
for b.Loop() {
JA4(hello)
}
}
func TestGreaseFilter(t *testing.T) {
greaseValues := map[uint16]bool{
0x0A0A: true,
0x1A1A: true,
0x2A2A: true,
0x3A3A: true,
0x4A4A: true,
0x5A5A: true,
0x6A6A: true,
0x7A7A: true,
0x8A8A: true,
0x9A9A: true,
0xAAAA: true,
0xBABA: true,
0xCACA: true,
0xDADA: true,
0xEAEA: true,
0xFAFA: true,
}
for i := range math.MaxUint16 {
// test the fast method with the slow known-value check
value := uint16(i)
expected := greaseValues[value]
result := greaseFilter(value)
if result != expected {
t.Errorf("For value 0x%04X, expected %v, but got %v", value, expected, result)
}
}
}
func TestExtensionHash(t *testing.T) {
tests := []struct {
name string
extensions []uint16
signatureSchemes []tls.SignatureScheme
expected string
}{
{
name: "No extensions or signature schemes",
extensions: []uint16{},
signatureSchemes: []tls.SignatureScheme{},
expected: "000000000000",
},
{
name: "Extensions with SNI and ALPN",
extensions: []uint16{0x0000, 0x0010},
signatureSchemes: []tls.SignatureScheme{},
expected: "000000000000",
},
{
name: "Valid extensions without signature schemes",
extensions: []uint16{0x0001, 0x0002},
signatureSchemes: []tls.SignatureScheme{},
expected: "5b7701cdea2c",
},
{
name: "Valid unsorted extensions without signature schemes",
extensions: []uint16{0x0002, 0x0001},
signatureSchemes: []tls.SignatureScheme{},
expected: "5b7701cdea2c",
},
{
name: "Valid extensions with signature schemes",
extensions: []uint16{0x0001, 0x0002},
signatureSchemes: []tls.SignatureScheme{tls.PKCS1WithSHA256, tls.ECDSAWithP256AndSHA256},
expected: "7ca161ef5d14",
},
{
name: "Only signature schemes",
extensions: []uint16{},
signatureSchemes: []tls.SignatureScheme{tls.PKCS1WithSHA256, tls.ECDSAWithP256AndSHA256},
expected: "000000000000",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
hash := extensionHash(tt.extensions, tt.signatureSchemes)
if got := hex.EncodeToString(hash[:]); got != tt.expected {
t.Errorf("Expected %s, but got %s", tt.expected, got)
}
})
}
}