コード例 #1
0
ファイル: MapleStream.cs プロジェクト: anhtanh95/MapleShark
        Dictionary <int, EncryptedOpcode> PopulateEncryptedOpcode(byte[] packetBuffer, int bufferSize)
        {
            Dictionary <int, EncryptedOpcode> encryptedOpcodes = new Dictionary <int, EncryptedOpcode>();

            byte[] aData = new byte[bufferSize];
            Buffer.BlockCopy(packetBuffer, 8, aData, 0, bufferSize);

            byte[] aKey       = new byte[24];
            byte[] aSecretKey = Encoding.ASCII.GetBytes("M@PleStoryMaPLe!");
            Buffer.BlockCopy(aSecretKey, 0, aKey, 0, aSecretKey.Length);
            Buffer.BlockCopy(aSecretKey, 0, aKey, 16, 8);

            string sOpcode = TripleDESCipher.Decrypt(aData, aKey);

            int opcodeBeginIdx = 0;              // this should be ClientPacket.UserBegin
            int opcodeCountIdx = sOpcode.Length; // this should be ClientPacket.Count
            int loopStep       = 4;              // this should be 1

            for (int i = opcodeBeginIdx; i < opcodeCountIdx; i += loopStep)
            {
                string enOpString = sOpcode.Substring(i, 4);
                short  realIdx    = (short)(i / loopStep);
                byte[] aRawData   = Encoding.ASCII.GetBytes(enOpString);
                string data       = BitConverter.ToString(aRawData).Replace('-', ' ');
                if (int.TryParse(enOpString, out int encryptOp))
                {
                    if (encryptedOpcodes.ContainsKey(encryptOp))
                    {
                        Console.WriteLine("Detected duplicate opcode 0x{0:X}. Break the loop.", encryptOp);
                        break;
                    }
                    else
                    {
                        // This is for temporary and need to more handle. We should set default value of 'realOp' as negative value to determine this opcode as decrypted but undefined yet.
                        ushort          realOp          = (ushort)(realIdx + 200);
                        EncryptedOpcode encryptedOpcode = new EncryptedOpcode(encryptOp, aRawData, realIdx, realOp);
                        encryptedOpcodes.Add(encryptOp, encryptedOpcode);
                        Console.WriteLine("OpcodeEncryption [{0}] -> 0x{1:X4} | Index:{2}", data, encryptOp, realIdx);
                    }
                }
                else
                {
                    //Console.WriteLine("Parse Failed: [{0}] | Index:{1}", data, realIdx);
                    break;
                }
            }
            return(encryptedOpcodes);
        }
コード例 #2
0
        private void ProcessTCPPacket(TcpPacket pTCPPacket, ref uint pSequence, Dictionary <uint, byte[]> pBuffer, MapleStream pStream, DateTime pArrivalDate)
        {
            if (pTCPPacket.SequenceNumber > pSequence)
            {
                byte[] data;

                while (pBuffer.TryGetValue(pSequence, out data))
                {
                    pBuffer.Remove(pSequence);
                    pStream.Append(data);
                    pSequence += (uint)data.Length;
                }
                if (pTCPPacket.SequenceNumber > pSequence)
                {
                    pBuffer[(uint)pTCPPacket.SequenceNumber] = pTCPPacket.PayloadData;
                }
            }
            if (pTCPPacket.SequenceNumber < pSequence)
            {
                int difference = (int)(pSequence - pTCPPacket.SequenceNumber);
                if (difference > 0)
                {
                    byte[] data = pTCPPacket.PayloadData;
                    if (data.Length > difference)
                    {
                        pStream.Append(data, difference, data.Length - difference);
                        pSequence += (uint)(data.Length - difference);
                    }
                }
            }
            else if (pTCPPacket.SequenceNumber == pSequence)
            {
                byte[] data = pTCPPacket.PayloadData;
                pStream.Append(data);
                pSequence += (uint)data.Length;
            }

            MaplePacket packet;
            bool        refreshOpcodes = false;

            try
            {
                mPacketList.BeginUpdate();

                while ((packet = pStream.Read(pArrivalDate)) != null)
                {
                    if (Locale == MapleLocale.GLOBAL && Build >= 193)
                    {
                        if (packet.Outbound)
                        {
                            if (isOpcodeTableLoaded)
                            {
                                ushort realOpcode = 0;

                                if (mOpcodeTable.TryGetValue(packet.Opcode, out realOpcode))
                                {
                                    packet.Opcode = realOpcode;
                                }

                                packet.SubItems[3].Text = $"0x{packet.Opcode.ToString("X4")}";
                            }
                        }
                        else
                        {
                            if (!isOpcodeTableLoaded && packet.Opcode == HeaderTableOpcode)
                            {
                                bool healthy = false;
                                int  blockSize, length = 0;

                                healthy = packet.ReadInt(out blockSize);
                                healthy = packet.ReadInt(out length);

                                byte[] buffer = new byte[length];
                                byte[] key    = Encoding.ASCII.GetBytes("M@PleStoryMaPLe!");

                                healthy = packet.ReadBytes(buffer);

                                if (healthy)
                                {
                                    string opcodes = TripleDESCipher.Decrypt(buffer, key);

                                    for (ushort i = 0; i < 0x0A7F - Begin_User; i++)
                                    {
                                        if (i * 4 + 4 <= opcodes.Length)
                                        {
                                            string sOpcode = opcodes.Substring(i * 4, 4);

                                            ushort uOpcode;
                                            if (!UInt16.TryParse(sOpcode, out uOpcode))
                                            {
                                                break;
                                            }

                                            mOpcodeTable.Add(uOpcode, (ushort)(Begin_User + i));
                                        }
                                        else
                                        {
                                            healthy = false;
                                        }
                                    }
                                }

                                if (healthy)
                                {
                                    isOpcodeTableLoaded = true;
                                }
                            }
                        }
                    }

                    AddPacket(packet);
                    Definition definition = Config.Instance.GetDefinition(mBuild, mLocale, packet.Outbound, packet.Opcode);
                    if (!mOpcodes.Exists(op => op.Outbound == packet.Outbound && op.Header == packet.Opcode))
                    {
                        mOpcodes.Add(new Opcode(packet.Outbound, packet.Opcode));
                        refreshOpcodes = true;
                    }
                    if (definition != null && !mViewIgnoredMenu.Checked && definition.Ignore)
                    {
                        continue;
                    }
                    if (packet.Outbound && !mViewOutboundMenu.Checked)
                    {
                        continue;
                    }
                    if (!packet.Outbound && !mViewInboundMenu.Checked)
                    {
                        continue;
                    }

                    mPacketList.Items.Add(packet);
                    if (mPacketList.SelectedItems.Count == 0)
                    {
                        packet.EnsureVisible();
                    }
                }

                mPacketList.EndUpdate();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                mTerminated = true;
                Text       += " (Terminated)";
                //MainForm.CloseSession(this);
                return;
            }

            if (DockPanel != null && DockPanel.ActiveDocument == this && refreshOpcodes)
            {
                MainForm.SearchForm.RefreshOpcodes(true);
            }
        }