April 1, 2010

While listening to a lecture the other day about computer networks, I spent my time working on a port scanner in Java. Oh how fun it is to be a geek...

Anyhow, here's CryptScanner (*Again, some code was borrowed*):

import java.net.*;
import java.io.*;
import java.io.IOException;
import java.awt.*;
import java.awt.event.*;

public class CryptScanner
{
public static void main(String[] args) throws IOException
{
InetAddress target=null;
String hostAddr=null;

try
{
BufferedReader input = new BufferedReader ( new InputStreamReader(System.in) );
System.out.print("Host name to scan: ");
hostAddr = input.readLine();

if(hostAddr!=null)
{
target = InetAddress.getByName(hostAddr);
scan(target);
}

}

catch (UnknownHostException e)
{
System.err.println(e);
}

finish();
}

public static void scan(final InetAddress remote) throws UnknownHostException
{
int port=0;
String hostname = remote.getHostName();

InetAddress ipAddress = InetAddress.getByName(hostname);
System.out.print("\nScan results for " + hostname + " (" + ipAddress.getHostAddress() + ")" +"\n");
System.out.print("---------------------------------------------------------\n");

for (port = 0; port < 65536; port++)
{
try
{
Socket sock = new Socket(remote,port);
System.out.print("Listening on port " + port +"\n");
sock.close();
}

catch (IOException ex)
{
System.out.print("Not Listening on port " + port +"\n");
}
}
}

public static void finish()
{
System.exit(0);
}
}

0 comments:

Post a Comment

Subscribe to RSS Feed Follow me on Twitter!