Beispiel #1
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)
                {
                    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);
            }
        }
Beispiel #2
0
        internal Results BufferTCPPacket_MS2(TcpPacket pTCPPacket, DateTime pArrivalTime)
        {
            if (pTCPPacket.Fin || pTCPPacket.Rst)
            {
                mTerminated = true;
                Text       += " (Terminated)";

                return(mPackets.Count == 0 ? Results.CloseMe : Results.Terminated);
            }
            if (pTCPPacket.Syn && !pTCPPacket.Ack)
            {
                mLocalPort        = (ushort)pTCPPacket.SourcePort;
                mRemotePort       = (ushort)pTCPPacket.DestinationPort;
                mOutboundSequence = (uint)(pTCPPacket.SequenceNumber + 1);
                Text      = "Port " + mLocalPort + " - " + mRemotePort;
                startTime = DateTime.Now;

                try
                {
                    mRemoteEndpoint = ((PacketDotNet.IPv4Packet)pTCPPacket.ParentPacket).SourceAddress.ToString() + ":" + pTCPPacket.SourcePort.ToString();
                    mLocalEndpoint  = ((PacketDotNet.IPv4Packet)pTCPPacket.ParentPacket).DestinationAddress.ToString() + ":" + pTCPPacket.DestinationPort.ToString();
                    Console.WriteLine("[CONNECTION] From {0} to {1}", mRemoteEndpoint, mLocalEndpoint);

                    return(Results.Continue);
                }
                catch
                {
                    return(Results.CloseMe);
                }
            }
            if (pTCPPacket.Syn && pTCPPacket.Ack)
            {
                mInboundSequence = (uint)(pTCPPacket.SequenceNumber + 1); return(Results.Continue);
            }
            if (pTCPPacket.PayloadData.Length == 0)
            {
                return(Results.Continue);
            }
            if (mBuild == 0)
            {
                byte[] tcpData = pTCPPacket.PayloadData;

                if (pTCPPacket.SourcePort == mLocalPort)
                {
                    mOutboundSequence += (uint)tcpData.Length;
                }
                else
                {
                    mInboundSequence += (uint)tcpData.Length;
                }

                byte[] headerData = new byte[tcpData.Length];
                Buffer.BlockCopy(tcpData, 0, headerData, 0, tcpData.Length);

                PacketReader pr     = new PacketReader(headerData);
                ushort       rawSeq = pr.ReadUShort();
                int          length = pr.ReadInt();
                if (headerData.Length - 6 < length)
                {
                    Console.WriteLine("Connection on port {0} did not have a MapleStory2 Handshake", mLocalEndpoint);
                    return(Results.CloseMe);
                }

                ushort header = pr.ReadUShort();
                if (header != 1)//RequestVersion
                {
                    Console.WriteLine("Connection on port {0} did not have a valid MapleStory2 Connection Header", mLocalEndpoint);
                    return(Results.CloseMe);
                }
                uint version  = pr.ReadUInt();
                uint localIV  = pr.ReadUInt();
                uint remoteIV = pr.ReadUInt();
                uint blockIV  = pr.ReadUInt();
                byte ignored  = pr.ReadByte();

                mBuild         = version;
                mLocale        = 0;//TODO: Handle regions somehow since handshake doesn't contain it
                mPatchLocation = "MST";

                mOutboundStream = new MapleStream(true, rawSeq, mBuild, localIV, blockIV);
                mInboundStream  = new MapleStream(false, rawSeq, mBuild, remoteIV, blockIV);

                // Another packet was sent with handshake...
                if (pr.Remaining > 0)
                {
                    // Buffer it since it is encrypted
                    mInboundStream.Append(pr.ReadBytes(pr.Remaining));
                }

                // Generate HandShake packet
                Definition definition = Config.Instance.GetDefinition(mBuild, mLocale, false, header);
                if (definition == null)
                {
                    definition          = new Definition();
                    definition.Outbound = false;
                    definition.Locale   = mLocale;
                    definition.Opcode   = header;
                    definition.Name     = "RequestVersion";
                    definition.Build    = mBuild;
                    SaveDefinition(definition);
                }

                {
                    var filename = Helpers.GetScriptPath(mLocale, mBuild, false, header);
                    Helpers.MakeSureFileDirectoryExists(filename);

                    // Create main script
                    if (!File.Exists(filename))
                    {
                        string contents = @"
using (ScriptAPI) {
    AddShort(""Raw Sequence"");
    AddField(""Packet Length"", 4);
    AddShort(""Opcode"");
    AddField(""MapleStory2 Version"", 4);
    AddField(""Local Initializing Vector (IV)"", 4);
    AddField(""Remote Initializing Vector (IV)"", 4);
    AddField(""Block Initializing Vector (IV)"", 4);
}
";
                        File.WriteAllText(filename, contents);
                    }
                }

                // Initial TCP packet may not be split up properly, copy only handshake portion
                byte[] handshakePacketData = new byte[6 + length];
                Buffer.BlockCopy(tcpData, 0, handshakePacketData, 0, length + 6);

                MaplePacket packet = new MaplePacket(pArrivalTime, false, mBuild, mLocale, header, definition == null ? "" : definition.Name, handshakePacketData, (uint)0, remoteIV);
                if (!mOpcodes.Exists(op => op.Outbound == packet.Outbound && op.Header == packet.Opcode)) // Should be false, but w/e
                {
                    mOpcodes.Add(new Opcode(packet.Outbound, packet.Opcode));
                }

                mPacketList.Items.Add(packet);
                AddPacket(packet);

                Console.WriteLine("[CONNECTION] MapleStory2 V{2}", mLocalEndpoint, mRemoteEndpoint, mBuild);

                ProcessTCPPacket(pTCPPacket, ref mInboundSequence, mInboundBuffer, mInboundStream, pArrivalTime);
                return(Results.Show);
            }
            if (pTCPPacket.SourcePort == mLocalPort)
            {
                ProcessTCPPacket(pTCPPacket, ref mOutboundSequence, mOutboundBuffer, mOutboundStream, pArrivalTime);
            }
            else
            {
                ProcessTCPPacket(pTCPPacket, ref mInboundSequence, mInboundBuffer, mInboundStream, pArrivalTime);
            }
            return(Results.Continue);
        }
Beispiel #3
0
        private void ProcessTCPPacket(TcpPacket pTCPPacket, ref uint pSequence, Dictionary<uint, byte[]> pBuffer, MapleStream pStream, DateTime pArrivalDate)
        {
            if (pTCPPacket.SequenceNumber > pSequence)
            {
                byte[] data;
                while ((data = pBuffer.GetOrDefault(pSequence, null)) != null)
                {
                    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
            {
                while ((packet = pStream.Read(pArrivalDate, mBuild, mLocale)) != null)
                {
                    mPackets.Add(packet);
                    Definition definition = Config.Instance.GetDefinition(mBuild, mLocale, packet.Outbound, packet.Opcode);
                    if (!mOpcodes.Exists(kv => kv.First == packet.Outbound && kv.Second == packet.Opcode))
                    {
                        mOpcodes.Add(new Pair<bool, ushort>(packet.Outbound, packet.Opcode));
                        refreshOpcodes = true;
                    }
                    if (definition != null && !mViewIgnoredMenu.Checked && definition.Ignore) continue;
                    mPacketList.Items.Add(packet);
                    if (mPacketList.SelectedItems.Count == 0) packet.EnsureVisible();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                mTerminated = true;
                Text += " (Terminated)";
                //MainForm.CloseSession(this);
                return;
            }

            if (DockPanel.ActiveDocument == this && refreshOpcodes) MainForm.SearchForm.RefreshOpcodes(true);
        }
Beispiel #4
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);
            }
        }
        private void ProcessTCPPacket(TcpPacket pTCPPacket, ref uint pSequence, Dictionary <uint, byte[]> pBuffer, MapleStream pStream, DateTime pArrivalDate, bool isInbound)
        {
            /*
             *
             * if (pTCPPacket.SequenceNumber > pSequence)
             * {
             *  byte[] data;
             *  while ((data = pBuffer.GetOrDefault(pSequence, null)) != null)
             *  {
             *      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;
            if (isInbound)
            {
                pStream.Append(data, ref inboundBuffer);
            }
            else
            {
                pStream.Append(data, ref outboundBuffer);
            }
            //pSequence += (uint)data.Length;
            // }

            MaplePacket packet;
            bool        refreshOpcodes = false;

            try
            {
                while ((isInbound ? (packet = pStream.Read(pArrivalDate, mBuild, mLocale, ref isFirstPacket, ref inboundBuffer, ref curInboundIV)) : (packet = pStream.Read(pArrivalDate, mBuild, mLocale, ref isFirstPacket, ref outboundBuffer, ref curOutboundIV))) != null)
                {
                    Console.WriteLine("OK");
                    Console.WriteLine(" ");
                    mPackets.Add(packet);
                    Definition definition = Config.Instance.GetDefinition(mBuild, mLocale, packet.Outbound, packet.Opcode);
                    if (!mOpcodes.Exists(kv => kv.First == packet.Outbound && kv.Second == packet.Opcode))
                    {
                        mOpcodes.Add(new Pair <bool, ushort>(packet.Outbound, packet.Opcode));
                        refreshOpcodes = true;
                    }
                    //if (definition != null && !mViewIgnoredMenu.Checked && definition.Ignore) continue;
                    mPacketList.Items.Add(packet);
                    if (mPacketList.SelectedItems.Count == 0)
                    {
                        packet.EnsureVisible();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                mTerminated = true;
                Text       += " (Terminated)";
                //MainForm.CloseSession(this);

                return;
            }

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