Jump to content
Hak5 Forums
blueghosties

PortPoke Java Port Scanner

Recommended Posts


/**
 * Created by blueghosties on 31/03/16.
 * this is a simple port scanner that I wrote.
 * Nothing spectacular, just does what it's suppose to
 * do.
 * When it hits an open port, it logs time, date,
 * IP and port.
 * I was going to implement ability to jump across
 * a range of different IP addresses, then
 * decided against it as it would raise flags.
 * Thinking about introducing randomised time
 * between scans as that would lessen the
 * suspicion of the scan.
 *
 * OK...so I implemented the random timing, so shoot me.
 * You like...no? (said in an outrageous French accent)
 *
 * This has been tested and run on Mac OS X
 * el Capitain, Sierra, Windows 10 and the intel Edison.
 * Yes, it runs on that awesome, little board.
 *
 * I am not responsible for any misuse by others
 * from running this program or damage that
 * may be caused by it. You're on your own.
 *
 * Enjoy.
 */

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Date;
import java.util.Random;
import java.util.Scanner;

public class PortScanner  // Super secret code name clPortPoke
{
        public static void main(String []args) throws IOException {

            int starting_port, ending_port, rand_delay;
            // BufferedReader s_in = null;

            // Time to get some....input
            Scanner input = new Scanner (System.in);
            System.out.println("ᦐ Blue Ghosties' Simple Java Port Scanner ᦐ");
            System.out.println("--------------------------------------------");
            System.out.println("Enter in IP ADDRESS: ");
            String ip_add = input.next();
            System.out.println("Enter in starting port: ");
            starting_port = input.nextInt();
            System.out.println("Enter in an ending port (65535 is your limit): ");
            ending_port = input.nextInt();
            System.out.println("Did you want to randomise scanning delay? y/n ");
            String delay_it = input.next();

            String delay_ans;
            Random randomGenerator = new Random();

            // using a delay range....muhahahaha....EVILLE!!!
            rand_delay = randomGenerator.nextInt(666);

            if(delay_it.equals("y"))
            {
                System.out.println("Delay starting at: " + rand_delay);
                delay_ans = "yes";
            }
            else
            {
                System.out.println("Full throttle...perfect. Vroom vroom!!!");
                rand_delay = 0;
                delay_ans = "no";
            }
            System.out.println("Delay starting at: " + rand_delay);
            System.out.println("Now checking: " + ip_add);
            System.out.println("----------------------------------------");

            // log file will be written wherever the app is located
            File file = new File("java_hits.txt");

            // You can scan to port 65535
            for (int port = starting_port; port <= ending_port; port++)
            {
                if(rand_delay > 0)
                {
                    // Take a random nap...program has narcolepsy
                    // Making sure rand_delay isn't 0
                    // Trying to mask scan with erratic signal
                    rand_delay = randomGenerator.nextInt(666);
                    rand_delay = rand_delay + 1;
                    System.out.println("Delay set at: " + rand_delay);
                }
                else
                {
                    rand_delay = 0;
                }

                try
                {
                    Thread.sleep(rand_delay);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println("IP -> " + ip_add + " Port: " + port);
                try
                {
                    if(!file.exists())
                    {
                        file.createNewFile();
                    }
                    else
                    {
                        // instantiate new socket object
                        Socket socket;
                        socket = new Socket();
                        socket.connect(new InetSocketAddress(ip_add, port), 100);


                        // s_in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                        // Need to make sure we log any hits with time, date and ports.
                        FileWriter fileWriter = new FileWriter(file, true);
                        Date now = new Date();

                        // All output to file is below
                        System.out.println("IP -> * " + ip_add + " Port " + port + " is open *");
                        // System.out.println("Server responded: " + response);
                        String str_start_port = String.valueOf(starting_port);
                        String str_ending_port = String.valueOf(ending_port);
                        String bline = "--------------------------------------------------------------------------\r\n";
                        String strPort = "ᦐ Scanning ports between " + str_start_port + " and " + str_ending_port + " ᦐ\r\n";
                        String data = "--> IP Address: " + ip_add + " Port: " + port + " is open. " + now + " <--\r\n";
                        String delay_set = "--> Was random delay set for the scan? <-- " + delay_ans + "\r\n";

                        // IF we hit something...log it!
                        fileWriter.write(strPort);
                        fileWriter.write(data);
                        fileWriter.write(delay_set);

                        //fileWriter.write(server_response);
                        fileWriter.write(bline);

                        // These pipes are clean! Close the connection.
                        fileWriter.flush();
                        fileWriter.close();
                        socket.close();

                    }
                }
                catch (IOException ex)
                {
                    //ex.printStackTrace();
                }
            }
            System.out.println("We're finished.");
        }
    }

 

  • Upvote 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.

×