-
Notifications
You must be signed in to change notification settings - Fork 61
/
transport.go
194 lines (174 loc) · 5.25 KB
/
transport.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
package ja3transport
import (
"crypto/sha256"
"fmt"
"net"
"net/http"
"net/url"
"strconv"
"strings"
tls "github.com/refraction-networking/utls"
)
// greasePlaceholder is a random value (well, kindof '0x?a?a) specified in a
// random RFC.
const greasePlaceholder = 0x0a0a
// ErrExtensionNotExist is returned when an extension is not supported by the library
type ErrExtensionNotExist string
// Error is the error value which contains the extension that does not exist
func (e ErrExtensionNotExist) Error() string {
return fmt.Sprintf("Extension does not exist: %s\n", string(e))
}
// extMap maps extension values to the TLSExtension object associated with the
// number. Some values are not put in here because they must be applied in a
// special way. For example, "10" is the SupportedCurves extension which is also
// used to calculate the JA3 signature. These JA3-dependent values are applied
// after the instantiation of the map.
var extMap = map[string]tls.TLSExtension{
"0": &tls.SNIExtension{},
"5": &tls.StatusRequestExtension{},
// These are applied later
// "10": &tls.SupportedCurvesExtension{...}
// "11": &tls.SupportedPointsExtension{...}
"13": &tls.SignatureAlgorithmsExtension{
SupportedSignatureAlgorithms: []tls.SignatureScheme{
tls.ECDSAWithP256AndSHA256,
tls.PSSWithSHA256,
tls.PKCS1WithSHA256,
tls.ECDSAWithP384AndSHA384,
tls.PSSWithSHA384,
tls.PKCS1WithSHA384,
tls.PSSWithSHA512,
tls.PKCS1WithSHA512,
tls.PKCS1WithSHA1,
},
},
"16": &tls.ALPNExtension{
AlpnProtocols: []string{"h2", "http/1.1"},
},
"18": &tls.SCTExtension{},
"21": &tls.UtlsPaddingExtension{GetPaddingLen: tls.BoringPaddingStyle},
"23": &tls.UtlsExtendedMasterSecretExtension{},
"27": &tls.FakeCertCompressionAlgsExtension{},
"28": &tls.FakeRecordSizeLimitExtension{},
"35": &tls.SessionTicketExtension{},
"43": &tls.SupportedVersionsExtension{Versions: []uint16{
tls.GREASE_PLACEHOLDER,
tls.VersionTLS13,
tls.VersionTLS12,
tls.VersionTLS11,
tls.VersionTLS10}},
"44": &tls.CookieExtension{},
"45": &tls.PSKKeyExchangeModesExtension{
Modes: []uint8{
tls.PskModeDHE,
}},
"51": &tls.KeyShareExtension{KeyShares: []tls.KeyShare{}},
"13172": &tls.NPNExtension{},
"65281": &tls.RenegotiationInfoExtension{
Renegotiation: tls.RenegotiateOnceAsClient,
},
}
// NewTransport creates an http.Transport which mocks the given JA3 signature when HTTPS is used
func NewTransport(ja3 string) (*http.Transport, error) {
return NewTransportWithConfig(ja3, &tls.Config{})
}
// NewTransportWithConfig creates an http.Transport object given a utls.Config
func NewTransportWithConfig(ja3 string, config *tls.Config) (*http.Transport, error) {
spec, err := stringToSpec(ja3)
if err != nil {
return nil, err
}
dialtls := func(network, addr string) (net.Conn, error) {
dialConn, err := net.Dial(network, addr)
if err != nil {
return nil, err
}
config.ServerName = strings.Split(addr, ":")[0]
uTLSConn := tls.UClient(dialConn, config, tls.HelloCustom)
if err := uTLSConn.ApplyPreset(spec); err != nil {
return nil, err
}
if err := uTLSConn.Handshake(); err != nil {
return nil, err
}
return uTLSConn, nil
}
return &http.Transport{DialTLS: dialtls}, nil
}
// stringToSpec creates a ClientHelloSpec based on a JA3 string
func stringToSpec(ja3 string) (*tls.ClientHelloSpec, error) {
tokens := strings.Split(ja3, ",")
version := tokens[0]
ciphers := strings.Split(tokens[1], "-")
extensions := strings.Split(tokens[2], "-")
curves := strings.Split(tokens[3], "-")
if len(curves) == 1 && curves[0] == "" {
curves = []string{}
}
pointFormats := strings.Split(tokens[4], "-")
if len(pointFormats) == 1 && pointFormats[0] == "" {
pointFormats = []string{}
}
// parse curves
var targetCurves []tls.CurveID
for _, c := range curves {
cid, err := strconv.ParseUint(c, 10, 16)
if err != nil {
return nil, err
}
targetCurves = append(targetCurves, tls.CurveID(cid))
}
extMap["10"] = &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 nil, err
}
targetPointFormats = append(targetPointFormats, byte(pid))
}
extMap["11"] = &tls.SupportedPointsExtension{SupportedPoints: targetPointFormats}
// build extenions list
var exts []tls.TLSExtension
for _, e := range extensions {
te, ok := extMap[e]
if !ok {
return nil, ErrExtensionNotExist(e)
}
exts = append(exts, te)
}
// build SSLVersion
vid64, err := strconv.ParseUint(version, 10, 16)
if err != nil {
return nil, err
}
vid := uint16(vid64)
// build CipherSuites
var suites []uint16
for _, c := range ciphers {
cid, err := strconv.ParseUint(c, 10, 16)
if err != nil {
return nil, err
}
suites = append(suites, uint16(cid))
}
return &tls.ClientHelloSpec{
TLSVersMin: vid,
TLSVersMax: vid,
CipherSuites: suites,
CompressionMethods: []byte{0},
Extensions: exts,
GetSessionID: sha256.Sum256,
}, nil
}
func urlToHost(target *url.URL) *url.URL {
if !strings.Contains(target.Host, ":") {
if target.Scheme == "http" {
target.Host = target.Host + ":80"
} else if target.Scheme == "https" {
target.Host = target.Host + ":443"
}
}
return target
}