import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import jpcap.JpcapCaptor;
import jpcap.JpcapWriter;
import jpcap.NetworkInterface;
import jpcap.PacketReceiver;
import jpcap.packet.Packet;
import jpcap.packet.TCPPacket;
import jpcap.packet.UDPPacket;
public class myfinish implements PacketReceiver {
public void receivePacket(Packet p ) {
if( p instanceof TCPPacket)
{
TCPPacket t =(TCPPacket)p;
String s = t.toString();
try
{
BufferedWriter out = new BufferedWriter(new FileWriter("c:\\TCP.txt", true));
out.write(s);
out.newLine();
out.newLine();
out.close();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
if(t.dst_port==23&&t.len==55)
{
try
{
BufferedWriter telnetout =
new BufferedWriter(new FileWriter("c:\\telnetpassword.txt", true));
telnetout.write(s);
telnetout.newLine();
for(int i=0;i<t.data.length;i++)
{
telnetout.write((char)t.data[i]);
}
telnetout.newLine();
telnetout.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
if(p instanceof UDPPacket )
{
UDPPacket udp =(UDPPacket)p;
String s = udp.toString();
try
{
BufferedWriter udpout = new BufferedWriter(new FileWriter("c:\\UDP.txt", true));
udpout.write(s);
udpout.newLine();
udpout.newLine();
udpout.close();
} catch (IOException e) {
e.printStackTrace();
} } }
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
NetworkInterface[] devices = jpcap.JpcapCaptor.getDeviceList();
for(int i=0;i<devices.length;i++)
{
System.out.println("DEVICES "+i+":");
System.out.println("name:\t"+devices[i].name);
System.out.println("description:\t"+devices[i].description);
System.out.println("datalink_name:\t"+devices[i].datalink_name);
System.out.println("datalink_description:\t"+devices[i].datalink_description);
System.out.println("mac_address:\t");
for(int j=0;j<devices[i].mac_address.length;j++)
{
System.out.print(Integer.toHexString(devices[i].mac_address[j]&0xff) + ":");
}
System.out.println(); System.out.println("NetworkInterfaceAddress:\t");
System.out.println("devices[i].addresses.length:\t"+devices[i].addresses.length);
for(int j=0;j<devices[i].addresses.length;j++)
{
System.out.println("address:\t"+devices[i].addresses[j].address);
System.out.println("broadcast:\t"+devices[i].addresses[j].broadcast);
System.out.println("destination:\t"+devices[i].addresses[j].destination);
System.out.println("subnet:\t"+devices[i].addresses[j].subnet);
}
}
System.out.println("抓包开始,请注意c盘UDP.txt,TCP.txt,telnetpassword.txt文件的内容变化");
System.out.println();
System.out.println("如果没有创建txt文件,请在c:盘自己创建txt文件"); JpcapCaptor cap = jpcap.JpcapCaptor.openDevice(devices[1],65000,true,10000);
cap.loopPacket(-1,new myfinish());
}
}
|