Exemplo n.º 1
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);
            }
        }
Exemplo n.º 2
0
        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.");
        }
Exemplo n.º 3
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();
        }