Esempio n. 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());
            }
        }
Esempio n. 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());
            }
        }
Esempio n. 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());
            }
        }
Esempio n. 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());
            }
        }