/* AdminScanner.java */

/*  January 23, 1999 */

/*

    Copyright (c) 1999 Mark D. LaDue
    You may study, use, modify, and distribute this example for any purpose.
    This example is provided WITHOUT WARRANTY either expressed or implied.

*/

/*

    Author: Dr. Mark D. LaDue
    Home Page: http://www.rstcorp.com/hostile-applets/index.html

*/


/*

    This simple utility scans a host and looks for responses that characterize
    the admin servlets running on Sun's Java Web Server and IBM's WebSphere.
    Any admin servlets found can then be attacked with the CrackAdmin utility.

*/ 


import java.io.*;

import java.net.*;

public class AdminScanner {

    // How many Threads are currently active?

    static int active = 0;

    // What's the maximum number of Threads allowed to be active?

    static final int MAXACTIVE = 100;

    // How many seconds should we sleep if too many Threads are active?

    static final int SLEEPTIME = 1;

    /* Create */

    public static void main(String[] argv) {

        if (argv.length != 3) {

            complainAndQuit();

        }

        // Who's the target host?

        String JWSHost = argv[0];

        // What's the lowest port number to scan?

        int nextPort = Integer.parseInt(argv[1]);

        // What's the highest port number to scan?

        int MAXPORT = Integer.parseInt(argv[2]);

        if (nextPort < 1 || nextPort > 65535 || MAXPORT < 1 || MAXPORT > 65535 || nextPort > MAXPORT) {

            complainAndQuit();

        }

        System.out.println("Scanning " + JWSHost + " from port " + nextPort + " to port " + MAXPORT + ":");

        while (nextPort <= MAXPORT) {

            // Sleep a bit if too many Threads are active

            while (activeStep(0) > MAXACTIVE) {

                try {

                    Thread.currentThread().sleep(1000*SLEEPTIME);

                }

                catch (InterruptedException ie) {}

            }

            // Clean up every once in a while to keep Windows NT from getting hosed

            if (nextPort % 1000 == 0) {

                System.out.println("\n(" + nextPort + ") Running Finalization and Garbage Collection");

                System.runFinalization();

                System.gc();

            }

            // Scan the next target port

            PortScanner scanner = new PortScanner(JWSHost, nextPort);

            Thread scannerThread = new Thread(scanner , "PortScanner-" + nextPort);

            if (scannerThread != null) {

                scannerThread.start();

            }

            nextPort++;

        }

    }

    /* Manipulate the count of Threads that are currently active */

    static synchronized int activeStep(int step) {

        switch (step) {

            case -1: return active--;

            case 1: return active++;

            case 0: return active;

            default: return active;

        }

    }

    private static void complainAndQuit() {

        System.out.println("Usage: java AdminScanner hostname Low_Port High_Port");

        System.out.println("With 0 < Low_Port <= High_Port < 65536");

        System.exit(-1);

    }

}