pmhwp
04-02-2009, 08:55
Ciao,
dovrei realizzare un piccolo sniffer e vorrei utilizzare questa libreria.
Qualcuno mi puo' dare una mano a comprenderne l'utilizzo?
Per ora ho provato qualche esempio in rete ma non sono riuscito a capire come ottenere per ogni paccheto una stampa precisa dell'IP destinazione/sorgente , protocollo utilizzato e altro.
Ecco quello che ho provato:
package simple_sniffer;
//import jpcap.*;
import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;
import jpcap.packet.Packet;
import jpcap.JpcapCaptor;
import jpcap.JpcapSender;
import jpcap.JpcapWriter;
import jpcap.NetworkInterface;
import jpcap.NetworkInterfaceAddress;
import jpcap.packet.*;
public class Main {
/* variables */
JpcapCaptor captor;
NetworkInterface[] list;
String str,info;
int x, choice;
String infos;
public void handlePacket(Packet packet){
// Qui si possono elaborare i pacchetti catturati.
// stampa tutto il contenuto del pacchetto.
System.out.println( packet );
}
public static void main(String args[]) throws IOException {
new Main();
}
public Main() throws IOException {
Packet info;
String scelta;
Console console = System.console();
JpcapWriter writer;
/* first fetch available interfaces to listen on */
list = JpcapCaptor.getDeviceList();
NetworkInterface[] lists = JpcapCaptor.getDeviceList();
System.out.println("Available interfaces: ");
for(x=0; x<list.length; x++) {
System.out.println(x+" !!->!! "+list[x].description);
}
if (list.length==0){
System.out.println("Non ho trovato interfacce");
}else{
System.out.println("-------------------------\nChoose interface (0,1..): ");
choice = Integer.parseInt(getInput("Choose interface (0,1..): "));
System.out.println( choice);
System.out.println("Listening on interface -> "+list[choice].description);
System.out.println("-------------------------\n");
/*Setup device listener */
try {
captor=JpcapCaptor.openDevice(list[choice], 65535, false, 20);
/* listen for TCP/IP only */
captor.setFilter("ip and tcp", true);
}
catch(IOException ioe) { ioe.printStackTrace(); }
/* start listening for packets */
while (true) {
info = captor.getPacket();
if(info != null)
//infos= getPacketText(info);
System.out.print(getPacketText(info));
writer=JpcapWriter.openDumpFile(captor,"captured.txt");
}
}
}
/* return packet data in true text */
String getPacketText(Packet pack){
int i=0,j=0;
byte[] bytes=new byte[pack.header.length + pack.data.length];
System.arraycopy(pack.header, 0, bytes, 0, pack.header.length);
System.arraycopy(pack.data, 0, bytes, pack.header.length, pack.data.length);
StringBuffer buffer = new StringBuffer();
for(i=0; i<bytes.length;) {
for(j=0;j<8 && i<bytes.length;j++,i++) {
String d = Integer.toHexString((int)(bytes [i] &0xff));
buffer.append((d.length() == 1 ? "0" + d:d ) + " ");
if(bytes[i]<32 || bytes[i]>126)
bytes[i] = 46;
}
}
return new String(bytes,i - j, j);
}
/* get user input */
public static String getInput(String q) {
String input = "";
System.out.print(q);
BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(System.in));
try {
input = bufferedreader.readLine();
}
catch(IOException ioexception) { }
return input;
}
}
Grazie.
dovrei realizzare un piccolo sniffer e vorrei utilizzare questa libreria.
Qualcuno mi puo' dare una mano a comprenderne l'utilizzo?
Per ora ho provato qualche esempio in rete ma non sono riuscito a capire come ottenere per ogni paccheto una stampa precisa dell'IP destinazione/sorgente , protocollo utilizzato e altro.
Ecco quello che ho provato:
package simple_sniffer;
//import jpcap.*;
import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;
import jpcap.packet.Packet;
import jpcap.JpcapCaptor;
import jpcap.JpcapSender;
import jpcap.JpcapWriter;
import jpcap.NetworkInterface;
import jpcap.NetworkInterfaceAddress;
import jpcap.packet.*;
public class Main {
/* variables */
JpcapCaptor captor;
NetworkInterface[] list;
String str,info;
int x, choice;
String infos;
public void handlePacket(Packet packet){
// Qui si possono elaborare i pacchetti catturati.
// stampa tutto il contenuto del pacchetto.
System.out.println( packet );
}
public static void main(String args[]) throws IOException {
new Main();
}
public Main() throws IOException {
Packet info;
String scelta;
Console console = System.console();
JpcapWriter writer;
/* first fetch available interfaces to listen on */
list = JpcapCaptor.getDeviceList();
NetworkInterface[] lists = JpcapCaptor.getDeviceList();
System.out.println("Available interfaces: ");
for(x=0; x<list.length; x++) {
System.out.println(x+" !!->!! "+list[x].description);
}
if (list.length==0){
System.out.println("Non ho trovato interfacce");
}else{
System.out.println("-------------------------\nChoose interface (0,1..): ");
choice = Integer.parseInt(getInput("Choose interface (0,1..): "));
System.out.println( choice);
System.out.println("Listening on interface -> "+list[choice].description);
System.out.println("-------------------------\n");
/*Setup device listener */
try {
captor=JpcapCaptor.openDevice(list[choice], 65535, false, 20);
/* listen for TCP/IP only */
captor.setFilter("ip and tcp", true);
}
catch(IOException ioe) { ioe.printStackTrace(); }
/* start listening for packets */
while (true) {
info = captor.getPacket();
if(info != null)
//infos= getPacketText(info);
System.out.print(getPacketText(info));
writer=JpcapWriter.openDumpFile(captor,"captured.txt");
}
}
}
/* return packet data in true text */
String getPacketText(Packet pack){
int i=0,j=0;
byte[] bytes=new byte[pack.header.length + pack.data.length];
System.arraycopy(pack.header, 0, bytes, 0, pack.header.length);
System.arraycopy(pack.data, 0, bytes, pack.header.length, pack.data.length);
StringBuffer buffer = new StringBuffer();
for(i=0; i<bytes.length;) {
for(j=0;j<8 && i<bytes.length;j++,i++) {
String d = Integer.toHexString((int)(bytes [i] &0xff));
buffer.append((d.length() == 1 ? "0" + d:d ) + " ");
if(bytes[i]<32 || bytes[i]>126)
bytes[i] = 46;
}
}
return new String(bytes,i - j, j);
}
/* get user input */
public static String getInput(String q) {
String input = "";
System.out.print(q);
BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(System.in));
try {
input = bufferedreader.readLine();
}
catch(IOException ioexception) { }
return input;
}
}
Grazie.