Explorar el Código

Delete old obfuscated2 package

tags/v2.1.11^2^2
9seconds hace 2 meses
padre
commit
3e75e4fa63

+ 0
- 54
mtglib/internal/obfuscated2/client_handshake.go Ver fichero

@@ -1,54 +0,0 @@
1
-package obfuscated2
2
-
3
-import (
4
-	"crypto/cipher"
5
-	"crypto/subtle"
6
-	"encoding/hex"
7
-	"fmt"
8
-	"io"
9
-)
10
-
11
-type clientHandhakeFrame struct {
12
-	handshakeFrame
13
-}
14
-
15
-func (c *clientHandhakeFrame) decryptor(secret []byte) cipher.Stream {
16
-	hasher := acquireSha256Hasher()
17
-	defer releaseSha256Hasher(hasher)
18
-
19
-	hasher.Write(c.key())
20
-	hasher.Write(secret)
21
-
22
-	return makeAesCtr(hasher.Sum(nil), c.iv())
23
-}
24
-
25
-func (c *clientHandhakeFrame) encryptor(secret []byte) cipher.Stream {
26
-	invertedHandshake := c.invert()
27
-
28
-	hasher := acquireSha256Hasher()
29
-	defer releaseSha256Hasher(hasher)
30
-
31
-	hasher.Write(invertedHandshake.key())
32
-	hasher.Write(secret)
33
-
34
-	return makeAesCtr(hasher.Sum(nil), invertedHandshake.iv())
35
-}
36
-
37
-func ClientHandshake(secret []byte, reader io.Reader) (int, cipher.Stream, cipher.Stream, error) {
38
-	handshake := clientHandhakeFrame{}
39
-
40
-	if _, err := io.ReadFull(reader, handshake.data[:]); err != nil {
41
-		return 0, nil, nil, fmt.Errorf("cannot read frame: %w", err)
42
-	}
43
-
44
-	decryptor := handshake.decryptor(secret)
45
-	encryptor := handshake.encryptor(secret)
46
-
47
-	decryptor.XORKeyStream(handshake.data[:], handshake.data[:])
48
-
49
-	if val := handshake.connectionType(); subtle.ConstantTimeCompare(handshakeConnectionType, val) != 1 {
50
-		return 0, nil, nil, fmt.Errorf("unsupported connection type: %s", hex.EncodeToString(val))
51
-	}
52
-
53
-	return handshake.dc(), encryptor, decryptor, nil
54
-}

+ 0
- 32
mtglib/internal/obfuscated2/client_handshake_fuzz_internal_test.go Ver fichero

@@ -1,32 +0,0 @@
1
-package obfuscated2
2
-
3
-import (
4
-	"bytes"
5
-	"testing"
6
-
7
-	"github.com/stretchr/testify/require"
8
-)
9
-
10
-var FuzzClientHandshakeSecret = []byte{1, 2, 3}
11
-
12
-func FuzzClientHandshake(f *testing.F) {
13
-	f.Add([]byte{1, 2, 3})
14
-
15
-	f.Fuzz(func(t *testing.T, frame []byte) {
16
-		data := bytes.NewReader(frame)
17
-
18
-		if _, _, _, err := ClientHandshake(FuzzClientHandshakeSecret, data); err != nil {
19
-			return
20
-		}
21
-
22
-		handshake := clientHandhakeFrame{}
23
-		require.Len(t, frame, handshakeFrameLen)
24
-
25
-		copy(handshake.data[:], frame)
26
-
27
-		decryptor := handshake.decryptor(FuzzClientHandshakeSecret)
28
-		decryptor.XORKeyStream(handshake.data[:], handshake.data[:])
29
-
30
-		require.Equal(t, handshakeConnectionType, handshake.connectionType())
31
-	})
32
-}

+ 0
- 89
mtglib/internal/obfuscated2/client_handshake_test.go Ver fichero

@@ -1,89 +0,0 @@
1
-package obfuscated2_test
2
-
3
-import (
4
-	"bytes"
5
-	"testing"
6
-
7
-	"github.com/9seconds/mtg/v2/internal/testlib"
8
-	"github.com/9seconds/mtg/v2/mtglib/internal/obfuscated2"
9
-	"github.com/stretchr/testify/assert"
10
-	"github.com/stretchr/testify/mock"
11
-	"github.com/stretchr/testify/suite"
12
-)
13
-
14
-type ClientHandshakeTestSuite struct {
15
-	suite.Suite
16
-	SnapshotTestSuite
17
-}
18
-
19
-func (suite *ClientHandshakeTestSuite) SetupSuite() {
20
-	suite.NoError(suite.IngestSnapshots(".", "client-handshake-snapshot-"))
21
-}
22
-
23
-func (suite *ClientHandshakeTestSuite) TestCannotRead() {
24
-	buf := bytes.NewBuffer([]byte{1, 2, 3})
25
-	_, _, _, err := obfuscated2.ClientHandshake([]byte{1, 2, 3}, buf) //nolint: dogsled
26
-
27
-	suite.Error(err)
28
-}
29
-
30
-func (suite *ClientHandshakeTestSuite) TestOk() {
31
-	for nameV, snapshotV := range suite.snapshots {
32
-		snapshot := snapshotV
33
-
34
-		suite.T().Run(nameV, func(t *testing.T) {
35
-			buf := bytes.NewBuffer(snapshot.Frame.data)
36
-
37
-			dc, encryptor, decryptor, err := obfuscated2.ClientHandshake(
38
-				snapshot.Secret.data, buf)
39
-			assert.NoError(t, err)
40
-			assert.EqualValues(t, snapshot.DC, dc)
41
-
42
-			writeData := make([]byte, len(snapshot.Encrypted.Text.data))
43
-			readData := make([]byte, len(snapshot.Decrypted.Text.data))
44
-
45
-			connMock := &testlib.EssentialsConnMock{}
46
-			connMock.On("Read", mock.Anything).
47
-				Once().
48
-				Return(len(snapshot.Decrypted.Text.data), nil).
49
-				Run(func(args mock.Arguments) {
50
-					arr, ok := args.Get(0).([]byte)
51
-
52
-					suite.True(ok)
53
-					copy(arr, snapshot.Decrypted.Cipher.data)
54
-				})
55
-			connMock.On("Write", mock.Anything).
56
-				Once().
57
-				Return(len(snapshot.Encrypted.Text.data), nil).
58
-				Run(func(args mock.Arguments) {
59
-					arr, ok := args.Get(0).([]byte)
60
-
61
-					suite.True(ok)
62
-					copy(writeData, arr)
63
-				})
64
-
65
-			conn := obfuscated2.Conn{
66
-				Conn:      connMock,
67
-				Encryptor: encryptor,
68
-				Decryptor: decryptor,
69
-			}
70
-
71
-			n, err := conn.Read(readData)
72
-			assert.Equal(t, len(readData), n)
73
-			assert.NoError(t, err)
74
-			assert.Equal(t, snapshot.Decrypted.Text.data, readData)
75
-
76
-			n, err = conn.Write(snapshot.Encrypted.Text.data)
77
-			assert.Equal(t, len(writeData), n)
78
-			assert.NoError(t, err)
79
-			assert.Equal(t, snapshot.Encrypted.Cipher.data, writeData)
80
-
81
-			connMock.AssertExpectations(t)
82
-		})
83
-	}
84
-}
85
-
86
-func TestClientHandshake(t *testing.T) {
87
-	t.Parallel()
88
-	suite.Run(t, &ClientHandshakeTestSuite{})
89
-}

+ 0
- 37
mtglib/internal/obfuscated2/conn.go Ver fichero

@@ -1,37 +0,0 @@
1
-package obfuscated2
2
-
3
-import (
4
-	"crypto/cipher"
5
-
6
-	"github.com/9seconds/mtg/v2/essentials"
7
-)
8
-
9
-type Conn struct {
10
-	essentials.Conn
11
-
12
-	Encryptor cipher.Stream
13
-	Decryptor cipher.Stream
14
-}
15
-
16
-func (c Conn) Read(p []byte) (int, error) {
17
-	n, err := c.Conn.Read(p)
18
-	if err != nil {
19
-		return n, err //nolint: wrapcheck
20
-	}
21
-
22
-	c.Decryptor.XORKeyStream(p, p[:n])
23
-
24
-	return n, nil
25
-}
26
-
27
-func (c Conn) Write(p []byte) (int, error) {
28
-	buf := acquireBytesBuffer()
29
-	defer releaseBytesBuffer(buf)
30
-
31
-	buf.Write(p)
32
-
33
-	payload := buf.Bytes()
34
-	c.Encryptor.XORKeyStream(payload, payload)
35
-
36
-	return c.Conn.Write(payload) //nolint: wrapcheck
37
-}

+ 0
- 71
mtglib/internal/obfuscated2/handshake_frame.go Ver fichero

@@ -1,71 +0,0 @@
1
-package obfuscated2
2
-
3
-const (
4
-	// DefaultDC defines a number of the default DC to use. This value used
5
-	// only if a value from obfuscated2 handshake frame is 0 (default).
6
-	DefaultDC = 2
7
-
8
-	handshakeFrameLen = 64
9
-
10
-	handshakeFrameLenKey            = 32
11
-	handshakeFrameLenIV             = 16
12
-	handshakeFrameLenConnectionType = 4
13
-
14
-	handshakeFrameOffsetStart          = 8
15
-	handshakeFrameOffsetKey            = handshakeFrameOffsetStart
16
-	handshakeFrameOffsetIV             = handshakeFrameOffsetKey + handshakeFrameLenKey
17
-	handshakeFrameOffsetConnectionType = handshakeFrameOffsetIV + handshakeFrameLenIV
18
-	handshakeFrameOffsetDC             = handshakeFrameOffsetConnectionType + handshakeFrameLenConnectionType
19
-)
20
-
21
-// Connection-Type: Secure. We support only fake tls.
22
-var handshakeConnectionType = []byte{0xdd, 0xdd, 0xdd, 0xdd}
23
-
24
-// A structure of obfuscated2 handshake frame is following:
25
-//
26
-//	[frameOffsetFirst:frameOffsetKey:frameOffsetIV:frameOffsetMagic:frameOffsetDC:frameOffsetEnd].
27
-//
28
-//	- 8 bytes of noise
29
-//	- 32 bytes of AES Key
30
-//	- 16 bytes of AES IV
31
-//	- 4 bytes of 'connection type' - this has some setting like a connection type
32
-//	- 2 bytes of 'DC'. DC is little endian int16
33
-//	- 2 bytes of noise
34
-type handshakeFrame struct {
35
-	data [handshakeFrameLen]byte
36
-}
37
-
38
-func (h *handshakeFrame) dc() int {
39
-	idx := int16(h.data[handshakeFrameOffsetDC]) | int16(h.data[handshakeFrameOffsetDC+1])<<8 //nolint: lll // little endian for int16 is here
40
-
41
-	switch {
42
-	case idx > 0:
43
-		return int(idx)
44
-	case idx < 0:
45
-		return -int(idx)
46
-	default:
47
-		return DefaultDC
48
-	}
49
-}
50
-
51
-func (h *handshakeFrame) key() []byte {
52
-	return h.data[handshakeFrameOffsetKey:handshakeFrameOffsetIV]
53
-}
54
-
55
-func (h *handshakeFrame) iv() []byte {
56
-	return h.data[handshakeFrameOffsetIV:handshakeFrameOffsetConnectionType]
57
-}
58
-
59
-func (h *handshakeFrame) connectionType() []byte {
60
-	return h.data[handshakeFrameOffsetConnectionType:handshakeFrameOffsetDC]
61
-}
62
-
63
-func (h *handshakeFrame) invert() handshakeFrame {
64
-	copyFrame := *h
65
-
66
-	for i := range handshakeFrameLenKey + handshakeFrameLenIV {
67
-		copyFrame.data[handshakeFrameOffsetKey+i] = h.data[handshakeFrameOffsetConnectionType-1-i]
68
-	}
69
-
70
-	return copyFrame
71
-}

+ 0
- 73
mtglib/internal/obfuscated2/handshake_frame_internal_test.go Ver fichero

@@ -1,73 +0,0 @@
1
-package obfuscated2
2
-
3
-import (
4
-	"crypto/rand"
5
-	"encoding/base64"
6
-	"strconv"
7
-	"testing"
8
-
9
-	"github.com/stretchr/testify/assert"
10
-	"github.com/stretchr/testify/suite"
11
-)
12
-
13
-type HandshakeFrameTestSuite struct {
14
-	suite.Suite
15
-}
16
-
17
-func (suite *HandshakeFrameTestSuite) Decode(value string) []byte {
18
-	v, err := base64.RawStdEncoding.DecodeString(value)
19
-	suite.NoError(err)
20
-
21
-	return v
22
-}
23
-
24
-func (suite *HandshakeFrameTestSuite) Encode(value []byte) string {
25
-	return base64.RawStdEncoding.EncodeToString(value)
26
-}
27
-
28
-func (suite *HandshakeFrameTestSuite) TestOk() {
29
-	hf := handshakeFrame{}
30
-	testFrame := suite.Decode(
31
-		"L9TmCzzxl9bPKODBpZeVM/qqNUxQ/axxBup1S2ymbIfUd6f7YSyzzM9EmTFv2/XzGqJGEHuj2zofmUGBLghu5g")
32
-	copy(hf.data[:], testFrame)
33
-
34
-	suite.Equal("zyjgwaWXlTP6qjVMUP2scQbqdUtspmyH1Hen+2Ess8w", suite.Encode(hf.key()))
35
-	suite.Equal("z0SZMW/b9fMaokYQe6PbOg", suite.Encode(hf.iv()))
36
-	suite.Equal("H5lBgQ", suite.Encode(hf.connectionType()))
37
-	suite.EqualValues(2094, hf.dc())
38
-
39
-	inverted := hf.invert()
40
-	suite.Equal("OtujexBGohrz9dtvMZlEz8yzLGH7p3fUh2ymbEt16gY", suite.Encode(inverted.key()))
41
-	suite.Equal("caz9UEw1qvozlZelweAozw", suite.Encode(inverted.iv()))
42
-	suite.Equal("H5lBgQ", suite.Encode(inverted.connectionType()))
43
-	suite.EqualValues(2094, inverted.dc())
44
-}
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
-
70
-func TestHandshakeFrame(t *testing.T) {
71
-	t.Parallel()
72
-	suite.Run(t, &HandshakeFrameTestSuite{})
73
-}

+ 0
- 137
mtglib/internal/obfuscated2/init_test.go Ver fichero

@@ -1,137 +0,0 @@
1
-package obfuscated2_test
2
-
3
-import (
4
-	"bytes"
5
-	"crypto/aes"
6
-	"crypto/cipher"
7
-	"encoding/base64"
8
-	"encoding/json"
9
-	"fmt"
10
-	"os"
11
-	"path/filepath"
12
-	"strings"
13
-	"testing"
14
-
15
-	"github.com/9seconds/mtg/v2/internal/testlib"
16
-	"github.com/9seconds/mtg/v2/mtglib/internal/obfuscated2"
17
-	"github.com/stretchr/testify/require"
18
-)
19
-
20
-type snapshotBytes struct {
21
-	data []byte
22
-}
23
-
24
-func (s snapshotBytes) MarshalText() ([]byte, error) {
25
-	if len(s.data) == 0 {
26
-		return nil, nil
27
-	}
28
-
29
-	return []byte(base64.RawStdEncoding.EncodeToString(s.data)), nil
30
-}
31
-
32
-func (s *snapshotBytes) UnmarshalText(data []byte) error {
33
-	val, err := base64.RawStdEncoding.DecodeString(string(data))
34
-	if err != nil {
35
-		return fmt.Errorf("cannot unmarshal %v: %w", len(val), err)
36
-	}
37
-
38
-	s.data = val
39
-
40
-	return nil
41
-}
42
-
43
-type Obfuscated2Snapshot struct {
44
-	Secret    snapshotBytes `json:"secret"`
45
-	Frame     snapshotBytes `json:"frame"`
46
-	DC        int16         `json:"dc"`
47
-	Encrypted struct {
48
-		Text   snapshotBytes `json:"text"`
49
-		Cipher snapshotBytes `json:"cipher"`
50
-	} `json:"encrypted"`
51
-	Decrypted struct {
52
-		Text   snapshotBytes `json:"text"`
53
-		Cipher snapshotBytes `json:"cipher"`
54
-	} `json:"decrypted"`
55
-}
56
-
57
-type SnapshotTestSuite struct {
58
-	snapshots map[string]*Obfuscated2Snapshot
59
-}
60
-
61
-type ServerHandshakeTestData struct {
62
-	connMock *testlib.EssentialsConnMock
63
-
64
-	proxyConn obfuscated2.Conn
65
-	encryptor cipher.Stream
66
-	decryptor cipher.Stream
67
-}
68
-
69
-func (suite *SnapshotTestSuite) IngestSnapshots(dirname, namePrefix string) error {
70
-	suite.snapshots = map[string]*Obfuscated2Snapshot{}
71
-
72
-	files, err := os.ReadDir(filepath.Join("testdata", dirname))
73
-	if err != nil {
74
-		return fmt.Errorf("cannot ingest snapshots: %w", err)
75
-	}
76
-
77
-	for _, v := range files {
78
-		if !strings.HasPrefix(v.Name(), namePrefix) {
79
-			continue
80
-		}
81
-
82
-		filename := filepath.Join("testdata", dirname, v.Name())
83
-
84
-		contents, err := os.ReadFile(filename)
85
-		if err != nil {
86
-			return fmt.Errorf("cannot read %s: %w", filename, err)
87
-		}
88
-
89
-		value := &Obfuscated2Snapshot{}
90
-
91
-		if err := json.Unmarshal(contents, value); err != nil {
92
-			return fmt.Errorf("cannot unmarshal %s: %w", filename, err)
93
-		}
94
-
95
-		suite.snapshots[v.Name()] = value
96
-	}
97
-
98
-	return nil
99
-}
100
-
101
-func NewServerHandshakeTestData(t *testing.T) ServerHandshakeTestData {
102
-	buf := &bytes.Buffer{}
103
-	connMock := &testlib.EssentialsConnMock{}
104
-
105
-	handshakeEnc, handshakeDec, err := obfuscated2.ServerHandshake(buf)
106
-	require.NoError(t, err)
107
-
108
-	serverEncrypted := buf.Bytes()
109
-	decBlock, _ := aes.NewCipher(serverEncrypted[8 : 8+32])
110
-	decryptor := cipher.NewCTR(decBlock, serverEncrypted[8+32:8+32+16])
111
-
112
-	serverDecrypted := make([]byte, len(serverEncrypted))
113
-	decryptor.XORKeyStream(serverDecrypted, serverEncrypted)
114
-
115
-	require.Equal(t, "3d3d3Q",
116
-		base64.RawStdEncoding.EncodeToString(serverDecrypted[8+32+16:8+32+16+4]))
117
-
118
-	serverEncryptedReverted := make([]byte, len(serverEncrypted))
119
-
120
-	for i := range 32 + 16 {
121
-		serverEncryptedReverted[8+i] = serverEncrypted[8+32+16-1-i]
122
-	}
123
-
124
-	encBlock, _ := aes.NewCipher(serverEncryptedReverted[8 : 8+32])
125
-	encryptor := cipher.NewCTR(encBlock, serverEncryptedReverted[8+32:8+32+16])
126
-
127
-	return ServerHandshakeTestData{
128
-		connMock: connMock,
129
-		proxyConn: obfuscated2.Conn{
130
-			Conn:      connMock,
131
-			Encryptor: handshakeEnc,
132
-			Decryptor: handshakeDec,
133
-		},
134
-		encryptor: encryptor,
135
-		decryptor: decryptor,
136
-	}
137
-}

+ 0
- 39
mtglib/internal/obfuscated2/pools.go Ver fichero

@@ -1,39 +0,0 @@
1
-package obfuscated2
2
-
3
-import (
4
-	"bytes"
5
-	"crypto/sha256"
6
-	"hash"
7
-	"sync"
8
-)
9
-
10
-var (
11
-	sha256HasherPool = sync.Pool{
12
-		New: func() any {
13
-			return sha256.New()
14
-		},
15
-	}
16
-	bytesBufferPool = sync.Pool{
17
-		New: func() any {
18
-			return &bytes.Buffer{}
19
-		},
20
-	}
21
-)
22
-
23
-func acquireSha256Hasher() hash.Hash {
24
-	return sha256HasherPool.Get().(hash.Hash) //nolint: forcetypeassert
25
-}
26
-
27
-func releaseSha256Hasher(h hash.Hash) {
28
-	h.Reset()
29
-	sha256HasherPool.Put(h)
30
-}
31
-
32
-func acquireBytesBuffer() *bytes.Buffer {
33
-	return bytesBufferPool.Get().(*bytes.Buffer) //nolint: forcetypeassert
34
-}
35
-
36
-func releaseBytesBuffer(buf *bytes.Buffer) {
37
-	buf.Reset()
38
-	bytesBufferPool.Put(buf)
39
-}

+ 0
- 67
mtglib/internal/obfuscated2/server_handshake.go Ver fichero

@@ -1,67 +0,0 @@
1
-package obfuscated2
2
-
3
-import (
4
-	"crypto/cipher"
5
-	"crypto/rand"
6
-	"encoding/binary"
7
-	"fmt"
8
-	"io"
9
-)
10
-
11
-type serverHandshakeFrame struct {
12
-	handshakeFrame
13
-}
14
-
15
-func (s *serverHandshakeFrame) decryptor() cipher.Stream {
16
-	invertedHandshake := s.invert()
17
-
18
-	return makeAesCtr(invertedHandshake.key(), invertedHandshake.iv())
19
-}
20
-
21
-func (s *serverHandshakeFrame) encryptor() cipher.Stream {
22
-	return makeAesCtr(s.key(), s.iv())
23
-}
24
-
25
-func ServerHandshake(writer io.Writer) (cipher.Stream, cipher.Stream, error) {
26
-	handshake := generateServerHanshakeFrame()
27
-	copyHandshake := handshake
28
-	encryptor := handshake.encryptor()
29
-	decryptor := handshake.decryptor()
30
-
31
-	encryptor.XORKeyStream(handshake.data[:], handshake.data[:])
32
-	copy(handshake.key(), copyHandshake.key())
33
-	copy(handshake.iv(), copyHandshake.iv())
34
-
35
-	if _, err := writer.Write(handshake.data[:]); err != nil {
36
-		return nil, nil, fmt.Errorf("cannot send a handshake frame to telegram: %w", err)
37
-	}
38
-
39
-	return encryptor, decryptor, nil
40
-}
41
-
42
-func generateServerHanshakeFrame() serverHandshakeFrame {
43
-	frame := serverHandshakeFrame{}
44
-
45
-	for {
46
-		if _, err := rand.Read(frame.data[:]); err != nil {
47
-			panic(err)
48
-		}
49
-
50
-		if frame.data[0] == 0xef { // taken from tg sources
51
-			continue
52
-		}
53
-
54
-		switch binary.LittleEndian.Uint32(frame.data[:4]) {
55
-		case 0x44414548, 0x54534f50, 0x20544547, 0x4954504f, 0xeeeeeeee: // taken from tg sources
56
-			continue
57
-		}
58
-
59
-		if frame.data[4]|frame.data[5]|frame.data[6]|frame.data[7] == 0 {
60
-			continue
61
-		}
62
-
63
-		copy(frame.connectionType(), handshakeConnectionType)
64
-
65
-		return frame
66
-	}
67
-}

+ 0
- 30
mtglib/internal/obfuscated2/server_handshake_fuzz_internal_test.go Ver fichero

@@ -1,30 +0,0 @@
1
-package obfuscated2
2
-
3
-import (
4
-	"encoding/binary"
5
-	"testing"
6
-
7
-	"github.com/stretchr/testify/assert"
8
-)
9
-
10
-func FuzzServerGenerateHandshakeFrame(f *testing.F) {
11
-	f.Fuzz(func(t *testing.T, arg int) {
12
-		frame := generateServerHanshakeFrame()
13
-
14
-		assert.NotEqualValues(t, 0xef, frame.data[0])
15
-
16
-		firstBytes := binary.LittleEndian.Uint32(frame.data[:4])
17
-		assert.NotEqualValues(t, 0x44414548, firstBytes)
18
-		assert.NotEqualValues(t, 0x54534f50, firstBytes)
19
-		assert.NotEqualValues(t, 0x20544547, firstBytes)
20
-		assert.NotEqualValues(t, 0x4954504f, firstBytes)
21
-		assert.NotEqualValues(t, 0xeeeeeeee, firstBytes)
22
-
23
-		assert.NotEqualValues(
24
-			t,
25
-			0,
26
-			frame.data[4]|frame.data[5]|frame.data[6]|frame.data[7])
27
-
28
-		assert.Equal(t, handshakeConnectionType, frame.connectionType())
29
-	})
30
-}

+ 0
- 58
mtglib/internal/obfuscated2/server_handshake_fuzz_test.go Ver fichero

@@ -1,58 +0,0 @@
1
-package obfuscated2_test
2
-
3
-import (
4
-	"testing"
5
-
6
-	"github.com/stretchr/testify/assert"
7
-	"github.com/stretchr/testify/mock"
8
-)
9
-
10
-func FuzzServerSend(f *testing.F) {
11
-	f.Add([]byte{1, 2, 3, 4, 5})
12
-
13
-	f.Fuzz(func(t *testing.T, data []byte) {
14
-		handshakeData := NewServerHandshakeTestData(t)
15
-
16
-		handshakeData.connMock.
17
-			On("Write", mock.Anything).
18
-			Return(len(data), nil).
19
-			Once().
20
-			Run(func(args mock.Arguments) {
21
-				message := make([]byte, len(data))
22
-				handshakeData.decryptor.XORKeyStream(message, args.Get(0).([]byte)) //nolint: forcetypeassert
23
-				assert.Equal(t, message, data)
24
-			})
25
-
26
-		n, err := handshakeData.proxyConn.Write(data)
27
-
28
-		assert.EqualValues(t, len(data), n)
29
-		assert.NoError(t, err)
30
-		handshakeData.connMock.AssertExpectations(t)
31
-	})
32
-}
33
-
34
-func FuzzServerReceive(f *testing.F) {
35
-	f.Add([]byte{1, 2, 3, 4, 5})
36
-
37
-	f.Fuzz(func(t *testing.T, data []byte) {
38
-		handshakeData := NewServerHandshakeTestData(t)
39
-		buffer := make([]byte, len(data))
40
-
41
-		handshakeData.connMock.
42
-			On("Read", mock.Anything).
43
-			Return(len(data), nil).
44
-			Once().
45
-			Run(func(args mock.Arguments) {
46
-				message := make([]byte, len(data))
47
-				handshakeData.encryptor.XORKeyStream(message, data)
48
-				copy(args.Get(0).([]byte), message) //nolint: forcetypeassert
49
-			})
50
-
51
-		n, err := handshakeData.proxyConn.Read(buffer)
52
-
53
-		assert.EqualValues(t, len(data), n)
54
-		assert.NoError(t, err)
55
-		assert.Equal(t, data, buffer)
56
-		handshakeData.connMock.AssertExpectations(t)
57
-	})
58
-}

+ 0
- 65
mtglib/internal/obfuscated2/server_handshake_test.go Ver fichero

@@ -1,65 +0,0 @@
1
-package obfuscated2_test
2
-
3
-import (
4
-	"testing"
5
-
6
-	"github.com/stretchr/testify/mock"
7
-	"github.com/stretchr/testify/suite"
8
-)
9
-
10
-type ServerHandshakeTestSuite struct {
11
-	suite.Suite
12
-
13
-	data ServerHandshakeTestData
14
-}
15
-
16
-func (suite *ServerHandshakeTestSuite) SetupTest() {
17
-	suite.data = NewServerHandshakeTestData(suite.T())
18
-}
19
-
20
-func (suite *ServerHandshakeTestSuite) TearDownTest() {
21
-	suite.data.connMock.AssertExpectations(suite.T())
22
-}
23
-
24
-func (suite *ServerHandshakeTestSuite) TestSendToTelegram() {
25
-	messageToTelegram := []byte{10, 11, 12, 13, 14, 'a'}
26
-
27
-	suite.data.connMock.
28
-		On("Write", mock.Anything).
29
-		Return(len(messageToTelegram), nil).
30
-		Once().
31
-		Run(func(args mock.Arguments) {
32
-			message := make([]byte, len(messageToTelegram))
33
-			suite.data.decryptor.XORKeyStream(message, args.Get(0).([]byte)) //nolint: forcetypeassert
34
-			suite.Equal(messageToTelegram, message)
35
-		})
36
-
37
-	n, err := suite.data.proxyConn.Write(messageToTelegram)
38
-	suite.EqualValues(len(messageToTelegram), n)
39
-	suite.NoError(err)
40
-}
41
-
42
-func (suite *ServerHandshakeTestSuite) TestRecieveFromTelegram() {
43
-	messageFromTelegram := []byte{10, 11, 12, 13, 14, 'a'}
44
-	buffer := make([]byte, len(messageFromTelegram))
45
-
46
-	suite.data.connMock.
47
-		On("Read", mock.Anything).
48
-		Return(len(messageFromTelegram), nil).
49
-		Once().
50
-		Run(func(args mock.Arguments) {
51
-			message := make([]byte, len(messageFromTelegram))
52
-			suite.data.encryptor.XORKeyStream(message, messageFromTelegram)
53
-			copy(args.Get(0).([]byte), message) //nolint: forcetypeassert
54
-		})
55
-
56
-	n, err := suite.data.proxyConn.Read(buffer)
57
-	suite.EqualValues(len(messageFromTelegram), n)
58
-	suite.NoError(err)
59
-	suite.Equal(messageFromTelegram, buffer)
60
-}
61
-
62
-func TestServerHandshake(t *testing.T) {
63
-	t.Parallel()
64
-	suite.Run(t, &ServerHandshakeTestSuite{})
65
-}

+ 0
- 13
mtglib/internal/obfuscated2/testdata/client-handshake-snapshot-4529d55776e2d427.json Ver fichero

@@ -1,13 +0,0 @@
1
-{
2
-  "secret": "NnoYmu4Y+jHBkAVO/UqOlQ",
3
-  "frame": "gDcXwaMY4RwlR+nJw+ILDr123UJHHjjE/U5pF4m/Y04AmH7lEpEL6UYRnIYDbDlOHSDxc1ToziPvNlJJh8RMow",
4
-  "dc": 2,
5
-  "encrypted": {
6
-    "text": "AQIDBAUGBwgJCg",
7
-    "cipher": "wZV3TR39l9nRoQ"
8
-  },
9
-  "decrypted": {
10
-    "text": "4wZj6mUUew",
11
-    "cipher": "YWJjZGVmZw"
12
-  }
13
-}

+ 0
- 13
mtglib/internal/obfuscated2/testdata/client-handshake-snapshot-585c944d672f60a2.json Ver fichero

@@ -1,13 +0,0 @@
1
-{
2
-  "secret": "NnoYmu4Y+jHBkAVO/UqOlQ",
3
-  "frame": "M2WyxeiwIQB+ZOFxNzSNHtu9OdESkfxv3JkKFimCxUoYA3BD/Ql9nXB/OIonCKLUKCcS0VzZ2P6/+5oQ9GI8YA",
4
-  "dc": 2,
5
-  "encrypted": {
6
-    "text": "AQIDBAUGBwgJCg",
7
-    "cipher": "tzAwrCz00odERg"
8
-  },
9
-  "decrypted": {
10
-    "text": "QkIvwGQDgA",
11
-    "cipher": "YWJjZGVmZw"
12
-  }
13
-}

+ 0
- 15
mtglib/internal/obfuscated2/utils.go Ver fichero

@@ -1,15 +0,0 @@
1
-package obfuscated2
2
-
3
-import (
4
-	"crypto/aes"
5
-	"crypto/cipher"
6
-)
7
-
8
-func makeAesCtr(key, iv []byte) cipher.Stream {
9
-	block, err := aes.NewCipher(key)
10
-	if err != nil {
11
-		panic(err)
12
-	}
13
-
14
-	return cipher.NewCTR(block, iv)
15
-}

Loading…
Cancelar
Guardar