-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathja3.go
More file actions
142 lines (125 loc) · 4.12 KB
/
ja3.go
File metadata and controls
142 lines (125 loc) · 4.12 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
package caddy_ja3
import (
"bytes"
"crypto/md5"
"encoding/hex"
"sort"
"strconv"
"github.com/dreadl0ck/tlsx"
)
var (
sepValueByte = byte(45)
sepFieldByte = byte(44)
// Ciphers, extensions and elliptic curves, should be filtered so GREASE values are not added to the ja3 digest
// GREASE RFC: https://tools.ietf.org/html/draft-davidben-tls-grease-01
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,
}
)
// BareToDigestHex converts a bare []byte to a hex string.
func BareToDigestHex(bare []byte) string {
sum := md5.Sum(bare)
return hex.EncodeToString(sum[:])
}
func BareJa3(hello *tlsx.ClientHelloBasic, shouldSort bool) []byte {
var (
maxPossibleBufferLength = 5 + 1 + // Version = uint16 => maximum = 65536 = 5chars + 1 field sep
(5+1)*len(hello.CipherSuites) + // CipherSuite = uint16 => maximum = 65536 = 5chars
(5+1)*len(hello.AllExtensions) + // uint16 = 2B => maximum = 65536 = 5chars
(5+1)*len(hello.SupportedGroups) + // uint16 = 2B => maximum = 65536 = 5chars
(3+1)*len(hello.SupportedPoints) // uint8 = 1B => maximum = 256 = 3chars
buffer = make([]byte, 0, maxPossibleBufferLength)
)
buffer = strconv.AppendInt(buffer, int64(hello.HandshakeVersion), 10)
buffer = append(buffer, sepFieldByte)
////////// Cipher Suites //////////
// Collect cipher suites
lastElem := len(hello.CipherSuites) - 1
if len(hello.CipherSuites) > 1 {
for _, e := range hello.CipherSuites[:lastElem] {
// Filter GREASE values
if !greaseValues[uint16(e)] {
buffer = strconv.AppendInt(buffer, int64(e), 10)
buffer = append(buffer, sepValueByte)
}
}
}
// Append last element if cipher suites are not empty
if lastElem != -1 {
// Filter GREASE values
if !greaseValues[uint16(hello.CipherSuites[lastElem])] {
buffer = strconv.AppendInt(buffer, int64(hello.CipherSuites[lastElem]), 10)
}
}
buffer = bytes.TrimSuffix(buffer, []byte{sepValueByte})
buffer = append(buffer, sepFieldByte)
////////// Extensions //////////
// Sort extensions
if shouldSort {
sort.Slice(hello.AllExtensions, func(i, j int) bool {
return hello.AllExtensions[i] < hello.AllExtensions[j]
})
}
// Collect extensions
lastElem = len(hello.AllExtensions) - 1
if len(hello.AllExtensions) > 1 {
for _, e := range hello.AllExtensions[:lastElem] {
// filter GREASE values
if !greaseValues[e] {
buffer = strconv.AppendInt(buffer, int64(e), 10)
buffer = append(buffer, sepValueByte)
}
}
}
// Append last element if extensions are not empty
if lastElem != -1 {
// Filter GREASE values
if !greaseValues[hello.AllExtensions[lastElem]] {
buffer = strconv.AppendInt(buffer, int64(hello.AllExtensions[lastElem]), 10)
}
}
buffer = bytes.TrimSuffix(buffer, []byte{sepValueByte})
buffer = append(buffer, sepFieldByte)
////////// Supported Groups //////////
// Collect supported groups
lastElem = len(hello.SupportedGroups) - 1
if len(hello.SupportedGroups) > 1 {
for _, e := range hello.SupportedGroups[:lastElem] {
// filter GREASE values
if !greaseValues[uint16(e)] {
buffer = strconv.AppendInt(buffer, int64(e), 10)
buffer = append(buffer, sepValueByte)
}
}
}
// Append last element if supported groups are not empty
if lastElem != -1 {
// Filter GREASE values
if !greaseValues[uint16(hello.SupportedGroups[lastElem])] {
buffer = strconv.AppendInt(buffer, int64(hello.SupportedGroups[lastElem]), 10)
}
}
buffer = bytes.TrimSuffix(buffer, []byte{sepValueByte})
buffer = append(buffer, sepFieldByte)
////////// Supported Points //////////
// Collect supported points
lastElem = len(hello.SupportedPoints) - 1
if len(hello.SupportedPoints) > 1 {
for _, e := range hello.SupportedPoints[:lastElem] {
buffer = strconv.AppendInt(buffer, int64(e), 10)
buffer = append(buffer, sepValueByte)
}
}
// Append last element if supported points are not empty
if lastElem != -1 {
buffer = strconv.AppendInt(buffer, int64(hello.SupportedPoints[lastElem]), 10)
}
return buffer
}