public MaplePacket Read(DateTime pTransmitted, ushort pBuild, byte pLocale) { if (mCursor < 4) { return(null); } if (!mAES.ConfirmHeader(mBuffer, 0)) { throw new Exception("Failed to confirm packet header"); } ushort packetSize = mAES.GetHeaderLength(mBuffer, 0, pBuild == 255 && pLocale == 1); if (mCursor < (packetSize + 4)) { return(null); } byte[] packetBuffer = new byte[packetSize]; Buffer.BlockCopy(mBuffer, 4, packetBuffer, 0, packetSize); bool byteheader = false; if (pBuild == 40 && pLocale == 5) { // WvsBeta Decrypt(packetBuffer, pBuild, pLocale, TransformLocale.MCRYPTO); byteheader = true; } else if (pLocale == 1 && pBuild == 255) { // KMSB lol Decrypt(packetBuffer, pBuild, pLocale, TransformLocale.OLDEST_MCRYPTO); byteheader = true; // Still reset header. mAES.ShiftIVOld(); } else if (pLocale == 1 || pLocale == 2) { // KMS / KMST Decrypt(packetBuffer, pBuild, pLocale, TransformLocale.SPECIAL); } else { // All others lol Decrypt(packetBuffer, pBuild, pLocale, TransformLocale.AES_MCRYPTO); } mCursor -= (packetSize + 4); if (mCursor > 0) { Buffer.BlockCopy(mBuffer, packetSize + 4, mBuffer, 0, mCursor); } ushort opcode; if (byteheader) { opcode = (ushort)(packetBuffer[0]); Buffer.BlockCopy(packetBuffer, 1, packetBuffer, 0, packetSize - 1); Array.Resize(ref packetBuffer, packetSize - 1); } else { opcode = (ushort)(packetBuffer[0] | (packetBuffer[1] << 8)); Buffer.BlockCopy(packetBuffer, 2, packetBuffer, 0, packetSize - 2); Array.Resize(ref packetBuffer, packetSize - 2); } Definition definition = Config.Instance.GetDefinition(pBuild, pLocale, mOutbound, opcode); return(new MaplePacket(pTransmitted, mOutbound, pBuild, pLocale, opcode, definition == null ? "" : definition.Name, packetBuffer)); }
public MaplePacket Read(DateTime pTransmitted) { if (mCursor < _expectedDataSize) { return(null); } if (!mAES.ConfirmHeader(mBuffer, 0)) { mCursor -= lastAddedLength; //return null; throw new Exception("Failed to confirm packet header"); } int headerLength = MapleAES.GetHeaderLength(mBuffer, mCursor, _usesOldHeader); _expectedDataSize = headerLength; if (mCursor < headerLength) { return(null); } int packetSize = MapleAES.GetPacketLength(mBuffer, mCursor, _usesOldHeader); _expectedDataSize = packetSize + headerLength; if (mCursor < (packetSize + headerLength)) { return(null); } byte[] packetBuffer = new byte[packetSize]; Buffer.BlockCopy(mBuffer, headerLength, packetBuffer, 0, packetSize); var preDecodeIV = BitConverter.ToUInt32(mAES.mIV, 0); Decrypt(packetBuffer, _transformMethod); var postDecodeIV = BitConverter.ToUInt32(mAES.mIV, 0); mCursor -= _expectedDataSize; if (mCursor > 0) { Buffer.BlockCopy(mBuffer, _expectedDataSize, mBuffer, 0, mCursor); } ushort opcode; if (_usesByteHeader) { opcode = (ushort)(packetBuffer[0]); Buffer.BlockCopy(packetBuffer, 1, packetBuffer, 0, packetSize - 1); Array.Resize(ref packetBuffer, packetSize - 1); } else { opcode = (ushort)(packetBuffer[0] | (packetBuffer[1] << 8)); Buffer.BlockCopy(packetBuffer, 2, packetBuffer, 0, packetSize - 2); Array.Resize(ref packetBuffer, packetSize - 2); } _expectedDataSize = 4; Definition definition = Config.Instance.GetDefinition(Build, Locale, mOutbound, opcode); return(new MaplePacket(pTransmitted, mOutbound, Build, Locale, opcode, definition == null ? "" : definition.Name, packetBuffer, preDecodeIV, postDecodeIV)); }
public MaplePacket Read(DateTime pTransmitted, bool bMaple2) { if (mCursor < _expectedDataSize) { return(null); } if (bMaple2) { if (DecodeSeqBase(IV) != Build) { //throw new Exception("Failed to confirm packet header"); Console.WriteLine("ERROR: Failed to confirm packet header!"); } } else { if (!mAES.ConfirmHeader(mBuffer, 0)) { throw new Exception("Failed to confirm packet header"); } } int headerLength = bMaple2 ? 6 : MapleAES.GetHeaderLength(mBuffer, mCursor, _usesOldHeader); _expectedDataSize = headerLength; if (mCursor < headerLength) { return(null); } int packetSize = MapleAES.GetPacketLength(mBuffer, mCursor, _usesOldHeader, bMaple2); _expectedDataSize = packetSize + headerLength; if (mCursor < (packetSize + headerLength)) { return(null); } byte[] packetBuffer = new byte[packetSize]; Buffer.BlockCopy(mBuffer, headerLength, packetBuffer, 0, packetSize); uint preDecodeIV; if (bMaple2) { preDecodeIV = IV; mCrypt.Decrypt(packetBuffer, packetSize, BlockIV, IV); ShiftIV(); } else { preDecodeIV = BitConverter.ToUInt32(mAES.mIV, 0); Decrypt(packetBuffer, _transformMethod); } var postDecodeIV = bMaple2 ? IV : BitConverter.ToUInt32(mAES.mIV, 0); mCursor -= _expectedDataSize; if (mCursor > 0) { Buffer.BlockCopy(mBuffer, _expectedDataSize, mBuffer, 0, mCursor); } ushort opcode; if (_usesByteHeader) { opcode = (ushort)(packetBuffer[0]); Buffer.BlockCopy(packetBuffer, 1, packetBuffer, 0, packetSize - 1); Array.Resize(ref packetBuffer, packetSize - 1); } else { opcode = (ushort)(packetBuffer[0] | (packetBuffer[1] << 8)); Buffer.BlockCopy(packetBuffer, 2, packetBuffer, 0, packetSize - 2); Array.Resize(ref packetBuffer, packetSize - 2); } _expectedDataSize = bMaple2 ? 6 : 4; Definition definition = Config.Instance.GetDefinition(Build, Locale, mOutbound, opcode); return(new MaplePacket(pTransmitted, mOutbound, Build, Locale, opcode, definition == null ? "" : definition.Name, packetBuffer, preDecodeIV, postDecodeIV)); }
public MaplePacket Read(DateTime pTransmitted) { if (mCursor < _expectedDataSize) { return(null); } if (!mAES.ConfirmHeader(mBuffer, 0)) { throw new Exception("Failed to confirm packet header"); } int headerLength = MapleAES.GetHeaderLength(mBuffer, mCursor, _usesOldHeader); _expectedDataSize = headerLength; if (mCursor < headerLength) { return(null); } int packetSize = MapleAES.GetPacketLength(mBuffer, mCursor, _usesOldHeader); _expectedDataSize = packetSize + headerLength; if (mCursor < (packetSize + headerLength)) { return(null); } byte[] packetBuffer = new byte[packetSize]; Buffer.BlockCopy(mBuffer, headerLength, packetBuffer, 0, packetSize); var preDecodeIV = BitConverter.ToUInt32(mAES.mIV, 0); Decrypt(packetBuffer, _transformMethod); var postDecodeIV = BitConverter.ToUInt32(mAES.mIV, 0); mCursor -= _expectedDataSize; if (mCursor > 0) { Buffer.BlockCopy(mBuffer, _expectedDataSize, mBuffer, 0, mCursor); } ushort opcode; if (_usesByteHeader) { opcode = (ushort)(packetBuffer[0]); Buffer.BlockCopy(packetBuffer, 1, packetBuffer, 0, packetSize - 1); Array.Resize(ref packetBuffer, packetSize - 1); } else { opcode = (ushort)(packetBuffer[0] | (packetBuffer[1] << 8)); Buffer.BlockCopy(packetBuffer, 2, packetBuffer, 0, packetSize - 2); Array.Resize(ref packetBuffer, packetSize - 2); } _expectedDataSize = 4; Definition definition = null; // Detect OpcodeEncryption Packet if (packetBuffer.Length == (8 + 1 + short.MaxValue)) // 32776 { int blockSize = BitConverter.ToInt32(packetBuffer, 0); int bufferSize = BitConverter.ToInt32(packetBuffer, 4); if (blockSize == 4 && bufferSize == (1 + short.MaxValue)) { Console.WriteLine("Recv OpcodeEncryption Packet with Opcode:0x{0:X} | BlockSize:{1} | BufferSize:{2}", opcode, blockSize, bufferSize); OpcodeEncryptedEventArgs recvOpcodeEventArgs = new OpcodeEncryptedEventArgs(); recvOpcodeEventArgs.OpcodeEncrypted = true; recvOpcodeEventArgs.EncryptedOpcodes = PopulateEncryptedOpcode(packetBuffer, bufferSize); OnRecvOpcodeEncryptPacket(recvOpcodeEventArgs); // Generate OpcodeEncryption packet { definition = Config.Instance.GetDefinition(Build, Locale, mOutbound, opcode); if (definition == null) { definition = new Definition(); definition.Outbound = mOutbound; definition.Locale = Locale; definition.Opcode = opcode; definition.Name = "Opcode Encryption"; definition.Build = Build; DefinitionsContainer.Instance.SaveDefinition(definition); } var filename = Helpers.GetScriptPath(Locale, Build, mOutbound, opcode); Helpers.MakeSureFileDirectoryExists(filename); // Create main script if (!File.Exists(filename)) { string contents = @" using (ScriptAPI) { AddInt(""Block Size""); AddInt(""Buffer Size""); AddByte(""Unknown""); AddField(""Buffer With Encrypted Opcodes"", 32767); } "; File.WriteAllText(filename, contents); } } } } // Get the real opcode if (OpcodeEncrypted && mOutbound && EncryptedOpcodes != null && EncryptedOpcodes.ContainsKey(opcode)) { opcode = EncryptedOpcodes[opcode].RealOp; } definition = Config.Instance.GetDefinition(Build, Locale, mOutbound, opcode); return(new MaplePacket(pTransmitted, mOutbound, Build, Locale, opcode, definition == null ? "" : definition.Name, packetBuffer, preDecodeIV, postDecodeIV)); }