Parcourir la source

Move clienhello to faketls

tags/v2.0.0-rc1
9seconds il y a 5 ans
Parent
révision
d3551aa9cc

mtglib/internal/faketls/clienthello/clienthello.go → mtglib/internal/faketls/clienthello.go Voir le fichier

@@ -1,4 +1,4 @@
1
-package clienthello
1
+package faketls
2 2
 
3 3
 import (
4 4
 	"crypto/hmac"
@@ -16,10 +16,10 @@ type ClientHello struct {
16 16
 	SessionID []byte
17 17
 }
18 18
 
19
-func ParseHandshake(secret, handshake []byte) (ClientHello, error) {
19
+func ParseClientHello(secret, handshake []byte) (ClientHello, error) {
20 20
 	hello := ClientHello{}
21 21
 
22
-	if len(handshake) < MinLen {
22
+	if len(handshake) < ClientHelloMinLen {
23 23
 		return hello, fmt.Errorf("lengh of handshake is too small: %d", len(handshake))
24 24
 	}
25 25
 
@@ -27,9 +27,9 @@ func ParseHandshake(secret, handshake []byte) (ClientHello, error) {
27 27
 		return hello, fmt.Errorf("unknown handshake type %#x", handshake[0])
28 28
 	}
29 29
 
30
-	copy(hello.Digest[:], handshake[RandomOffset:])
30
+	copy(hello.Digest[:], handshake[ClientHelloRandomOffset:])
31 31
 
32
-	for i := RandomOffset; i < RandomOffset+RandomLen; i++ {
32
+	for i := ClientHelloRandomOffset; i < ClientHelloRandomOffset+RandomLen; i++ {
33 33
 		handshake[i] = 0
34 34
 	}
35 35
 
@@ -60,8 +60,8 @@ func ParseHandshake(secret, handshake []byte) (ClientHello, error) {
60 60
 	timestamp := int64(binary.LittleEndian.Uint32(computedDigest[RandomLen-4:]))
61 61
 	hello.Time = time.Unix(timestamp, 0)
62 62
 
63
-	hello.SessionID = make([]byte, handshake[SessionIDOffset])
64
-	copy(hello.SessionID, handshake[SessionIDOffset+1:])
63
+	hello.SessionID = make([]byte, handshake[ClientHelloSessionIDOffset])
64
+	copy(hello.SessionID, handshake[ClientHelloSessionIDOffset+1:])
65 65
 
66 66
 	return hello, nil
67 67
 }

+ 0
- 17
mtglib/internal/faketls/clienthello/init.go Voir le fichier

@@ -1,17 +0,0 @@
1
-package clienthello
2
-
3
-import "errors"
4
-
5
-const (
6
-	RandomLen       = 32
7
-	RandomOffset    = 6
8
-	SessionIDOffset = RandomOffset + RandomLen
9
-	MinLen          = SessionIDOffset + 1
10
-
11
-	HandshakeTypeClient = 0x01
12
-)
13
-
14
-var (
15
-	ErrBadDigest        = errors.New("bad digest")
16
-	ErrAntiReplayAttack = errors.New("antireplay attack was detected")
17
-)

+ 18
- 0
mtglib/internal/faketls/init.go Voir le fichier

@@ -0,0 +1,18 @@
1
+package faketls
2
+
3
+import "errors"
4
+
5
+const (
6
+	RandomLen = 32
7
+
8
+	ClientHelloRandomOffset    = 6
9
+	ClientHelloSessionIDOffset = ClientHelloRandomOffset + RandomLen
10
+	ClientHelloMinLen      = ClientHelloSessionIDOffset + 1
11
+
12
+	HandshakeTypeClient = 0x01
13
+)
14
+
15
+var (
16
+	ErrBadDigest        = errors.New("bad digest")
17
+	ErrAntiReplayAttack = errors.New("antireplay attack was detected")
18
+)

+ 17
- 6
mtglib/proxy.go Voir le fichier

@@ -8,7 +8,7 @@ import (
8 8
 	"sync"
9 9
 	"time"
10 10
 
11
-	"github.com/9seconds/mtg/v2/mtglib/internal/faketls/clienthello"
11
+	"github.com/9seconds/mtg/v2/mtglib/internal/faketls"
12 12
 	"github.com/9seconds/mtg/v2/mtglib/internal/faketls/record"
13 13
 	"github.com/9seconds/mtg/v2/mtglib/internal/obfuscated2"
14 14
 	"github.com/9seconds/mtg/v2/mtglib/internal/relay"
@@ -58,7 +58,7 @@ func (p *Proxy) ServeConn(conn net.Conn) {
58 58
 		ctx.logger.Info("Stream has been finished")
59 59
 	}()
60 60
 
61
-	if err := p.doFakeTLSHandshake(ctx); err != nil {
61
+	if err := p.doFakeTLSHandshake(ctx, ctx.clientConn); err != nil {
62 62
 		p.logger.InfoError("faketls handshake is failed", err)
63 63
 
64 64
 		return
@@ -121,17 +121,28 @@ func (p *Proxy) Shutdown() {
121 121
 	p.workerPool.Release()
122 122
 }
123 123
 
124
-func (p *Proxy) doFakeTLSHandshake(ctx *streamContext) error {
124
+func (p *Proxy) doFakeTLSHandshake(ctx *streamContext, conn net.Conn) error {
125 125
 	clientHelloRecord := record.AcquireRecord()
126 126
 	defer record.ReleaseRecord(clientHelloRecord)
127 127
 
128
-	if err := clientHelloRecord.Read(ctx.clientConn); err != nil {
128
+	if err := clientHelloRecord.Read(conn); err != nil {
129 129
 		return fmt.Errorf("cannot read client hello: %w", err)
130 130
 	}
131 131
 
132
-	hello, _ := clienthello.ParseHandshake(p.secret.Key[:],
132
+	hello, err := faketls.ParseClientHello(p.secret.Key[:],
133 133
 		clientHelloRecord.Payload.Bytes())
134
-	fmt.Println(hello)
134
+	if err != nil {
135
+		return fmt.Errorf("cannot parse client hello: %w", err)
136
+	}
137
+
138
+	if err := p.timeAttackDetector.Valid(hello.Time); err != nil {
139
+		return fmt.Errorf("invalid time: %w", err)
140
+	}
141
+	if p.antiReplayCache.SeenBefore(hello.SessionID) {
142
+		p.logger.Warning("anti replay attack was detected")
143
+
144
+		return fmt.Errorf("anti replay attack from %s", ctx.ClientIP().String())
145
+	}
135 146
 
136 147
 	return fmt.Errorf("SUCCESS")
137 148
 }

Chargement…
Annuler
Enregistrer