BLOGGER TEMPLATES AND Blogger Templates »

Selasa, 25 September 2012

Port Scanner with Java

Hello guys, it's been a while. Let's continue.

(Yeah, that's it. I'm not a big fan of being talkative. Sue me.)

In this post, I'm about to show you how easy it is to make simple port scanner application using our everyday - lo and behold - Java SE language.

The idea is simple; just check all the ports available, one by one, and if you got lucky (you know what I mean) we memorize the port number. In this code example, we use the Socket class from the client to try to connect to all the ports available in a host (Too many "to"? Sue me).

The only drawback from this simple source code is that it runs very slow. Hell, it takes one port a second to scan on my AMD C-60 netbook. You don't like it? Well, I did say this is a simple program. You still don't like it? Sue me.

And since this is - again - a simple program, I only used the basic text interface, so you might want to run this app from DOS mode or the Terminal Linux equivalent. Why? It makes you look cool, working on text-based program and such. You don't like it? Sue me.


Okay, here's the code:

import java.net.*;
import java.io.*;

/**
 *
 * @author Fariz
 */
public class PortScanner
{

    public static void main(String[] args)
    {
        Socket theSocket = null;
        BufferedReader reader = null;
        int numPortScanned = 0, numPortOpened = 0, startPort = 0, endPort = 65535;
        int listPortOpened[] = new int[65537];
        String destinationAddress = null;

        //initialize the opened port list by marking them with "closed" value, I used -1 on this
        for (int p = 0; p < listPortOpened.length; p++)
        {
            listPortOpened[p] = -1;
        }


        try        //enter the required information (target host and port range)
        {
            reader = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter hostname or IP Address: ");
            destinationAddress = reader.readLine();

            System.out.print("Enter first port: ");
            startPort = Integer.parseInt(reader.readLine());

            System.out.print("Enter last port: ");
            endPort = Integer.parseInt(reader.readLine());
        } catch (Exception e)
        {
            e.printStackTrace();
        }

        numPortScanned = startPort;
        System.out.println("");

        //check all the freaking ports
        while (numPortScanned <= endPort)
        {
            try
            {
                System.out.println("Connecting to " + destinationAddress +  " on port " + numPortScanned + "...");
                theSocket = new Socket(destinationAddress, numPortScanned);
                numPortOpened++;
                System.out.println("Port IS OPEN.");

                //if it's opened, remember the port number
                listPortOpened[numPortScanned] = numPortScanned;

                //check the next port
                numPortScanned++;
                theSocket.close();
            } catch (Exception e)
            {
                System.out.println("Port is closed.");

                //if the port's closed, simply skip it
                numPortScanned++;
                listPortOpened[numPortScanned] = -1;
            }
        }
      
        //show all the opened ports
        if (numPortOpened == 0)
        {
            System.out.println("\nNo port opened in " + destinationAddress + ", better luck next time!.");
        } else
        {
             System.out.println("\nFound in " + destinationAddress + ", there are " + numPortOpened + " ports opened: ");
            for (int i = 0; i < listPortOpened.length; i++)
            {
                if (listPortOpened[i] != -1)
                {
                    System.out.print(listPortOpened[i] + " ");
                }
            }
            System.out.println("\n");
        }

    }

}

Enjoy!

0 komentar: