public static void On_SMSG_WARDEN_DATA(ref Packets.PacketClass Packet)
        {
            // START Warden Decryption
            byte[] b = new byte[(Packet.Data.Length - 4)];
            Buffer.BlockCopy(Packet.Data, 4, b, 0, b.Length);
            RC4.Crypt(ref b, Maiev.KeyIn);
            Buffer.BlockCopy(b, 0, Packet.Data, 4, b.Length);
            // END

            byte[] WardenData = new byte[(Packet.Data.Length - 4)];
            Buffer.BlockCopy(Packet.Data, 4, WardenData, 0, WardenData.Length);
            MaievOpcode Opcode = (MaievOpcode)Packet.GetInt8();

            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("SMSG_WARDEN_DATA [{0}]", Opcode);
            Console.ForegroundColor = ConsoleColor.White;
            switch (Opcode)
            {
            case MaievOpcode.MAIEV_MODULE_INFORMATION:
            {
                byte[] Name = Packet.GetByteArray(16);
                byte[] Key  = Packet.GetByteArray(16);
                uint   Size = Packet.GetUInt32();
                Maiev.ModuleName = BitConverter.ToString(Name).Replace("-", "");
                Maiev.ModuleKey  = Key;
                ModuleLength     = (int)Size;
                Maiev.ModuleData = Array.Empty <byte>();
                if (File.Exists(@"modules\" + Maiev.ModuleName + ".mod") == false)
                {
                    Console.WriteLine("[{0}][WARDEN] Module is missing.", Strings.Format(DateAndTime.TimeOfDay, "HH:mm:ss"));
                    Packets.PacketClass response = new(OPCODES.CMSG_WARDEN_DATA);
                    response.AddInt8((byte)MaievResponse.MAIEV_RESPONSE_FAILED_OR_MISSING);
                    SendWardenPacket(ref response);
                    response.Dispose();
                }
                else
                {
                    Console.WriteLine("[{0}][WARDEN] Module is initiated.", Strings.Format(DateAndTime.TimeOfDay, "HH:mm:ss"));
                    Maiev.ModuleData = File.ReadAllBytes(@"modules\" + Maiev.ModuleName + ".mod");
                    if (Maiev.LoadModule(Maiev.ModuleName, ref Maiev.ModuleData, Maiev.ModuleKey))
                    {
                        Console.WriteLine("[{0}][WARDEN] Successfully loaded the module.", Strings.Format(DateAndTime.TimeOfDay, "HH:mm:ss"));
                        Packets.PacketClass response = new(OPCODES.CMSG_WARDEN_DATA);
                        response.AddInt8((byte)MaievResponse.MAIEV_RESPONSE_SUCCESS);
                        SendWardenPacket(ref response);
                        response.Dispose();
                    }
                    else
                    {
                        Packets.PacketClass response = new(OPCODES.CMSG_WARDEN_DATA);
                        response.AddInt8((byte)MaievResponse.MAIEV_RESPONSE_FAILED_OR_MISSING);
                        SendWardenPacket(ref response);
                        response.Dispose();
                    }
                }

                break;
            }

            case MaievOpcode.MAIEV_MODULE_TRANSFER:
            {
                ushort Size = Packet.GetUInt16();
                byte[] Data = Packet.GetByteArray(Size);
                Maiev.ModuleData = Realmserver.Concat(Maiev.ModuleData, Data);
                ModuleLength    -= Size;
                if (ModuleLength <= 0)
                {
                    Console.WriteLine("[{0}][WARDEN] Module is fully transfered.", Strings.Format(DateAndTime.TimeOfDay, "HH:mm:ss"));
                    if (Directory.Exists("modules") == false)
                    {
                        Directory.CreateDirectory("modules");
                    }

                    File.WriteAllBytes(@"modules\" + Maiev.ModuleName + ".mod", Maiev.ModuleData);
                    if (Maiev.LoadModule(Maiev.ModuleName, ref Maiev.ModuleData, Maiev.ModuleKey))
                    {
                        Console.WriteLine("[{0}][WARDEN] Successfully loaded the module.", Strings.Format(DateAndTime.TimeOfDay, "HH:mm:ss"));
                        Packets.PacketClass response = new(OPCODES.CMSG_WARDEN_DATA);
                        response.AddInt8((byte)MaievResponse.MAIEV_RESPONSE_SUCCESS);
                        SendWardenPacket(ref response);
                        response.Dispose();
                    }
                }
                else
                {
                    Console.WriteLine("[{0}][WARDEN] Module transfer. Bytes left: {1}", Strings.Format(DateAndTime.TimeOfDay, "HH:mm:ss"), ModuleLength);
                }

                break;
            }

            case MaievOpcode.MAIEV_MODULE_RUN:
            {
                Console.WriteLine("[{0}][WARDEN] Requesting a scan.", Strings.Format(DateAndTime.TimeOfDay, "HH:mm:ss"));

                // TODO: Encrypt?
                Maiev.ReadKeys2();
                RC4.Crypt(ref WardenData, Maiev.ModKeyIn);
                int HandledBytes = Maiev.HandlePacket(WardenData);
                if (HandledBytes <= 0)
                {
                    return;
                }

                byte[] thePacket = Maiev.ReadPacket();
                if (thePacket.Length == 0)
                {
                    return;
                }

                RC4.Crypt(ref WardenData, Maiev.ModKeyOut);

                // TODO: Decrypt?

                Packets.DumpPacket(thePacket);
                Packets.PacketClass response = new(OPCODES.CMSG_WARDEN_DATA);
                response.AddByteArray(thePacket);
                SendWardenPacket(ref response);
                response.Dispose();
                break;
            }

            case MaievOpcode.MAIEV_MODULE_UNK:
            {
                // TODO: Encrypt?
                Maiev.ReadKeys2();
                RC4.Crypt(ref WardenData, Maiev.ModKeyIn);
                int HandledBytes = Maiev.HandlePacket(WardenData);
                if (HandledBytes <= 0)
                {
                    return;
                }

                byte[] thePacket = Maiev.ReadPacket();
                if (thePacket.Length == 0)
                {
                    return;
                }

                RC4.Crypt(ref WardenData, Maiev.ModKeyOut);
                // TODO: Decrypt?

                Packets.DumpPacket(thePacket);
                Packets.PacketClass response = new(OPCODES.CMSG_WARDEN_DATA);
                response.AddByteArray(thePacket);
                SendWardenPacket(ref response);
                response.Dispose();
                break;
            }

            case MaievOpcode.MAIEV_MODULE_SEED:
            {
                Maiev.GenerateNewRC4Keys(Realmserver.SS_Hash);
                int HandledBytes = Maiev.HandlePacket(WardenData);
                if (HandledBytes <= 0)
                {
                    return;
                }

                byte[] thePacket = Maiev.ReadPacket();
                Maiev.ModKeyIn  = new byte[258];
                Maiev.ModKeyOut = new byte[258];
                Packets.PacketClass response = new(OPCODES.CMSG_WARDEN_DATA);
                response.AddByteArray(thePacket);
                SendWardenPacket(ref response);
                response.Dispose();
                Maiev.ReadKeys();
                break;
            }

            default:
            {
                Console.WriteLine("[{0}][WARDEN] Unhandled Opcode [{1}] 0x{2:X}", Strings.Format(DateAndTime.TimeOfDay, "HH:mm:ss"), Opcode, Conversions.ToInteger(Opcode));
                break;
            }
            }
        }
Exemple #2
0
        public static void OnData(byte[] Buffer)
        {
            var Packet = new Packets.PacketClass(ref Buffer, true);

            switch (Packet.OpCode)
            {
            case CMD_AUTH_LOGON_CHALLENGE:
            {
                Console.WriteLine("[{0}][Realm] Received Logon Challenged.", Strings.Format(DateAndTime.TimeOfDay, "HH:mm:ss"));
                switch (Buffer[1])
                {
                case 0:             // No error
                {
                    Console.WriteLine("[{0}][Realm] Challenge Success.", Strings.Format(DateAndTime.TimeOfDay, "HH:mm:ss"));
                    Packet.Offset = 3;
                    ServerB       = Packet.GetByteArray(32);
                    byte G_len = Packet.GetInt8();
                    G = Packet.GetByteArray(G_len);
                    byte N_len = Packet.GetInt8();
                    N       = Packet.GetByteArray(N_len);
                    Salt    = Packet.GetByteArray(32);
                    CrcSalt = Packet.GetByteArray(16);
                    CalculateProof();
                    Thread.Sleep(100);
                    var LogonProof = new Packets.PacketClass(CMD_AUTH_LOGON_PROOF);
                    LogonProof.AddByteArray(PublicA);
                    LogonProof.AddByteArray(M1);
                    LogonProof.AddByteArray(CrcHash);
                    LogonProof.AddInt8(0);                 // Added in 1.12.x client branch? Security Flags (&H0...&H4)?
                    SendR(LogonProof);
                    LogonProof.Dispose();
                    break;
                }

                case 4:
                case 5:             // Bad user
                {
                    Console.WriteLine("[{0}][Realm] Bad account information, the account did not exist.", Strings.Format(DateAndTime.TimeOfDay, "HH:mm:ss"));
                    Connection.Close();
                    break;
                }

                case 9:             // Bad version
                {
                    Console.WriteLine("[{0}][Realm] Bad client version (the server does not allow our version).", Strings.Format(DateAndTime.TimeOfDay, "HH:mm:ss"));
                    Connection.Close();
                    break;
                }

                default:
                {
                    Console.WriteLine("[{0}][Realm] Unknown challenge error [{1}].", Strings.Format(DateAndTime.TimeOfDay, "HH:mm:ss"), Buffer[1]);
                    Connection.Close();
                    break;
                }
                }

                break;
            }

            case CMD_AUTH_LOGON_PROOF:
            {
                Console.WriteLine("[{0}][Realm] Received Logon Proof.", Strings.Format(DateAndTime.TimeOfDay, "HH:mm:ss"));
                switch (Buffer[1])
                {
                case 0:             // No error
                {
                    Console.WriteLine("[{0}][Realm] Proof Success.", Strings.Format(DateAndTime.TimeOfDay, "HH:mm:ss"));
                    var RealmList = new Packets.PacketClass(CMD_AUTH_REALMLIST);
                    RealmList.AddInt32(0);
                    SendR(RealmList);
                    RealmList.Dispose();
                    break;
                }

                case 4:
                case 5:             // Bad user
                {
                    Console.WriteLine("[{0}][Realm] Bad account information, your password was incorrect.", Strings.Format(DateAndTime.TimeOfDay, "HH:mm:ss"));
                    Connection.Close();
                    break;
                }

                case 9:             // Bad version
                {
                    Console.WriteLine("[{0}][Realm] Bad client version (the crc files are either too old or to new for this server).", Strings.Format(DateAndTime.TimeOfDay, "HH:mm:ss"));
                    Connection.Close();
                    break;
                }

                default:
                {
                    Console.WriteLine("[{0}][Realm] Unknown proof error [{1}].", Strings.Format(DateAndTime.TimeOfDay, "HH:mm:ss"), Buffer[1]);
                    Connection.Close();
                    break;
                }
                }

                break;
            }

            case CMD_AUTH_REALMLIST:
            {
                Console.WriteLine("[{0}][Realm] Received Realm List.", Strings.Format(DateAndTime.TimeOfDay, "HH:mm:ss"));
                Packet.Offset = 7;
                int RealmCount = Packet.GetInt8();
                if (RealmCount > 0)
                {
                    for (int i = 1, loopTo = RealmCount; i <= loopTo; i++)
                    {
                        byte   RealmType       = Packet.GetInt8();
                        byte   RealmLocked     = Packet.GetInt8();
                        byte   Unk1            = Packet.GetInt8();
                        byte   Unk2            = Packet.GetInt8();
                        byte   RealmStatus     = Packet.GetInt8();
                        string RealmName       = Packet.GetString();
                        string RealmIP         = Packet.GetString();
                        float  RealmPopulation = Packet.GetFloat();
                        byte   RealmCharacters = Packet.GetInt8();
                        byte   RealmTimezone   = Packet.GetInt8();
                        byte   Unk3            = Packet.GetInt8();
                        Console.WriteLine("[{0}][Realm] Connecting to realm [{1}][{2}].", Strings.Format(DateAndTime.TimeOfDay, "HH:mm:ss"), RealmName, RealmIP);
                        if (Strings.InStr(RealmIP, ":") > 0)
                        {
                            var SplitIP = Strings.Split(RealmIP, ":");
                            if (SplitIP.Length == 2)
                            {
                                if (Information.IsNumeric(SplitIP[1]))
                                {
                                    Worldserver.ConnectToServer(SplitIP[0], Conversions.ToInteger(SplitIP[1]));
                                }
                                else
                                {
                                    Console.WriteLine("[{0}][Realm] Invalid IP in realmlist [{1}].", Strings.Format(DateAndTime.TimeOfDay, "HH:mm:ss"), RealmIP);
                                }
                            }
                            else
                            {
                                Console.WriteLine("[{0}][Realm] Invalid IP in realmlist [{1}].", Strings.Format(DateAndTime.TimeOfDay, "HH:mm:ss"), RealmIP);
                            }
                        }
                        else
                        {
                            Console.WriteLine("[{0}][Realm] Invalid IP in realmlist [{1}].", Strings.Format(DateAndTime.TimeOfDay, "HH:mm:ss"), RealmIP);
                        }

                        break;
                    }
                }
                else
                {
                    Console.WriteLine("[{0}][Realm] No realms were found.", Strings.Format(DateAndTime.TimeOfDay, "HH:mm:ss"));
                }

                break;
            }

            default:
            {
                Console.WriteLine("[{0}][Realm] Unknown opcode [{1}].", Strings.Format(DateAndTime.TimeOfDay, "HH:mm:ss"), Packet.OpCode);
                break;
            }
            }
        }