private void ProcessTCPPacket(TCPPacket pTCPPacket, ref uint pSequence, SortedDictionary<uint, byte[]> pBuffer, RiftStream pStream) { if (pTCPPacket.SequenceNumber > pSequence) pBuffer[(uint)pTCPPacket.SequenceNumber] = pTCPPacket.TCPData; if (pTCPPacket.SequenceNumber < pSequence) { int difference = (int)(pSequence - pTCPPacket.SequenceNumber); byte[] data = pTCPPacket.TCPData; if (data.Length > difference) { pStream.Append(data, difference, data.Length - difference); pSequence += (uint)(data.Length - difference); } } else if (pTCPPacket.SequenceNumber == pSequence) { byte[] data = pTCPPacket.TCPData; pStream.Append(data); pSequence += (uint)data.Length; bool found; do { SortedDictionary<uint, byte[]>.Enumerator enumerator = pBuffer.GetEnumerator(); if ((found = (enumerator.MoveNext() && enumerator.Current.Key <= pSequence))) { int difference = (int)(pSequence - enumerator.Current.Key); if (enumerator.Current.Value.Length > difference) { pStream.Append(enumerator.Current.Value, difference, enumerator.Current.Value.Length - difference); pSequence += (uint)(enumerator.Current.Value.Length - difference); } pBuffer.Remove(enumerator.Current.Key); } } while (found); } RiftPacket packet; while ((packet = pStream.Read(pTCPPacket.Timeval.Date)) != null) { AddPacket(packet); if (packet.Opcode == 0x01B7) mIsCharacterSession = true; else if (packet.Opcode == 0x040B) { RiftPacketField fieldServerPublicKey; if (packet.GetFieldByIndex(out fieldServerPublicKey, 1) && fieldServerPublicKey.Type == ERiftPacketFieldType.ByteArray && fieldServerPublicKey.Value.Bytes.Length == 128) { if (mClientPrivateKeys != null && mClientPrivateKeys.ContainsKey(mIsCharacterSession)) mClientPrivateKey = BigNumber.FromArray(mClientPrivateKeys[mIsCharacterSession]); if (mClientPrivateKey == null) { // Scan for rift.exe, read memory to pointers, get client private key } mServerPublicKey = BigNumber.FromArray(fieldServerPublicKey.Value.Bytes); DH dh = new DH(mModulus, mGenerator, BigNumber.One, mClientPrivateKey); mSharedSecretKey = dh.ComputeKey(mServerPublicKey); } } else if (packet.Opcode == 0x19) { pStream.EnableInflater(); if (packet.Outbound) { mInboundStream.EnableEncryption(mSharedSecretKey); mOutboundStream.EnableEncryption(mSharedSecretKey); } } } }
private void ProcessTCPPacket(TCPPacket pTCPPacket, ref uint pSequence, SortedDictionary<uint, byte[]> pBuffer, RiftStream pStream) { if (pTCPPacket.SequenceNumber > pSequence) pBuffer[(uint)pTCPPacket.SequenceNumber] = pTCPPacket.TCPData; if (pTCPPacket.SequenceNumber < pSequence) { int difference = (int)(pSequence - pTCPPacket.SequenceNumber); byte[] data = pTCPPacket.TCPData; if (data.Length > difference) { pStream.Append(data, difference, data.Length - difference); pSequence += (uint)(data.Length - difference); } } else if (pTCPPacket.SequenceNumber == pSequence) { byte[] data = pTCPPacket.TCPData; pStream.Append(data); pSequence += (uint)data.Length; bool found; do { SortedDictionary<uint, byte[]>.Enumerator enumerator = pBuffer.GetEnumerator(); if ((found = (enumerator.MoveNext() && enumerator.Current.Key <= pSequence))) { int difference = (int)(pSequence - enumerator.Current.Key); if (enumerator.Current.Value.Length > difference) { pStream.Append(enumerator.Current.Value, difference, enumerator.Current.Value.Length - difference); pSequence += (uint)(enumerator.Current.Value.Length - difference); } pBuffer.Remove(enumerator.Current.Key); } } while (found); } RiftPacket packet; while ((packet = pStream.Read(pTCPPacket.Timeval.Date)) != null) { AddPacket(packet); if (packet.Opcode == 0x01B7) { mIsCharacterSession = true; } else if (packet.Opcode == 0x040B) { RiftPacketField fieldServerPublicKey; if (packet.GetFieldByIndex(out fieldServerPublicKey, 1) && fieldServerPublicKey.Type == ERiftPacketFieldType.ByteArray && fieldServerPublicKey.Value.Bytes.Length == 128) { if (mClientPrivateKeys == null) { DateTime started = DateTime.Now; while (!Program.LiveKeys.ContainsKey(mIsCharacterSession) && DateTime.Now.Subtract(started).TotalSeconds < 10) Thread.Sleep(1); if (Program.LiveKeys.ContainsKey(mIsCharacterSession)) mClientPrivateKeys = Program.LiveKeys; else { MessageBox.Show(this, "The required key was unable to be found for some reason, let the developers know this happened.", "Key Grab Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); mTerminated = true; return; } } mClientPrivateKey = BigNumber.FromArray(mClientPrivateKeys[mIsCharacterSession]); mServerPublicKey = BigNumber.FromArray(fieldServerPublicKey.Value.Bytes); DH dh = new DH(mModulus, mGenerator, BigNumber.One, mClientPrivateKey); mSharedSecretKey = dh.ComputeKey(mServerPublicKey); } } else if (packet.Opcode == 0x19) { pStream.EnableInflater(); if (packet.Outbound) { mInboundStream.EnableEncryption(mSharedSecretKey); mOutboundStream.EnableEncryption(mSharedSecretKey); } } } }