forked from PretendoNetwork/nex-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet_v0.go
332 lines (254 loc) · 7.96 KB
/
packet_v0.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package nex
import (
"crypto/hmac"
"crypto/md5"
"encoding/binary"
"errors"
"fmt"
)
// PacketV0 reresents a PRUDPv0 packet
type PacketV0 struct {
Packet
checksum uint32
}
// SetChecksum sets the packet checksum
func (packet *PacketV0) SetChecksum(checksum uint32) {
packet.checksum = checksum
}
// Checksum returns the packet checksum
func (packet *PacketV0) Checksum() uint32 {
return packet.checksum
}
// Decode decodes the packet
func (packet *PacketV0) Decode() error {
if len(packet.Data()) < 9 {
return errors.New("[PRUDPv0] Packet length less than header minimum")
}
var checksumSize int
var payloadSize uint16
var typeFlags uint16
if packet.Sender().Server().ChecksumVersion() == 0 {
checksumSize = 4
} else {
checksumSize = 1
}
stream := NewStreamIn(packet.Data(), packet.Sender().Server())
packet.SetSource(stream.ReadUInt8())
packet.SetDestination(stream.ReadUInt8())
typeFlags = stream.ReadUInt16LE()
packet.SetSessionID(stream.ReadUInt8())
packet.SetSignature(stream.ReadBytesNext(4))
packet.SetSequenceID(stream.ReadUInt16LE())
if packet.Sender().Server().FlagsVersion() == 0 {
packet.SetType(typeFlags & 7)
packet.SetFlags(typeFlags >> 3)
} else {
packet.SetType(typeFlags & 0xF)
packet.SetFlags(typeFlags >> 4)
}
if _, ok := validTypes[packet.Type()]; !ok {
return errors.New("[PRUDPv0] Packet type not valid type")
}
if packet.Type() == SynPacket || packet.Type() == ConnectPacket {
if len(packet.Data()[stream.ByteOffset():]) < 4 {
return errors.New("[PRUDPv0] Packet specific data not large enough for connection signature")
}
packet.SetConnectionSignature(stream.ReadBytesNext(4))
}
if packet.Type() == DataPacket {
if len(packet.Data()[stream.ByteOffset():]) < 1 {
return errors.New("[PRUDPv0] Packet specific data not large enough for fragment ID")
}
packet.SetFragmentID(stream.ReadUInt8())
}
if packet.HasFlag(FlagHasSize) {
if len(packet.Data()[stream.ByteOffset():]) < 2 {
return errors.New("[PRUDPv0] Packet specific data not large enough for payload size")
}
payloadSize = stream.ReadUInt16LE()
} else {
payloadSize = uint16(len(packet.data) - int(stream.ByteOffset()) - checksumSize)
}
if payloadSize > 0 {
if len(packet.Data()[stream.ByteOffset():]) < int(payloadSize) {
return errors.New("[PRUDPv0] Packet data length less than payload length")
}
payloadCrypted := stream.ReadBytesNext(int64(payloadSize))
packet.SetPayload(payloadCrypted)
if packet.Type() == DataPacket {
ciphered := make([]byte, payloadSize)
packet.Sender().Decipher().XORKeyStream(ciphered, payloadCrypted)
request, err := NewRMCRequest(ciphered)
if err != nil {
return errors.New("[PRUDPv0] Error parsing RMC request: " + err.Error())
}
packet.rmcRequest = request
}
}
if len(packet.Data()[stream.ByteOffset():]) < int(checksumSize) {
return errors.New("[PRUDPv0] Packet data length less than checksum length")
}
if checksumSize == 1 {
packet.SetChecksum(uint32(stream.ReadUInt8()))
} else {
packet.SetChecksum(stream.ReadUInt32LE())
}
packetBody := stream.Bytes()
calculatedChecksum := packet.calculateChecksum(packetBody[:len(packetBody)-checksumSize])
if calculatedChecksum != packet.Checksum() {
fmt.Println("[ERROR] Calculated checksum did not match")
}
return nil
}
// Bytes encodes the packet and returns a byte array
func (packet *PacketV0) Bytes() []byte {
if packet.Type() == DataPacket {
if packet.HasFlag(FlagAck) {
packet.SetPayload([]byte{})
} else {
payload := packet.Payload()
if payload != nil || len(payload) > 0 {
payloadSize := len(payload)
encrypted := make([]byte, payloadSize)
packet.Sender().Cipher().XORKeyStream(encrypted, payload)
packet.SetPayload(encrypted)
}
}
if !packet.HasFlag(FlagHasSize) {
packet.AddFlag(FlagHasSize)
}
}
var typeFlags uint16
if packet.Sender().Server().FlagsVersion() == 0 {
typeFlags = packet.Type() | packet.Flags()<<3
} else {
typeFlags = packet.Type() | packet.Flags()<<4
}
stream := NewStreamOut(packet.Sender().Server())
packetSignature := packet.calculateSignature()
stream.WriteUInt8(packet.Source())
stream.WriteUInt8(packet.Destination())
stream.WriteUInt16LE(typeFlags)
stream.WriteUInt8(packet.SessionID())
stream.Grow(int64(len(packetSignature)))
stream.WriteBytesNext(packetSignature)
stream.WriteUInt16LE(packet.SequenceID())
options := packet.encodeOptions()
optionsLength := len(options)
if optionsLength > 0 {
stream.Grow(int64(optionsLength))
stream.WriteBytesNext(options)
}
payload := packet.Payload()
if payload != nil && len(payload) > 0 {
stream.Grow(int64(len(payload)))
stream.WriteBytesNext(payload)
}
checksum := packet.calculateChecksum(stream.Bytes())
if packet.Sender().Server().ChecksumVersion() == 0 {
stream.WriteUInt32LE(checksum)
} else {
stream.WriteUInt8(uint8(checksum))
}
return stream.Bytes()
}
func (packet *PacketV0) calculateSignature() []byte {
// Friends server handles signatures differently, so check for the Friends server access key
if packet.Sender().Server().AccessKey() == "ridfebb9" {
if packet.Type() == DataPacket {
payload := packet.Payload()
if payload == nil || len(payload) <= 0 {
signature := NewStreamOut(packet.Sender().Server())
signature.WriteUInt32LE(0x12345678)
return signature.Bytes()
}
key := packet.Sender().SignatureKey()
cipher := hmac.New(md5.New, key)
cipher.Write(payload)
return cipher.Sum(nil)[:4]
}
clientConnectionSignature := packet.Sender().ClientConnectionSignature()
if clientConnectionSignature != nil {
return clientConnectionSignature
}
return []byte{0x0, 0x0, 0x0, 0x0}
} else { // Normal signature handling
if packet.Type() == DataPacket || packet.Type() == DisconnectPacket {
payload := NewStreamOut(packet.Sender().Server())
sessionKey := packet.Sender().SessionKey()
if sessionKey != nil {
payload.Grow(int64(len(sessionKey)))
payload.WriteBytesNext(sessionKey)
}
payload.WriteUInt16LE(packet.sequenceID)
payload.Grow(1)
payload.WriteByteNext(packet.fragmentID)
pktpay := packet.Payload()
if pktpay != nil && len(pktpay) > 0 {
payload.Grow(int64(len(pktpay)))
payload.WriteBytesNext(pktpay)
}
key := packet.Sender().SignatureKey()
cipher := hmac.New(md5.New, key)
cipher.Write(payload.Bytes())
return cipher.Sum(nil)[:4]
} else {
clientConnectionSignature := packet.Sender().ClientConnectionSignature()
if clientConnectionSignature != nil {
return clientConnectionSignature
}
}
}
return []byte{}
}
func (packet *PacketV0) encodeOptions() []byte {
stream := NewStreamOut(packet.Sender().Server())
if packet.Type() == SynPacket {
stream.Grow(4)
stream.WriteBytesNext(packet.Sender().ServerConnectionSignature())
}
if packet.Type() == ConnectPacket {
stream.Grow(4)
stream.WriteBytesNext(packet.Sender().ClientConnectionSignature())
}
if packet.Type() == DataPacket {
stream.WriteUInt8(packet.FragmentID())
}
if packet.HasFlag(FlagHasSize) {
payload := packet.Payload()
if payload != nil {
stream.WriteUInt16LE(uint16(len(payload)))
} else {
stream.WriteUInt16LE(0)
}
}
return stream.Bytes()
}
func (packet *PacketV0) calculateChecksum(data []byte) uint32 {
signatureBase := packet.Sender().SignatureBase()
steps := len(data) / 4
var temp uint32
for i := 0; i < steps; i++ {
offset := i * 4
temp += binary.LittleEndian.Uint32(data[offset : offset+4])
}
temp &= 0xFFFFFFFF
buff := make([]byte, 4)
binary.LittleEndian.PutUint32(buff, temp)
checksum := signatureBase
checksum += sum(data[len(data) & ^3:])
checksum += sum(buff)
return uint32(checksum & 0xFF)
}
// NewPacketV0 returns a new PRUDPv0 packet
func NewPacketV0(client *Client, data []byte) (*PacketV0, error) {
packet := NewPacket(client, data)
packetv0 := PacketV0{Packet: packet}
if data != nil {
err := packetv0.Decode()
if err != nil {
return &PacketV0{}, errors.New("[PRUDPv0] Error decoding packet data: " + err.Error())
}
}
return &packetv0, nil
}