Ejemplo n.º 1
0
        public byte[] GetPacket()
        {
            var finalPacket = new List<byte>();

            finalPacket.AddRange(EncodeHelper.EncodeShort((short) (Payload.Count + 8), true));      // Packet length.
            finalPacket.AddRange(EncodeHelper.CreatePadding(4));                                    // Unknown.
            finalPacket.AddRange(EncodeHelper.EncodeShort(Header, true));                           // Packet header.
            finalPacket.AddRange(Payload);

            return finalPacket.ToArray();
        }
Ejemplo n.º 2
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                var byteCount = _socket.EndReceive(ar);
                if (byteCount == 0)
                {
                    _socket.Close();
                    _socket.Dispose();

                    Logger.Debug($"[{_clientId}] Connection from {_remoteEndPoint.Address} was dropped.");
                    return;
                }

                var packet = new byte[byteCount];
                Buffer.BlockCopy(_buffer, 0, packet, 0, byteCount);

                /*  Packet 502 - RsLoginFail
                 *
                 *  new Packet(502)
                 *      .Append(EncodeHelper.CreatePadding(4))
                 *      .Append(EncodeHelper.EncodeShort(1101, true));
                 */

                /*  Packet 602 - LsLoginFail
                 *
                 *  new Packet(602)
                 *      .Append(EncodeHelper.CreatePadding(4))
                 *      .Append(EncodeHelper.EncodeInteger(819, true))                      // Custom error
                 *      .Append(Encoding.Unicode.GetBytes("Some kind of custom error"));    // Custom error
                 */

                var packetLength = EncodeHelper.DecodeShort(new[] { packet[1], packet[0] });
                var packetHeader = packet[2]; // Not sure :)

                Logger.Trace($"[{_clientId}] Incoming packet:{Environment.NewLine}" +
                             $"Packet Length Raw: {packet.Length}{Environment.NewLine}" +
                             $"Packet Length: {packetLength}{Environment.NewLine}" +
                             $"Packet ID: {packetHeader}{Environment.NewLine}" +
                             $"Packet Hex:{Environment.NewLine}" +
                             $"{HexUtils.HexDump(packet)}");

                var packetList = new List <byte[]>();
                switch (packetHeader)
                {
                case 255:
                {
                    // Connect to port 8005??
                    packetList.Add(new Packet(501)
                                   .Append(EncodeHelper.CreatePadding(4))
                                   .Append(0x74)
                                   .Append(0x0)
                                   .Append(0x0)
                                   .Append(0x1)
                                   .GetPacket());

                    break;
                }

                case 5:
                {
                    packetList.Add(new Packet(601)
                                   .Append(EncodeHelper.CreatePadding(4))
                                   .Append(Encoding.ASCII.GetBytes("Cool"))
                                   .Append(EncodeHelper.CreatePadding(42))
                                   .Append(Encoding.Unicode.GetBytes("Test"))
                                   .GetPacket());
                    break;
                }

                case 3:
                {
                    packetList.Add(new Packet(2).GetPacket());
                    break;
                }

                default:
                    Logger.Error($"[{_clientId}] Incoming packet was not known.");
                    break;
                }

                if (packetList.Count > 0)
                {
                    foreach (var outgoingPacket in packetList)
                    {
                        SendPacket(outgoingPacket);
                    }
                }
            }
            catch (Exception exception)
            {
                Logger.Error(exception, $"[{_clientId}] An exception occured during receive.");
            }
            finally
            {
                WaitForData();
            }
        }
Ejemplo n.º 3
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                var byteCount = _socket.EndReceive(ar);
                if (byteCount == 0)
                {
                    _socket.Close();
                    _socket.Dispose();

                    Console.WriteLine($"Connection from {_remoteEndPoint.Address} was dropped.");
                    return;
                }

                var packet = new byte[byteCount];
                Buffer.BlockCopy(_buffer, 0, packet, 0, byteCount);

                /* Packet processor */
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine(BitConverter.ToString(packet));
                Console.WriteLine(BitConverter.ToString(packet).Replace("-", ""));

                var packetLength = EncodeHelper.DecodeShort(new[] { packet[1], packet[0] });
                var packetHeader = EncodeHelper.DecodeShort(new[] { packet[3], packet[2] });
                var command      = EncodeHelper.DecodeShort(new[] { packet[7], packet[6] });

                Console.WriteLine($"Packet Length [Short]: {packetLength}");
                Console.WriteLine($"Packet ID [Short]: {packetHeader}");
                Console.WriteLine($"Command [Short]: {command}");
                Console.WriteLine($"Packet Raw: {Encoding.UTF8.GetString(packet)}");

                /* 0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15
                 * 10-00-11-00-04-00-01-00-01-00-00-00-E4-00-00-00
                 * | Length
                 *       | ID
                 *             |?
                 *                   | CMD
                 *
                 * Second Packet:
                 *   CMD 7&8: Launcht het spel / gameguard update
                 */

                /* Packet responder */
                var packetList = new List <byte>();
                switch (command)
                {
                case 1:
                    /* Error example
                     * List<byte> packetList = new List<byte>();
                     * packetList.AddRange(Encoder.CreatePadding(6)); // pos 0,1,2,3,4,5 (6)
                     * packetList.AddRange(Encoder.EncodeShort(99).Reverse()); // COMMAND pos 6,7 (2)
                     * packetList.AddRange(Encoder.CreatePadding(4)); // pos 8,9,10,11 (4)
                     * packetList.AddRange(Encoder.EncodeInteger(1337).Reverse()); // pos 12,13,14,15 (5)
                     * packetList.AddRange(Encoding.Unicode.GetBytes("l33t")); // pos 16+ */

                    packetList.AddRange(EncodeHelper.CreatePadding(6));
                    packetList.AddRange(EncodeHelper.EncodeShort(2).Reverse());
                    packetList.AddRange(EncodeHelper.CreatePadding(8));

                    _socket.Send(packetList.ToArray());
                    break;

                case 6:
                    packetList.AddRange(EncodeHelper.CreatePadding(6));
                    packetList.AddRange(EncodeHelper.EncodeShort(8).Reverse());
                    packetList.AddRange(EncodeHelper.CreatePadding(4));

                    _socket.Send(packetList.ToArray());
                    break;
                }

                /* Finalize */
                Console.ResetColor();
                Console.WriteLine("Received bytes: " + byteCount);
            }
            catch (Exception exception)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(exception.StackTrace);
                Console.ResetColor();
            }
            finally
            {
                WaitForData();
            }
        }
Ejemplo n.º 4
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                int byteCount = _socket.EndReceive(ar);

                if (byteCount == 0)
                {
                    _socket.Close();
                    _socket.Dispose();

                    Console.WriteLine($"Lost connection from {_remoteEndPoint.Address}.");
                    return;
                }

                var packet = new byte[byteCount];
                Buffer.BlockCopy(_buffer, 0, packet, 0, byteCount);

                /* Packet processor */
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine(BitConverter.ToString(packet));

                var packetLength = EncodeHelper.DecodeShort(new[] { packet[1], packet[0] });
                var packetHeader = packet[2]; // Not sure :)

                Console.WriteLine($"Packet Length [Short]: {packetLength}");
                Console.WriteLine($"Packet ID [Byte]: {packetHeader}");
                Console.WriteLine($"Packet Raw: {Encoding.UTF8.GetString(packet)}");
                Console.WriteLine($"Packet Raw Unicode: {Encoding.Unicode.GetString(packet)}");

                var packetList = new List <byte>();
                switch (packetHeader)
                {
                case 5:
                    packetList.AddRange(EncodeHelper.CreatePadding(4));
                    packetList.AddRange(BitEndianConverter.GetBytes((short)502, true));     // Packet ID
                    packetList.AddRange(EncodeHelper.CreatePadding(4));
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);
                    packetList.Add(0x05);

                    packetList.InsertRange(0, BitEndianConverter.GetBytes((short)(packetList.Count + 2), true));

                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine(BitConverter.ToString(packetList.ToArray()));
                    Console.WriteLine("Bytes: " + packetList.Count);

                    _socket.Send(packetList.ToArray());

                    break;

                case 3:
                    packetList.Add(0x03);
                    packetList.Add(0x04);
                    packetList.Add(0x01);
                    packetList.Add(0x02);
                    packetList.Add(0x02);     // Must be 2?

                    packetList.InsertRange(0, BitEndianConverter.GetBytes((short)(packetList.Count + 2), true));

                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine(BitConverter.ToString(packetList.ToArray()));
                    Console.WriteLine("Bytes: " + packetList.Count);

                    _socket.Send(packetList.ToArray());

                    break;

                default:
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Unknown packet received, trying to send it back lol.");

                    _socket.Send(packet);

                    break;
                }

                /* Finalize */
                Console.ResetColor();
                Console.WriteLine("Received bytes: " + byteCount);
            }
            catch (Exception exception)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(exception);
                Console.ResetColor();
            }
            finally
            {
                WaitForData();
            }
        }