Esempio n. 1
0
        private void OnRecvOpcodeEncryptPacket(OpcodeEncryptedEventArgs e)
        {
            EventHandler <OpcodeEncryptedEventArgs> handler = RecvOpcodeEncryptPacket;

            handler?.Invoke(this, e);
        }
Esempio n. 2
0
 private void MapleStream_RecvOpcodeEncryptPacket(object sender, OpcodeEncryptedEventArgs e)
 {
     mOutboundStream.OpcodeEncrypted  = e.OpcodeEncrypted;
     mOutboundStream.EncryptedOpcodes = e.EncryptedOpcodes;
 }
Esempio n. 3
0
        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));
        }