public void SaveDefinition(Definition pDefinition)
        {
            if (!_definitions.ContainsKey(pDefinition.Locale)) _definitions.Add(pDefinition.Locale, new Dictionary<ushort, List<Definition>>());
            if (!_definitions[pDefinition.Locale].ContainsKey(pDefinition.Build)) _definitions[pDefinition.Locale].Add(pDefinition.Build, new List<Definition>());

            _definitions[pDefinition.Locale][pDefinition.Build].RemoveAll(d => {
                return d.Outbound == pDefinition.Outbound && d.Opcode == pDefinition.Opcode;
            });

            _definitions[pDefinition.Locale][pDefinition.Build].Add(pDefinition);
        }
Esempio n. 2
0
        private void AddOpcode(ushort pBuild, byte pLocale, bool pOutbound, ushort pOpcode, string pName)
        {
            Definition def = Config.Instance.GetDefinition(pBuild, pLocale, pOutbound, pOpcode);
            if (def == null)
            {
                def = new Definition();
                txtLog.AppendText(string.Format("Adding opcode {1}: 0x{0:X4}\r\n", pOpcode, pName));
            }
            else
            {
                txtLog.AppendText(string.Format("Replacing opcode {1} 0x{0:X4} for {2}\r\n", pOpcode, def.Name, pName));
            }

            def.Build = pBuild;
            def.Locale = pLocale;
            def.Opcode = pOpcode;
            def.Outbound = pOutbound;
            def.Name = pName;
            def.Ignore = false;

            DefinitionsContainer.Instance.SaveDefinition(def);
        }
Esempio n. 3
0
        private void mPacketContextNameBox_KeyDown(object pSender, KeyEventArgs pArgs)
        {
            if (pArgs.Modifiers == Keys.None && pArgs.KeyCode == Keys.Enter)
            {
                MaplePacket packet = mPacketList.SelectedItems[0] as MaplePacket;
                Definition definition = Config.Instance.GetDefinition(mBuild, mLocale, packet.Outbound, packet.Opcode);
                if (definition == null)
                {
                    definition = new Definition();
                    definition.Build = mBuild;
                    definition.Outbound = packet.Outbound;
                    definition.Opcode = packet.Opcode;
                    definition.Locale = mLocale;
                }
                definition.Name = mPacketContextNameBox.Text;
                DefinitionsContainer.Instance.SaveDefinition(definition);
                pArgs.SuppressKeyPress = true;
                mPacketContextMenu.Close();
                RefreshPackets();

                packet.EnsureVisible();
            }
        }
Esempio n. 4
0
        private void mPacketContextIgnoreMenu_CheckedChanged(object pSender, EventArgs pArgs)
        {
            if (openingContextMenu) return;
            MaplePacket packet = mPacketList.SelectedItems[0] as MaplePacket;
            Definition definition = Config.Instance.GetDefinition(mBuild, mLocale, packet.Outbound, packet.Opcode);
            if (definition == null)
            {
                definition = new Definition();
                definition.Locale = mLocale;
                definition.Build = mBuild;
                definition.Outbound = packet.Outbound;
                definition.Opcode = packet.Opcode;
                definition.Locale = mLocale;
            }
            definition.Ignore = mPacketContextIgnoreMenu.Checked;
            DefinitionsContainer.Instance.SaveDefinition(definition);

            int newIndex = packet.Index - 1;
            for (var i = packet.Index - 1; i > 0; i--)
            {
                var pack = mPacketList.Items[i] as MaplePacket;
                var def = Config.Instance.GetDefinition(mBuild, mLocale, pack.Outbound, pack.Opcode);
                if (def == definition)
                {
                    newIndex--;
                }
            }

            RefreshPackets();

            if (newIndex != 0 && mPacketList.Items[newIndex] != null)
            {
                packet = mPacketList.Items[newIndex] as MaplePacket;
                packet.Selected = true;
                packet.EnsureVisible();
            }
        }
Esempio n. 5
0
        internal Results BufferTCPPacket(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;

                ushort length = (ushort)(BitConverter.ToUInt16(tcpData, 0) + 2);
                byte[] headerData = new byte[tcpData.Length];
                Buffer.BlockCopy(tcpData, 0, headerData, 0, tcpData.Length);

                bool mIsKMS = false;

                PacketReader pr = new PacketReader(headerData);

                if (length != tcpData.Length || tcpData.Length < 13)
                {
                    if (socks5 > 0 && socks5 < 7)
                    {
                        if (pr.ReadByte() == 5 && pr.ReadByte() == 1)
                        {
                            pr.ReadByte();
                            mProxyEndpoint = mLocalEndpoint;
                            mLocalEndpoint = "";
                            switch (pr.ReadByte())
                            {
                                case 1://IPv4
                                    for (int i = 0; i < 4; i++)
                                    {
                                        mLocalEndpoint += pr.ReadByte();
                                        if (i < 3)
                                        {
                                            mLocalEndpoint += ".";
                                        }
                                    }
                                    break;
                                case 3://Domain
                                    //readInt - String Length
                                    //readAsciiString - Address
                                    break;
                                case 4://IPv6
                                    for (int i = 0; i < 16; i++)
                                    {
                                        pr.ReadByte();
                                    }
                                    break;
                            }
                            byte[] ports = new byte[2];
                            for (int i = 1; i >= 0; i--)
                            {
                                ports[i] = pr.ReadByte();
                            }
                            PacketReader portr = new PacketReader(ports);
                            mProxyPort = mRemotePort;
                            mRemotePort = portr.ReadUShort();
                            mLocalEndpoint += ":" + mRemotePort;
                            Text = "Port " + mLocalPort + " - " + mRemotePort + "(Proxy" + mProxyPort + ")";
                            Console.WriteLine("[socks5] From {0} to {1} (Proxy {2})", mRemoteEndpoint, mLocalEndpoint, mProxyEndpoint);
                        }
                        socks5++;
                        return Results.Continue;
                    }
                    else if (tcpData.Length == 3 && pr.ReadByte() == 5)
                    {
                        socks5 = 1;
                        return Results.Continue;
                    }
                    Console.WriteLine("Connection on port {0} did not have a MapleStory Handshake", mLocalEndpoint);
                    return Results.CloseMe;
                }

                pr.ReadUShort();
                ushort version = pr.ReadUShort();
                byte subVersion = 1;
                string patchLocation = pr.ReadMapleString();
                byte[] localIV = pr.ReadBytes(4);
                byte[] remoteIV = pr.ReadBytes(4);
                byte serverLocale = pr.ReadByte();

                if (serverLocale > 0x12)
                {
                    return Results.CloseMe;
                }

                if (serverLocale == 0x02 || (serverLocale == 0x01 && version > 255)) mIsKMS = true;
                else mIsKMS = false;

                if (mIsKMS)
                {
                    int test = int.Parse(patchLocation);
                    version = (ushort)(test & 0x7FFF);
                    subVersion = (byte)((test >> 16) & 0xFF);
                }
                else if (patchLocation.All(character => { return character >= '0' && character <= '9'; }))
                {
                    if (!byte.TryParse(patchLocation, out subVersion))
                        Console.WriteLine("Failed to parse subVersion");
                }

                mBuild = version;

                mLocale = serverLocale;
                mPatchLocation = patchLocation;

                mOutboundStream = new MapleStream(true, mBuild, mLocale, localIV, subVersion);
                mInboundStream = new MapleStream(false, mBuild, mLocale, remoteIV, subVersion);

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

                {
                    string filename = "Scripts" +
                        Path.DirectorySeparatorChar + mLocale.ToString() +
                        Path.DirectorySeparatorChar + mBuild.ToString() +
                        Path.DirectorySeparatorChar + "Inbound" +
                        Path.DirectorySeparatorChar + "0xFFFF.txt";
                    if (!Directory.Exists(Path.GetDirectoryName(filename))) Directory.CreateDirectory(Path.GetDirectoryName(filename));
                    if (!File.Exists(filename))
                    {
                        string contents = "";
                        contents += "using (ScriptAPI) {\r\n";
                        contents += "\tAddShort(\"Packet Size\");\r\n";
                        contents += "\tAddUShort(\"MapleStory Version\");\r\n";
                        contents += "\tAddString(\"MapleStory Patch Location/Subversion\");\r\n";
                        contents += "\tAddField(\"Local Initializing Vector (IV)\", 4);\r\n";
                        contents += "\tAddField(\"Remote Initializing Vector (IV)\", 4);\r\n";
                        contents += "\tAddByte(\"MapleStory Locale\");\r\n";
                        if (mRemotePort == 8484 && ((mLocale == MapleLocale.GLOBAL && version >= 160) ||
                                                    (mLocale == MapleLocale.TAIWAN && version >= 176) ||
                                                    (mLocale == MapleLocale.CHINA && version >= 122)))
                            contents += "\tAddByte(\"Unknown\");\r\n";
                        contents += "}";
                        File.WriteAllText(filename, contents);
                    }
                }

                MaplePacket packet = new MaplePacket(pArrivalTime, false, mBuild, mLocale, 0xFFFF, definition == null ? "" : definition.Name, tcpData, (uint)0, BitConverter.ToUInt32(remoteIV, 0));
                if (!mOpcodes.Exists(kv => kv.First == packet.Outbound && kv.Second == packet.Opcode)) // Should be false, but w/e
                {
                    mOpcodes.Add(new Pair<bool, ushort>(packet.Outbound, packet.Opcode));
                }

                mPacketList.Items.Add(packet);
                mPackets.Add(packet);
                MainForm.SearchForm.RefreshOpcodes(true);
                Console.WriteLine("[CONNECTION] MapleStory V{2}.{3} Locale {4}", mLocalEndpoint, mRemoteEndpoint, mBuild, subVersion, serverLocale);

            }
            if (pTCPPacket.SourcePort == mLocalPort) ProcessTCPPacket(pTCPPacket, ref mOutboundSequence, mOutboundBuffer, mOutboundStream, pArrivalTime);
            else ProcessTCPPacket(pTCPPacket, ref mInboundSequence, mInboundBuffer, mInboundStream, pArrivalTime);
            return Results.Continue;
        }
Esempio n. 6
0
 private void mPacketContextIgnoreMenu_CheckedChanged(object pSender, EventArgs pArgs)
 {
     MaplePacket packet = mPacketList.SelectedItems[0] as MaplePacket;
     Definition definition = Config.Instance.GetDefinition(mBuild, mLocale, packet.Outbound, packet.Opcode);
     if (definition == null)
     {
         definition = new Definition();
         definition.Locale = mLocale;
         definition.Build = mBuild;
         definition.Outbound = packet.Outbound;
         definition.Opcode = packet.Opcode;
         definition.Locale = mLocale;
         Config.Instance.Definitions.Add(definition);
     }
     definition.Ignore = mPacketContextIgnoreMenu.Checked;
     Config.Instance.Save();
     RefreshPackets();
 }
Esempio n. 7
0
        internal Results BufferTCPPacket(TcpPacket pTCPPacket, DateTime pArrivalTime)
        {
            if (pTCPPacket.Fin || pTCPPacket.Rst)
            {
                mTerminated = true;
                Text += " (Terminated)";
                if (mPackets.Count == 0)
                {
                    // f**k
                    return Results.CloseMe;
                }
                else
                {
                    return Results.Terminated;
                }
            }
            if (pTCPPacket.Syn && !pTCPPacket.Ack)
            {
                mLocalPort = (ushort)pTCPPacket.SourcePort;
                mRemotePort = (ushort)pTCPPacket.DestinationPort;
                mOutboundSequence = (uint)(pTCPPacket.SequenceNumber + 1);
                Text = "Port " + mLocalPort.ToString();
                startTime = DateTime.Now;

                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}", mLocalEndpoint, mRemoteEndpoint);

                return Results.Continue;
            }
            if (pTCPPacket.Syn && pTCPPacket.Ack) { mInboundSequence = (uint)(pTCPPacket.SequenceNumber + 1); return Results.Continue; }
            if (pTCPPacket.PayloadData.Length == 0) return Results.Continue;
            if (mBuild == 0)
            {
                if (pTCPPacket.PayloadData.Length < 13) return Results.CloseMe;
                byte[] tcpData = pTCPPacket.PayloadData;
                //mBuild = (ushort)(tcpData[2] | (tcpData[3] << 8));

                bool mIsKMS = false;

                PacketReader pr = new PacketReader(tcpData);
                pr.ReadShort();
                ushort version = pr.ReadUShort();
                var pos = pr.Position;
                {
                    var shrt = pr.ReadShort();
                    if (shrt < 0 || shrt > 0x0020)
                    {
                        return Results.CloseMe;
                    }
                }
                pr.Reset(pos);
                string patchLocation = pr.ReadMapleString();
                byte[] localIV = pr.ReadBytes(4);
                byte[] remoteIV = pr.ReadBytes(4);
                byte serverLocale = pr.ReadByte();

                if (pr.Remaining > 0 || serverLocale > 0x12)
                {
                    return Results.CloseMe;
                }

                if (serverLocale == 0x02 || (serverLocale == 0x01 && version > 255)) mIsKMS = true;
                else mIsKMS = false;

                if (mIsKMS)
                {
                    int test = int.Parse(patchLocation);
                    ushort t1 = (ushort)(test & 0x7FFF);
                    int t2 = (test >> 15) & 1;
                    int t3 = (test >> 16) & 0xFF;
                    Console.WriteLine("Logging KMS connection. Version {0} | {1} | {2}", t1, t2, t3);
                    mBuild = t1;
                }
                else
                {
                    mBuild = version;
                }

                mLocale = serverLocale;
                mPatchLocation = patchLocation;

                mOutboundStream = new MapleStream(true, mBuild, mLocale, localIV);
                mInboundStream = new MapleStream(false, (ushort)(0xFFFF - mBuild), mLocale, remoteIV);
                mInboundSequence += (uint)tcpData.Length;

                // Generate HandShake packet
                Definition definition = Config.Instance.GetDefinition(mBuild, mLocale, false, 0xFFFF);
                if (definition == null)
                {
                    definition = new Definition();
                    definition.Outbound = false;
                    definition.Locale = mLocale;
                    definition.Opcode = 0xFFFF;
                    definition.Name = "Maple Handshake";
                    definition.Build = mBuild;
                    Config.Instance.Definitions.Add(definition);
                }

                {
                    string filename = "Scripts" +
                        Path.DirectorySeparatorChar + mLocale.ToString() +
                        Path.DirectorySeparatorChar + mBuild.ToString() +
                        Path.DirectorySeparatorChar + "Inbound" +
                        Path.DirectorySeparatorChar + "0xFFFF.txt";
                    if (!Directory.Exists(Path.GetDirectoryName(filename))) Directory.CreateDirectory(Path.GetDirectoryName(filename));
                    if (!File.Exists(filename))
                    {
                        string contents = "";
                        contents += "using (ScriptAPI) {\r\n";
                        contents += "\tAddShort(\"Packet Size\");\r\n";
                        contents += "\tAddUShort(\"MapleStory Version\");\r\n";
                        contents += "\tAddString(\"MapleStory Patch Location\");\r\n";
                        contents += "\tAddField(\"Local Initializing Vector (IV)\", 4);\r\n";
                        contents += "\tAddField(\"Remote Initializing Vector (IV)\", 4);\r\n";
                        contents += "\tAddByte(\"MapleStory Locale\");\r\n";
                        contents += "}";
                        File.WriteAllText(filename, contents);
                    }
                }

                MaplePacket packet = new MaplePacket(pArrivalTime, false, mBuild, mLocale, 0xFFFF, definition == null ? "" : definition.Name, tcpData);
                if (!mOpcodes.Exists(kv => kv.First == packet.Outbound && kv.Second == packet.Opcode))
                { // Should be false, but w/e
                    mOpcodes.Add(new Pair<bool, ushort>(packet.Outbound, packet.Opcode));
                }

                mPacketList.Items.Add(packet);
                mPackets.Add(packet);
                MainForm.SearchForm.RefreshOpcodes(true);
                Console.WriteLine("[CONNECTION] MapleStory V{2}.{3} Locale {4}", mLocalEndpoint, mRemoteEndpoint, mBuild, patchLocation, serverLocale);
            }
            if (pTCPPacket.SourcePort == mLocalPort) ProcessTCPPacket(pTCPPacket, ref mOutboundSequence, mOutboundBuffer, mOutboundStream, pArrivalTime);
            else ProcessTCPPacket(pTCPPacket, ref mInboundSequence, mInboundBuffer, mInboundStream, pArrivalTime);
            return Results.Continue;
        }
Esempio n. 8
0
        private void mFileExportMenu_Click(object pSender, EventArgs pArgs)
        {
            mExportDialog.FileName = string.Format("Port {0}", mLocalPort);
            if (mExportDialog.ShowDialog(this) != DialogResult.OK)
            {
                return;
            }

            bool includeNames = MessageBox.Show("Export opcode names? (slow + generates big files!!!)", "-", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes;

            string tmp = "";

            tmp += string.Format("=== MapleStory2 Version: {0}; Region: {1} ===\r\n", mBuild, mLocale);
            tmp += string.Format("Endpoint From: {0}\r\n", mLocalEndpoint);
            tmp += string.Format("Endpoint To: {0}\r\n", mRemoteEndpoint);
            tmp += string.Format("- Packets: {0}\r\n", mPackets.Count);

            long dataSize = 0;

            foreach (var packet in mPackets)
            {
                dataSize += 2 + packet.Buffer.Length;
            }

            tmp += string.Format("- Data: {0:N0} bytes\r\n", dataSize);
            tmp += string.Format("================================================\r\n");
            File.WriteAllText(mExportDialog.FileName, tmp);

            tmp = "";

            int outboundCount = 0;
            int inboundCount  = 0;
            int i             = 0;

            foreach (var packet in mPackets)
            {
                if (packet.Outbound)
                {
                    ++outboundCount;
                }
                else
                {
                    ++inboundCount;
                }

                Definition definition = Config.Instance.GetDefinition(mBuild, mLocale, packet.Outbound, packet.Opcode);

                tmp += string.Format("[{0}][{2}] [{3:X4}{5}] {4}\r\n",
                                     packet.Timestamp.ToString("yyyy-MM-dd HH:mm:ss.fff"),
                                     (packet.Outbound ? outboundCount : inboundCount),
                                     (packet.Outbound ? "Outbound" : "Inbound "),
                                     packet.Opcode,
                                     BitConverter.ToString(packet.Buffer).Replace('-', ' '),
                                     includeNames ? " | " + (definition == null ? "N/A" : definition.Name) : "");
                i++;
                if (i % 1000 == 0)
                {
                    File.AppendAllText(mExportDialog.FileName, tmp);
                    tmp = "";
                }
            }
            File.AppendAllText(mExportDialog.FileName, tmp);
        }
Esempio n. 9
0
        public void OpenReadOnly(string pFilename)
        {
            // mFileSaveMenu.Enabled = false;
            Saved = true;

            mTerminated = true;
            using (FileStream stream = new FileStream(pFilename, FileMode.Open, FileAccess.Read))
            {
                BinaryReader reader            = new BinaryReader(stream);
                ushort       MapleSharkVersion = reader.ReadUInt16();
                mBuild = MapleSharkVersion;
                if (MapleSharkVersion < 0x2000)
                {
                    mLocalPort = reader.ReadUInt16();
                    // Old version
                    frmLocale loc = new frmLocale();
                    var       res = loc.ShowDialog();
                    if (res == System.Windows.Forms.DialogResult.OK)
                    {
                        mLocale = loc.ChosenLocale;
                    }
                }
                else
                {
                    byte v1 = (byte)((MapleSharkVersion >> 12) & 0xF),
                         v2 = (byte)((MapleSharkVersion >> 8) & 0xF),
                         v3 = (byte)((MapleSharkVersion >> 4) & 0xF),
                         v4 = (byte)((MapleSharkVersion >> 0) & 0xF);
                    Console.WriteLine("Loading MSB file, saved by MapleShark V{0}.{1}.{2}.{3}", v1, v2, v3, v4);

                    if (MapleSharkVersion == 0x2012)
                    {
                        mLocale    = (byte)reader.ReadUInt16();
                        mBuild     = reader.ReadUInt16();
                        mLocalPort = reader.ReadUInt16();
                    }
                    else if (MapleSharkVersion == 0x2014)
                    {
                        mLocalEndpoint  = reader.ReadString();
                        mLocalPort      = reader.ReadUInt16();
                        mRemoteEndpoint = reader.ReadString();
                        mRemotePort     = reader.ReadUInt16();

                        mLocale = (byte)reader.ReadUInt16();
                        mBuild  = reader.ReadUInt16();
                    }
                    else if (MapleSharkVersion == 0x2015 || MapleSharkVersion >= 0x2020)
                    {
                        mLocalEndpoint  = reader.ReadString();
                        mLocalPort      = reader.ReadUInt16();
                        mRemoteEndpoint = reader.ReadString();
                        mRemotePort     = reader.ReadUInt16();

                        mLocale = reader.ReadByte();
                        mBuild  = reader.ReadUInt32();

                        if (MapleSharkVersion >= 0x2021 && !Config.Instance.Maple2)
                        {
                            mPatchLocation = reader.ReadString();
                        }
                    }
                    else
                    {
                        MessageBox.Show("I have no idea how to open this MSB file. It looks to me as a version " + string.Format("{0}.{1}.{2}.{3}", v1, v2, v3, v4) + " MapleShark MSB file... O.o?!");
                        return;
                    }
                }

                mPacketList.BeginUpdate();
                while (stream.Position < stream.Length)
                {
                    long   timestamp = reader.ReadInt64();
                    int    size      = MapleSharkVersion < 0x2027 ? reader.ReadUInt16() : reader.ReadInt32();
                    ushort opcode    = reader.ReadUInt16();
                    bool   outbound;

                    if (MapleSharkVersion >= 0x2020)
                    {
                        outbound = reader.ReadBoolean();
                    }
                    else
                    {
                        outbound = (size & 0x8000) != 0;
                        size     = (ushort)(size & 0x7FFF);
                    }

                    byte[] buffer = reader.ReadBytes(size);

                    uint preDecodeIV = 0, postDecodeIV = 0;
                    if (MapleSharkVersion >= 0x2025)
                    {
                        preDecodeIV  = reader.ReadUInt32();
                        postDecodeIV = reader.ReadUInt32();
                    }

                    Definition  definition = Config.Instance.GetDefinition(mBuild, mLocale, outbound, opcode);
                    MaplePacket packet     = new MaplePacket(new DateTime(timestamp), outbound, mBuild, mLocale, opcode, definition == null ? "" : definition.Name, buffer, preDecodeIV, postDecodeIV);
                    AddPacket(packet);
                    if (!mOpcodes.Exists(op => op.Outbound == packet.Outbound && op.Header == packet.Opcode))
                    {
                        mOpcodes.Add(new Opcode(packet.Outbound, packet.Opcode));
                    }
                    if (definition != null && definition.Ignore)
                    {
                        continue;
                    }
                    mPacketList.Items.Add(packet);
                }
                mPacketList.EndUpdate();
                if (mPacketList.Items.Count > 0)
                {
                    mPacketList.EnsureVisible(0);
                }
            }

            Text = string.Format("{0} (ReadOnly)", Path.GetFileName(pFilename));
            Console.WriteLine("Loaded file: {0}", pFilename);
        }
Esempio n. 10
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);
            }
        }
Esempio n. 11
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();
                uint   length = pr.ReadUInt();
                if (length != 19 && length != 32)
                {
                    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();

                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);

                // 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);
                    }
                }

                MaplePacket packet = new MaplePacket(pArrivalTime, false, mBuild, mLocale, header, definition == null ? "" : definition.Name, tcpData, (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);
        }
Esempio n. 12
0
        internal Results BufferTCPPacket(TcpPacket pTCPPacket, DateTime pArrivalTime)
        {
            if (Config.Instance.Maple2)
            {
                return(BufferTCPPacket_MS2(pTCPPacket, 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;
                }

                ushort length     = (ushort)(BitConverter.ToUInt16(tcpData, 0) + 2);
                byte[] headerData = new byte[tcpData.Length];
                Buffer.BlockCopy(tcpData, 0, headerData, 0, tcpData.Length);

                bool mIsKMS = false;

                PacketReader pr = new PacketReader(headerData);

                if (length != tcpData.Length || tcpData.Length < 13)
                {
                    if (socks5 > 0 && socks5 < 7)
                    {
                        if (pr.ReadByte() == 5 && pr.ReadByte() == 1)
                        {
                            pr.ReadByte();
                            mProxyEndpoint = mLocalEndpoint;
                            mLocalEndpoint = "";
                            switch (pr.ReadByte())
                            {
                            case 1:    //IPv4
                                for (int i = 0; i < 4; i++)
                                {
                                    mLocalEndpoint += pr.ReadByte();
                                    if (i < 3)
                                    {
                                        mLocalEndpoint += ".";
                                    }
                                }
                                break;

                            case 3:    //Domain
                                //readInt - String Length
                                //readAsciiString - Address
                                break;

                            case 4:    //IPv6
                                for (int i = 0; i < 16; i++)
                                {
                                    pr.ReadByte();
                                }
                                break;
                            }
                            byte[] ports = new byte[2];
                            for (int i = 1; i >= 0; i--)
                            {
                                ports[i] = pr.ReadByte();
                            }
                            PacketReader portr = new PacketReader(ports);
                            mProxyPort      = mRemotePort;
                            mRemotePort     = portr.ReadUShort();
                            mLocalEndpoint += ":" + mRemotePort;
                            Text            = "Port " + mLocalPort + " - " + mRemotePort + "(Proxy" + mProxyPort + ")";
                            Console.WriteLine("[socks5] From {0} to {1} (Proxy {2})", mRemoteEndpoint, mLocalEndpoint, mProxyEndpoint);
                        }
                        socks5++;
                        return(Results.Continue);
                    }
                    else if (tcpData.Length == 3 && pr.ReadByte() == 5)
                    {
                        socks5 = 1;
                        return(Results.Continue);
                    }
                    Console.WriteLine("Connection on port {0} did not have a MapleStory Handshake", mLocalEndpoint);
                    return(Results.CloseMe);
                }

                pr.ReadUShort();
                ushort version       = pr.ReadUShort();
                byte   subVersion    = 1;
                string patchLocation = pr.ReadMapleString();
                byte[] localIV       = pr.ReadBytes(4);
                byte[] remoteIV      = pr.ReadBytes(4);
                byte   serverLocale  = pr.ReadByte();

                if (serverLocale > 0x12)
                {
                    return(Results.CloseMe);
                }

                if (serverLocale == 0x02 || (serverLocale == 0x01 && version > 255))
                {
                    mIsKMS = true;
                }
                else
                {
                    mIsKMS = false;
                }

                if (mIsKMS)
                {
                    int test = int.Parse(patchLocation);
                    version    = (ushort)(test & 0x7FFF);
                    subVersion = (byte)((test >> 16) & 0xFF);
                }
                else if (patchLocation.All(character => { return(character >= '0' && character <= '9'); }))
                {
                    if (!byte.TryParse(patchLocation, out subVersion))
                    {
                        Console.WriteLine("Failed to parse subVersion");
                    }
                }

                mBuild = version;

                mLocale        = serverLocale;
                mPatchLocation = patchLocation;

                mOutboundStream = new MapleStream(true, version, mLocale, localIV, subVersion);
                mInboundStream  = new MapleStream(false, version, mLocale, remoteIV, subVersion);

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

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

                    // Create main script
                    if (!File.Exists(filename))
                    {
                        string contents = @"
using (ScriptAPI) {
    AddShort(""Packet Size"");
    AddUShort(""MapleStory Version"");
    AddString(""MapleStory Patch Location/Subversion"");
    AddField(""Local Initializing Vector (IV)"", 4);
    AddField(""Remote Initializing Vector (IV)"", 4);
    AddByte(""MapleStory Locale"");
}
";
                        File.WriteAllText(filename, contents);
                    }
                }

                MaplePacket packet = new MaplePacket(pArrivalTime, false, mBuild, mLocale, 0xFFFF, definition == null ? "" : definition.Name, tcpData, (uint)0, BitConverter.ToUInt32(remoteIV, 0));
                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] MapleStory V{2}.{3} Locale {4}", mLocalEndpoint, mRemoteEndpoint, mBuild, subVersion, serverLocale);

                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);
        }
Esempio n. 13
0
 private void SaveDefinition(Definition definition)
 {
     DefinitionsContainer.Instance.SaveDefinition(definition);
 }