Skip to content

Commit

Permalink
fix(binary): Pack tea packet error
Browse files Browse the repository at this point in the history
optimize: use tea.EncryptTo to reduce mem usage
  • Loading branch information
fumiama committed May 7, 2024
1 parent 3c20435 commit 1b3a26d
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 612 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/RomiChan/protobuf v0.1.1-0.20230204044148-2ed269a2e54d
github.com/fumiama/gofastTEA v0.0.10
github.com/fumiama/imgsz v0.0.4
github.com/mattn/go-colorable v0.1.13
github.com/sirupsen/logrus v1.9.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7/go.mod h1:vD7Ra3Q9o
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fumiama/gofastTEA v0.0.10 h1:JJJ+brWD4kie+mmK2TkspDXKzqq0IjXm89aGYfoGhhQ=
github.com/fumiama/gofastTEA v0.0.10/go.mod h1:RIdbYZyB4MbH6ZBlPymRaXn3cD6SedlCu5W/HHfMPBk=
github.com/fumiama/imgsz v0.0.4 h1:Lsasu2hdSSFS+vnD+nvR1UkiRMK7hcpyYCC0FzgSMFI=
github.com/fumiama/imgsz v0.0.4/go.mod h1:bISOQVTlw9sRytPwe8ir7tAaEmyz9hSNj9n8mXMBG0E=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
Expand Down
5 changes: 3 additions & 2 deletions packets/tlv/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package tlv
import (
"strconv"

ftea "github.com/fumiama/gofastTEA"

"github.com/LagrangeDev/LagrangeGo/info"
"github.com/LagrangeDev/LagrangeGo/utils"
"github.com/LagrangeDev/LagrangeGo/utils/binary"
"github.com/LagrangeDev/LagrangeGo/utils/crypto"
)

// T18 默认参数 pingVersion, unknown = 0, ssoVersion = 5
Expand Down Expand Up @@ -60,7 +61,7 @@ func T106(appId, appClientVersion, uin int, guid string, passwordMd5, tgtgtKey,
ToBytes()

return binary.NewBuilder(nil).
WritePacketBytes(crypto.NewTeaCipher(key).Encrypt(body), "u32", true).
WritePacketBytes(ftea.NewTeaCipher(key).Encrypt(body), "u32", true).
Pack(0x106)
}

Expand Down
9 changes: 5 additions & 4 deletions packets/wtlogin/oicq.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"fmt"
"strconv"

ftea "github.com/fumiama/gofastTEA"

"github.com/LagrangeDev/LagrangeGo/info"
"github.com/LagrangeDev/LagrangeGo/packets/pb"
"github.com/LagrangeDev/LagrangeGo/utils"
"github.com/LagrangeDev/LagrangeGo/utils/binary"
"github.com/LagrangeDev/LagrangeGo/utils/crypto"
"github.com/LagrangeDev/LagrangeGo/utils/crypto/ecdh"
"github.com/LagrangeDev/LagrangeGo/utils/proto"
)
Expand Down Expand Up @@ -42,7 +43,7 @@ func BuildCode2dPacket(uin uint32, cmdID int, appInfo *info.AppInfo, body []byte
}

func BuildLoginPacket(uin uint32, cmd string, appinfo *info.AppInfo, body []byte) []byte {
encBody := crypto.NewTeaCipher(ecdh.S192().SharedKey()).Encrypt(body)
encBody := ftea.NewTeaCipher(ecdh.S192().SharedKey()).Encrypt(body)

var _cmd uint16
if cmd == "wtlogin.login" {
Expand Down Expand Up @@ -121,7 +122,7 @@ func BuildUniPacket(uin, seq int, cmd string, sign map[string]string,
WritePacketBytes(body, "u32", true).
ToBytes()

encrypted := crypto.NewTeaCipher(sigInfo.D2Key).Encrypt(ssoPacket)
encrypted := ftea.NewTeaCipher(sigInfo.D2Key).Encrypt(ssoPacket)

var _s uint8
if len(sigInfo.D2) == 0 {
Expand Down Expand Up @@ -151,7 +152,7 @@ func DecodeLoginResponse(buf []byte, sig *info.SigInfo) error {
var title, content string

if typ == 0 {
reader = binary.NewReader(crypto.NewTeaCipher(sig.Tgtgt).Decrypt(tlv[0x119]))
reader = binary.NewReader(ftea.NewTeaCipher(sig.Tgtgt).Decrypt(tlv[0x119]))
tlv = reader.ReadTlv()
if tgt, ok := tlv[0x10A]; ok {
sig.Tgt = tgt
Expand Down
9 changes: 5 additions & 4 deletions packets/wtlogin/sso.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"fmt"
"strings"

ftea "github.com/fumiama/gofastTEA"

"github.com/LagrangeDev/LagrangeGo/utils"
binary2 "github.com/LagrangeDev/LagrangeGo/utils/binary"
"github.com/LagrangeDev/LagrangeGo/utils/crypto"
"github.com/LagrangeDev/LagrangeGo/utils/crypto/ecdh"
)

Expand Down Expand Up @@ -64,11 +65,11 @@ func ParseSSOHeader(raw, d2Key []byte) (*SSOHeader, error) {
} else if flag == 1 { // enc with d2key
temp := make([]byte, buf.Len())
_ = binary.Read(buf, binary.BigEndian, temp)
dec = crypto.NewTeaCipher(d2Key).Decrypt(temp)
dec = ftea.NewTeaCipher(d2Key).Decrypt(temp)
} else if flag == 2 { // enc with \x00*16
temp := make([]byte, buf.Len())
_ = binary.Read(buf, binary.BigEndian, temp)
dec = crypto.NewTeaCipher(nil).Decrypt(temp)
dec = ftea.NewTeaCipher(nil).Decrypt(temp)
} else {
return nil, fmt.Errorf("invalid encrypt flag: %d", flag)
}
Expand Down Expand Up @@ -135,7 +136,7 @@ func ParseOicqBody(buf []byte) ([]byte, error) {

body := buf[16 : len(buf)-1]
if encType == 0 {
return crypto.NewTeaCipher(ecdh.S192().SharedKey()).Decrypt(body), nil
return ftea.NewTeaCipher(ecdh.S192().SharedKey()).Decrypt(body), nil
} else {
return nil, fmt.Errorf("unknown encrypt type: %d", encType)
}
Expand Down
28 changes: 19 additions & 9 deletions utils/binary/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ import (
"math"
"strconv"

ftea "github.com/fumiama/gofastTEA"

"github.com/LagrangeDev/LagrangeGo/utils"
"github.com/LagrangeDev/LagrangeGo/utils/crypto"
)

type Builder struct {
buffer []byte
key crypto.TEA
key ftea.TEA
usetea bool
}

func NewBuilder(key []byte) *Builder {
return &Builder{
buffer: make([]byte, 0, 64),
key: crypto.NewTeaCipher(key),
key: ftea.NewTeaCipher(key),
usetea: len(key) == 16,
}
}
Expand All @@ -46,17 +47,26 @@ func (b *Builder) pack(v any) {
b.append(buf.Bytes())
}

// ToBytes return data with tea encryption
func (b *Builder) ToBytes() []byte {
return b.data()
}

// Pack TLV without tea encryption
// Pack TLV with tea encryption
func (b *Builder) Pack(typ uint16) []byte {
buf := make([]byte, b.Len()+2+2)
binary.BigEndian.PutUint16(buf[0:2], typ) // type
binary.BigEndian.PutUint16(buf[2:2+2], uint16(len(b.buffer))) // length
copy(buf[2+2:], b.buffer)
return buf
buf := make([]byte, b.Len()+2+2+16)

n := 0
if b.usetea {
n = b.key.EncryptTo(b.buffer, buf[2+2:])
} else {
n = copy(buf[2+2:], b.buffer)
}

binary.BigEndian.PutUint16(buf[0:2], typ) // type
binary.BigEndian.PutUint16(buf[2:2+2], uint16(n)) // length

return buf[:n+2+2]
}

func (b *Builder) WriteBool(v bool) *Builder {
Expand Down
145 changes: 0 additions & 145 deletions utils/crypto/tea.go

This file was deleted.

Loading

0 comments on commit 1b3a26d

Please sign in to comment.