Beispiel #1
0
        private static void DoClientAuth(object stateo)
        {
            var state = (ClientAuthstate)stateo;

            // obtain key auth
            try
            {
                var npticket = DWTickets.ParseNPTicket(state.Ticket);
                // /validate/:token/:ip/:server/:username
                var url = "http://127.0.0.1:6378/validate/" + npticket.SessionID + "/0/false/" + npticket.NickName;
                //var url = "http://server.repziw4.de/check_session.php?sid=" + npticket.SessionID;
                var wc        = new WebClient();
                var webString = wc.DownloadString(url);
                var authData  = webString.Split(' ');
                Log.Debug(webString);
                Log.Debug(authData[0]);
                if (authData[0] == "1")
                {
                    var userID = int.Parse(authData[1]);

                    if ((uint)(npticket.SteamID & 0xFFFFFFFF) == userID)
                    {
                        var ivBase = BitConverter.ToUInt32(DWCrypto.GenerateRandom(4), 0);
                        var iv     = DWCrypto.CalculateInitialVector(ivBase);
                        var key    = npticket.EncryptionKey;

                        var globalKey = DWCrypto.GenerateRandom(24);

                        var gameTicket = DWTickets.BuildGameTicket(globalKey, state.GameID, npticket.NickName, 0);
                        var lsgTicket  = DWTickets.BuildLSGTicket(globalKey, npticket.SteamID, userID, npticket.NickName);

                        var encryptedGameTicket = DWCrypto.Encrypt(iv, key, gameTicket);

                        var reply = state.Packet.MakeReply(29, true);
                        reply.BitBuffer.UseDataTypes = false;
                        reply.BitBuffer.WriteBoolean(false);
                        reply.BitBuffer.WriteUInt32(700);
                        reply.BitBuffer.WriteUInt32(ivBase);
                        reply.BitBuffer.WriteBytes(encryptedGameTicket);
                        reply.BitBuffer.WriteBytes(lsgTicket);
                        reply.Send(false);

                        Log.Debug("user " + userID + " authenticated client: " + state.Source + "");
                        return;
                    }
                }
                wc.Dispose();
            }
            catch (Exception e)
            {
                Log.Debug("Exception: " + e.ToString());
            }
        }
Beispiel #2
0
        private static void DoServerAuth(object stateo)
        {
            var state = (ServerAuthstate)stateo;

            // obtain key auth
            try
            {
                // /validate/:token/:ip/:server/:username
                //var url = "http://server.repziw4.de/check_key.php?key=" + state.KeyData.ToString("x16");
                var url        = "http://127.0.0.1:6378/validate/" + state.KeyData.ToString("x16") + "/0/true/0";
                var wc         = new WebClient();
                var resultData = wc.DownloadString(url);
                var authData   = resultData.Split(' ');

                if (authData[0] == "1")
                {
                    var licenseType = int.Parse(authData[1]);
                    var userID      = int.Parse(authData[3]);

                    var key = Extensions.SwapBytes64(Extensions.ParseHexString(authData[2]));

                    // generate iv
                    var ivBase = BitConverter.ToUInt32(DWCrypto.GenerateRandom(4), 0);
                    var iv     = DWCrypto.CalculateInitialVector(ivBase);

                    // blah
                    var globalKey = DWCrypto.GenerateRandom(24);

                    var gameTicket = DWTickets.BuildGameTicket(globalKey, state.GameID, "", (byte)licenseType); // 4: official
                    var lsgTicket  = DWTickets.BuildLSGTicket(globalKey, state.KeyData, userID, "");

                    var encryptedGameTicket = DWCrypto.Encrypt(iv, key, gameTicket);

                    var reply = state.Packet.MakeReply(13, true);
                    reply.BitBuffer.UseDataTypes = false;
                    reply.BitBuffer.WriteBoolean(false);
                    reply.BitBuffer.WriteUInt32(700);
                    reply.BitBuffer.WriteUInt32(ivBase);
                    reply.BitBuffer.WriteBytes(encryptedGameTicket);
                    reply.BitBuffer.WriteBytes(lsgTicket);
                    reply.Send(false);

                    Log.Debug("user " + userID + " authenticated server");
                    return;
                }
            }
            catch (Exception e)
            {
                Log.Debug("Exception: " + e.ToString());
            }
        }
Beispiel #3
0
        private static void DoIW5ServerAuth(object stateo)
        {
            var state = (ServerAuthstate)stateo;

            // obtain key auth
            try
            {
                var query  = Query.EQ("keyHash", (long)state.KeyData);
                var result = Database.AServerKeys.Find(query);

                if (result.Count() > 0)
                {
                    var keyEntry  = result.First();
                    var keyString = keyEntry.key;

                    var thash = new TigerHash();
                    var key   = thash.ComputeHash(Encoding.ASCII.GetBytes(keyString));

                    // generate iv
                    var ivBase = BitConverter.ToUInt32(DWCrypto.GenerateRandom(4), 0);
                    var iv     = DWCrypto.CalculateInitialVector(ivBase);

                    // blah
                    var globalKey = DWCrypto.GenerateRandom(24);

                    var gameTicket = DWTickets.BuildGameTicket(globalKey, state.GameID, "", 0); // 4: official
                    var lsgTicket  = DWTickets.BuildLSGTicket(globalKey, state.KeyData, 1, "");

                    var encryptedGameTicket = DWCrypto.Encrypt(iv, key, gameTicket);

                    var reply = state.Packet.MakeReply(13, true);
                    reply.BitBuffer.UseDataTypes = false;
                    reply.BitBuffer.WriteBoolean(false);
                    reply.BitBuffer.WriteUInt32(700);
                    reply.BitBuffer.WriteUInt32(ivBase);
                    reply.BitBuffer.WriteBytes(encryptedGameTicket);
                    reply.BitBuffer.WriteBytes(lsgTicket);
                    reply.Send(false);
                    return;
                }
            }
            catch (Exception e)
            {
                Log.Debug("Exception: " + e.ToString());
            }
        }
Beispiel #4
0
        private static void CreateIW5ServerKey(object stateo)
        {
            var state = (IW5ServerAuthstate)stateo;

            // obtain key auth
            try
            {
                Log.Debug("got a request for a new IW5 dedi key; seems fun to me");

                // actually the advanced RSA stuff should be used but that might be incompatible with the .NET implementation of RSA
                var passGen = new PasswordGenerator();
                passGen.Maximum = 20;
                passGen.Minimum = 20;
                var key = passGen.Generate();

                key = string.Format("X{0}-{1}-{2}-{3}-{4}", key.Substring(1, 3), key.Substring(4, 4), key.Substring(8, 4), key.Substring(12, 4), key.Substring(16, 4));

                var thash   = new TigerHash();
                var hash    = thash.ComputeHash(Encoding.ASCII.GetBytes(key));
                var keyHash = BitConverter.ToInt64(hash, 0);

                var keyEntry = new ServerKey();
                keyEntry.key     = key;
                keyEntry.keyHash = keyHash;
                keyEntry.unkInt  = new Random().Next();
                Database.AServerKeys.Save(keyEntry);

                var keyStuff = new byte[86];
                Array.Copy(Encoding.ASCII.GetBytes(key), keyStuff, key.Length);

                var obfuscationKey = "43FCB2ACF2D72593DD7CD1C69E0F03C07229F4C83166F7B05BA0C5FE3AA3A2D93EK2495783KDKN92939DK";
                var i = 0;

                foreach (var character in obfuscationKey)
                {
                    keyStuff[i] ^= (byte)character;
                    i++;
                }

                // generate iv
                var ivBase = BitConverter.ToUInt32(DWCrypto.GenerateRandom(4), 0);
                var iv     = DWCrypto.CalculateInitialVector(ivBase);

                // blah
                var globalKey = DWCrypto.GenerateRandom(24);

                var gameTicket = DWTickets.BuildGameTicket(globalKey, state.GameID, "", (byte)0); // 4: official
                var lsgTicket  = DWTickets.BuildLSGTicket(globalKey, (ulong)keyHash, 1, "");

                var encryptedGameTicket = DWCrypto.Encrypt(iv, hash, gameTicket);

                var reply = state.Packet.MakeReply(25, true);
                reply.BitBuffer.UseDataTypes = false;
                reply.BitBuffer.WriteBoolean(false);
                reply.BitBuffer.WriteUInt32(700);
                reply.BitBuffer.WriteUInt32(ivBase);
                reply.BitBuffer.WriteBytes(encryptedGameTicket);
                reply.BitBuffer.WriteBytes(lsgTicket);
                reply.BitBuffer.WriteBytes(keyStuff);
                reply.BitBuffer.WriteInt32(keyEntry.unkInt);

                reply.Send(false);
            }
            catch (Exception e)
            {
                Log.Debug("Exception: " + e.ToString());
            }
        }
Beispiel #5
0
        public static void DW_PacketReceived(MessageData data)
        {
            var type = data.Get <int>("type");

            if (type == 7)
            {
                var packet = DWRouter.GetMessage(data);

                bool   initialBool;
                uint   gameID, randomNumber;
                byte[] ticket;

                packet.BitBuffer.UseDataTypes = false;
                packet.BitBuffer.ReadBoolean(out initialBool);
                packet.BitBuffer.UseDataTypes = true;

                packet.BitBuffer.ReadUInt32(out gameID);
                packet.BitBuffer.ReadUInt32(out randomNumber);

                packet.BitBuffer.ReadBytes(128, out ticket);

                // parse LSG ticket
                var key = DWTickets.GetKeyFromLSGTicket(ticket);
                DWRouter.SetGlobalKey(packet.Data, key);

                lock (DWRouter.Connections)
                {
                    var id = DWTickets.GetIDFromLSGTicket(ticket);
                    DWRouter.Connections[id] = packet.Data.Get <string>("cid");
                }

                lock (DWRouter.CIDToUser)
                {
                    var id = DWTickets.GetUserFromLSGTicket(ticket);
                    DWRouter.CIDToUser[packet.Data.Get <string>("cid")] = id;
                }

                lock (DWRouter.ConnectionsReverse)
                {
                    var id = DWTickets.GetIDFromLSGTicket(ticket);
                    DWRouter.ConnectionsReverse[packet.Data.Get <string>("cid")] = id;
                }

                lock (DWRouter.CIDToName)
                {
                    var name = DWTickets.GetNameFromLSGTicket(ticket);
                    DWRouter.CIDToName[packet.Data.Get <string>("cid")] = name;
                }

                lock (DWRouter.CIDToTitle)
                {
                    var title = (TitleID)gameID;
                    DWRouter.CIDToTitle[packet.Data.Get <string>("cid")] = title;
                }

                // now make a reply.

                /*
                 * var reply = packet.MakeReply(1, false);
                 * reply.ByteBuffer.Write(0x8000000000000000);
                 * reply.ByteBuffer.Write(0);
                 * reply.ByteBuffer.Write((byte)6);
                 * reply.ByteBuffer.Write(1);
                 * reply.ByteBuffer.Write(1);
                 * reply.ByteBuffer.Write(0x12345678);
                 * reply.Send(true);*/
            }
        }