dh fully works, refactor packet handling stuff

This commit is contained in:
raphaeIl
2025-01-12 19:57:52 -05:00
parent 730fff1545
commit 71f602b7fe
13 changed files with 418 additions and 576 deletions

View File

@@ -0,0 +1,270 @@
using Google.Protobuf;
using Novaria.Common.Crypto;
using Proto;
using System.Reflection;
using Serilog;
using Novaria.Common.Util;
namespace Novaria.Common.Core
{
public class HttpNetworkManager : Singleton<HttpNetworkManager>
{
public byte[] BuildIkeResponse(Packet packet)
{
BinaryWriter packetWriter = new BinaryWriter(new MemoryStream());
byte[] msgIdBytes = BitConverter.GetBytes(packet.msgId);
if (BitConverter.IsLittleEndian)
{
Array.Reverse<byte>(msgIdBytes);
}
packetWriter.Write(msgIdBytes.AsSpan<byte>());
packetWriter.Write(packet.msgBody.AsSpan<byte>());
byte[] packetData = ((MemoryStream)packetWriter.BaseStream).ToArray();
Span<byte> encryptedPacketData = (new byte[packetData.Length + 16]).AsSpan();
AeadTool.Encrypt_ChaCha20(encryptedPacketData, AeadTool.FIRST_IKE_KEY, AeadTool.PS_REQUEST_NONCE, packetData, false);
BinaryWriter rawResponseWriter = new BinaryWriter(new MemoryStream());
rawResponseWriter.Write(AeadTool.PS_REQUEST_NONCE);
rawResponseWriter.Write(encryptedPacketData);
return ((MemoryStream)rawResponseWriter.BaseStream).ToArray();
}
public T DecodePacket<T>(Packet packet) where T : IMessage
{
Assembly assembly = Assembly.GetAssembly(typeof(LoginReq));
Type targetType = typeof(T);
PropertyInfo parserProperty = targetType.GetProperty("Parser", BindingFlags.Static | BindingFlags.Public);
object parserInstance = parserProperty.GetValue(null);
MethodInfo parseFromMethod = parserInstance.GetType().GetMethod("ParseFrom", new[] { typeof(byte[]) });
IMessage parsedMessage = (IMessage)parseFromMethod.Invoke(parserInstance, new object[] { packet.msgBody });
if (parsedMessage == null)
{
throw new InvalidOperationException("Failed to parse message.");
}
return (T)parsedMessage;
}
public IMessage DecodePacket(Type targetType, Packet packet)
{
PropertyInfo parserProperty = targetType.GetProperty("Parser", BindingFlags.Static | BindingFlags.Public);
object parserInstance = parserProperty.GetValue(null);
MethodInfo parseFromMethod = parserInstance.GetType().GetMethod("ParseFrom", new[] { typeof(byte[]) });
IMessage parsedMessage = (IMessage)parseFromMethod.Invoke(parserInstance, new object[] { packet.msgBody });
if (parsedMessage == null)
{
throw new InvalidOperationException("Failed to parse message.");
}
return parsedMessage;
}
public byte[] BuildResponse(Packet packet)
{
BinaryWriter packetWriter = new BinaryWriter(new MemoryStream());
byte[] msgIdBytes = BitConverter.GetBytes(packet.msgId);
if (BitConverter.IsLittleEndian)
{
Array.Reverse<byte>(msgIdBytes);
}
packetWriter.Write(msgIdBytes.AsSpan<byte>());
packetWriter.Write(packet.msgBody.AsSpan<byte>());
byte[] packetData = ((MemoryStream)packetWriter.BaseStream).ToArray();
Span<byte> encryptedPacketData = (new byte[packetData.Length + 16]).AsSpan();
AeadTool.Encrypt_ChaCha20(encryptedPacketData, AeadTool.key3, AeadTool.PS_REQUEST_NONCE, packetData, false);
BinaryWriter rawResponseWriter = new BinaryWriter(new MemoryStream());
rawResponseWriter.Write(AeadTool.PS_REQUEST_NONCE);
rawResponseWriter.Write(encryptedPacketData);
return ((MemoryStream)rawResponseWriter.BaseStream).ToArray();
}
public Packet ParseRequest(byte[] encryptedRawPayload)
{
BinaryReader reader = new BinaryReader(new MemoryStream(encryptedRawPayload));
byte[] nonceBytes = new byte[12]; // nonce 12 bytes length
reader.Read(nonceBytes);
int packetSize = encryptedRawPayload.Length - nonceBytes.Length; // skip nonce length (12)
byte[] packetBytes = new byte[packetSize];
reader.Read(packetBytes);
if (reader.BaseStream.Position != encryptedRawPayload.Length)
{
Log.Error("something went wrong, not all the bytes were read");
Log.Error("reader pos: " + reader.BaseStream.Position);
Log.Error("original len:" + encryptedRawPayload.Length);
}
Span<byte> decrypt_result = new Span<byte>(new byte[packetSize - 16]); // for chacha20 decrypt, the result is 16 bytes less than the input data
Span<byte> nonce = nonceBytes.AsSpan();
Span<byte> packet_data = packetBytes.AsSpan();
bool success = AeadTool.Dencrypt_ChaCha20(decrypt_result, AeadTool.key3, nonce, packet_data, null);
if (!success)
{
Log.Error("something went wrong when chacha20 decrypting the data");
}
byte[] decrypted_bytes = decrypt_result.ToArray();
// might wanna use reader here
byte[] msgid_bytes = decrypted_bytes[10..12]; // two bytes before msg data is msgid
Array.Reverse<byte>(msgid_bytes); // should check BitConverter.IsLittleEndian (if true -> reverse, was true on my pc)
short msgId = BitConverter.ToInt16(msgid_bytes);
Packet packet = new Packet()
{
msgId = msgId,
msgBody = decrypted_bytes[12..], // 2 + 2 (seqid) + 8 (timestamp)
};
return packet;
}
// used for parsing ike requests in ps (or any request ig) client -> server
public IKEReq ParseIkeRequest(byte[] encryptedRawPayload)
{
bool flag = true; // original this flag is: flag = msg.msgId == 1, so true
BinaryReader reader = new BinaryReader(new MemoryStream(encryptedRawPayload));
byte[] nonceBytes = new byte[12]; // nonce 12 bytes length
reader.Read(nonceBytes);
byte[] aeadBytes = new byte[1]; // aead byte is 1 byte, original field in AeadTool
int packetSize = encryptedRawPayload.Length - nonceBytes.Length; // skip nonce length (12)
if (flag)
{
reader.Read(aeadBytes); // read to skip in memory, idk whats the use for this
packetSize--; // skip in payload
}
byte[] packetBytes = new byte[packetSize];
reader.Read(packetBytes);
if (reader.BaseStream.Position != encryptedRawPayload.Length)
{
Log.Error("something went wrong, not all the bytes were read");
Log.Error("reader pos: " + reader.BaseStream.Position);
Log.Error("original len:" + encryptedRawPayload.Length);
}
Span<byte> decrypt_result = new Span<byte>(new byte[packetSize - 16]); // for chacha20 decrypt, the result is 16 bytes less than the input data
Span<byte> nonce = nonceBytes.AsSpan();
Span<byte> packet_data = packetBytes.AsSpan();
byte[] associateData = new byte[13]; // this is needed for req decrypt (size: nonce(12) + 1)
nonceBytes.CopyTo(associateData, 0); // associateData: [nonce, 1], 1 means AesGcm not supported
associateData[associateData.Length - 1] = 1;
bool success = AeadTool.Dencrypt_ChaCha20(decrypt_result, AeadTool.FIRST_IKE_KEY, nonce, packet_data, associateData);
if (!success)
{
Log.Error("something went wrong when chacha20 decrypting the data");
}
byte[] decrypted_bytes = decrypt_result.ToArray();
byte[] msgid_bytes = decrypted_bytes[..2]; // first two bytes is msgid
Array.Reverse<byte>(msgid_bytes); // should check BitConverter.IsLittleEndian (if true -> reverse, was true on my pc)
short msgId = BitConverter.ToInt16(msgid_bytes);
Packet packet = new Packet()
{
msgId = msgId,
msgBody = decrypted_bytes[2..],
};
IKEReq ike_request = IKEReq.Parser.ParseFrom(packet.msgBody);
return ike_request;
}
// used to parse offical pcaps, server -> client
public IKEResp ParseIkeResponse(byte[] encryptedRawPayload)
{
bool flag = false; // false for this pcap ike resp
BinaryReader reader = new BinaryReader(new MemoryStream(encryptedRawPayload));
byte[] nonceBytes = new byte[12]; // nonce 12 bytes length
reader.Read(nonceBytes);
byte[] aeadBytes = new byte[1]; // aead byte is 1 byte, original field in AeadTool
int packetSize = encryptedRawPayload.Length - nonceBytes.Length; // skip nonce length (12)
if (flag)
{
reader.Read(aeadBytes); // read to skip in memory, idk whats the use for this
packetSize--; // skip in payload
}
byte[] packetBytes = new byte[packetSize];
reader.Read(packetBytes);
if (reader.BaseStream.Position != encryptedRawPayload.Length)
{
Log.Error("something went wrong, not all the bytes were read");
Log.Error("reader pos: " + reader.BaseStream.Position);
Log.Error("original len:" + encryptedRawPayload.Length);
}
Span<byte> decrypt_result = new Span<byte>(new byte[packetSize - 16]); // for chacha20, the result is 16 bytes less than the input data
Span<byte> nonce = nonceBytes.AsSpan();
Span<byte> packet_data = packetBytes.AsSpan();
bool success = AeadTool.Dencrypt_ChaCha20(decrypt_result, AeadTool.FIRST_IKE_KEY, nonce, packet_data, null); // associateData NULL FOR THIS response pcap
if (!success)
{
Log.Error("something went wrong when chacha20 decrypting the data");
}
byte[] decrypted_bytes = decrypt_result.ToArray();
byte[] msgid_bytes = decrypted_bytes[..2]; // first two bytes is msgid
Array.Reverse<byte>(msgid_bytes); // should check BitConverter.IsLittleEndian (if true -> reverse, was true on my pc)
short msgId = BitConverter.ToInt16(msgid_bytes);
Packet packet = new Packet()
{
msgId = msgId,
msgBody = decrypted_bytes[2..],
};
IKEResp ike_response = IKEResp.Parser.ParseFrom(packet.msgBody);
return ike_response;
}
}
}

View File

@@ -4,6 +4,8 @@ using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Novaria.Common.Util;
//using Mono.Security.Cryptography;
using NSec.Cryptography;
using static System.Runtime.InteropServices.JavaScript.JSType;
@@ -12,10 +14,10 @@ namespace Novaria.Common.Crypto
{
public static class AeadTool
{
public static byte[] CLIENT_PUBLIC_KEY { get; set; }
public static ClientType clientType; // apprently the ike key for pc and mobile is different somehow
public static byte[] PS_REQUEST_NONCE = new byte[12];
public static byte[] PS_PUBLIC_KEY = new byte[96]; // this is my public key, [69, 0, 0, 0, ...., 0, 0, 0, 69], 69s so i actually know i sent it correctly in frida log
public static byte[] PS_PUBLIC_KEY { get => DiffieHellman.Instance.ServerPublicKey.ToByteArray(); }
public static string TOKEN = "seggs"; // hardcoded for now
public static readonly byte[] DEFAULT_SERVERLIST_KEY;
@@ -25,16 +27,6 @@ namespace Novaria.Common.Crypto
public static readonly byte[] associatedData;
public static byte[] GenerateRandomPublicKey()
{
byte[] key = new byte[96];
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
rng.GetBytes(key);
}
return key;
}
public static ReadOnlySpan<byte> AEADMARK
{
get
@@ -47,32 +39,33 @@ namespace Novaria.Common.Crypto
static AeadTool()
{
AeadTool.clientType = ClientType.Mobile;
DEFAULT_SERVERLIST_KEY = new byte[] { 74, 72, 42, 67, 80, 51, 50, 57, 89, 120, 111, 103, 81, 74, 69, 120 };
DEFAULT_SERVERLIST_IV = new byte[] { 225, 92, 61, 72, 193, 89, 3, 64, 50, 61, 50, 145, 59, 128, 99, 72 };
FIRST_IKE_KEY = new byte[] { 51, 76, 83, 57, 38, 111, 89, 100, 115, 112, 94, 53, 119, 105, 56, 38, 90, 120, 67, 35, 99, 55, 77, 90, 103, 55, 51, 104, 98, 69, 68, 119 };
key3 = new byte[32];
if (clientType == ClientType.Mobile)
{
FIRST_IKE_KEY = Encoding.ASCII.GetBytes("3LS9&oYdsp^5wi8&ZxC#c7MZg73hbEDw");
}
else
{
FIRST_IKE_KEY = Encoding.ASCII.GetBytes("#$*;1H&x*)0!@,/OcIe4VbiL[~fLyE7t");
}
associatedData = new byte[13];
// assume AesGcm not supported (frida log)
associatedData[associatedData.Length - 1] = 1;
//byte[] testkey = Encoding.ASCII.GetBytes("#$*;1H&x*)0!@,/OcIe4VbiL[~fLyE7t"); // apprently key different for pc
//[75,49,239,215,37,193,247,16,183,230,183,161,235,3,201,156,64,192,54,208,139,46,144,123,142,80,149,85,161,26,15,195]
//Console.WriteLine("test key" + testkey.Length);
//Utils.Utils.PrintByteArray(testkey);
//Console.WriteLine();
//testkey.CopyTo(FIRST_IKE_KEY, 0);
PS_PUBLIC_KEY[0] = 69;
PS_PUBLIC_KEY[95] = 69;
PS_REQUEST_NONCE[0] = 42;
PS_REQUEST_NONCE[11] = 42;
key3[0] = 111;
key3[31] = 111;
}
public static bool Dencrypt_ChaCha20(Span<byte> result, ReadOnlySpan<byte> key, ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> data, ReadOnlySpan<byte> associatedData)
@@ -81,7 +74,7 @@ namespace Novaria.Common.Crypto
KeyBlobFormat keyBlobFormat = KeyBlobFormat.RawSymmetricKey;
KeyCreationParameters keyCreationParameters = default(KeyCreationParameters);
bool result2;
//System.ArgumentException: 'The length of the plaintext must be equal to the length of the ciphertext minus the tag size. (Parameter
using (Key key2 = Key.Import(chaCha20Poly, key, keyBlobFormat, ref keyCreationParameters))
{
result2 = AeadAlgorithm.ChaCha20Poly1305.Decrypt(key2, nonce, associatedData, data, result);
@@ -141,6 +134,12 @@ namespace Novaria.Common.Crypto
}
}
}
}
public enum ClientType
{
PC,
Mobile,
}
}

View File

@@ -0,0 +1,41 @@
using Novaria.Common.Util;
using System.Numerics;
namespace Novaria.Common.Crypto
{
public class DiffieHellman : Singleton<DiffieHellman>
{
private BigInteger p = BigInteger.Parse("1552518092300708935130918131258481755631334049434514313202351194902966239949102107258669453876591642442910007680288864229150803718918046342632727613031282983744380820890196288509170691316593175367469551763119843371637221007210577919");
private BigInteger g = 2;
private BigInteger spriv = new BigInteger(new byte[] { 1, 2, 3, 4 }); // hardcoded server priv key
public BigInteger ServerPublicKey { get; set; }
public DiffieHellman()
{
//g** Spriv mod p
ServerPublicKey = BigInteger.ModPow(g, spriv, p);
}
public byte[] CalculateKey(byte[] clientPubKey) // server calculates key like this
{
BigInteger clientPubKeyInt = new BigInteger(clientPubKey.Reverse().ToArray());
//Cpub**Spriv mod p
return BigInteger.ModPow(clientPubKeyInt, spriv, p).ToByteArray(true, true)[..32];
}
// this is for pcap parsing use only, officalServerPubKey is in the first IKE response, client priv will be pcaped too
public byte[] CalculateKey(byte[] officalServerPubKey, byte[] officialClientPriv)
{
BigInteger officalServerPubKeyInt = new BigInteger(officalServerPubKey.Reverse().ToArray());
BigInteger officialClientPrivInt = new BigInteger(officialClientPriv.Reverse().ToArray());
BigInteger result = BigInteger.ModPow(officalServerPubKeyInt, officialClientPrivInt, p);
return result.ToByteArray(true, true)[..32];
}
}
}

View File

@@ -11,6 +11,7 @@
<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.24.3" />
<PackageReference Include="NSec.Cryptography" Version="24.4.0" />
<PackageReference Include="Serilog" Version="3.1.1" />
</ItemGroup>

View File

@@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Novaria.Common.Utils
namespace Novaria.Common.Util
{
public class PB
{

View File

@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Novaria.Common.Util
{
public abstract class Singleton<T> where T : new()
{
static T instance;
public static T Instance
{
get
{
if (instance == null)
instance = new T();
return instance;
}
set => instance = value;
}
}
}

View File

@@ -1,11 +1,4 @@
using Google.Protobuf.Reflection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Novaria.Common.Utils
namespace Novaria.Common.Util
{
public static class Utils
{

View File

@@ -1,67 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace Novaria.Common.Utils
{
public class DiffieHellman
{
private static DiffieHellman instance;
public static DiffieHellman Instance
{
get
{
if (instance == null)
instance = new DiffieHellman();
return instance;
}
set => instance = value;
}
private const int g = 2;
private readonly byte[] p = new byte[] { 255,255,255,255,255,255,255,255,201,15,218,162,33,104,194,52,196,198,98,139,128,220,28,209,41,2,78,8,138,103,204,116,2,11,190,166,59,19,155,34,81,74,8,121,142,52,4,221,239,149,25,179,205,58,67,27,48,43,10,109,242,95,20,55,79,225,53,109,109,81,194,69,228,133,181,118,98,94,126,198,244,76,66,233,166,58,54,32,255,255,255,255,255,255,255,255 };
private byte[] privateKey; // Private key (Spriv or Cpriv)
private byte[] publicKey; // Public key (Spub or Cpub)
public byte[] PublicKey => publicKey;
public DiffieHellman()
{
GenerateKeys();
}
private void GenerateKeys()
{
// Generate a random private key
using (var rng = RandomNumberGenerator.Create())
{
privateKey = new byte[128]; // 1024-bit private key
rng.GetBytes(privateKey);
}
// Calculate the public key: g^privateKey mod p
BigInteger privKeyInt = new BigInteger(privateKey, isUnsigned: true, isBigEndian: true);
BigInteger pInt = new BigInteger(p, isUnsigned: true, isBigEndian: true);
BigInteger pubKeyInt = BigInteger.ModPow(g, privKeyInt, pInt);
publicKey = pubKeyInt.ToByteArray(isUnsigned: true, isBigEndian: true);
}
public byte[] CalculateSharedSecret(byte[] otherPublicKey)
{
BigInteger otherPubKeyInt = new BigInteger(otherPublicKey, isUnsigned: true, isBigEndian: true);
BigInteger privKeyInt = new BigInteger(privateKey, isUnsigned: true, isBigEndian: true);
BigInteger pInt = new BigInteger(p, isUnsigned: true, isBigEndian: true);
// Calculate the shared secret: (otherPublicKey^privateKey) mod p
BigInteger sharedSecretInt = BigInteger.ModPow(otherPubKeyInt, privKeyInt, pInt);
return sharedSecretInt.ToByteArray(isUnsigned: true, isBigEndian: true);
}
}
}

View File

@@ -1,23 +1,12 @@
using Microsoft.AspNetCore.Mvc;
using Novaria.Common.Crypto;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.Json;
using System.Text;
using Serilog;
using Novaria.Common.Utils;
using System.IO;
using System.Numerics;
using System.Security.Cryptography;
using Microsoft.AspNetCore.Http;
using Novaria.Common.Core;
using Proto;
using Google.Protobuf;
using System.Collections;
using System.Reflection;
using System.Net.Sockets;
using Novaria.SDKServer.Controllers.Api.ProtocolHandlers;
//using Mono.Security.Cryptography;
using Novaria.Common.Util;
namespace Novaria.SDKServer.Controllers.Api
{
@@ -44,7 +33,7 @@ namespace Novaria.SDKServer.Controllers.Api
Utils.PrintByteArray(rawPayload);
Packet requestPacket = ParseRequest(rawPayload);
Packet requestPacket = HttpNetworkManager.Instance.ParseRequest(rawPayload);
Log.Information("");
@@ -65,7 +54,7 @@ namespace Novaria.SDKServer.Controllers.Api
throw new InvalidDataException("invalid protocol msgid");
}
IMessage decodedPayload = DecodePacket(requestType, requestPacket);
IMessage decodedPayload = HttpNetworkManager.Instance.DecodePacket(requestType, requestPacket);
// originally i returned a IMessage from handlers,
// but since in this game apprently one req can return diff resp msgids thus they can not be hardcoded in the packet or increments,
@@ -88,7 +77,7 @@ namespace Novaria.SDKServer.Controllers.Api
Log.Information($"Response Packet msgid: {responsePacket.msgId}: {(short)responsePacket.msgId}");
responsePackeBytes = BuildResponse(responsePacket);
responsePackeBytes = HttpNetworkManager.Instance.BuildResponse(responsePacket);
if (responsePackeBytes == null)
{
@@ -97,8 +86,6 @@ namespace Novaria.SDKServer.Controllers.Api
}
Log.Information("Sucessfully responsed with a respone packet msgid: " + (short)responsePacket.msgId);
//Console.WriteLine("Built bytes: ");
//Utils.PrintByteArray(responsePackeBytes);
Response.Body.Write(responsePackeBytes, 0, responsePackeBytes.Length);
@@ -115,27 +102,27 @@ namespace Novaria.SDKServer.Controllers.Api
Request.Body.CopyTo(memoryStream); // Copy request body to MemoryStream
byte[] rawPayload = memoryStream.ToArray(); // Get raw bytes from MemoryStream
//Utils.PrintByteArray(rawPayload);
IKEReq ikeRequest = ParseIkeRequest(rawPayload);
IKEReq ikeRequest = HttpNetworkManager.Instance.ParseIkeRequest(rawPayload);
Log.Information("Decoded Packet: " + JsonSerializer.Serialize(ikeRequest));
AeadTool.CLIENT_PUBLIC_KEY = ikeRequest.PubKey.ToArray();
byte[] clientPublicKey = ikeRequest.PubKey.ToArray();
Log.Information("RECEIVED client public key: ");
Utils.PrintByteArray(clientPublicKey);
AeadTool.key3 = DiffieHellman.Instance.CalculateKey(clientPublicKey);
Log.Information("KEY3 (chacha20 key) calculated: ");
Utils.PrintByteArray(AeadTool.key3);
IKEResp ikeResponse = new IKEResp()
{
PubKey = ByteString.CopyFrom(DiffieHellman.Instance.PublicKey),
PubKey = ByteString.CopyFrom(AeadTool.PS_PUBLIC_KEY.Reverse().ToArray()),
Token = AeadTool.TOKEN
};
Log.Information("RECEIVED client public key: ");
Utils.PrintByteArray(AeadTool.CLIENT_PUBLIC_KEY);
byte[] calculatedKey = DiffieHellman.Instance.CalculateSharedSecret(AeadTool.CLIENT_PUBLIC_KEY);
Log.Information("CALCULATED KEY: ");
Utils.PrintByteArray(calculatedKey);
Log.Information("Sending ps server public key: ");
Utils.PrintByteArray(AeadTool.PS_PUBLIC_KEY);
Packet ikePacket = new Packet()
{
@@ -143,7 +130,7 @@ namespace Novaria.SDKServer.Controllers.Api
msgBody = ikeResponse.ToByteArray()
};
var responsePayload = BuildIkeResponse(ikePacket);
var responsePayload = HttpNetworkManager.Instance. BuildIkeResponse(ikePacket);
Log.Information("Sending ike response packet: " + JsonSerializer.Serialize(ikeResponse));
Log.Information("Built bytes: ");
@@ -153,311 +140,5 @@ namespace Novaria.SDKServer.Controllers.Api
return new EmptyResult();
}
public static byte[] BuildIkeResponse(Packet packet)
{
BinaryWriter packetWriter = new BinaryWriter(new MemoryStream());
byte[] msgIdBytes = BitConverter.GetBytes(packet.msgId);
if (BitConverter.IsLittleEndian)
{
Array.Reverse<byte>(msgIdBytes);
}
packetWriter.Write(msgIdBytes.AsSpan<byte>());
packetWriter.Write(packet.msgBody.AsSpan<byte>());
byte[] packetData = ((MemoryStream)packetWriter.BaseStream).ToArray();
Span<byte> encryptedPacketData = (new byte[packetData.Length + 16]).AsSpan();
AeadTool.Encrypt_ChaCha20(encryptedPacketData, AeadTool.FIRST_IKE_KEY, AeadTool.PS_REQUEST_NONCE, packetData, false);
Log.Information("build: encrypted data:");
Utils.PrintByteArray(encryptedPacketData.ToArray());
BinaryWriter rawResponseWriter = new BinaryWriter(new MemoryStream());
rawResponseWriter.Write(AeadTool.PS_REQUEST_NONCE);
rawResponseWriter.Write(encryptedPacketData);
return ((MemoryStream)rawResponseWriter.BaseStream).ToArray();
}
public static T DecodePacket<T>(Packet packet) where T : IMessage
{
Assembly assembly = Assembly.GetAssembly(typeof(LoginReq));
Type targetType = typeof(T);
PropertyInfo parserProperty = targetType.GetProperty("Parser", BindingFlags.Static | BindingFlags.Public);
object parserInstance = parserProperty.GetValue(null);
MethodInfo parseFromMethod = parserInstance.GetType().GetMethod("ParseFrom", new[] { typeof(byte[]) });
IMessage parsedMessage = (IMessage)parseFromMethod.Invoke(parserInstance, new object[] { packet.msgBody });
if (parsedMessage == null)
{
throw new InvalidOperationException("Failed to parse message.");
}
return (T)parsedMessage;
}
public static IMessage DecodePacket(Type targetType, Packet packet)
{
PropertyInfo parserProperty = targetType.GetProperty("Parser", BindingFlags.Static | BindingFlags.Public);
object parserInstance = parserProperty.GetValue(null);
MethodInfo parseFromMethod = parserInstance.GetType().GetMethod("ParseFrom", new[] { typeof(byte[]) });
IMessage parsedMessage = (IMessage)parseFromMethod.Invoke(parserInstance, new object[] { packet.msgBody });
if (parsedMessage == null)
{
throw new InvalidOperationException("Failed to parse message.");
}
return parsedMessage;
}
public static byte[] BuildResponse(Packet packet)
{
BinaryWriter packetWriter = new BinaryWriter(new MemoryStream());
byte[] msgIdBytes = BitConverter.GetBytes(packet.msgId);
if (BitConverter.IsLittleEndian)
{
Array.Reverse<byte>(msgIdBytes);
}
packetWriter.Write(msgIdBytes.AsSpan<byte>());
packetWriter.Write(packet.msgBody.AsSpan<byte>());
byte[] packetData = ((MemoryStream)packetWriter.BaseStream).ToArray();
Span<byte> encryptedPacketData = (new byte[packetData.Length + 16]).AsSpan();
AeadTool.Encrypt_ChaCha20(encryptedPacketData, AeadTool.key3, AeadTool.PS_REQUEST_NONCE, packetData, false);
Log.Information("build: encrypted data:");
Utils.PrintByteArray(encryptedPacketData.ToArray());
BinaryWriter rawResponseWriter = new BinaryWriter(new MemoryStream());
rawResponseWriter.Write(AeadTool.PS_REQUEST_NONCE);
rawResponseWriter.Write(encryptedPacketData);
return ((MemoryStream)rawResponseWriter.BaseStream).ToArray();
}
public static Packet ParseRequest(byte[] encryptedRawPayload)
{
BinaryReader reader = new BinaryReader(new MemoryStream(encryptedRawPayload));
byte[] nonceBytes = new byte[12]; // nonce 12 bytes length
reader.Read(nonceBytes);
int packetSize = encryptedRawPayload.Length - nonceBytes.Length; // skip nonce length (12)
byte[] packetBytes = new byte[packetSize];
reader.Read(packetBytes);
if (reader.BaseStream.Position != encryptedRawPayload.Length)
{
Log.Error("something went wrong, not all the bytes were read");
Log.Error("reader pos: " + reader.BaseStream.Position);
Log.Error("original len:" + encryptedRawPayload.Length);
}
Span<byte> decrypt_result = new Span<byte>(new byte[packetSize - 16]); // for chacha20 decrypt, the result is 16 bytes less than the input data
Span<byte> nonce = nonceBytes.AsSpan();
Span<byte> packet_data = packetBytes.AsSpan();
bool success = AeadTool.Dencrypt_ChaCha20(decrypt_result, AeadTool.key3, nonce, packet_data, null);
if (!success)
{
Log.Error("something went wrong when chacha20 decrypting the data");
}
byte[] decrypted_bytes = decrypt_result.ToArray();
// might wanna use reader here
byte[] msgid_bytes = decrypted_bytes[10..12]; // two bytes before msg data is msgid
Array.Reverse<byte>(msgid_bytes); // should check BitConverter.IsLittleEndian (if true -> reverse, was true on my pc)
short msgId = BitConverter.ToInt16(msgid_bytes);
Packet packet = new Packet()
{
msgId = msgId,
msgBody = decrypted_bytes[12..], // 2 + 2 (seqid) + 8 (timestamp)
};
return packet;
}
// used for parsing ike requests in ps (or any request ig) client -> server
public static IKEReq ParseIkeRequest(byte[] encryptedRawPayload)
{
bool flag = true; // original this flag is: flag = msg.msgId == 1, so true
BinaryReader reader = new BinaryReader(new MemoryStream(encryptedRawPayload));
byte[] nonceBytes = new byte[12]; // nonce 12 bytes length
reader.Read(nonceBytes);
byte[] aeadBytes = new byte[1]; // aead byte is 1 byte, original field in AeadTool
int packetSize = encryptedRawPayload.Length - nonceBytes.Length; // skip nonce length (12)
if (flag)
{
reader.Read(aeadBytes); // read to skip in memory, idk whats the use for this
packetSize--; // skip in payload
}
byte[] packetBytes = new byte[packetSize];
reader.Read(packetBytes);
if (reader.BaseStream.Position != encryptedRawPayload.Length)
{
Log.Error("something went wrong, not all the bytes were read");
Log.Error("reader pos: " + reader.BaseStream.Position);
Log.Error("original len:" + encryptedRawPayload.Length);
}
Span<byte> decrypt_result = new Span<byte>(new byte[packetSize - 16]); // for chacha20 decrypt, the result is 16 bytes less than the input data
Span<byte> nonce = nonceBytes.AsSpan();
Span<byte> packet_data = packetBytes.AsSpan();
//Console.WriteLine($"Nonce[{nonce.Length}]");
//Utils.PrintByteArray(nonce.ToArray());
//Console.WriteLine($"packet_data[{packet_data.Length}]: ");
//Utils.PrintByteArray(packet_data.ToArray());
//Console.WriteLine($"key[{AeadTool.FIRST_IKE_KEY.Length}]: ");
//Utils.PrintByteArray(AeadTool.FIRST_IKE_KEY.ToArray());
//Console.WriteLine($"Decrypted bytes[{decrypt_result.Length}]: ");
byte[] associateData = new byte[13]; // this is needed for req decrypt (size: nonce(12) + 1)
nonceBytes.CopyTo(associateData, 0); // associateData: [nonce, 1], 1 means AesGcm not supported
associateData[associateData.Length - 1] = 1;
bool success = AeadTool.Dencrypt_ChaCha20(decrypt_result, AeadTool.FIRST_IKE_KEY, nonce, packet_data, associateData);
if (!success)
{
Log.Error("something went wrong when chacha20 decrypting the data");
}
//Utils.PrintByteArray(decrypt_result.ToArray());
byte[] decrypted_bytes = decrypt_result.ToArray();
//Utils.PrintByteArray(decrypted_bytes);
byte[] msgid_bytes = decrypted_bytes[..2]; // first two bytes is msgid
Array.Reverse<byte>(msgid_bytes); // should check BitConverter.IsLittleEndian (if true -> reverse, was true on my pc)
short msgId = BitConverter.ToInt16(msgid_bytes);
Packet packet = new Packet()
{
msgId = msgId,
msgBody = decrypted_bytes[2..],
};
//Console.WriteLine("msgid: " + msgId);
//Console.WriteLine("msgBody length: " + packet.msgBody.Length);
IKEReq ike_request = IKEReq.Parser.ParseFrom(packet.msgBody);
return ike_request;
}
// used to parse offical pcaps, server -> client
public static IKEResp ParseIkeResponse(byte[] encryptedRawPayload)
{
bool flag = false; // false for this pcap ike resp
BinaryReader reader = new BinaryReader(new MemoryStream(encryptedRawPayload));
byte[] nonceBytes = new byte[12]; // nonce 12 bytes length
reader.Read(nonceBytes);
byte[] aeadBytes = new byte[1]; // aead byte is 1 byte, original field in AeadTool
int packetSize = encryptedRawPayload.Length - nonceBytes.Length; // skip nonce length (12)
if (flag)
{
reader.Read(aeadBytes); // read to skip in memory, idk whats the use for this
packetSize--; // skip in payload
}
byte[] packetBytes = new byte[packetSize];
reader.Read(packetBytes);
if (reader.BaseStream.Position != encryptedRawPayload.Length)
{
Log.Error("something went wrong, not all the bytes were read");
Log.Error("reader pos: " + reader.BaseStream.Position);
Log.Error("original len:" + encryptedRawPayload.Length);
}
Span<byte> decrypt_result = new Span<byte>(new byte[packetSize - 16]); // for chacha20, the result is 16 bytes less than the input data
Span<byte> nonce = nonceBytes.AsSpan();
Span<byte> packet_data = packetBytes.AsSpan();
bool success = AeadTool.Dencrypt_ChaCha20(decrypt_result, AeadTool.FIRST_IKE_KEY, nonce, packet_data, null); // associateData NULL FOR THIS response pcap
if (!success)
{
Log.Error("something went wrong when chacha20 decrypting the data");
}
byte[] decrypted_bytes = decrypt_result.ToArray();
byte[] msgid_bytes = decrypted_bytes[..2]; // first two bytes is msgid
Array.Reverse<byte>(msgid_bytes); // should check BitConverter.IsLittleEndian (if true -> reverse, was true on my pc)
short msgId = BitConverter.ToInt16(msgid_bytes);
Packet packet = new Packet()
{
msgId = msgId,
msgBody = decrypted_bytes[2..],
};
IKEResp ike_response = IKEResp.Parser.ParseFrom(packet.msgBody);
return ike_response;
}
//private DiffieHellmanManaged SendKey;
//// put in connection prob
//public static void SetAeadKey(byte[] pubKey) // original lead to HttpNetworkManager
//{
// byte[] array = this.SendKey.DecryptKeyExchange(pubKey);
// int num = array.Length;
// if (num > 32)
// {
// num = 32;
// }
// this.key3 = new byte[32];
// Buffer.BlockCopy(array, 0, this.key3, 0, num);
// this.HasKey3 = true;
// this.seq = 1;
//}
//public byte[] DecryptKeyExchange(byte[] keyEx)
//{
// BigInteger bigInteger = new BigInteger(keyEx).ModPow(this.m_X, this.m_P);
// byte[] bytes = bigInteger.GetBytes();
// bigInteger.Clear();
// return bytes;
//}
}
}

View File

@@ -18,9 +18,9 @@ namespace Novaria.SDKServer.CoNovariaollers
""Data"": {
""Identity"": {
""BirthDate"": """",
""IDCard"": ""500*********11861*"",
""IDCard"": ""123*********34567*"",
""PI"": """",
""RealName"": ""*"",
""RealName"": ""**"",
""State"": 1,
""Type"": 0,
""Underage"": false

View File

@@ -1,7 +1,7 @@
using Google.Protobuf;
using Microsoft.AspNetCore.Mvc;
using Novaria.Common.Crypto;
using Novaria.Common.Utils;
using Novaria.Common.Util;
using Pb;
using Serilog;

View File

@@ -46,9 +46,9 @@ namespace Novaria.SDKServer.CoNovariaollers
},
""Identity"": {
""BirthDate"": """",
""IDCard"": ""500*********11861*"",
""IDCard"": ""123*********34567*"",
""PI"": """",
""RealName"": ""*"",
""RealName"": ""**"",
""State"": 1,
""Type"": 0,
""Underage"": false
@@ -65,13 +65,13 @@ namespace Novaria.SDKServer.CoNovariaollers
""ID"": 1,
""PID"": ""CN-NOVA"",
""State"": 1,
""Token"": ""f94d936f7235f84493564ee0282e845cccd44828""
""Token"": ""f94d936f723asdasd5f84493564ee0282e845cccd44828""
},
""Yostar"": {
""CreatedAt"": 1735902342,
""CreatedAt"": 1735902322,
""DefaultNickName"": """",
""ID"": 1,
""NickName"": ""夏萝莉是小楠梁"",
""NickName"": ""seggs"",
""Picture"": """",
""State"": 1
}
@@ -106,13 +106,13 @@ namespace Novaria.SDKServer.CoNovariaollers
""ID"": 1,
""PID"": ""CN-NOVA"",
""State"": 1,
""Token"": ""f94d936f7235f84493564ee0282e845cccd44828""
""Token"": ""f9s243e483e3e322138""
},
""Yostar"": {
""CreatedAt"": 1,
""DefaultNickName"": """",
""ID"": 1,
""NickName"": ""夏萝莉是小楠梁"",
""NickName"": ""seggs"",
""Picture"": """",
""State"": 1
}

View File

@@ -1,23 +1,15 @@
using System.Collections;
using System.Reflection;
using Google.Protobuf;
using Google.Protobuf.Reflection;
using System.Reflection;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Novaria.Common.Crypto;
using Novaria.Common.Utils;
using Novaria.Common.Core;
using Proto;
using Serilog;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Http;
using System.IO;
using NSec.Cryptography;
using static System.Runtime.InteropServices.JavaScript.JSType;
using Novaria.SDKServer.Controllers.Api;
using Novaria.SDKServer.Controllers.Api.ProtocolHandlers;
using Novaria.Common.Util;
using System.Numerics;
using Microsoft.AspNetCore.DataProtection;
using System.Text;
namespace Novaria.SDKServer
{
@@ -25,125 +17,33 @@ namespace Novaria.SDKServer
{
public static void Main(string[] args)
{
//byte[] result = new byte[] { 72, 115, 142, 228, 125, 80, 99, 136, 69, 104, 100, 1, 40, 52, 96, 130, 76, 79, 164, 172, 151, 94, 234, 141, 235, 198, 110, 211, 218, 213, 155, 111, 64, 170, 181, 53, 117, 77, 234, 162, 247, 154, 219, 157, 50, 59, 84, 148, 165, 122, 236, 76, 15, 134, 140, 229, 224, 105, 90, 2, 226, 240, 240, 16, 6, 209, 29, 11, 52, 224, 26, 174, 113, 114, 54, 169, 108, 63, 53, 170, 94, 142, 153, 65, 192, 116, 145, 127, 107, 137, 193, 141, 203, 149, 9, 188, 91, 78, 191, 117, 145, 14, 105, 108, 203, 106, 161, 97, 133, 123, 132, 205, 151, 184, 240, 165 };
// BigInteger p = BigInteger.Parse("1552518092300708935130918131258481755631334049434514313202351194902966239949102107258669453876591642442910007680288864229150803718918046342632727613031282983744380820890196288509170691316593175367469551763119843371637221007210577919");
// BigInteger g = 2;
//byte[] key = new byte[] { 51, 76, 83, 57, 38, 111, 89, 100, 115, 112, 94, 53, 119, 105, 56, 38, 90, 120, 67, 35, 99, 55, 77, 90, 103, 55, 51, 104, 98, 69, 68, 119 };
//byte[] nonce = new byte[] { 50, 103, 122, 81, 54, 57, 80, 109, 55, 49, 86, 55 };
//byte[] data = new byte[] { 0, 1, 10, 96, 30, 72, 185, 252, 24, 18, 102, 148, 64, 6, 58, 221, 16, 109, 243, 95, 183, 216, 77, 49, 124, 134, 97, 3, 136, 61, 184, 215, 41, 126, 194, 60, 133, 13, 82, 220, 41, 147, 27, 174, 137, 10, 6, 222, 251, 18, 201, 192, 53, 249, 223, 27, 67, 209, 125, 129, 139, 32, 32, 225, 233, 158, 23, 191, 187, 238, 129, 226, 180, 85, 152, 50, 137, 33, 65, 129, 225, 180, 154, 172, 39, 177, 136, 36, 141, 168, 43, 144, 155, 99, 85, 21, 213, 194, 139, 112 };
// byte[] cprivBytes = new byte[] { 40, 155, 225, 54 }.Reverse().ToArray();
// BigInteger cpriv = new BigInteger(cprivBytes);
//byte[] real_result = new byte[result.Length];
//byte[] decrypted_result = new byte[data.Length];
// byte[] spubBytes = new byte[] {
// 100, 141, 208, 69, 213, 140, 78, 33, 138, 99, 79, 253, 190, 117, 15, 56,
// 17, 222, 37, 150, 138, 74, 93, 159, 17, 17, 178, 23, 44, 140, 32, 73,
// 170, 24, 92, 31, 123, 53, 174, 72, 7, 70, 248, 248, 223, 0, 69, 92,
// 72, 152, 30, 173, 95, 36, 76, 19, 213, 82, 134, 122, 182, 190, 153, 145,
// 5, 116, 66, 91, 123, 140, 191, 23, 162, 169, 48, 142, 252, 184, 200, 25,
// 94, 34, 174, 238, 100, 171, 37, 30, 65, 63, 37, 106, 127, 116, 168, 36
//}.Reverse().ToArray();
// BigInteger spub = new BigInteger(spubBytes);
//AeadTool.Encrypt_ChaCha20(real_result, key.AsSpan(), nonce.AsSpan(), data.AsSpan(), true);
//Console.WriteLine();
//Console.WriteLine();
// BigInteger secret = BigInteger.ModPow(spub, cpriv, p);
// Utils.PrintByteArray(secret.ToByteArray(true, true)[..32]);
//byte[] cpriv = new byte[] { 40, 155, 225, 54 };
//byte[] spub = new byte[] { 176, 163, 182, 200, 125, 114, 33, 250, 247, 248, 251, 221, 150, 201, 88, 110, 59, 57, 39, 50, 101, 192, 39, 150, 201, 13, 105, 244, 142, 114, 153, 140, 233, 105, 24, 51, 152, 226, 89, 227, 40, 228, 32, 129, 51, 97, 4, 41, 80, 121, 253, 208, 220, 54, 124, 169, 147, 192, 144, 127, 182, 71, 7, 163, 138, 11, 161, 112, 200, 163, 221, 153, 140, 185, 6, 1, 101, 45, 158, 155, 251, 143, 113, 250, 62, 51, 79, 240, 227, 125, 239, 245, 73, 103, 176, 219, 0 };
//byte[] result = DiffieHellman.Instance.CalculateKey(spub);
//Utils.PrintByteArray(result);
//Console.WriteLine();
//Console.WriteLine();
//Utils.PrintByteArray(real_result);
//byte[] associateData = new byte[13];
//nonce.CopyTo(associateData, 0);
//associateData[associateData.Length - 1] = 1;
//bool succ = AeadTool.Dencrypt_ChaCha20(decrypted_result.AsSpan(), key.AsSpan(), nonce.AsSpan(), result.AsSpan(), associateData.AsSpan());
//Console.WriteLine(succ);
//Utils.PrintByteArray(decrypted_result);
//return;
//byte[] ike_req_bytes = new byte[] { 10, 96, 108, 183, 187, 238, 19, 29, 80, 121, 193, 82, 107, 114, 202, 58, 16, 52, 131, 72, 179, 174, 18, 170, 8, 124, 161, 231, 241, 121, 27, 190, 176, 218, 90, 9, 9, 88, 190, 59, 190, 128, 9, 247, 199, 212, 136, 3, 198, 175, 89, 222, 172, 222, 157, 99, 247, 254, 237, 66, 68, 238, 188, 125, 169, 248, 17, 130, 150, 12, 213, 10, 197, 36, 122, 134, 139, 88, 52, 177, 53, 38, 114, 145, 99, 40, 111, 82, 237, 245, 178, 106, 108, 170, 57, 235, 136, 25 };
//byte[] player_login_req_bytes = new byte[] { 106, 8, 79, 102, 102, 105, 99, 105, 97, 108, 26, 48, 8, 240, 186, 151, 225, 3, 18, 40, 50, 50, 97, 99, 56, 56, 54, 101, 53, 102, 98, 101, 101, 102, 52, 100, 98, 98, 56, 100, 56, 51, 54, 100, 56, 98, 99, 98, 49, 54, 54, 54, 56, 100, 98, 55, 99, 97, 55, 100, 98, 5, 122, 104, 95, 67, 78, 120, 1, 114, 16, 100, 99, 49, 56, 101, 49, 50, 98, 57, 54, 50, 57, 101, 57, 52, 101, 88, 2 };
//byte[] ike_req_raw_bytes = new byte[] { 50, 103, 122, 81, 54, 57, 80, 109, 55, 49, 86, 55, 1, 72, 115, 142, 228, 125, 80, 99, 136, 69, 104, 100, 1, 40, 52, 96, 130, 76, 79, 164, 172, 151, 94, 234, 141, 235, 198, 110, 211, 218, 213, 155, 111, 64, 170, 181, 53, 117, 77, 234, 162, 247, 154, 219, 157, 50, 59, 84, 148, 165, 122, 236, 76, 15, 134, 140, 229, 224, 105, 90, 2, 226, 240, 240, 16, 6, 209, 29, 11, 52, 224, 26, 174, 113, 114, 54, 169, 108, 63, 53, 170, 94, 142, 153, 65, 192, 116, 145, 127, 107, 137, 193, 141, 203, 149, 9, 188, 91, 78, 191, 117, 145, 14, 105, 108, 203, 106, 161, 97, 133, 123, 132, 205, 151, 184, 240, 165 };
//ike_req_raw_bytes = new byte[] { 238, 229, 238, 9, 37, 193, 132, 180, 61, 153, 199, 186, 4, 244, 254, 140, 91, 117, 71, 80, 159, 135, 140, 203, 228, 77, 27, 179, 254, 183, 72, 176, 185, 189, 30, 193, 34, 117, 133, 225, 239, 52, 174, 149, 44, 203, 37, 116, 83, 34, 75, 49, 105, 102, 145, 145, 115, 125, 172, 100, 65, 173, 57, 198, 79, 9, 134, 169, 31, 138, 51, 247, 255, 85, 206, 195, 234, 144, 11, 167, 183, 159, 116, 21, 251, 193, 223, 68, 116, 202, 26, 223, 20, 101, 176, 136, 156, 88, 127, 113, 169, 34, 76, 180, 3, 157, 201, 127, 241, 46, 61, 56, 25, 18, 187, 103, 34, 64, 7, 115, 227, 135, 159, 149, 199, 166, 11, 119, 122, 225, 23, 134, 5, 63, 21, 182, 106, 94, 200, 13, 63, 231, 212, 51, 255, 81, 237, 238, 4, 156, 94, 149, 215, 120, 81, 14, 191, 209, 237, 139, 209, 238, 130, 202, 51, 63, 238, 214, 68, 109, 114, 108, 191, 208, 255, 32, 171, 246, 208, 139, 6, 223, 0, 120, 1, 104, 44, 82, 72, 129, 181, 237, 8, 213, 196, 193, 87, 114, 247, 12, 12, 218, 159, 182, 248, 63, 22, 207, 124, 217, 93, 121, 0, 49, 49, 237, 167, 215, 85, 173, 89, 152, 81, 151, 134, 240, 142, 208, 89, 9, 103, 243, 130, 221, 142, 44, 141, 64, 254, 215, 116, 155, 127, 32, 108, 178, 226 };
//ike_req_raw_bytes = File.ReadAllBytes("E:\\documents\\Decompiling\\Extracted\\NOVA\\Novaria\\Novaria.SDKServer\\options_req");
//Console.WriteLine(ike_req_raw_bytes.Length);
//var ikereq = IKEReq.Parser.ParseFrom(ike_req_bytes);
//var ikerespBytes = new byte[] { 78, 173, 172, 234, 27, 192, 183, 83, 241, 101, 79, 213, 243, 225, 240, 5, 41, 112, 109, 167, 81, 176, 134, 17, 168, 113, 190, 218, 149, 218, 125, 173, 222, 18, 22, 107, 231, 62, 150, 140, 247, 49, 202, 165, 28, 147, 44, 92, 44, 195, 74, 239, 43, 79, 21, 38, 84, 1, 3, 140, 144, 217, 89, 35, 181, 20, 42, 253, 94, 67, 157, 192, 69, 30, 75, 36, 91, 207, 176, 221, 143, 71, 129, 114, 101, 77, 166, 231, 250, 60, 67, 69, 152, 179, 125, 156, 175, 58, 157, 189, 111, 172, 198, 143, 3, 82, 0, 57, 236, 247, 184, 229, 91, 46, 60, 37, 190, 188, 21, 215, 130, 113, 225, 167, 175, 218, 71, 96, 174, 38, 191, 244, 1, 31, 82, 131, 179, 160, 194, 226, 79, 196, 119, 109, 159, 228, 153, 1, 93, 60, 154, 175, 53, 65, 202, 30, 24, 63, 50, 54, 39, 195, 43, 49, 123, 4, 231, 215, 171, 236, 54, 120, 247, 71, 239, 187, 84, 35, 60, 52, 139, 72, 88, 234, 54, 137, 174, 200, 232, 118, 126, 152, 212, 205, 252, 197, 10, 183, 218, 198, 155, 248, 14, 154, 13, 177, 46, 216, 236, 121, 93, 0, 99, 6, 118, 202, 39, 60, 214, 193, 93, 158, 220, 186, 103, 120, 134, 58, 48, 185, 234, 17, 157, 233, 101, 30, 207, 112, 247, 201, 11, 35, 152, 102, 121, 235, 113 };
//var loginreq = LoginResp.Parser.ParseFrom(player_login_req_bytes);
//var ikeResponse = GatewayController.ParseIkeResponse(ikerespBytes);
//IKEResp ikeResp = new IKEResp()
//{
// PubKey = ByteString.;
//}
//IKEResp ikeResponse = new IKEResp()
//{
// PubKey = ByteString.CopyFrom(AeadTool.PS_PUBLIC_KEY),
// Token = AeadTool.TOKEN
//};
//Console.WriteLine(JsonSerializer.Serialize(ikeResponse));
//Packet ikePacket = new Packet()
//{
// msgId = 2,
// msgBody = ikeResponse.ToByteArray()
//};
//var responsePayload = GatewayController.BuildIkeResponse(ikePacket);
//Console.WriteLine();
//Console.WriteLine("built response");
//Utils.PrintByteArray(responsePayload);
//Console.WriteLine("now deserializing...");
//IKEResp reparsed = GatewayController.ParseIkeResponse(responsePayload);
//Console.WriteLine();
//Console.WriteLine("reparsed:");
//Console.WriteLine(JsonSerializer.Serialize(reparsed));
//Console.WriteLine(JsonSerializer.Serialize(ikeresp));
//Console.WriteLine(Encoding.ASCII.GetString(new byte[] { 18, 111, 108, 118, 121, 50, 118, 73, 75, 73, 70, 78, 106, 74, 72, 106, 101, 122, 87, 70, 83, 50, 71, 47, 69, 109, 108, 48, 106, 99, 50, 56, 102, 122, 98, 74, 75, 54, 111, 57, 98, 50, 90, 70, 88, 43, 113, 69, 84, 69, 109, 97, 74, 77, 97, 80, 43, 80, 43, 110, 43, 79, 105, 77, 78, 77, 82, 108, 108, 82, 74, 67, 74, 106, 56, 110, 54, 73, 103, 54, 76, 104, 77, 98, 47, 80, 50, 120, 88, 89, 103, 122, 71, 43, 112, 86, 105, 73, 66, 47, 111, 49, 97, 116, 52, 104, 115, 50, 81, 114, 57, 70, 48, 24, 193, 167, 138, 188, 6 }));
//Utils.PrintByteArray(Encoding.ASCII.GetBytes("Di9OhjFgkabvOO26XfjOzQ4/IcQ6yaFuK23tE2yw9Q7yYs5B53Zffs1e4DygW4IFgCFBtDKwAJtddxYmPfnjCfpCGk5UOAdLCH1/0NLHf+tl/Qc4GuG7jaK0Lcs75gHcSmRUkA"));
//return;
//DiffieHellman diffieHellman = new DiffieHellman();
//AeadTool.CLIENT_PUBLIC_KEY = new byte[] { 151, 9, 53, 80, 250, 198, 4, 44, 108, 79, 114, 12, 63, 205, 144, 209, 138, 249, 76, 59, 211, 197, 174, 161, 78, 160, 16, 207, 169, 191, 75, 62, 211, 60, 248, 150, 194, 187, 98, 205, 240, 94, 229, 220, 213, 80, 209, 248, 240, 233, 81, 202, 102, 219, 205, 218, 235, 231, 180, 151, 148, 128, 206, 225, 244, 53, 207, 81, 136, 29, 113, 12, 51, 35, 193, 207, 174, 213, 19, 223, 29, 97, 200, 115, 243, 241, 88, 200, 255, 172, 225, 108, 132, 68, 150, 36 };
//byte[] calculatedKey = diffieHellman.CalculateSharedSecret(AeadTool.CLIENT_PUBLIC_KEY);
//Console.WriteLine("CALCULATED KEY: ");
//Utils.PrintByteArray(calculatedKey);
//return;
//byte[] firstReq = new byte[] { 90, 118, 89, 105, 76, 78, 54, 66, 118, 90, 97, 118, 69, 106, 227, 29, 185, 133, 2, 13, 102, 246, 198, 128, 110, 183, 97, 177, 211, 238, 130, 14, 32, 18, 36, 207, 124, 183, 86, 150, 155, 206, 31, 224, 74, 248, 142, 124, 168, 12, 179, 96, 157, 140, 21, 1, 223, 64, 54, 118, 137, 202, 12, 11, 229, 151, 82, 48, 229, 8, 170, 35, 236, 196, 247, 249, 235, 178, 227, 252, 146, 54, 17, 205, 93, 175, 7, 196, 123, 136, 204, 154, 60, 33, 179, 87, 206, 138, 76, 87, 64, 109, 147, 254, 148, 70, 26, 195, 231, 190, 186, 118, 218, 247, 13, 63, 240, 89, 41, 17, 56, 151, 5, 211, 4 };
//Packet requestPacket = GatewayController.ParseRequest(firstReq);
//Console.WriteLine("---");
//Utils.PrintByteArray(requestPacket.msgBody);
//Console.WriteLine("---");
//Log.Information("Sucessfully parsed request packet, id: " + requestPacket.msgId);
//LoginReq loginreq = GatewayController.DecodePacket<LoginReq>(requestPacket);
//Log.Information("login_req received, contents: " + JsonSerializer.Serialize(loginreq));
//Log.Information("Building login resp...");
//LoginResp loginResp = new LoginResp()
//{
// Token = "seggstoken",
//};
//Packet responsePacket = new Packet()
//{
// msgId = 5,
// msgBody = loginResp.ToByteArray()
//};
//byte[] responsePackeBytes = GatewayController.BuildResponse(requestPacket);
//Utils.PrintByteArray(responsePackeBytes);
//return;
Log.Information("Starting SDK Server...");
try