From d2ef1034f1a1fd22c20c20e197532d1d466e926c Mon Sep 17 00:00:00 2001 From: Timothy Schenk Date: Thu, 10 Aug 2023 08:45:51 +0200 Subject: [PATCH] feat: refactor sessions --- Server/AuthSession.cs | 207 ++++-------------------- Server/ChannelSession.cs | 66 ++++++++ Server/Services/WonderkingAuthServer.cs | 2 +- Server/WonderkingSession.cs | 21 --- 4 files changed, 102 insertions(+), 194 deletions(-) create mode 100644 Server/ChannelSession.cs delete mode 100644 Server/WonderkingSession.cs diff --git a/Server/AuthSession.cs b/Server/AuthSession.cs index 9c33a07..d5aee69 100644 --- a/Server/AuthSession.cs +++ b/Server/AuthSession.cs @@ -11,192 +11,55 @@ public abstract class AuthSession : TcpSession private readonly ILogger _logger; private readonly IMediator _mediator; - private static readonly byte[] Key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7 } - .Reverse().ToArray(); - - private static readonly byte[] IV = new byte[] - { 0xfe, 220, 0xba, 0x98, 0x76, 0x54, 50, 0x10, 15, 30, 0x2d, 60, 0x4b, 90, 0x69, 120 }.Reverse().ToArray(); - - private readonly ICryptoTransform _encryptor; - private readonly ICryptoTransform _decryptor; - protected AuthSession(TcpServer server, ILogger logger, IMediator mediator) : base(server) { _logger = logger; _mediator = mediator; - var aes = Aes.Create(); - aes.Key = Key; - aes.IV = IV; - aes.Padding = PaddingMode.None; - aes.Mode = CipherMode.ECB; - - _decryptor = aes.CreateDecryptor(aes.Key, aes.IV); - _encryptor = aes.CreateEncryptor(aes.Key, aes.IV); - } - - public override long Send(byte[] buffer) - { - byte[] encryptedBuffer; - using (var ms = new MemoryStream()) - using (var cs = new CryptoStream(ms, _encryptor, CryptoStreamMode.Write)) - { - cs.Write(buffer); - cs.FlushFinalBlock(); - encryptedBuffer = ms.ToArray(); - } - - return base.Send(encryptedBuffer); - } - - public override long Send(byte[] buffer, long offset, long size) - { - byte[] encryptedBuffer; - using (var ms = new MemoryStream()) - using (var cs = new CryptoStream(ms, _encryptor, CryptoStreamMode.Write)) - { - cs.Write(buffer, (int)offset, (int)size); - cs.FlushFinalBlock(); - encryptedBuffer = ms.ToArray(); - } - - return base.Send(encryptedBuffer, 0, encryptedBuffer.Length); - } - - public override long Send(ReadOnlySpan buffer) - { - Span encryptedBuffer; - using (var ms = new MemoryStream()) - using (var cs = new CryptoStream(ms, _encryptor, CryptoStreamMode.Write)) - { - cs.Write(buffer); - cs.FlushFinalBlock(); - encryptedBuffer = ms.ToArray(); - } - - return base.Send(encryptedBuffer); - } - - public override bool SendAsync(byte[] buffer) - { - byte[] encryptedBuffer; - using (var ms = new MemoryStream()) - using (var cs = new CryptoStream(ms, _encryptor, CryptoStreamMode.Write)) - { - cs.Write(buffer); - cs.FlushFinalBlock(); - encryptedBuffer = ms.ToArray(); - } - - return base.SendAsync(encryptedBuffer); - } - - public override bool SendAsync(byte[] buffer, long offset, long size) - { - byte[] encryptedBuffer; - using (var ms = new MemoryStream()) - using (var cs = new CryptoStream(ms, _encryptor, CryptoStreamMode.Write)) - { - cs.Write(buffer, (int)offset, (int)size); - cs.FlushFinalBlock(); - encryptedBuffer = ms.ToArray(); - } - - return base.SendAsync(encryptedBuffer, 0, encryptedBuffer.Length); - } - - public override bool SendAsync(ReadOnlySpan buffer) - { - Span encryptedBuffer; - using (var ms = new MemoryStream()) - using (var cs = new CryptoStream(ms, _encryptor, CryptoStreamMode.Write)) - { - cs.Write(buffer); - cs.FlushFinalBlock(); - encryptedBuffer = ms.ToArray(); - } - - return base.SendAsync(encryptedBuffer); - } - - public override long Receive(byte[] buffer) - { - Span decryptedBuffer; - using (var ms = new MemoryStream()) - using (var cs = new CryptoStream(ms, _decryptor, CryptoStreamMode.Write)) - { - cs.Write(buffer); - cs.FlushFinalBlock(); - decryptedBuffer = ms.ToArray(); - } - - return base.Receive(decryptedBuffer.ToArray()); - } - - public override long Receive(byte[] buffer, long offset, long size) - { - byte[] decryptedBuffer; - using (var ms = new MemoryStream()) - using (var cs = new CryptoStream(ms, _decryptor, CryptoStreamMode.Write)) - { - cs.Write(buffer, (int)offset, (int)size); - cs.FlushFinalBlock(); - decryptedBuffer = ms.ToArray(); - } - - return base.Receive(decryptedBuffer, 0, decryptedBuffer.Length); } protected override void OnReceived(byte[] buffer, long offset, long size) { - try + Console.WriteLine($"Length: {size} & offset: {offset}"); + //Console.WriteLine(BitConverter.ToString(buffer.ToArray()).Replace("-", string.Empty)); + Span decryptedBuffer = new byte[size]; + + + // xor every value after the first 8 bytes + // then apply AES decryption + var dataBuffer = Decrypt(new ArraySegment(buffer, 8, (int)size - 8).ToArray()); + /*using (var ms = new MemoryStream(dataBuffer)) + using (var cs = new CryptoStream(ms, _decryptor, CryptoStreamMode.Read)) { - Console.WriteLine($"Length: {size} & offset: {offset}"); - //Console.WriteLine(BitConverter.ToString(buffer.ToArray()).Replace("-", string.Empty)); - Span decryptedBuffer = new byte[size]; + cs.Read(decryptedBuffer); + }*/ - // xor every value after the first 8 bytes - // then apply AES decryption - var dataBuffer = Decrypt(new ArraySegment(buffer, 8, (int)size - 8).ToArray()); - /*using (var ms = new MemoryStream(dataBuffer)) - using (var cs = new CryptoStream(ms, _decryptor, CryptoStreamMode.Read)) - { - cs.Read(decryptedBuffer); - }*/ + Console.WriteLine("Length " + BitConverter.ToUInt16(buffer, 0)); + var opCode = BitConverter.ToUInt16(buffer.ToArray(), 2); + Console.WriteLine("Packet Op Code: " + opCode); + Console.WriteLine("Some Value: " + buffer[4]); + /* + * 20s = 5 + * 15s = 4 + * 10s = 3 + * + * client alive time * 5s => uptime + */ + var clientAliveTime = BitConverter.ToUInt16(buffer.ToArray(), 5); + Console.WriteLine("Client Alive time: " + clientAliveTime); + Console.WriteLine("Might be a flag:" + buffer[7]); + Console.WriteLine("username: " + Encoding.ASCII.GetString(dataBuffer.ToArray(), 0, 20)); + Console.WriteLine("password: " + Encoding.ASCII.GetString(dataBuffer.ToArray(), 20, 32)); + Console.WriteLine("Full buffer: " + Encoding.ASCII.GetString(dataBuffer.ToArray())); - Console.WriteLine("Length " + BitConverter.ToUInt16(buffer, 0)); - var opCode = BitConverter.ToUInt16(buffer.ToArray(), 2); - Console.WriteLine("Packet Op Code: " + opCode); - Console.WriteLine("Some Value: " + buffer[4]); - /* - * 20s = 5 - * 15s = 4 - * 10s = 3 - * - * client alive time * 5s => uptime - */ - var clientAliveTime = BitConverter.ToUInt16(buffer.ToArray(), 5); - Console.WriteLine("Client Alive time: " + clientAliveTime); - Console.WriteLine("Might be a flag:" + buffer[7]); - - Console.WriteLine("username: " + Encoding.ASCII.GetString(dataBuffer.ToArray(), 0, 20)); - Console.WriteLine("password: " + Encoding.ASCII.GetString(dataBuffer.ToArray(), 20, 32)); - Console.WriteLine("Full buffer: " + Encoding.ASCII.GetString(dataBuffer.ToArray())); - - RawPacket rawPacket = new RawPacket((OperationCode)opCode, dataBuffer, clientAliveTime, buffer[0], - buffer[3], - Id, this); - Parallel.Invoke(() => _mediator.Send(rawPacket)); - _logger.LogInformation("Connection from: {@RemoteEndpoint}", Socket.RemoteEndPoint?.ToString()); - base.OnReceived(decryptedBuffer.ToArray(), offset, decryptedBuffer.Length); - } - catch (CryptographicException ex) - { - _logger.LogError("An error has occured while decrypting: {ErrorMessage}", ex.Message); - _logger.LogError("Default buffer message: {Message}", Encoding.ASCII.GetString(buffer)); - } + RawPacket rawPacket = new RawPacket((OperationCode)opCode, dataBuffer, clientAliveTime, buffer[0], + buffer[3], + Id, this); + Parallel.Invoke(() => _mediator.Send(rawPacket)); + _logger.LogInformation("Connection from: {@RemoteEndpoint}", Socket.RemoteEndPoint?.ToString()); + base.OnReceived(decryptedBuffer.ToArray(), offset, decryptedBuffer.Length); } private byte[] Decrypt(byte[] buffer) diff --git a/Server/ChannelSession.cs b/Server/ChannelSession.cs new file mode 100644 index 0000000..eb5c127 --- /dev/null +++ b/Server/ChannelSession.cs @@ -0,0 +1,66 @@ +using System.Security.Cryptography; +using System.Text; +using MassTransit; +using MassTransit.Mediator; +using Microsoft.Extensions.Logging; +using NetCoreServer; + +namespace Server; + +public class ChannelSession : TcpSession +{ + private readonly IMediator _mediator; + private readonly ILogger _logger; + + private static readonly byte[] Key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7 } + .Reverse().ToArray(); + + private static readonly byte[] IV = new byte[] + { 0xfe, 220, 0xba, 0x98, 0x76, 0x54, 50, 0x10, 15, 30, 0x2d, 60, 0x4b, 90, 0x69, 120 }.Reverse().ToArray(); + + private readonly ICryptoTransform _encryptor; + private readonly ICryptoTransform _decryptor; + + public ChannelSession(TcpServer server, IMediator mediator, ILogger logger) : base(server) + { + _mediator = mediator; + _logger = logger; + var aes = Aes.Create(); + aes.Key = Key; + aes.IV = IV; + aes.Padding = PaddingMode.None; + aes.Mode = CipherMode.ECB; + + _decryptor = aes.CreateDecryptor(aes.Key, aes.IV); + _encryptor = aes.CreateEncryptor(aes.Key, aes.IV); + } + + protected override void OnReceived(byte[] buffer, long offset, long size) + { + try + { + using (var ms = new MemoryStream(Decrypt(buffer))) + using (var cs = new CryptoStream(ms, _decryptor, CryptoStreamMode.Read)) + { + cs.Read(buffer); + } + + base.OnReceived(buffer, offset, size); + } + catch (CryptographicException ex) + { + _logger.LogError("An error has occured while decrypting: {ErrorMessage}", ex.Message); + _logger.LogError("Default buffer message: {Message}", Encoding.ASCII.GetString(buffer)); + } + } + + private byte[] Decrypt(byte[] buffer) + { + for (int i = 0; i < buffer.Length; ++i) + { + buffer[i] = (byte)(buffer[i] ^ i ^ (3 * (0xFE - i))); + } + + return buffer; + } +} \ No newline at end of file diff --git a/Server/Services/WonderkingAuthServer.cs b/Server/Services/WonderkingAuthServer.cs index 2b16dd8..e1fefa3 100644 --- a/Server/Services/WonderkingAuthServer.cs +++ b/Server/Services/WonderkingAuthServer.cs @@ -24,7 +24,7 @@ public class WonderkingAuthServer : TcpServer, IHostedService } protected override TcpSession CreateSession() => - ActivatorUtilities.CreateInstance(_serviceProvider, this); + ActivatorUtilities.CreateInstance(_serviceProvider, this); protected override void OnStarting() { diff --git a/Server/WonderkingSession.cs b/Server/WonderkingSession.cs deleted file mode 100644 index 2cfa63a..0000000 --- a/Server/WonderkingSession.cs +++ /dev/null @@ -1,21 +0,0 @@ -using MassTransit; -using MassTransit.Mediator; -using Microsoft.Extensions.Logging; -using NetCoreServer; - -namespace Server; - -public class WonderkingSession : AuthSession -{ - private readonly IMediator _mediator; - - public WonderkingSession(TcpServer server, IMediator mediator, ILogger logger) : base(server, logger, mediator) - { - _mediator = mediator; - } - - protected override void OnReceived(byte[] buffer, long offset, long size) - { - base.OnReceived(buffer, offset, size); - } -} \ No newline at end of file