[Java] openScan - An open source port scanner

treebeard

New member
Mar 25, 2018
7
1
3
Middle Earth
fokushotel.com
#1
Greetings,

openScan is an open source port scanner that I am making in Java, for practice as well as a medium to further my own knowledge. This is a very early release and is only a crude version with minimal features. I'm working through some of the main features and methods in a single class and then I'm going to start from the beginning and structure the project in a more proper OOP style. When I get some more work done I'll post a link to the Github repo in case anyone is interested enough to keep up with it.

I'm really posting this here to get some activity on the forum as well as to inspire others to work on their own projects, contribute to open source projects, and post some dang content!

You do not have permission to view link Log in or register now.


I hope you enjoy, thanks ^.^

********** openScan is for educational purposes only and I'm in no way responsible for the misuse or misconduct of any individual who chooses to break rules **********​


Java:
// Created by: Justin T



// Date: April 28, 2018



// Useful Information: Created using JDK 8 in IntelliJ IDEA.



// Purpose: Simple port scanner capable of reading an IPv4 address, starting port, and ending port.



//          Will print a message if the port is open.



// In Progress: Threading, IPv6 support, Search by domain name, Intervals of ports to scan, Designing simple GUI,



//              Display common port uses that are read from file, Write results to log file, Timeout.



//







import java.net.*;



import java.util.Scanner;







public class Main {



    public static void main(String[] args) {



        int startPort = 0;



        int endPort = 0;



        String targetIP;







        menu();







        startPort = setStartPort();



        endPort = setEndPort();



        targetIP = setTargetIP();







        scanPorts(targetIP, startPort, endPort);







        System.out.println("-----------------------------------------------------");



        System.out.println("The Port scan is complete.");







    }







    public static void menu() {



        System.out.println("             Welcome to openScan v1.0.0              ");



        System.out.println("-----------------------------------------------------");



        System.out.println("As of now the target must be entered in the form of an");



        System.out.println("IPv4 address. Attempting to use a domain name or an");



        System.out.println("IPv6 address will result in an error or the test not");



        System.out.println("running at all. Example: XXX.XXX.XXX or 127.0.0.1");



        return;



    }







    public static boolean isValidIPv4(String hostAddress) {



        boolean isValid;







        String[] splitIP = hostAddress.split("[.]");



        int ipPartOne = Integer.parseInt(splitIP[0]);



        int ipPartTwo = Integer.parseInt(splitIP[1]);



        int ipPartThree = Integer.parseInt(splitIP[2]);



        int ipPartFour = Integer.parseInt(splitIP[3]);







        if(! isValidPart(ipPartOne) || ! isValidPart(ipPartTwo) || ! isValidPart(ipPartThree)|| ! isValidPart(ipPartFour)){



            isValid = false;



            System.out.println("You have entered an invalid IPv4 address, please try again.");



        } else if (isReservedIP(splitIP)) {



            isValid = false;



            System.out.println("You have entered a reserved IPv4 address, please try again.");



        } else {



            isValid = true;



            System.out.println("Valid IPv4 address!! Attempting to connect now...");



        }







        return isValid;



    }







    public static boolean isValidPart(int ipPart) {



        if (ipPart > 255 || ipPart < 0) {



            return false;



        }



        return true;



    }







    public static boolean isReservedIP(String[] ipAddress) {



       if (ipAddress[0].equals("0") && ipAddress[1].equals("0") && ipAddress[2].equals("0") && ipAddress[3].equals("0")) {



            return true;



       } else if (ipAddress[0].equals("255") && ipAddress[1].equals("255") && ipAddress[2].equals("255") && ipAddress[3].equals("255")) {



            return true;



        } else {



            return false;



        }



    }







    public static boolean isValidPort(int port) {



        if (port > 65535 || port < 0){



            return false;



        }



        return true;



    }







    public static int setStartPort() {



        Scanner keyboard = new Scanner(System.in);



        boolean isValid;



        int tempPort = 0;







        do {



            System.out.println("Enter a starting port.");



            tempPort = Integer.parseInt(keyboard.nextLine());







            isValid = isValidPort(tempPort);







        } while(isValid == false);



        keyboard.close();



        return tempPort;



    }







    public static int setEndPort() {



        Scanner keyboard = new Scanner(System.in);



        boolean isValid;



        int tempPort = 0;







        do {



            System.out.println("Enter the ending port.");



            tempPort = Integer.parseInt(keyboard.nextLine());







            isValid = isValidPort(tempPort);







        } while(isValid == false);



        keyboard.close();



        return tempPort;



    }







    public static String setTargetIP() {



        Scanner keyboard = new Scanner(System.in);



        boolean isValid;



        String tempIP;







        do {



            System.out.println("Enter the IP of the machine you want to scan.");



            tempIP = keyboard.nextLine().trim();







            if (! isValidIPv4(tempIP)) {



                isValid = false;



            }







            isValid = true;







        } while (isValid == false);



        keyboard.close();



        return tempIP;



    }







    public static void scanPorts(String targetIP, int startPort, int endPort) {



        for(int i = startPort ;i <= endPort; i++) {



            try {



                Socket sock = new Socket(targetIP, i);



                System.out.println("Port: "+ i + " is open.");



                sock.close();



            } catch(Exception e) {



                // Port closed



            }



        }



        return;



    }







}

[doublepost=1525398581,1524897502][/doublepost]I re-wrote the application now that I planned most of the main features; all of the basic features from the first design are re-implemented already.

I'm currently working on writing log files for each scan. I've been working on another project that has a bit more of my attention but I'll continue to work on this in my spare time! After I finish the log files I'll start to work on threading, scan by domain name & IPv6, & scanning intervals of ports rather than just picking a start and end port.



Here's a little sample:

Code:
import java.util.Scanner;



public class ScanSettings {

    private final int startPort;

    private final int endPort;

    private final String targetIP;



    // Constructors



    ScanSettings() {

        this.startPort = setStartPort();

        this.endPort = setEndPort();

        this.targetIP = setTargetIP();

    }



    ScanSettings(int newStartPort, int newEndPort, String newTargetString) {

        this.startPort = newStartPort;

        this.endPort = newEndPort;

        this.targetIP = newTargetString;

    }



    // Accessor Methods



    public int getStartPort() {

        return this.startPort;

    }



    public int getEndPort() {

        return this.endPort;

    }



    public String getTargetIP() {

        return this.targetIP;

    }



    // Mutator Methods



    private static int setStartPort() {

        Scanner keyboard = new Scanner(System.in);

        boolean isValid;

        int tempPort = 0;



        do {

            System.out.println("Enter a starting port.");

            tempPort = Integer.parseInt(keyboard.nextLine());



            isValid = PortUtils.isValidPort(tempPort);



        } while(isValid == false);

        return tempPort;

    }



    private static int setEndPort() {

        Scanner keyboard = new Scanner(System.in);

        boolean isValid;

        int tempPort = 0;



        do {

            System.out.println("Enter the ending port.");

            tempPort = Integer.parseInt(keyboard.nextLine());



            isValid = PortUtils.isValidPort(tempPort);



        } while(isValid == false);

        return tempPort;

    }



    private static String setTargetIP() {

        Scanner keyboard = new Scanner(System.in);

        boolean isValid;

        String tempIP;



        do {

            System.out.println("Enter the IP of the machine you want to scan.");

            tempIP = keyboard.nextLine().trim();



            if (! IPUtils.isValidIPv4(tempIP)) {

                isValid = false;

            }



            isValid = true;



        } while (isValid == false);

        keyboard.close();

        return tempIP;

    }













}




The Github repo for this project can be found at:
You do not have permission to view link Log in or register now.


For those of you interested in following the project or contributing/forking it.
 
Last edited: