mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 11:54:01 +03:00
Delete old obfuscated2 package
This commit is contained in:
@@ -1,54 +0,0 @@
|
||||
package obfuscated2
|
||||
|
||||
import (
|
||||
"crypto/cipher"
|
||||
"crypto/subtle"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
type clientHandhakeFrame struct {
|
||||
handshakeFrame
|
||||
}
|
||||
|
||||
func (c *clientHandhakeFrame) decryptor(secret []byte) cipher.Stream {
|
||||
hasher := acquireSha256Hasher()
|
||||
defer releaseSha256Hasher(hasher)
|
||||
|
||||
hasher.Write(c.key())
|
||||
hasher.Write(secret)
|
||||
|
||||
return makeAesCtr(hasher.Sum(nil), c.iv())
|
||||
}
|
||||
|
||||
func (c *clientHandhakeFrame) encryptor(secret []byte) cipher.Stream {
|
||||
invertedHandshake := c.invert()
|
||||
|
||||
hasher := acquireSha256Hasher()
|
||||
defer releaseSha256Hasher(hasher)
|
||||
|
||||
hasher.Write(invertedHandshake.key())
|
||||
hasher.Write(secret)
|
||||
|
||||
return makeAesCtr(hasher.Sum(nil), invertedHandshake.iv())
|
||||
}
|
||||
|
||||
func ClientHandshake(secret []byte, reader io.Reader) (int, cipher.Stream, cipher.Stream, error) {
|
||||
handshake := clientHandhakeFrame{}
|
||||
|
||||
if _, err := io.ReadFull(reader, handshake.data[:]); err != nil {
|
||||
return 0, nil, nil, fmt.Errorf("cannot read frame: %w", err)
|
||||
}
|
||||
|
||||
decryptor := handshake.decryptor(secret)
|
||||
encryptor := handshake.encryptor(secret)
|
||||
|
||||
decryptor.XORKeyStream(handshake.data[:], handshake.data[:])
|
||||
|
||||
if val := handshake.connectionType(); subtle.ConstantTimeCompare(handshakeConnectionType, val) != 1 {
|
||||
return 0, nil, nil, fmt.Errorf("unsupported connection type: %s", hex.EncodeToString(val))
|
||||
}
|
||||
|
||||
return handshake.dc(), encryptor, decryptor, nil
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package obfuscated2
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var FuzzClientHandshakeSecret = []byte{1, 2, 3}
|
||||
|
||||
func FuzzClientHandshake(f *testing.F) {
|
||||
f.Add([]byte{1, 2, 3})
|
||||
|
||||
f.Fuzz(func(t *testing.T, frame []byte) {
|
||||
data := bytes.NewReader(frame)
|
||||
|
||||
if _, _, _, err := ClientHandshake(FuzzClientHandshakeSecret, data); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
handshake := clientHandhakeFrame{}
|
||||
require.Len(t, frame, handshakeFrameLen)
|
||||
|
||||
copy(handshake.data[:], frame)
|
||||
|
||||
decryptor := handshake.decryptor(FuzzClientHandshakeSecret)
|
||||
decryptor.XORKeyStream(handshake.data[:], handshake.data[:])
|
||||
|
||||
require.Equal(t, handshakeConnectionType, handshake.connectionType())
|
||||
})
|
||||
}
|
||||
@@ -1,89 +0,0 @@
|
||||
package obfuscated2_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/9seconds/mtg/v2/internal/testlib"
|
||||
"github.com/9seconds/mtg/v2/mtglib/internal/obfuscated2"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
type ClientHandshakeTestSuite struct {
|
||||
suite.Suite
|
||||
SnapshotTestSuite
|
||||
}
|
||||
|
||||
func (suite *ClientHandshakeTestSuite) SetupSuite() {
|
||||
suite.NoError(suite.IngestSnapshots(".", "client-handshake-snapshot-"))
|
||||
}
|
||||
|
||||
func (suite *ClientHandshakeTestSuite) TestCannotRead() {
|
||||
buf := bytes.NewBuffer([]byte{1, 2, 3})
|
||||
_, _, _, err := obfuscated2.ClientHandshake([]byte{1, 2, 3}, buf) //nolint: dogsled
|
||||
|
||||
suite.Error(err)
|
||||
}
|
||||
|
||||
func (suite *ClientHandshakeTestSuite) TestOk() {
|
||||
for nameV, snapshotV := range suite.snapshots {
|
||||
snapshot := snapshotV
|
||||
|
||||
suite.T().Run(nameV, func(t *testing.T) {
|
||||
buf := bytes.NewBuffer(snapshot.Frame.data)
|
||||
|
||||
dc, encryptor, decryptor, err := obfuscated2.ClientHandshake(
|
||||
snapshot.Secret.data, buf)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, snapshot.DC, dc)
|
||||
|
||||
writeData := make([]byte, len(snapshot.Encrypted.Text.data))
|
||||
readData := make([]byte, len(snapshot.Decrypted.Text.data))
|
||||
|
||||
connMock := &testlib.EssentialsConnMock{}
|
||||
connMock.On("Read", mock.Anything).
|
||||
Once().
|
||||
Return(len(snapshot.Decrypted.Text.data), nil).
|
||||
Run(func(args mock.Arguments) {
|
||||
arr, ok := args.Get(0).([]byte)
|
||||
|
||||
suite.True(ok)
|
||||
copy(arr, snapshot.Decrypted.Cipher.data)
|
||||
})
|
||||
connMock.On("Write", mock.Anything).
|
||||
Once().
|
||||
Return(len(snapshot.Encrypted.Text.data), nil).
|
||||
Run(func(args mock.Arguments) {
|
||||
arr, ok := args.Get(0).([]byte)
|
||||
|
||||
suite.True(ok)
|
||||
copy(writeData, arr)
|
||||
})
|
||||
|
||||
conn := obfuscated2.Conn{
|
||||
Conn: connMock,
|
||||
Encryptor: encryptor,
|
||||
Decryptor: decryptor,
|
||||
}
|
||||
|
||||
n, err := conn.Read(readData)
|
||||
assert.Equal(t, len(readData), n)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, snapshot.Decrypted.Text.data, readData)
|
||||
|
||||
n, err = conn.Write(snapshot.Encrypted.Text.data)
|
||||
assert.Equal(t, len(writeData), n)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, snapshot.Encrypted.Cipher.data, writeData)
|
||||
|
||||
connMock.AssertExpectations(t)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientHandshake(t *testing.T) {
|
||||
t.Parallel()
|
||||
suite.Run(t, &ClientHandshakeTestSuite{})
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package obfuscated2
|
||||
|
||||
import (
|
||||
"crypto/cipher"
|
||||
|
||||
"github.com/9seconds/mtg/v2/essentials"
|
||||
)
|
||||
|
||||
type Conn struct {
|
||||
essentials.Conn
|
||||
|
||||
Encryptor cipher.Stream
|
||||
Decryptor cipher.Stream
|
||||
}
|
||||
|
||||
func (c Conn) Read(p []byte) (int, error) {
|
||||
n, err := c.Conn.Read(p)
|
||||
if err != nil {
|
||||
return n, err //nolint: wrapcheck
|
||||
}
|
||||
|
||||
c.Decryptor.XORKeyStream(p, p[:n])
|
||||
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (c Conn) Write(p []byte) (int, error) {
|
||||
buf := acquireBytesBuffer()
|
||||
defer releaseBytesBuffer(buf)
|
||||
|
||||
buf.Write(p)
|
||||
|
||||
payload := buf.Bytes()
|
||||
c.Encryptor.XORKeyStream(payload, payload)
|
||||
|
||||
return c.Conn.Write(payload) //nolint: wrapcheck
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
package obfuscated2
|
||||
|
||||
const (
|
||||
// DefaultDC defines a number of the default DC to use. This value used
|
||||
// only if a value from obfuscated2 handshake frame is 0 (default).
|
||||
DefaultDC = 2
|
||||
|
||||
handshakeFrameLen = 64
|
||||
|
||||
handshakeFrameLenKey = 32
|
||||
handshakeFrameLenIV = 16
|
||||
handshakeFrameLenConnectionType = 4
|
||||
|
||||
handshakeFrameOffsetStart = 8
|
||||
handshakeFrameOffsetKey = handshakeFrameOffsetStart
|
||||
handshakeFrameOffsetIV = handshakeFrameOffsetKey + handshakeFrameLenKey
|
||||
handshakeFrameOffsetConnectionType = handshakeFrameOffsetIV + handshakeFrameLenIV
|
||||
handshakeFrameOffsetDC = handshakeFrameOffsetConnectionType + handshakeFrameLenConnectionType
|
||||
)
|
||||
|
||||
// Connection-Type: Secure. We support only fake tls.
|
||||
var handshakeConnectionType = []byte{0xdd, 0xdd, 0xdd, 0xdd}
|
||||
|
||||
// A structure of obfuscated2 handshake frame is following:
|
||||
//
|
||||
// [frameOffsetFirst:frameOffsetKey:frameOffsetIV:frameOffsetMagic:frameOffsetDC:frameOffsetEnd].
|
||||
//
|
||||
// - 8 bytes of noise
|
||||
// - 32 bytes of AES Key
|
||||
// - 16 bytes of AES IV
|
||||
// - 4 bytes of 'connection type' - this has some setting like a connection type
|
||||
// - 2 bytes of 'DC'. DC is little endian int16
|
||||
// - 2 bytes of noise
|
||||
type handshakeFrame struct {
|
||||
data [handshakeFrameLen]byte
|
||||
}
|
||||
|
||||
func (h *handshakeFrame) dc() int {
|
||||
idx := int16(h.data[handshakeFrameOffsetDC]) | int16(h.data[handshakeFrameOffsetDC+1])<<8 //nolint: lll // little endian for int16 is here
|
||||
|
||||
switch {
|
||||
case idx > 0:
|
||||
return int(idx)
|
||||
case idx < 0:
|
||||
return -int(idx)
|
||||
default:
|
||||
return DefaultDC
|
||||
}
|
||||
}
|
||||
|
||||
func (h *handshakeFrame) key() []byte {
|
||||
return h.data[handshakeFrameOffsetKey:handshakeFrameOffsetIV]
|
||||
}
|
||||
|
||||
func (h *handshakeFrame) iv() []byte {
|
||||
return h.data[handshakeFrameOffsetIV:handshakeFrameOffsetConnectionType]
|
||||
}
|
||||
|
||||
func (h *handshakeFrame) connectionType() []byte {
|
||||
return h.data[handshakeFrameOffsetConnectionType:handshakeFrameOffsetDC]
|
||||
}
|
||||
|
||||
func (h *handshakeFrame) invert() handshakeFrame {
|
||||
copyFrame := *h
|
||||
|
||||
for i := range handshakeFrameLenKey + handshakeFrameLenIV {
|
||||
copyFrame.data[handshakeFrameOffsetKey+i] = h.data[handshakeFrameOffsetConnectionType-1-i]
|
||||
}
|
||||
|
||||
return copyFrame
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
package obfuscated2
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
type HandshakeFrameTestSuite struct {
|
||||
suite.Suite
|
||||
}
|
||||
|
||||
func (suite *HandshakeFrameTestSuite) Decode(value string) []byte {
|
||||
v, err := base64.RawStdEncoding.DecodeString(value)
|
||||
suite.NoError(err)
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
func (suite *HandshakeFrameTestSuite) Encode(value []byte) string {
|
||||
return base64.RawStdEncoding.EncodeToString(value)
|
||||
}
|
||||
|
||||
func (suite *HandshakeFrameTestSuite) TestOk() {
|
||||
hf := handshakeFrame{}
|
||||
testFrame := suite.Decode(
|
||||
"L9TmCzzxl9bPKODBpZeVM/qqNUxQ/axxBup1S2ymbIfUd6f7YSyzzM9EmTFv2/XzGqJGEHuj2zofmUGBLghu5g")
|
||||
copy(hf.data[:], testFrame)
|
||||
|
||||
suite.Equal("zyjgwaWXlTP6qjVMUP2scQbqdUtspmyH1Hen+2Ess8w", suite.Encode(hf.key()))
|
||||
suite.Equal("z0SZMW/b9fMaokYQe6PbOg", suite.Encode(hf.iv()))
|
||||
suite.Equal("H5lBgQ", suite.Encode(hf.connectionType()))
|
||||
suite.EqualValues(2094, hf.dc())
|
||||
|
||||
inverted := hf.invert()
|
||||
suite.Equal("OtujexBGohrz9dtvMZlEz8yzLGH7p3fUh2ymbEt16gY", suite.Encode(inverted.key()))
|
||||
suite.Equal("caz9UEw1qvozlZelweAozw", suite.Encode(inverted.iv()))
|
||||
suite.Equal("H5lBgQ", suite.Encode(inverted.connectionType()))
|
||||
suite.EqualValues(2094, inverted.dc())
|
||||
}
|
||||
|
||||
func (suite *HandshakeFrameTestSuite) TestDC() {
|
||||
testData := map[int16]int{
|
||||
1: 1,
|
||||
-1: 1,
|
||||
0: DefaultDC,
|
||||
}
|
||||
|
||||
for k, v := range testData {
|
||||
incoming := k
|
||||
expected := v
|
||||
|
||||
suite.T().Run(strconv.Itoa(int(incoming)), func(t *testing.T) {
|
||||
frame := handshakeFrame{}
|
||||
|
||||
rand.Read(frame.data[:]) //nolint: errcheck
|
||||
|
||||
frame.data[handshakeFrameOffsetDC] = byte(incoming)
|
||||
frame.data[handshakeFrameOffsetDC+1] = byte(incoming >> 8)
|
||||
|
||||
assert.Equal(t, expected, frame.dc())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandshakeFrame(t *testing.T) {
|
||||
t.Parallel()
|
||||
suite.Run(t, &HandshakeFrameTestSuite{})
|
||||
}
|
||||
@@ -1,137 +0,0 @@
|
||||
package obfuscated2_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/9seconds/mtg/v2/internal/testlib"
|
||||
"github.com/9seconds/mtg/v2/mtglib/internal/obfuscated2"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type snapshotBytes struct {
|
||||
data []byte
|
||||
}
|
||||
|
||||
func (s snapshotBytes) MarshalText() ([]byte, error) {
|
||||
if len(s.data) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return []byte(base64.RawStdEncoding.EncodeToString(s.data)), nil
|
||||
}
|
||||
|
||||
func (s *snapshotBytes) UnmarshalText(data []byte) error {
|
||||
val, err := base64.RawStdEncoding.DecodeString(string(data))
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot unmarshal %v: %w", len(val), err)
|
||||
}
|
||||
|
||||
s.data = val
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type Obfuscated2Snapshot struct {
|
||||
Secret snapshotBytes `json:"secret"`
|
||||
Frame snapshotBytes `json:"frame"`
|
||||
DC int16 `json:"dc"`
|
||||
Encrypted struct {
|
||||
Text snapshotBytes `json:"text"`
|
||||
Cipher snapshotBytes `json:"cipher"`
|
||||
} `json:"encrypted"`
|
||||
Decrypted struct {
|
||||
Text snapshotBytes `json:"text"`
|
||||
Cipher snapshotBytes `json:"cipher"`
|
||||
} `json:"decrypted"`
|
||||
}
|
||||
|
||||
type SnapshotTestSuite struct {
|
||||
snapshots map[string]*Obfuscated2Snapshot
|
||||
}
|
||||
|
||||
type ServerHandshakeTestData struct {
|
||||
connMock *testlib.EssentialsConnMock
|
||||
|
||||
proxyConn obfuscated2.Conn
|
||||
encryptor cipher.Stream
|
||||
decryptor cipher.Stream
|
||||
}
|
||||
|
||||
func (suite *SnapshotTestSuite) IngestSnapshots(dirname, namePrefix string) error {
|
||||
suite.snapshots = map[string]*Obfuscated2Snapshot{}
|
||||
|
||||
files, err := os.ReadDir(filepath.Join("testdata", dirname))
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot ingest snapshots: %w", err)
|
||||
}
|
||||
|
||||
for _, v := range files {
|
||||
if !strings.HasPrefix(v.Name(), namePrefix) {
|
||||
continue
|
||||
}
|
||||
|
||||
filename := filepath.Join("testdata", dirname, v.Name())
|
||||
|
||||
contents, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot read %s: %w", filename, err)
|
||||
}
|
||||
|
||||
value := &Obfuscated2Snapshot{}
|
||||
|
||||
if err := json.Unmarshal(contents, value); err != nil {
|
||||
return fmt.Errorf("cannot unmarshal %s: %w", filename, err)
|
||||
}
|
||||
|
||||
suite.snapshots[v.Name()] = value
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewServerHandshakeTestData(t *testing.T) ServerHandshakeTestData {
|
||||
buf := &bytes.Buffer{}
|
||||
connMock := &testlib.EssentialsConnMock{}
|
||||
|
||||
handshakeEnc, handshakeDec, err := obfuscated2.ServerHandshake(buf)
|
||||
require.NoError(t, err)
|
||||
|
||||
serverEncrypted := buf.Bytes()
|
||||
decBlock, _ := aes.NewCipher(serverEncrypted[8 : 8+32])
|
||||
decryptor := cipher.NewCTR(decBlock, serverEncrypted[8+32:8+32+16])
|
||||
|
||||
serverDecrypted := make([]byte, len(serverEncrypted))
|
||||
decryptor.XORKeyStream(serverDecrypted, serverEncrypted)
|
||||
|
||||
require.Equal(t, "3d3d3Q",
|
||||
base64.RawStdEncoding.EncodeToString(serverDecrypted[8+32+16:8+32+16+4]))
|
||||
|
||||
serverEncryptedReverted := make([]byte, len(serverEncrypted))
|
||||
|
||||
for i := range 32 + 16 {
|
||||
serverEncryptedReverted[8+i] = serverEncrypted[8+32+16-1-i]
|
||||
}
|
||||
|
||||
encBlock, _ := aes.NewCipher(serverEncryptedReverted[8 : 8+32])
|
||||
encryptor := cipher.NewCTR(encBlock, serverEncryptedReverted[8+32:8+32+16])
|
||||
|
||||
return ServerHandshakeTestData{
|
||||
connMock: connMock,
|
||||
proxyConn: obfuscated2.Conn{
|
||||
Conn: connMock,
|
||||
Encryptor: handshakeEnc,
|
||||
Decryptor: handshakeDec,
|
||||
},
|
||||
encryptor: encryptor,
|
||||
decryptor: decryptor,
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
package obfuscated2
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"hash"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
sha256HasherPool = sync.Pool{
|
||||
New: func() any {
|
||||
return sha256.New()
|
||||
},
|
||||
}
|
||||
bytesBufferPool = sync.Pool{
|
||||
New: func() any {
|
||||
return &bytes.Buffer{}
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func acquireSha256Hasher() hash.Hash {
|
||||
return sha256HasherPool.Get().(hash.Hash) //nolint: forcetypeassert
|
||||
}
|
||||
|
||||
func releaseSha256Hasher(h hash.Hash) {
|
||||
h.Reset()
|
||||
sha256HasherPool.Put(h)
|
||||
}
|
||||
|
||||
func acquireBytesBuffer() *bytes.Buffer {
|
||||
return bytesBufferPool.Get().(*bytes.Buffer) //nolint: forcetypeassert
|
||||
}
|
||||
|
||||
func releaseBytesBuffer(buf *bytes.Buffer) {
|
||||
buf.Reset()
|
||||
bytesBufferPool.Put(buf)
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
package obfuscated2
|
||||
|
||||
import (
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
type serverHandshakeFrame struct {
|
||||
handshakeFrame
|
||||
}
|
||||
|
||||
func (s *serverHandshakeFrame) decryptor() cipher.Stream {
|
||||
invertedHandshake := s.invert()
|
||||
|
||||
return makeAesCtr(invertedHandshake.key(), invertedHandshake.iv())
|
||||
}
|
||||
|
||||
func (s *serverHandshakeFrame) encryptor() cipher.Stream {
|
||||
return makeAesCtr(s.key(), s.iv())
|
||||
}
|
||||
|
||||
func ServerHandshake(writer io.Writer) (cipher.Stream, cipher.Stream, error) {
|
||||
handshake := generateServerHanshakeFrame()
|
||||
copyHandshake := handshake
|
||||
encryptor := handshake.encryptor()
|
||||
decryptor := handshake.decryptor()
|
||||
|
||||
encryptor.XORKeyStream(handshake.data[:], handshake.data[:])
|
||||
copy(handshake.key(), copyHandshake.key())
|
||||
copy(handshake.iv(), copyHandshake.iv())
|
||||
|
||||
if _, err := writer.Write(handshake.data[:]); err != nil {
|
||||
return nil, nil, fmt.Errorf("cannot send a handshake frame to telegram: %w", err)
|
||||
}
|
||||
|
||||
return encryptor, decryptor, nil
|
||||
}
|
||||
|
||||
func generateServerHanshakeFrame() serverHandshakeFrame {
|
||||
frame := serverHandshakeFrame{}
|
||||
|
||||
for {
|
||||
if _, err := rand.Read(frame.data[:]); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if frame.data[0] == 0xef { // taken from tg sources
|
||||
continue
|
||||
}
|
||||
|
||||
switch binary.LittleEndian.Uint32(frame.data[:4]) {
|
||||
case 0x44414548, 0x54534f50, 0x20544547, 0x4954504f, 0xeeeeeeee: // taken from tg sources
|
||||
continue
|
||||
}
|
||||
|
||||
if frame.data[4]|frame.data[5]|frame.data[6]|frame.data[7] == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
copy(frame.connectionType(), handshakeConnectionType)
|
||||
|
||||
return frame
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package obfuscated2
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func FuzzServerGenerateHandshakeFrame(f *testing.F) {
|
||||
f.Fuzz(func(t *testing.T, arg int) {
|
||||
frame := generateServerHanshakeFrame()
|
||||
|
||||
assert.NotEqualValues(t, 0xef, frame.data[0])
|
||||
|
||||
firstBytes := binary.LittleEndian.Uint32(frame.data[:4])
|
||||
assert.NotEqualValues(t, 0x44414548, firstBytes)
|
||||
assert.NotEqualValues(t, 0x54534f50, firstBytes)
|
||||
assert.NotEqualValues(t, 0x20544547, firstBytes)
|
||||
assert.NotEqualValues(t, 0x4954504f, firstBytes)
|
||||
assert.NotEqualValues(t, 0xeeeeeeee, firstBytes)
|
||||
|
||||
assert.NotEqualValues(
|
||||
t,
|
||||
0,
|
||||
frame.data[4]|frame.data[5]|frame.data[6]|frame.data[7])
|
||||
|
||||
assert.Equal(t, handshakeConnectionType, frame.connectionType())
|
||||
})
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
package obfuscated2_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func FuzzServerSend(f *testing.F) {
|
||||
f.Add([]byte{1, 2, 3, 4, 5})
|
||||
|
||||
f.Fuzz(func(t *testing.T, data []byte) {
|
||||
handshakeData := NewServerHandshakeTestData(t)
|
||||
|
||||
handshakeData.connMock.
|
||||
On("Write", mock.Anything).
|
||||
Return(len(data), nil).
|
||||
Once().
|
||||
Run(func(args mock.Arguments) {
|
||||
message := make([]byte, len(data))
|
||||
handshakeData.decryptor.XORKeyStream(message, args.Get(0).([]byte)) //nolint: forcetypeassert
|
||||
assert.Equal(t, message, data)
|
||||
})
|
||||
|
||||
n, err := handshakeData.proxyConn.Write(data)
|
||||
|
||||
assert.EqualValues(t, len(data), n)
|
||||
assert.NoError(t, err)
|
||||
handshakeData.connMock.AssertExpectations(t)
|
||||
})
|
||||
}
|
||||
|
||||
func FuzzServerReceive(f *testing.F) {
|
||||
f.Add([]byte{1, 2, 3, 4, 5})
|
||||
|
||||
f.Fuzz(func(t *testing.T, data []byte) {
|
||||
handshakeData := NewServerHandshakeTestData(t)
|
||||
buffer := make([]byte, len(data))
|
||||
|
||||
handshakeData.connMock.
|
||||
On("Read", mock.Anything).
|
||||
Return(len(data), nil).
|
||||
Once().
|
||||
Run(func(args mock.Arguments) {
|
||||
message := make([]byte, len(data))
|
||||
handshakeData.encryptor.XORKeyStream(message, data)
|
||||
copy(args.Get(0).([]byte), message) //nolint: forcetypeassert
|
||||
})
|
||||
|
||||
n, err := handshakeData.proxyConn.Read(buffer)
|
||||
|
||||
assert.EqualValues(t, len(data), n)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, data, buffer)
|
||||
handshakeData.connMock.AssertExpectations(t)
|
||||
})
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
package obfuscated2_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
type ServerHandshakeTestSuite struct {
|
||||
suite.Suite
|
||||
|
||||
data ServerHandshakeTestData
|
||||
}
|
||||
|
||||
func (suite *ServerHandshakeTestSuite) SetupTest() {
|
||||
suite.data = NewServerHandshakeTestData(suite.T())
|
||||
}
|
||||
|
||||
func (suite *ServerHandshakeTestSuite) TearDownTest() {
|
||||
suite.data.connMock.AssertExpectations(suite.T())
|
||||
}
|
||||
|
||||
func (suite *ServerHandshakeTestSuite) TestSendToTelegram() {
|
||||
messageToTelegram := []byte{10, 11, 12, 13, 14, 'a'}
|
||||
|
||||
suite.data.connMock.
|
||||
On("Write", mock.Anything).
|
||||
Return(len(messageToTelegram), nil).
|
||||
Once().
|
||||
Run(func(args mock.Arguments) {
|
||||
message := make([]byte, len(messageToTelegram))
|
||||
suite.data.decryptor.XORKeyStream(message, args.Get(0).([]byte)) //nolint: forcetypeassert
|
||||
suite.Equal(messageToTelegram, message)
|
||||
})
|
||||
|
||||
n, err := suite.data.proxyConn.Write(messageToTelegram)
|
||||
suite.EqualValues(len(messageToTelegram), n)
|
||||
suite.NoError(err)
|
||||
}
|
||||
|
||||
func (suite *ServerHandshakeTestSuite) TestRecieveFromTelegram() {
|
||||
messageFromTelegram := []byte{10, 11, 12, 13, 14, 'a'}
|
||||
buffer := make([]byte, len(messageFromTelegram))
|
||||
|
||||
suite.data.connMock.
|
||||
On("Read", mock.Anything).
|
||||
Return(len(messageFromTelegram), nil).
|
||||
Once().
|
||||
Run(func(args mock.Arguments) {
|
||||
message := make([]byte, len(messageFromTelegram))
|
||||
suite.data.encryptor.XORKeyStream(message, messageFromTelegram)
|
||||
copy(args.Get(0).([]byte), message) //nolint: forcetypeassert
|
||||
})
|
||||
|
||||
n, err := suite.data.proxyConn.Read(buffer)
|
||||
suite.EqualValues(len(messageFromTelegram), n)
|
||||
suite.NoError(err)
|
||||
suite.Equal(messageFromTelegram, buffer)
|
||||
}
|
||||
|
||||
func TestServerHandshake(t *testing.T) {
|
||||
t.Parallel()
|
||||
suite.Run(t, &ServerHandshakeTestSuite{})
|
||||
}
|
||||
-13
@@ -1,13 +0,0 @@
|
||||
{
|
||||
"secret": "NnoYmu4Y+jHBkAVO/UqOlQ",
|
||||
"frame": "gDcXwaMY4RwlR+nJw+ILDr123UJHHjjE/U5pF4m/Y04AmH7lEpEL6UYRnIYDbDlOHSDxc1ToziPvNlJJh8RMow",
|
||||
"dc": 2,
|
||||
"encrypted": {
|
||||
"text": "AQIDBAUGBwgJCg",
|
||||
"cipher": "wZV3TR39l9nRoQ"
|
||||
},
|
||||
"decrypted": {
|
||||
"text": "4wZj6mUUew",
|
||||
"cipher": "YWJjZGVmZw"
|
||||
}
|
||||
}
|
||||
-13
@@ -1,13 +0,0 @@
|
||||
{
|
||||
"secret": "NnoYmu4Y+jHBkAVO/UqOlQ",
|
||||
"frame": "M2WyxeiwIQB+ZOFxNzSNHtu9OdESkfxv3JkKFimCxUoYA3BD/Ql9nXB/OIonCKLUKCcS0VzZ2P6/+5oQ9GI8YA",
|
||||
"dc": 2,
|
||||
"encrypted": {
|
||||
"text": "AQIDBAUGBwgJCg",
|
||||
"cipher": "tzAwrCz00odERg"
|
||||
},
|
||||
"decrypted": {
|
||||
"text": "QkIvwGQDgA",
|
||||
"cipher": "YWJjZGVmZw"
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package obfuscated2
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
)
|
||||
|
||||
func makeAesCtr(key, iv []byte) cipher.Stream {
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return cipher.NewCTR(block, iv)
|
||||
}
|
||||
Reference in New Issue
Block a user