Port Scaner
Esempio di come realizzare un semplice port scanner in Java.
File: PortScan (Socket)
File: PortScan2 (DatagramSocket)
PortScan.zip
File: PortScan (Socket)
Java
/*
* Semplice Port Scanner
*/
import java.io.*;
import java.net.*;
import java.util.*;
public class PortScan{
private int portStart = 1;
private int portStop = 1024;
private String ipAddress = "127.0.0.1";
public PortScan(){}
public void setIpAddress(String ipAddress){
this.ipAddress = ipAddress;
}
public void setPortStart(int portStart){
this.portStart = portStart;
}
public void setPortStop(int portStop){
this.portStop = portStop;
}
public void start(){
int inc;
int i;
ScanRange sr;
System.out.println(toString());
System.out.println("\nPorte aperte....\n");
inc = (int)((portStop - portStart) / 10.0 + .5);
for(i=portStart; i < (portStop - inc); i+=inc){
sr = new ScanRange(ipAddress, i, i+inc-1);
//System.out.println(sr.toString());
sr.start();
}
sr = new ScanRange(ipAddress, i, portStop);
//System.out.println(sr.toString());
sr.start();
}
public static void main (String[] args) {
PortScan ps = new PortScan();
// legge i parametri
if(args.length == 0){
System.out.println("Sintassi: PortScan <ipAddress> <portStart> <portStop>");
}else{
if(args.length > 0)
ps.setIpAddress(args[0]);
if(args.length > 1)
ps.setPortStart(Integer.parseInt(args[1]));
if(args.length > 2)
ps.setPortStop(Integer.parseInt(args[2]));
}
ps.start();
}
public String toString(){
return "Port scan su " + ipAddress + " dalla porta " + portStart + " alla " + portStop;
}
}
class ScanRange extends Thread{
private int startPort;
private int stopPort;
private String ipAddress;
private Socket s;
public ScanRange(String ipAddress, int startPort, int stopPort){
this.ipAddress = ipAddress;
this.startPort = startPort;
this.stopPort = stopPort;
}
public void run(){
for(int i=startPort; i <= stopPort; i++){
try {
s = new Socket(ipAddress, i);
System.out.print(" ( " + i + " ) ");
s.close();
s=null;
}catch(Exception e) {}
try {
sleep(100);
//yield();
}catch(Exception e) {}
}
}
public String toString(){
return "Thread range: " + startPort + " - " + stopPort + " avviato";
}
}
File: PortScan2 (DatagramSocket)
Java
/*
* Semplice Port Scanner
*/
import java.io.*;
import java.net.*;
import java.util.*;
public class PortScan2 {
private int portStart = 1;
private int portStop = 1024;
private String ipAddress = "127.0.0.1";
public PortScan2 () {}
public void setIpAddress(String ipAddress){
this.ipAddress = ipAddress;
}
public void setPortStart(int portStart){
this.portStart = portStart;
}
public void setPortStop(int portStop){
this.portStop = portStop;
}
public void start(){
int inc;
int i;
ScanRange sr;
System.out.println(toString());
System.out.println("\nPorte aperte....\n");
inc = (int)((portStop - portStart) / 10.0 + .5);
for(i=portStart; i < (portStop - inc); i+=inc){
sr = new ScanRange(ipAddress, i, i+inc-1);
//System.out.println(sr.toString());
sr.start();
}
sr = new ScanRange(ipAddress, i, portStop);
//System.out.println(sr.toString());
sr.start();
}
public static void main (String[] args) {
PortScan ps = new PortScan();
// legge i parametri
if(args.length == 0){
System.out.println("Sintassi: PortScan <ipAddress> <portStart> <portStop>");
}else{
if(args.length > 0)
ps.setIpAddress(args[0]);
if(args.length > 1)
ps.setPortStart(Integer.parseInt(args[1]));
if(args.length > 2)
ps.setPortStop(Integer.parseInt(args[2]));
}
ps.start();
}
public String toString(){
return "Port scan su " + ipAddress + " dalla porta " + portStart + " alla " + portStop;
}
}
class ScanRange extends Thread{
private int startPort;
private int stopPort;
private String ipAddress;
// private Socket s;
private DatagramSocket s;
public ScanRange(String ipAddress, int startPort, int stopPort){
this.ipAddress = ipAddress;
this.startPort = startPort;
this.stopPort = stopPort;
}
public void run(){
for(int i=startPort; i <= stopPort; i++){
try {
//s = new Socket(ipAddress, i);
s = new DatagramSocket(i, InetAddress.getByName(ipAddress));
System.out.print(" ( " + i + " ) ");
s.close();
s=null;
}catch(Exception e) {}
try {
sleep(100);
//yield();
}catch(Exception e) {}
}
}
public String toString(){
return "Thread range: " + startPort + " - " + stopPort + " avviato";
}
}
PortScan.zip