-2

I have created a port scanner with multi-threading that scans ports, starting at 1 and going up in numbers. It works great on my LAN but if I try to scan an IP outside of my LAN it says all ports are closed, which I assume is because the firewall is blocking the connection?

Is there any way to check if the port is open on an IP address outside my LAN? This is the code I've got

for (int port = startPort; port < 65535; port+=PortScanner.count) 
        {
            Socket socket;
                try 
                {
                    socket = new Socket();
                    socket.connect(new InetSocketAddress(host, port ), 500);

                    openPorts.add(port);

                    foundPort = true;
                    String temp = "Port " + port + " is open.\n";
                    System.out.write(temp.getBytes());

                    socket.close();
                }
                catch (IOException ioEx) 
                {
                    System.out.println("Port " + port + " is closed");
                }
         }
         Thread.yield();
Albin
  • 7
  • 6
  • 1
    Maybe you are simply making the wrong assumptions: If you take an IP address which is not used by any system or which is not reachable by your system it will also claim that all ports are closed. – Steffen Ullrich Oct 29 '19 at 18:45
  • I scanned a friends IP and he showed me some ports that were open but the scanner said were closed, so there is probably some problem with firewalls or similar. – Albin Oct 29 '19 at 18:49
  • 2
    Given the lack of details: I guess that your friends system was behind a typical Soo router and you either tried to scan its internal IP address which is not even reachable by you or you've tried to scan the public IP address of the router which did not show up any open ports too since there was no port forwarding installed to the internal system. A port scanner requires connectivity to the remote system in the first place, otherwise it will not be able to scan any ports there. – Steffen Ullrich Oct 29 '19 at 18:56
  • 1
    Is your friend's IP a public address? Are the ports open to the public? What happens if you try a web server (like www.google.com) using port 80? Does your program report that as open? – Duston Oct 29 '19 at 18:58
  • I Scanned my friend on his public IP address but it said all ports were closed, however, when I scanned "www.google.com" it said that port 80 is open. – Albin Oct 29 '19 at 19:02
  • @Albin From outside the LAN, the ports probably *are* closed and your tool is reporting correctly. – David Schwartz Oct 29 '19 at 20:57

2 Answers2

1
  1. Instead of scanning all port of a host, check a port on multi hosts, if you want to scan multiple host, then check another port ...
  2. Instead of full scan on ports, you can consider just well known ports[1].
  3. Putting sleep in your code, increase chance to by pass firewalls, but increase your scan time.
  4. You can use some proxy services that changing IP for each request[2].
  5. See "Fastest way to scan ports with Java"[3]

[1] https://www.webopedia.com/quick_ref/portnumbers.asp
[2] https://luminati.io/proxy-networks/residential-ips
[3] Fastest way to scan ports with Java

hbi640
  • 11
  • 2
-1

While testing and when actually running your scanner I would advise you to use proxy services so you'll get the idea where and when the problem occurs (if the problem still exists). You can find many proxy providers that can be useful in this case but I would suggest checking :

these have a lot to offer, work fast and are great when it comes to testing various programs, bots, scanners, tools, etc.