Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to scan ports with Java

I made a very simple port scanner, but it runs too slow, so I'm looking for a way to make it scan faster. Here is my code:

public boolean portIsOpen(String ip, int port, int timeout) {     try {         Socket socket = new Socket();         socket.connect(new InetSocketAddress(ip, port), timeout);         socket.close();         return true;     } catch (Exception ex) {         return false;     } } 

This code tests if a specific port is open on a specific ip. For timeout I used a minimum value of 200 because when I go lower it doesn't have enough time to test the port.

It works well, but takes too much to scan from 0 to 65535. Is there any other way that could maybe scan from 0 to 65535 in less than 5 minutes?

like image 898
Rohit Malish Avatar asked Jul 18 '12 17:07

Rohit Malish


People also ask

What is the most used tool for port scanning?

NMap is the most popular port scanner for system administrators, network engineers, and developers. Angry IP Scanner is also a popular tool for scanning the local network and the internet.

How do I scan for ports?

You'll use the netstat program to identify open ports, and then use the nmap program to get information about the state of a machine's ports on a network. When you're done you'll be able to identify common ports and scan your systems for open ports.

How do I scan all ports 65535?

Scan All TCP Ports with Range We can specify the port range with the -p option. As we know TCP port numbers are between and 65535 . We will use -p0-65535 as an option in order to scan all TCP ports. We do not specify the TCP protocol because the default protocol for Nmap port scan is TCP.

Which command helps to scan ports?

Type "netstat -a" in the Command Prompt window, and press "Enter." The computer displays a list of all open TCP and UDP ports.


1 Answers

If you need 200ms for each of the 65536 ports (in the worst case, a firewall is blocking everything, thus making you hit your timeout for every single port), the maths is pretty simple: you need 13k seconds, or about 3 hours and a half.

You have 2 (non-exclusive) options to make it faster:

  • reduce your timeout
  • paralellize your code

Since the operation is I/O bound (in contrast to CPU bound -- that is, you spend time waiting for I/O, and not for some huge calculation to complete), you can use many, many threads. Try starting with 20. They would divide the 3 hours and a half among them, so the maximum expected time is about 10 minutes. Just remember that this will put pressure on the other side, ie, the scanned host will see huge network activity with "unreasonable" or "strange" patterns, making the scan extremely easy to detect.

The easiest way (ie, with minimal changes) is to use the ExecutorService and Future APIs:

public static Future<Boolean> portIsOpen(final ExecutorService es, final String ip, final int port, final int timeout) {   return es.submit(new Callable<Boolean>() {       @Override public Boolean call() {         try {           Socket socket = new Socket();           socket.connect(new InetSocketAddress(ip, port), timeout);           socket.close();           return true;         } catch (Exception ex) {           return false;         }       }    }); } 

Then, you can do something like:

public static void main(final String... args) {   final ExecutorService es = Executors.newFixedThreadPool(20);   final String ip = "127.0.0.1";   final int timeout = 200;   final List<Future<Boolean>> futures = new ArrayList<>();   for (int port = 1; port <= 65535; port++) {     futures.add(portIsOpen(es, ip, port, timeout));   }   es.shutdown();   int openPorts = 0;   for (final Future<Boolean> f : futures) {     if (f.get()) {       openPorts++;     }   }   System.out.println("There are " + openPorts + " open ports on host " + ip + " (probed with a timeout of " + timeout + "ms)"); } 

If you need to know which ports are open (and not just how many, as in the above example), you'd need to change the return type of the function to Future<SomethingElse>, where SomethingElse would hold the port and the result of the scan, something like:

public final class ScanResult {   private final int port;   private final boolean isOpen;   // constructor   // getters } 

Then, change Boolean to ScanResult in the first snippet, and return new ScanResult(port, true) or new ScanResult(port, false) instead of just true or false

EDIT: Actually, I just noticed: in this particular case, you don't need the ScanResult class to hold result + port, and still know which port is open. Since you add the futures to a List, which is ordered, and, later on, you process them in the same order you added them, you could have a counter that you'd increment on each iteration to know which port you are dealing with. But, hey, this is just to be complete and precise. Don't ever try doing that, it is horrible, I'm mostly ashamed that I thought about this... Using the ScanResult object is much cleaner, the code is way easier to read and maintain, and allows you to, later, for example, use a CompletionService to improve the scanner.

like image 76
Bruno Reis Avatar answered Sep 23 '22 17:09

Bruno Reis