public ClientConnection(Socket pSocket)
            : base(pSocket)
        {
            uniqueid = Program.Random.Next(0, 10000);

            Pong = true;
            IsFake = false;
            Program.Clients.Add(this);
            Clear();
            _exporter = new MSBExporter();
            Logger_WriteLine("Client Connected!");

            byte[] sendkey = new byte[32], recvkey = new byte[32];
            Program.Random.NextBytes(sendkey);
            Program.Random.NextBytes(recvkey);

            using (MaplePacket pack = new MaplePacket(MaplePacket.CommunicationType.ServerPacket, 0xEEFF))
            {
                pack.WriteString(Logger.Version);

                // Add encryption keys
                pack.WriteBytes(recvkey);
                pack.WriteBytes(sendkey);

                for (byte i = 0; i < (byte)MaplePacket.CommunicationType.AMOUNT; i++)
                {
                    pack.WriteUShort((ushort)Program.ValidHeaders[i].Keys.Count);
                    foreach (var header in Program.ValidHeaders[i].Keys)
                        pack.WriteUShort(header);
                }

                pack.WriteByte((byte)Program.AcceptedIPs.Count);
                foreach (string ip in Program.AcceptedIPs)
                    pack.WriteString(ip);


#if LOCALE_GMS
                pack.WriteBool(true);
                pack.WriteBytes(GMSKeys.GetKeyForVersion());
#elif LOCALE_KMS
                pack.WriteBool(false);
#else
                pack.WriteBool(true);
                pack.WriteBytes(new byte[] { 0x13, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0xB4, 0x00, 0x00, 0x00,
            0x1B, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00, 0x52, 0x00, 0x00, 0x00 });
#endif


                pack.WriteByte(ServerMapleInfo.LOCALE);
                pack.WriteUShort(ServerMapleInfo.VERSION);

                SendPacket(pack);
            }

            SetKeys(sendkey, recvkey);

            SendInfoText("Welcome! Please open MapleStory.");
        }
Beispiel #2
0
        public static void Initialize()
        {
            Instance = new CommandHandler()
            {
                CommandHandlers = new Dictionary<string,Action<ClientConnection, string[]>>()
            };

            Instance.CommandHandlers.Add("report", (pConnection, pArguments) =>
            {
                return; // Disabled


                if (pArguments.Length == 1)
                {
                    string name = pArguments[0];
                    if (name.Length > 12) return;

                    bool found = false;
                    string query = "SELECT name FROM character_views WHERE name LIKE '" + MySql.Data.MySqlClient.MySqlHelper.EscapeString(name) + "%' AND mapid = " + pConnection.CharData.Stats.MapID;
                    using (var result = MySQL_Connection.Instance.RunQuery(query) as MySql.Data.MySqlClient.MySqlDataReader)
                    {
                        if (result.HasRows && result.Read())
                        {
                            name = result.GetString(0);
                            found = true;
                        }
                        else
                        {
                            pConnection.Logger_WriteLine("Could not find {0} for {1}", name, pConnection.CharData.Stats.Name);
                        }
                    }

                    if (found)
                    {
                        using (InsertQueryBuilder iqb = new InsertQueryBuilder("reports"))
                        {
                            iqb.AddColumns(false, "id", "name", "reported_by", "reported_when", "mapid", "screenshot");
                            iqb.AddRow(null, name, pConnection.CharacterInternalID, new MySQL_Connection.NowType(), pConnection.CharData.Stats.MapID, null);

                            int result = (int)MySQL_Connection.Instance.RunQuery(iqb.ToString());
                            if (result != 0)
                            {
                                pConnection.Logger_WriteLine("Reported {0} (by {1}). Requesting Screenshot...", name, pConnection.CharData.Stats.Name);

                                using (MaplePacket pack = new MaplePacket(MaplePacket.CommunicationType.ServerPacket, (ushort)0xEEFE))
                                {
                                    pack.WriteString("http://mapler.me/actions/upload_report.php");
                                    pack.WriteInt(MySQL_Connection.Instance.GetLastInsertId());
                                    pack.SwitchOver();
                                    pConnection.SendPacket(pack);
                                }
                            }
                            else
                            {
                                pConnection.Logger_WriteLine("Report FAIL {0} (by {1})", name, pConnection.CharData.Stats.Name);
                            }
                        }

                    }
                }
            });
        }
        public override void OnPacket(MaplePacket pPacket)
        {
            MasterThread.Instance.AddCallback((a) =>
            {
                try
                {
                    if (Disconnected) return; // Just to be sure...

                    if (_exporter != null)
                        _exporter.AddPacket(pPacket);

                    MaplePacket.CommunicationType type = (MaplePacket.CommunicationType)pPacket.ReadByte();
                    ushort opcode = pPacket.ReadUShort();

                    if (IsFake)
                    {
                        Logger.WriteLine("Emulating {0:X4} (Len: {1})", opcode, pPacket.Length);
                    }

                    if ((byte)type < Program.ValidHeaders.Length)
                    {
                        // Check if packet is accepted
                        var list = Program.ValidHeaders[(byte)type];
                        if (list.ContainsKey(opcode))
                        {
                            var action = list[opcode];
                            if (action != null)
                            {
                                try
                                {
                                    if (action.CanHandle == null || action.CanHandle(this))
                                        action.Handle(this, pPacket);
                                }
                                catch (Exception ex)
                                {
                                    Logger_ErrorLog("Failed parsing {0:X4} for {1}", opcode, type);
                                    Logger_WriteLine(ex.ToString());
                                    if (!IsFake)
                                    {
                                        LogFilename += "ERROR";
                                        SendInfoText("An error occurred on the Mapler.me server! Please report this :)");

                                        // Save exception to packet
                                        using (MaplePacket mp = new MaplePacket(MaplePacket.CommunicationType.ServerPacket, 0x9999))
                                        {
                                            mp.WriteString(ex.ToString());
                                            if (ex.ToString().Contains("MySql.Data.MySqlClient.MySqlException"))
                                            {
                                                Logger_ErrorLog("MySQL exception!");
                                                var queries = MySQL_Connection.Instance.GetRanQueries();
                                                mp.WriteInt(queries.Count);
                                                foreach (var kvp in queries)
                                                {
                                                    mp.WriteString(kvp.Key);
                                                    mp.WriteString(kvp.Value);
                                                }

                                            }
                                            mp.SwitchOver(); // Make read packet
                                            _exporter.AddPacket(mp);
                                        }

                                        Save(false, false);
                                    }
                                }
                            }
                            else
                            {
                                if (!IsFake)
                                    Logger_WriteLine("No action for {0:X4}", opcode);
                            }
                        }
                        else
                        {
                            if (!IsFake)
                                Logger_WriteLine("Client sent packet {0:X4} for {1} but this one is not handled!", opcode, type);
                        }
                    }
                    else
                    {
                        Logger_ErrorLog("Packet Type not accepted!!! {0:X4} {1}", opcode, (byte)type);
                    }
                }
                catch (Exception ex)
                {
                    Logger.ErrorLog("Failed handling packet: {0}", ex.ToString());
                    SendInfoText("An error occurred on the Mapler.me server! Please report this :)");
                }
                pPacket.Dispose();
                pPacket = null;
            });
        }
 public void SendInfoText(string pMessage, params object[] pParams)
 {
     if (IsFake) return;
     using (MaplePacket packet = new MaplePacket(MaplePacket.CommunicationType.ServerPacket, 0xEEFC))
     {
         packet.WriteString(string.Format(pMessage, pParams));
         SendPacket(packet);
     }
 }
 public void SendTimeUpdate()
 {
     if (IsFake) return;
     using (MaplePacket packet = new MaplePacket(MaplePacket.CommunicationType.ServerPacket, 0xEEFD))
     {
         packet.WriteString(LastLoggedCharacterName);
         SendPacket(packet);
     }
 }
 public void SendToken(string pToken)
 {
     using (MaplePacket mp = new MaplePacket(MaplePacket.CommunicationType.ClientPacket, 0xEE03))
     {
         mp.WriteString(pToken);
         SendPacket(mp);
     }
 }
Beispiel #7
0
        void device_OnPacketArrival(object sender, CaptureEventArgs e)
        {
            var packet = Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data);

            // Check if IP packet
            IpPacket ipPacket = packet.Extract(typeof(IpPacket)) as IpPacket;
            if (ipPacket == null)
                return;

            if (!CheckIfCorrectIP(ipPacket.SourceAddress.ToString()) && !CheckIfCorrectIP(ipPacket.DestinationAddress.ToString()))
            {
                return;
            }


            TcpPacket tcpPacket = packet.Extract(typeof(TcpPacket)) as TcpPacket;
            if (tcpPacket == null)
                return;

            if (!FoundConnection)
            {
                if (tcpPacket.Syn && tcpPacket.Ack)
                {

                    // Found new connection.

                    // Check version...

                    if (frmMain.Instance.CheckRunningEXEVersion())
                    {
                        // Correct version
                        FoundConnection = true;
                        _currentPortMap = new KeyValuePair<ushort, ushort>(tcpPacket.DestinationPort, tcpPacket.SourcePort);
                        _currentSession = new Session();

                        MasterThread.Instance.AddCallback((a) =>
                        {
                            using (MaplePacket p = new MaplePacket(0xEE00))
                            {
                                p.WriteBool(true);
                                p.WriteString(ipPacket.SourceAddress.ToString());
                                p.WriteUShort(tcpPacket.SourcePort);
                                p.SwitchOver();
                                p.Reset(0);
                                ServerConnection.Instance.ForwardPacket(MaplePacket.CommunicationType.ClientPacket, p);
                            }
                        });

                        Logger.WriteLine("[CON] New connection found on {0}!", e.Device.Description);
                        if (cache != 0)
                        {
                            _currentSession.SetOutboundSequence(cache);
                            cache = 0;
                        }
                        _currentSession.BufferTCPPacket(tcpPacket, !(_currentPortMap.Key == tcpPacket.SourcePort && _currentPortMap.Value == tcpPacket.DestinationPort));
                    }
                    else
                    {
                        Logger.WriteLine("Incorrect MapleStory version. Ignoring connection.");
                    }
                }
                else if (tcpPacket.Syn && !tcpPacket.Ack) // Heh fix
                {
                    cache = (int)(tcpPacket.SequenceNumber + 1);
                }
            }
            else if (FoundConnection && 
                (
                    (_currentPortMap.Key == tcpPacket.SourcePort && _currentPortMap.Value == tcpPacket.DestinationPort)
                    ||
                    (_currentPortMap.Value == tcpPacket.SourcePort && _currentPortMap.Key == tcpPacket.DestinationPort)
                )
                )
            {
                if (tcpPacket.Fin || tcpPacket.Rst)
                {
                    FoundConnection = false;
                    Logger.WriteLine("[CON] Connection Lost");

                    MasterThread.Instance.AddCallback((a) =>
                    {
                        using (MaplePacket p = new MaplePacket(0xEE00))
                        {
                            p.WriteBool(false);
                            p.SwitchOver();
                            p.Reset(0);
                            ServerConnection.Instance.ForwardPacket(MaplePacket.CommunicationType.ClientPacket, p);
                        }
                    });
                    return;
                }

                bool result = _currentSession.BufferTCPPacket(tcpPacket, !(_currentPortMap.Key == tcpPacket.SourcePort && _currentPortMap.Value == tcpPacket.DestinationPort));
                if (!result)
                {
                    FoundConnection = false;
                    _currentSession.Dispose();
                    _currentSession = null;
                }
            }
            else
            {
                // Logger.WriteLine("[DEBUG] {0} - {1} {2}", FoundConnection, tcpPacket.SourcePort, tcpPacket.DestinationPort);
            }
        }
Beispiel #8
0
        public void Parse(string pFile, bool pMapleSharkFile)
        {
            using (FileStream stream = new FileStream(pFile, FileMode.Open, FileAccess.Read))
            {
                BinaryReader reader = new BinaryReader(stream);
                if (reader.ReadUInt16() != 0x2020)
                {
                    return;
                }

                string ip2 = reader.ReadString(); // Local Endpoint
                ushort port2 = reader.ReadUInt16(); // Port
                string ip = reader.ReadString(); // Remote Endpoint
                ushort port = reader.ReadUInt16(); // Port

                

                byte locale = reader.ReadByte(); // Locale
                ushort version = reader.ReadUInt16(); // Version
                Logger.WriteLine("Emulating socket connection with connection from V{0}", version / 100);


                MaplePacket p;
                if (pMapleSharkFile)
                {
                    if (ip.Contains(":"))
                    {
                        // Shit.
                        ip = "1.2.3.4";
                    }
                    p = new MaplePacket(MaplePacket.CommunicationType.ClientPacket, 0xEE00);
                    p.WriteBool(true);
                    p.WriteString(ip);
                    p.WriteUShort(port);
                    p.SwitchOver();
                    p.Reset(0);
                    PacketHandler(p);
                }

                while (stream.Position < stream.Length)
                {
                    long timestamp = reader.ReadInt64();
                    ushort size = reader.ReadUInt16();
                    ushort opcode = reader.ReadUInt16();
                    bool outbound = reader.ReadBoolean();

                    byte[] buffer = new byte[3 + size];
                    buffer[0] = (byte)(outbound ? MaplePacket.CommunicationType.ClientPacket : MaplePacket.CommunicationType.ServerPacket);
                    Buffer.BlockCopy(BitConverter.GetBytes(opcode), 0, buffer, 1, 2);
                    Buffer.BlockCopy(reader.ReadBytes(size), 0, buffer, 3, size);
                    if (opcode >= 0xEE00) continue; // Ignore!

                    MaplePacket packet = new MaplePacket(buffer);
                    try
                    {
                        if (PacketHandler != null)
                            PacketHandler(packet);
                    }
                    catch (Exception ex)
                    {
                        Logger.WriteLine("Internal Packet Handling Exception");
                        throw new Exception("Internal Packet Handling Exception", ex);
                    }
                }



                if (pMapleSharkFile)
                {
                    p = new MaplePacket(MaplePacket.CommunicationType.ClientPacket, 0xEE00);
                    p.WriteBool(false);
                    p.SwitchOver();
                    p.Reset(0);
                    PacketHandler(p);
                }

            }

            DisconnectHandler();
        }