Преглед изворни кода

Move clienhello to faketls

tags/v2.0.0-rc1
9seconds пре 5 година
родитељ
комит
d3551aa9cc

mtglib/internal/faketls/clienthello/clienthello.go → mtglib/internal/faketls/clienthello.go Прегледај датотеку

1
-package clienthello
1
+package faketls
2
 
2
 
3
 import (
3
 import (
4
 	"crypto/hmac"
4
 	"crypto/hmac"
16
 	SessionID []byte
16
 	SessionID []byte
17
 }
17
 }
18
 
18
 
19
-func ParseHandshake(secret, handshake []byte) (ClientHello, error) {
19
+func ParseClientHello(secret, handshake []byte) (ClientHello, error) {
20
 	hello := ClientHello{}
20
 	hello := ClientHello{}
21
 
21
 
22
-	if len(handshake) < MinLen {
22
+	if len(handshake) < ClientHelloMinLen {
23
 		return hello, fmt.Errorf("lengh of handshake is too small: %d", len(handshake))
23
 		return hello, fmt.Errorf("lengh of handshake is too small: %d", len(handshake))
24
 	}
24
 	}
25
 
25
 
27
 		return hello, fmt.Errorf("unknown handshake type %#x", handshake[0])
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
 		handshake[i] = 0
33
 		handshake[i] = 0
34
 	}
34
 	}
35
 
35
 
60
 	timestamp := int64(binary.LittleEndian.Uint32(computedDigest[RandomLen-4:]))
60
 	timestamp := int64(binary.LittleEndian.Uint32(computedDigest[RandomLen-4:]))
61
 	hello.Time = time.Unix(timestamp, 0)
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
 	return hello, nil
66
 	return hello, nil
67
 }
67
 }

+ 0
- 17
mtglib/internal/faketls/clienthello/init.go Прегледај датотеку

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 Прегледај датотеку

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 Прегледај датотеку

8
 	"sync"
8
 	"sync"
9
 	"time"
9
 	"time"
10
 
10
 
11
-	"github.com/9seconds/mtg/v2/mtglib/internal/faketls/clienthello"
11
+	"github.com/9seconds/mtg/v2/mtglib/internal/faketls"
12
 	"github.com/9seconds/mtg/v2/mtglib/internal/faketls/record"
12
 	"github.com/9seconds/mtg/v2/mtglib/internal/faketls/record"
13
 	"github.com/9seconds/mtg/v2/mtglib/internal/obfuscated2"
13
 	"github.com/9seconds/mtg/v2/mtglib/internal/obfuscated2"
14
 	"github.com/9seconds/mtg/v2/mtglib/internal/relay"
14
 	"github.com/9seconds/mtg/v2/mtglib/internal/relay"
58
 		ctx.logger.Info("Stream has been finished")
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
 		p.logger.InfoError("faketls handshake is failed", err)
62
 		p.logger.InfoError("faketls handshake is failed", err)
63
 
63
 
64
 		return
64
 		return
121
 	p.workerPool.Release()
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
 	clientHelloRecord := record.AcquireRecord()
125
 	clientHelloRecord := record.AcquireRecord()
126
 	defer record.ReleaseRecord(clientHelloRecord)
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
 		return fmt.Errorf("cannot read client hello: %w", err)
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
 		clientHelloRecord.Payload.Bytes())
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
 	return fmt.Errorf("SUCCESS")
147
 	return fmt.Errorf("SUCCESS")
137
 }
148
 }

Loading…
Откажи
Сачувај