소스 검색

More correct calculation of dc for obfuscated2 frame

tags/v2.0.0-rc1
9seconds 5 년 전
부모
커밋
26070d5b3e
2개의 변경된 파일28개의 추가작업 그리고 6개의 파일을 삭제
  1. 1
    6
      mtglib/internal/obfuscated2/handshake_frame.go
  2. 27
    0
      mtglib/internal/obfuscated2/handshake_frame_internal_test.go

+ 1
- 6
mtglib/internal/obfuscated2/handshake_frame.go 파일 보기

1
 package obfuscated2
1
 package obfuscated2
2
 
2
 
3
-import "encoding/binary"
4
-
5
 const (
3
 const (
6
 	DefaultDC = 2
4
 	DefaultDC = 2
7
 
5
 
10
 	handshakeFrameLenKey            = 32
8
 	handshakeFrameLenKey            = 32
11
 	handshakeFrameLenIV             = 16
9
 	handshakeFrameLenIV             = 16
12
 	handshakeFrameLenConnectionType = 4
10
 	handshakeFrameLenConnectionType = 4
13
-	handshakeFrameLenDC             = 2
14
 
11
 
15
 	handshakeFrameOffsetStart          = 8
12
 	handshakeFrameOffsetStart          = 8
16
 	handshakeFrameOffsetKey            = handshakeFrameOffsetStart
13
 	handshakeFrameOffsetKey            = handshakeFrameOffsetStart
17
 	handshakeFrameOffsetIV             = handshakeFrameOffsetKey + handshakeFrameLenKey
14
 	handshakeFrameOffsetIV             = handshakeFrameOffsetKey + handshakeFrameLenKey
18
 	handshakeFrameOffsetConnectionType = handshakeFrameOffsetIV + handshakeFrameLenIV
15
 	handshakeFrameOffsetConnectionType = handshakeFrameOffsetIV + handshakeFrameLenIV
19
 	handshakeFrameOffsetDC             = handshakeFrameOffsetConnectionType + handshakeFrameLenConnectionType
16
 	handshakeFrameOffsetDC             = handshakeFrameOffsetConnectionType + handshakeFrameLenConnectionType
20
-	handshakeFrameOffsetEnd            = handshakeFrameOffsetDC + handshakeFrameLenDC
21
 )
17
 )
22
 
18
 
23
 // Connection-Type: Secure. We support only fake tls.
19
 // Connection-Type: Secure. We support only fake tls.
38
 }
34
 }
39
 
35
 
40
 func (h *handshakeFrame) dc() int {
36
 func (h *handshakeFrame) dc() int {
41
-	data := h.data[handshakeFrameOffsetDC:handshakeFrameOffsetEnd]
42
-	idx := int16(binary.LittleEndian.Uint16(data))
37
+	idx := int16(h.data[handshakeFrameOffsetDC]) | int16(h.data[handshakeFrameOffsetDC+1])<<8 // nolint: gomnd, lll // little endian for int16 is here
43
 
38
 
44
 	switch {
39
 	switch {
45
 	case idx > 0:
40
 	case idx > 0:

+ 27
- 0
mtglib/internal/obfuscated2/handshake_frame_internal_test.go 파일 보기

1
 package obfuscated2
1
 package obfuscated2
2
 
2
 
3
 import (
3
 import (
4
+	"crypto/rand"
4
 	"encoding/base64"
5
 	"encoding/base64"
6
+	"strconv"
5
 	"testing"
7
 	"testing"
6
 
8
 
9
+	"github.com/stretchr/testify/assert"
7
 	"github.com/stretchr/testify/suite"
10
 	"github.com/stretchr/testify/suite"
8
 )
11
 )
9
 
12
 
40
 	suite.EqualValues(2094, inverted.dc())
43
 	suite.EqualValues(2094, inverted.dc())
41
 }
44
 }
42
 
45
 
46
+func (suite *HandshakeFrameTestSuite) TestDC() {
47
+	testData := map[int16]int{
48
+		1:  1,
49
+		-1: 1,
50
+		0:  DefaultDC,
51
+	}
52
+
53
+	for k, v := range testData {
54
+		incoming := k
55
+		expected := v
56
+
57
+		suite.T().Run(strconv.Itoa(int(incoming)), func(t *testing.T) {
58
+			frame := handshakeFrame{}
59
+
60
+			rand.Read(frame.data[:]) // nolint: errcheck
61
+
62
+			frame.data[handshakeFrameOffsetDC] = byte(incoming)
63
+			frame.data[handshakeFrameOffsetDC+1] = byte(incoming >> 8)
64
+
65
+			assert.Equal(t, expected, frame.dc())
66
+		})
67
+	}
68
+}
69
+
43
 func TestHandshakeFrame(t *testing.T) {
70
 func TestHandshakeFrame(t *testing.T) {
44
 	t.Parallel()
71
 	t.Parallel()
45
 	suite.Run(t, &HandshakeFrameTestSuite{})
72
 	suite.Run(t, &HandshakeFrameTestSuite{})

Loading…
취소
저장