forked from bogdanfinn/tls-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathja3.go
210 lines (170 loc) · 7.53 KB
/
ja3.go
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
208
209
210
package tls_client
import (
"crypto/sha256"
"fmt"
"strconv"
"strings"
tls "github.com/bogdanfinn/utls"
)
func GetSpecFactoryFromJa3String(ja3String string, supportedSignatureAlgorithms, supportedDelegatedCredentialsAlgorithms, supportedVersions, keyShareCurves []string, certCompressionAlgo string) (func() (tls.ClientHelloSpec, error), error) {
return func() (tls.ClientHelloSpec, error) {
var mappedSignatureAlgorithms []tls.SignatureScheme
for _, supportedSignatureAlgorithm := range supportedSignatureAlgorithms {
signatureAlgorithm, ok := signatureAlgorithms[supportedSignatureAlgorithm]
if ok {
mappedSignatureAlgorithms = append(mappedSignatureAlgorithms, signatureAlgorithm)
} else {
supportedSignatureAlgorithmAsUint, err := strconv.ParseUint(supportedSignatureAlgorithm, 16, 16)
if err != nil {
return tls.ClientHelloSpec{}, fmt.Errorf("%s is not a valid supportedSignatureAlgorithm", supportedSignatureAlgorithm)
}
mappedSignatureAlgorithms = append(mappedSignatureAlgorithms, tls.SignatureScheme(uint16(supportedSignatureAlgorithmAsUint)))
}
}
var mappedDelegatedCredentialsAlgorithms []tls.SignatureScheme
for _, supportedDelegatedCredentialsAlgorithm := range supportedDelegatedCredentialsAlgorithms {
delegatedCredentialsAlgorithm, ok := delegatedCredentialsAlgorithms[supportedDelegatedCredentialsAlgorithm]
if ok {
mappedDelegatedCredentialsAlgorithms = append(mappedDelegatedCredentialsAlgorithms, delegatedCredentialsAlgorithm)
} else {
supportedDelegatedCredentialsAlgorithmAsUint, err := strconv.ParseUint(supportedDelegatedCredentialsAlgorithm, 16, 16)
if err != nil {
return tls.ClientHelloSpec{}, fmt.Errorf("%s is not a valid supportedDelegatedCredentialsAlgorithm", supportedDelegatedCredentialsAlgorithm)
}
mappedDelegatedCredentialsAlgorithms = append(mappedDelegatedCredentialsAlgorithms, tls.SignatureScheme(uint16(supportedDelegatedCredentialsAlgorithmAsUint)))
}
}
var mappedTlsVersions []uint16
for _, version := range supportedVersions {
mappedVersion, ok := tlsVersions[version]
if ok {
mappedTlsVersions = append(mappedTlsVersions, mappedVersion)
}
}
var mappedKeyShares []tls.KeyShare
for _, keyShareCurve := range keyShareCurves {
resolvedKeyShare, ok := curves[keyShareCurve]
if !ok {
continue
}
mappedKeyShare := tls.KeyShare{Group: resolvedKeyShare}
if keyShareCurve == "GREASE" {
mappedKeyShare.Data = []byte{0}
}
mappedKeyShares = append(mappedKeyShares, mappedKeyShare)
}
compressionAlgo, ok := certCompression[certCompressionAlgo]
if !ok {
return stringToSpec(ja3String, mappedSignatureAlgorithms, mappedDelegatedCredentialsAlgorithms, mappedTlsVersions, mappedKeyShares, nil)
}
return stringToSpec(ja3String, mappedSignatureAlgorithms, mappedDelegatedCredentialsAlgorithms, mappedTlsVersions, mappedKeyShares, &compressionAlgo)
}, nil
}
func stringToSpec(ja3 string, signatureAlgorithms []tls.SignatureScheme, delegatedCredentialsAlgorithms []tls.SignatureScheme, tlsVersions []uint16, keyShares []tls.KeyShare, certCompression *tls.CertCompressionAlgo) (tls.ClientHelloSpec, error) {
extMap := getExtensionBaseMap()
ja3StringParts := strings.Split(ja3, ",")
ciphers := strings.Split(ja3StringParts[1], "-")
extensions := strings.Split(ja3StringParts[2], "-")
curves := strings.Split(ja3StringParts[3], "-")
if len(curves) == 1 && curves[0] == "" {
curves = []string{}
}
pointFormats := strings.Split(ja3StringParts[4], "-")
if len(pointFormats) == 1 && pointFormats[0] == "" {
pointFormats = []string{}
}
var targetCurves []tls.CurveID
for _, c := range curves {
cid, err := strconv.ParseUint(c, 10, 16)
if err != nil {
return tls.ClientHelloSpec{}, err
}
targetCurves = append(targetCurves, tls.CurveID(cid))
}
extMap[tls.ExtensionSupportedCurves] = &tls.SupportedCurvesExtension{Curves: targetCurves}
// parse point formats
var targetPointFormats []byte
for _, p := range pointFormats {
pid, err := strconv.ParseUint(p, 10, 8)
if err != nil {
return tls.ClientHelloSpec{}, err
}
targetPointFormats = append(targetPointFormats, byte(pid))
}
if certCompression == nil && strings.Contains(ja3StringParts[2], fmt.Sprintf("%d", tls.ExtensionCompressCertificate)) {
fmt.Println("attention our ja3 defines ExtensionCompressCertificate but you did not specify certCompression")
}
if certCompression != nil {
extMap[tls.ExtensionCompressCertificate] = &tls.UtlsCompressCertExtension{Algorithms: []tls.CertCompressionAlgo{*certCompression}}
}
extMap[tls.ExtensionKeyShare] = &tls.KeyShareExtension{KeyShares: keyShares}
extMap[tls.ExtensionSupportedPoints] = &tls.SupportedPointsExtension{SupportedPoints: targetPointFormats}
extMap[tls.ExtensionSupportedVersions] = &tls.SupportedVersionsExtension{Versions: tlsVersions}
extMap[tls.ExtensionSignatureAlgorithms] = &tls.SignatureAlgorithmsExtension{
SupportedSignatureAlgorithms: signatureAlgorithms,
}
extMap[tls.ExtensionDelegatedCredentials] = &tls.DelegatedCredentialsExtension{
AlgorithmsSignature: delegatedCredentialsAlgorithms,
}
var exts []tls.TLSExtension
for _, e := range extensions {
eId, err := strconv.ParseUint(e, 10, 16)
if err != nil {
return tls.ClientHelloSpec{}, err
}
te, ok := extMap[uint16(eId)]
if !ok {
return tls.ClientHelloSpec{}, fmt.Errorf("unknown extension with id %s provided", e)
}
exts = append(exts, te)
}
var suites []uint16
for _, c := range ciphers {
cid, err := strconv.ParseUint(c, 10, 16)
if err != nil {
return tls.ClientHelloSpec{}, err
}
suites = append(suites, uint16(cid))
}
return tls.ClientHelloSpec{
CipherSuites: suites,
CompressionMethods: []byte{0},
Extensions: exts,
GetSessionID: sha256.Sum256,
}, nil
}
func getExtensionBaseMap() map[uint16]tls.TLSExtension {
return map[uint16]tls.TLSExtension{
tls.GREASE_PLACEHOLDER: &tls.UtlsGREASEExtension{},
tls.ExtensionServerName: &tls.SNIExtension{},
tls.ExtensionStatusRequest: &tls.StatusRequestExtension{},
// These are applied later
// tls.ExtensionSupportedCurves: &tls.SupportedCurvesExtension{...}
// tls.ExtensionSupportedPoints: &tls.SupportedPointsExtension{...}
// tls.ExtensionSignatureAlgorithms: &tls.SignatureAlgorithmsExtension{...}
// tls.ExtensionCompressCertificate: &tls.UtlsCompressCertExtension{...},
// tls.ExtensionSupportedVersions: &tls.SupportedVersionsExtension{...}
// tls.ExtensionKeyShare: &tls.KeyShareExtension{...},
// tls.ExtensionDelegatedCredentials: &tls.DelegatedCredentialsExtension{},
tls.ExtensionALPN: &tls.ALPNExtension{
AlpnProtocols: []string{"h2", "http/1.1"},
},
tls.ExtensionSCT: &tls.SCTExtension{},
tls.ExtensionPadding: &tls.UtlsPaddingExtension{GetPaddingLen: tls.BoringPaddingStyle},
tls.ExtensionExtendedMasterSecret: &tls.UtlsExtendedMasterSecretExtension{},
tls.ExtensionRecordSizeLimit: &tls.FakeRecordSizeLimitExtension{},
tls.ExtensionSessionTicket: &tls.SessionTicketExtension{},
tls.ExtensionPreSharedKey: &tls.PreSharedKeyExtension{},
tls.ExtensionEarlyData: &tls.GenericExtension{Id: tls.ExtensionEarlyData},
tls.ExtensionCookie: &tls.CookieExtension{},
tls.ExtensionPSKModes: &tls.PSKKeyExchangeModesExtension{
Modes: []uint8{
tls.PskModeDHE,
}},
tls.ExtensionNextProtoNeg: &tls.NPNExtension{},
tls.ExtensionALPS: &tls.ApplicationSettingsExtension{},
tls.ExtensionRenegotiationInfo: &tls.RenegotiationInfoExtension{
Renegotiation: tls.RenegotiateOnceAsClient,
},
}
}