private void mPrevOpcodeButton_Click(object sender, EventArgs e)
        {
            SessionForm session = DockPanel.ActiveDocument as SessionForm;

            if (session == null || mOpcodeCombo.SelectedIndex == -1)
            {
                return;
            }
            Pair <bool, ushort> search = (DockPanel.ActiveDocument as SessionForm).Opcodes[mOpcodeCombo.SelectedIndex];
            int initialIndex           = session.ListView.SelectedIndices.Count == 0 ? 0 : session.ListView.SelectedIndices[0];

            for (int index = initialIndex - 1; index > 0; --index)
            {
                MaplePacket packet = session.ListView.Items[index] as MaplePacket;
                if (packet.Outbound == search.First && packet.Opcode == search.Second)
                {
                    session.ListView.SelectedIndices.Clear();
                    session.ListView.SelectedIndices.Add(index);
                    packet.EnsureVisible();
                    session.ListView.Focus();
                    return;
                }
            }
            MessageBox.Show("No further packets found with the selected opcode.", "End Of Search", MessageBoxButtons.OK, MessageBoxIcon.Information);
            session.ListView.Focus();
        }
Beispiel #2
0
        private void mNextOpcodeButton_Click(object pSender, EventArgs pArgs)
        {
            SessionForm session = DockPanel.ActiveDocument as SessionForm;

            if (session == null || mOpcodeCombo.SelectedIndex == -1)
            {
                return;
            }
            Opcode search       = (DockPanel.ActiveDocument as SessionForm).Opcodes[mOpcodeCombo.SelectedIndex];
            int    initialIndex = session.ListView.SelectedIndices.Count == 0 ? 0 : session.ListView.SelectedIndices[0] + 1;

            for (int index = initialIndex; index < session.ListView.Items.Count; ++index)
            {
                MaplePacket packet = session.ListView.Items[index] as MaplePacket;
                if (packet.Outbound == search.Outbound && packet.Opcode == search.Header)
                {
                    session.ListView.SelectedIndices.Clear();
                    session.ListView.SelectedIndices.Add(index);
                    packet.EnsureVisible();
                    session.ListView.Focus();
                    return;
                }
            }
            MessageBox.Show("No further packets found with the selected opcode.", "End Of Search", MessageBoxButtons.OK, MessageBoxIcon.Information);
            session.ListView.Focus();
        }
Beispiel #3
0
        public void RefreshPackets()
        {
            Opcode      search   = (MainForm.SearchForm.ComboBox.SelectedIndex >= 0 ? mOpcodes[MainForm.SearchForm.ComboBox.SelectedIndex] : null);
            MaplePacket previous = mPacketList.SelectedItems.Count > 0 ? mPacketList.SelectedItems[0] as MaplePacket : null;

            mOpcodes.Clear();
            mPacketList.Items.Clear();

            MainForm.DataForm.HexBox.ByteProvider = null;
            MainForm.StructureForm.Tree.Nodes.Clear();
            MainForm.PropertyForm.Properties.SelectedObject = null;

            if (!mViewOutboundMenu.Checked && !mViewInboundMenu.Checked)
            {
                return;
            }
            mPacketList.BeginUpdate();
            for (int index = 0; index < mPackets.Count; ++index)
            {
                MaplePacket packet = mPackets[index];
                if (packet.Outbound && !mViewOutboundMenu.Checked)
                {
                    continue;
                }
                if (!packet.Outbound && !mViewInboundMenu.Checked)
                {
                    continue;
                }

                Definition definition = Config.Instance.GetDefinition(mBuild, mLocale, packet.Outbound, packet.Opcode);
                packet.Name = definition == null ? "" : definition.Name;
                if (!mOpcodes.Exists(op => op.Outbound == packet.Outbound && op.Header == packet.Opcode))
                {
                    mOpcodes.Add(new Opcode(packet.Outbound, packet.Opcode));
                }
                if (definition != null && !mViewIgnoredMenu.Checked && definition.Ignore)
                {
                    continue;
                }
                mPacketList.Items.Add(packet);

                if (packet == previous)
                {
                    packet.Selected = true;
                }
            }
            mPacketList.EndUpdate();
            MainForm.SearchForm.RefreshOpcodes(true);

            if (previous != null)
            {
                previous.EnsureVisible();
            }
        }
Beispiel #4
0
        private void CommonScript_FormClosed(object pSender, FormClosedEventArgs pArgs)
        {
            if (mPacketList.SelectedIndices.Count == 0)
            {
                return;
            }
            MaplePacket packet = mPacketList.SelectedItems[0] as MaplePacket;

            MainForm.StructureForm.ParseMaplePacket(packet);
            Activate();
        }
Beispiel #5
0
 public ScriptForm(string pPath, MaplePacket pPacket)
 {
     mPath   = pPath;
     mPacket = pPacket;
     InitializeComponent();
     if (pPacket != null)
     {
         Text = "Script 0x" + pPacket.Opcode.ToString("X4") + ", " + (pPacket.Outbound ? "Outbound" : "Inbound");
     }
     else
     {
         Text = "Common Script";
     }
 }
Beispiel #6
0
        private void NextSequence()
        {
            SessionForm session = DockPanel.ActiveDocument as SessionForm;

            if (session == null)
            {
                return;
            }
            int initialIndex = session.ListView.SelectedIndices.Count == 0 ? 0 : session.ListView.SelectedIndices[0];

            byte[] pattern    = (mSequenceHex.ByteProvider as DynamicByteProvider).Bytes.ToArray();
            long   startIndex = MainForm.DataForm.HexBox.SelectionLength > 0 ? MainForm.DataForm.HexBox.SelectionStart : -1;

            for (int index = initialIndex; index < session.ListView.Items.Count; ++index)
            {
                MaplePacket packet      = session.ListView.Items[index] as MaplePacket;
                long        searchIndex = startIndex + 1;
                bool        found       = false;
                while (searchIndex <= packet.Buffer.Length - pattern.Length)
                {
                    found = true;
                    for (int patternIndex = 0; found && patternIndex < pattern.Length; ++patternIndex)
                    {
                        found = packet.Buffer[searchIndex + patternIndex] == pattern[patternIndex];
                    }
                    if (found)
                    {
                        break;
                    }
                    ++searchIndex;
                }
                if (found)
                {
                    session.ListView.SelectedIndices.Clear();
                    session.ListView.SelectedIndices.Add(index);
                    packet.EnsureVisible();
                    MainForm.DataForm.HexBox.SelectionStart  = searchIndex;
                    MainForm.DataForm.HexBox.SelectionLength = pattern.Length;
                    MainForm.DataForm.HexBox.ScrollByteIntoView();
                    session.ListView.Focus();
                    return;
                }
                startIndex = -1;
            }
            MessageBox.Show("No further sequences found.", "End Of Search", MessageBoxButtons.OK, MessageBoxIcon.Information);
            session.ListView.Focus();
        }
Beispiel #7
0
        private void mPacketList_ItemActivate(object pSender, EventArgs pArgs)
        {
            if (mPacketList.SelectedIndices.Count == 0)
            {
                return;
            }
            MaplePacket packet = mPacketList.SelectedItems[0] as MaplePacket;

            var scriptPath = Helpers.GetScriptPath(mLocale, mBuild, packet.Outbound, packet.Opcode);

            Helpers.MakeSureFileDirectoryExists(scriptPath);

            ScriptForm script = new ScriptForm(scriptPath, packet);

            script.FormClosed += Script_FormClosed;
            script.Show(DockPanel, new Rectangle(MainForm.Location, new Size(600, 300)));
        }
Beispiel #8
0
        private void mPacketList_ItemActivate(object pSender, EventArgs pArgs)
        {
            if (mPacketList.SelectedIndices.Count == 0)
            {
                return;
            }
            MaplePacket packet     = mPacketList.SelectedItems[0] as MaplePacket;
            string      scriptPath = "Scripts" + Path.DirectorySeparatorChar + mLocale.ToString() + Path.DirectorySeparatorChar + mBuild.ToString() + Path.DirectorySeparatorChar + (packet.Outbound ? "Outbound" : "Inbound") + Path.DirectorySeparatorChar + "0x" + packet.Opcode.ToString("X4") + ".txt";

            if (!Directory.Exists(Path.GetDirectoryName(scriptPath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(scriptPath));
            }
            ScriptForm script = new ScriptForm(scriptPath, packet);

            script.FormClosed += Script_FormClosed;
            script.Show(DockPanel, new Rectangle(MainForm.Location, new Size(600, 300)));
        }
Beispiel #9
0
        internal void ParseMSnifferLine(string packetLine)
        {
            var match = _packetRegex.Match(packetLine);

            if (match.Captures.Count == 0)
            {
                return;
            }
            DateTime date = new DateTime(
                2012,
                10,
                10,
                int.Parse(match.Groups[1].Value),
                int.Parse(match.Groups[2].Value),
                int.Parse(match.Groups[3].Value)
                );
            int packetLength = int.Parse(match.Groups[4].Value);

            byte[] buffer   = new byte[packetLength - 2];
            bool   outbound = match.Groups[5].Value == "Send";

            string[] bytesText = match.Groups[6].Value.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            ushort opcode = (ushort)(byte.Parse(bytesText[0], System.Globalization.NumberStyles.HexNumber) | byte.Parse(bytesText[1], System.Globalization.NumberStyles.HexNumber) << 8);

            for (var i = 2; i < packetLength; i++)
            {
                buffer[i - 2] = byte.Parse(bytesText[i], System.Globalization.NumberStyles.HexNumber);
            }

            Definition  definition = Config.Instance.GetDefinition(mBuild, mLocale, outbound, opcode);
            MaplePacket packet     = new MaplePacket(date, outbound, mBuild, mLocale, opcode, definition == null ? "" : definition.Name, buffer, 0, 0);

            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)
            {
                return;
            }
            mPacketList.Items.Add(packet);
        }
Beispiel #10
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();
        }
Beispiel #11
0
 private void mPacketContextMenu_Opening(object pSender, CancelEventArgs pArgs)
 {
     mPacketContextNameBox.Text       = "";
     mPacketContextIgnoreMenu.Checked = false;
     if (mPacketList.SelectedItems.Count == 0)
     {
         pArgs.Cancel = true;
     }
     else
     {
         MaplePacket packet     = mPacketList.SelectedItems[0] as MaplePacket;
         Definition  definition = Config.Instance.GetDefinition(mBuild, mLocale, packet.Outbound, packet.Opcode);
         if (definition != null)
         {
             mPacketContextNameBox.Text       = definition.Name;
             mPacketContextIgnoreMenu.Checked = definition.Ignore;
         }
     }
 }
Beispiel #12
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;
            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();
            }
        }
Beispiel #13
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;
             Config.Instance.Definitions.Add(definition);
         }
         definition.Name = mPacketContextNameBox.Text;
         Config.Instance.Save();
         pArgs.SuppressKeyPress = true;
         mPacketContextMenu.Close();
         RefreshPackets();
     }
 }
Beispiel #14
0
        public void ParseMaplePacket(MaplePacket pPacket)
        {
            mTree.Nodes.Clear();
            mSubNodes.Clear();
            pPacket.Rewind();

            string scriptPath = Application.StartupPath + Path.DirectorySeparatorChar + "Scripts" + Path.DirectorySeparatorChar + pPacket.Locale.ToString() + Path.DirectorySeparatorChar + pPacket.Build.ToString() + Path.DirectorySeparatorChar + (pPacket.Outbound ? "Outbound" : "Inbound") + Path.DirectorySeparatorChar + "0x" + pPacket.Opcode.ToString("X4") + ".txt";
            string commonPath = Application.StartupPath + Path.DirectorySeparatorChar + "Scripts" + Path.DirectorySeparatorChar + pPacket.Locale.ToString() + Path.DirectorySeparatorChar + pPacket.Build.ToString() + Path.DirectorySeparatorChar + "Common.txt";

            if (File.Exists(scriptPath))
            {
                mParsing = pPacket;

                try
                {
                    StringBuilder scriptCode = new StringBuilder();
                    scriptCode.Append(File.ReadAllText(scriptPath));
                    if (File.Exists(commonPath))
                    {
                        scriptCode.Append(File.ReadAllText(commonPath));
                    }
                    Script script = Script.Compile(scriptCode.ToString());
                    script.Context.SetItem("ScriptAPI", new ScriptAPI(this));
                    script.Execute();
                }
                catch (Exception exc)
                {
                    OutputForm output = new OutputForm("Script Error");
                    output.Append(exc.ToString());
                    output.Show(DockPanel, new Rectangle(MainForm.Location, new Size(400, 400)));
                }

                mParsing = null;
            }
            if (pPacket.Remaining > 0)
            {
                mTree.Nodes.Add(new StructureNode("Undefined", pPacket.InnerBuffer, pPacket.Cursor, pPacket.Remaining));
            }
        }
Beispiel #15
0
        public void ParseMaplePacket(MaplePacket pPacket)
        {
            mTree.Nodes.Clear();
            mSubNodes.Clear();
            pPacket.Rewind();

            var scriptPath = Helpers.GetScriptPath(pPacket.Locale, pPacket.Build, pPacket.Outbound, pPacket.Opcode);
            var commonPath = Helpers.GetCommonScriptPath(pPacket.Locale, pPacket.Build);

            if (File.Exists(scriptPath))
            {
                mParsing = pPacket;

                try
                {
                    StringBuilder scriptCode = new StringBuilder();
                    scriptCode.Append(File.ReadAllText(scriptPath));
                    if (File.Exists(commonPath))
                    {
                        scriptCode.Append(File.ReadAllText(commonPath));
                    }
                    Script script = Script.Compile(scriptCode.ToString());
                    script.Context.SetItem("ScriptAPI", new ScriptAPI(this));
                    script.Execute();
                }
                catch (Exception exc)
                {
                    OutputForm output = new OutputForm("Script Error");
                    output.Append(exc.ToString());
                    output.Show(DockPanel, new Rectangle(MainForm.Location, new Size(400, 400)));
                }

                mParsing = null;
            }
            if (pPacket.Remaining > 0)
            {
                mTree.Nodes.Add(new StructureNode("Undefined", pPacket.Buffer, pPacket.Cursor, pPacket.Remaining));
            }
        }
Beispiel #16
0
        public void OpenReadOnly(string pFilename)
        {
            // mFileSaveMenu.Enabled = false;

            mTerminated = true;
            using (FileStream stream = new FileStream(pFilename, FileMode.Open, FileAccess.Read))
            {
                BinaryReader reader = new BinaryReader(stream);
                mBuild = reader.ReadUInt16();
                ushort version = mBuild;
                if (mBuild == 0x2012)
                {
                    mLocale    = (byte)reader.ReadUInt16();
                    mBuild     = reader.ReadUInt16();
                    mLocalPort = reader.ReadUInt16();
                }
                else if (mBuild == 0x2014)
                {
                    mLocalEndpoint  = reader.ReadString();
                    mLocalPort      = reader.ReadUInt16();
                    mRemoteEndpoint = reader.ReadString();
                    mRemotePort     = reader.ReadUInt16();

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

                    mLocale = reader.ReadByte();
                    mBuild  = reader.ReadUInt16();
                }
                else
                {
                    mLocalPort = reader.ReadUInt16();
                    // Old version
                    frmLocale loc = new frmLocale();
                    var       res = loc.ShowDialog();
                    if (res == System.Windows.Forms.DialogResult.OK)
                    {
                        mLocale = loc.ChosenLocale;
                    }
                }
                mPacketList.BeginUpdate();
                while (stream.Position < stream.Length)
                {
                    long   timestamp = reader.ReadInt64();
                    ushort size      = reader.ReadUInt16();
                    ushort opcode    = reader.ReadUInt16();
                    bool   outbound;
                    if (version >= 0x2020)
                    {
                        outbound = reader.ReadBoolean();
                    }
                    else
                    {
                        outbound = (size & 0x8000) != 0;
                        size     = (ushort)(size & 0x7FFF);
                    }
                    byte[]      buffer     = reader.ReadBytes(size);
                    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);
                    mPackets.Add(packet);
                    if (!mOpcodes.Exists(kv => kv.First == packet.Outbound && kv.Second == packet.Opcode))
                    {
                        mOpcodes.Add(new Pair <bool, ushort>(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);
        }
Beispiel #17
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 (serverLocale == 0x07 && pr.Remaining > 0)
                {
                    ushort unk = pr.ReadUShort();
                }

                if (pr.Remaining > 0 || serverLocale > 0x12)
                {
                    //MessageBox.Show("Connection closing. pr.remaining > 0 | ServerLocale > 0x12: " + (pr.Remaining > 0) + " - " + (serverLocale > 0x12));
                    //MessageBox.Show(string.Format("Version {0} patch location {1} serverlocale {4}", version, patchLocation, localIV, remoteIV, serverLocale));
                    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);
        }
Beispiel #18
0
        public void ParseMaplePacket(MaplePacket pPacket)
        {
            mTree.Nodes.Clear();
            mSubNodes.Clear();
            pPacket.Rewind();

            string scriptPath = Application.StartupPath + Path.DirectorySeparatorChar + "Scripts" + Path.DirectorySeparatorChar + pPacket.Locale.ToString() + Path.DirectorySeparatorChar + pPacket.Build.ToString() + Path.DirectorySeparatorChar + (pPacket.Outbound ? "发送" : "接收") + Path.DirectorySeparatorChar + "0x" + pPacket.Opcode.ToString("X4") + ".txt";
            string commonPath = Application.StartupPath + Path.DirectorySeparatorChar + "Scripts" + Path.DirectorySeparatorChar + pPacket.Locale.ToString() + Path.DirectorySeparatorChar + pPacket.Build.ToString() + Path.DirectorySeparatorChar + "Common.txt";

            if (File.Exists(scriptPath))
            {
                mParsing = pPacket;

                try
                {
                    StringBuilder scriptCode = new StringBuilder();
                    scriptCode.Append(File.ReadAllText(scriptPath));
                    if (File.Exists(commonPath))
                    {
                        scriptCode.Append(File.ReadAllText(commonPath));
                    }
                    // SSharp
                    // Script script = Script.Compile(scriptCode.ToString());
                    // script.Context.SetItem("ScriptAPI", new ScriptAPI(this));
                    // script.Execute();

                    //Jint
                    var engine = new Jint.Engine();
                    engine.SetValue("ScriptAPI", new ScriptAPI(this));
                    engine.SetValue("mplew", new mplew(this));
                    engine.Execute(scriptCode.ToString());

                    //var context = new NiL.JS.Core.Context();
                    //context.DefineVariable("ScriptAPI").Assign(NiL.JS.Core.JSValue.Marshal(new ScriptAPI(this)));
                    //context.Eval(scriptCode.ToString());
                }
                catch (Jint.Parser.ParserException exc)
                {
                    OutputForm output = new OutputForm("Script Error");
                    output.Append(exc.Message);
                    output.Show(DockPanel, new Rectangle(MainForm.Location, new Size(400, 400)));
                }
                catch (Jint.Runtime.JavaScriptException exc)
                {
                    OutputForm output = new OutputForm("Script Error");
                    output.Append(exc.LineNumber + " : " + exc.Message);
                    output.Show(DockPanel, new Rectangle(MainForm.Location, new Size(400, 400)));
                }
                catch (Exception exc)
                {
                    OutputForm output = new OutputForm("Script Error");
                    output.Append(exc.ToString());
                    output.Show(DockPanel, new Rectangle(MainForm.Location, new Size(400, 400)));
                }

                mParsing = null;
            }
            if (pPacket.Remaining > 0)
            {
                mTree.Nodes.Add(new StructureNode("Undefined", pPacket.Buffer, pPacket.Cursor, pPacket.Remaining));
            }
        }
Beispiel #19
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);
        }
Beispiel #20
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 #21
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);
        }
Beispiel #22
0
 private void AddPacket(MaplePacket packet)
 {
     mPackets.Add(packet);
 }