public DumpPacket(MaplePacket pPacket) { pPacket.Reset(); MaplePacket.CommunicationType type = (MaplePacket.CommunicationType)pPacket.ReadByte(); Outboud = type == MaplePacket.CommunicationType.ClientPacket; Opcode = pPacket.ReadUShort(); Data = new byte[pPacket.Length - 3]; Buffer.BlockCopy(pPacket.ToArray(), 3, Data, 0, Data.Length); // byte + short (header) ArrivalTime = MasterThread.CurrentDate; pPacket.Reset(); }
public void ForwardPacket(MaplePacket.CommunicationType pType, MaplePacket pPacket) { pPacket.Reset(); ushort header = pPacket.ReadUShort(); if (!_validHeaders[(byte)pType].Contains(header)) { return; } using (MaplePacket packet = new MaplePacket(pType, header)) { packet.WriteBytes(pPacket.ReadLeftoverBytes()); SendPacket(packet); } }
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); } }
internal bool BufferTCPPacket(TcpPacket pTCPPacket, bool pInbound) { if (pTCPPacket.Syn && pTCPPacket.Ack) { mInboundSequence = (uint)(pTCPPacket.SequenceNumber + 1); return true; } if (pTCPPacket.PayloadData.Length == 0) return true; if (_mapleVersion == 0) { if (pTCPPacket.PayloadData.Length < 13) return false; byte[] tcpData = pTCPPacket.PayloadData; MaplePacket pr = new MaplePacket(tcpData); pr.ReadShort(); _mapleVersion = pr.ReadUShort(); var pos = pr.Position; { var shrt = pr.ReadShort(); if (shrt < 0 || shrt > 0x0020) { return false; } } pr.Reset(pos); _maplePatchLocation = pr.ReadString(); byte[] localIV = pr.ReadBytes(4); byte[] remoteIV = pr.ReadBytes(4); _mapleLocale = pr.ReadByte(); if (pr.Length > pr.Position || _mapleLocale > 0x12) { return false; } pr.Dispose(); pr = null; mOutboundStream = new PacketStream(localIV, _mapleLocale, _mapleVersion, !pInbound); mInboundStream = new PacketStream(remoteIV, _mapleLocale, (ushort)(0xFFFF - _mapleVersion), pInbound); mInboundSequence += (uint)tcpData.Length; } if (!pInbound) ProcessTCPPacket(pTCPPacket, ref mOutboundSequence, mOutboundBuffer, mOutboundStream, pInbound); else ProcessTCPPacket(pTCPPacket, ref mInboundSequence, mInboundBuffer, mInboundStream, pInbound); return true; }
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(); }