void mSession_OnPacketReceived(PacketReader packet) { Logger.Write(Logger.LogTypes.대타, "받은 패킷 {0}", packet.ToArray().ToString2s()); IPacketHandler handler = PacketHandler.getInstance().GetHandler(packet.ReadShort()); if (handler != null) handler.handlePacket(this, packet); else { packet.Reset(0); Logger.Write(Logger.LogTypes.경고, "!!: {0}", HexEncoding.ToHex(packet.ReadShort())); } }
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; }