Cloak/internal/client/chrome.go

104 lines
4.2 KiB
Go
Raw Normal View History

2019-08-20 21:43:04 +00:00
// Fingerprint of Chrome 76
2018-10-07 17:09:45 +00:00
2019-08-02 15:02:25 +00:00
package client
2018-10-07 17:09:45 +00:00
import (
2019-08-02 19:06:41 +00:00
"crypto/rand"
2019-08-02 00:01:19 +00:00
"encoding/binary"
2018-10-07 17:09:45 +00:00
"encoding/hex"
)
2019-08-02 15:02:25 +00:00
type Chrome struct{}
2018-10-07 17:09:45 +00:00
2019-08-02 00:01:19 +00:00
func makeGREASE() []byte {
2018-10-07 17:09:45 +00:00
// see https://tools.ietf.org/html/draft-davidben-tls-grease-01
2019-08-02 15:02:25 +00:00
// This is exclusive to Chrome.
2019-08-02 19:06:41 +00:00
var one [1]byte
rand.Read(one[:])
sixteenth := one[0] % 16
2019-08-02 00:01:19 +00:00
monoGREASE := byte(sixteenth*16 + 0xA)
doubleGREASE := []byte{monoGREASE, monoGREASE}
return doubleGREASE
}
func (c *Chrome) composeExtensions(sni []byte, keyShare []byte) []byte {
2018-10-07 17:09:45 +00:00
makeSupportedGroups := func() []byte {
suppGroupListLen := []byte{0x00, 0x08}
ret := make([]byte, 2+8)
copy(ret[0:2], suppGroupListLen)
copy(ret[2:4], makeGREASE())
copy(ret[4:], []byte{0x00, 0x1d, 0x00, 0x17, 0x00, 0x18})
return ret
}
2019-08-02 00:01:19 +00:00
makeKeyShare := func(hidden []byte) []byte {
ret := make([]byte, 43)
ret[0], ret[1] = 0x00, 0x29 // length 41
copy(ret[2:4], makeGREASE())
ret[4], ret[5] = 0x00, 0x01 // length 1
ret[6] = 0x00
ret[7], ret[8] = 0x00, 0x1d // group x25519
ret[9], ret[10] = 0x00, 0x20 // length 32
copy(ret[11:43], hidden)
return ret
}
// extension length is always 401, and server name length is variable
var ext [17][]byte
ext[0] = addExtRec(makeGREASE(), nil) // First GREASE
ext[1] = addExtRec([]byte{0x00, 0x00}, sni) // server name indication
ext[2] = addExtRec([]byte{0x00, 0x17}, nil) // extended_master_secret
ext[3] = addExtRec([]byte{0xff, 0x01}, []byte{0x00}) // renegotiation_info
ext[4] = addExtRec([]byte{0x00, 0x0a}, makeSupportedGroups()) // supported groups
ext[5] = addExtRec([]byte{0x00, 0x0b}, []byte{0x01, 0x00}) // ec point formats
ext[6] = addExtRec([]byte{0x00, 0x23}, nil) // Session tickets
2018-10-07 17:09:45 +00:00
APLN, _ := hex.DecodeString("000c02683208687474702f312e31")
2019-08-02 00:01:19 +00:00
ext[7] = addExtRec([]byte{0x00, 0x10}, APLN) // app layer proto negotiation
ext[8] = addExtRec([]byte{0x00, 0x05}, []byte{0x01, 0x00, 0x00, 0x00, 0x00}) // status request
sigAlgo, _ := hex.DecodeString("0012040308040401050308050501080606010201")
ext[9] = addExtRec([]byte{0x00, 0x0d}, sigAlgo) // Signature Algorithms
ext[10] = addExtRec([]byte{0x00, 0x12}, nil) // signed cert timestamp
ext[11] = addExtRec([]byte{0x00, 0x33}, makeKeyShare(keyShare)) // key share
ext[12] = addExtRec([]byte{0x00, 0x2d}, []byte{0x01, 0x01}) // psk key exchange modes
suppVersions, _ := hex.DecodeString("0a9A9A0304030303020301") // 9A9A needs to be a GREASE
copy(suppVersions[1:3], makeGREASE())
ext[13] = addExtRec([]byte{0x00, 0x2b}, suppVersions) // supported versions
ext[14] = addExtRec([]byte{0x00, 0x1b}, []byte{0x02, 0x00, 0x02})
ext[15] = addExtRec(makeGREASE(), []byte{0x00}) // Last GREASE
// len(ext[1]) + 172 + len(ext[16]) = 401
// len(ext[16]) = 229 - len(ext[1])
// 2+2+len(padding) = 229 - len(ext[1])
// len(padding) = 225 - len(ext[1])
2019-08-02 17:03:08 +00:00
ext[16] = addExtRec([]byte{0x00, 0x15}, make([]byte, 225-len(ext[1]))) // padding
2018-10-07 17:09:45 +00:00
var ret []byte
2019-08-02 00:01:19 +00:00
for _, e := range ext {
ret = append(ret, e...)
2018-10-07 17:09:45 +00:00
}
return ret
}
func (c *Chrome) composeClientHello(hd chHiddenData) (ch []byte) {
2018-10-07 17:09:45 +00:00
var clientHello [12][]byte
2019-08-02 00:01:19 +00:00
clientHello[0] = []byte{0x01} // handshake type
clientHello[1] = []byte{0x00, 0x01, 0xfc} // length 508
clientHello[2] = []byte{0x03, 0x03} // client version
clientHello[3] = hd.chRandom // random
2019-08-02 00:01:19 +00:00
clientHello[4] = []byte{0x20} // session id length 32
clientHello[5] = hd.chSessionId // session id
2019-08-02 00:01:19 +00:00
clientHello[6] = []byte{0x00, 0x22} // cipher suites length 34
cipherSuites, _ := hex.DecodeString("130113021303c02bc02fc02cc030cca9cca8c013c014009c009d002f0035000a")
clientHello[7] = append(makeGREASE(), cipherSuites...) // cipher suites
clientHello[8] = []byte{0x01} // compression methods length 1
clientHello[9] = []byte{0x00} // compression methods
clientHello[11] = c.composeExtensions(hd.chExtSNI, hd.chX25519KeyShare)
2019-08-02 00:01:19 +00:00
clientHello[10] = []byte{0x00, 0x00} // extensions length 401
binary.BigEndian.PutUint16(clientHello[10], uint16(len(clientHello[11])))
2018-10-07 17:09:45 +00:00
var ret []byte
2019-08-02 00:01:19 +00:00
for _, c := range clientHello {
ret = append(ret, c...)
2018-10-07 17:09:45 +00:00
}
return ret
2018-10-07 17:09:45 +00:00
}