package Task3;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.*;
import jpcap.JpcapCaptor;
import jpcap.NetworkInterface;
import jpcap.PacketReceiver;
import jpcap.packet.*;
import sun.tools.jar.resources.jar;
public class Task3 extends JFrame{
static JTextArea ta2 = new JTextArea();
static JTextArea ta3 = new JTextArea();
static JTextArea ta4 = new JTextArea();
static JTextArea ta5 = new JTextArea();
static JTextArea ta6 = new JTextArea();
static JTextArea ta7 = new JTextArea();
static JTextArea ta8 = new JTextArea();
static JTextArea ta9 = new JTextArea();
public static void main(String args[]){
Task3 dn = new Task3("网络监听");
//dn.GrabTest();
}
public Task3(String title) {
super(title);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(40,0);
setSize(560,750);
JTabbedPane tabbedPane = new JTabbedPane();
JLabel label1 = new JLabel("协议名称");
label1.setBounds(new Rectangle(50, 30, 100, 20));
JLabel label2 = new JLabel("源IP:端口");
label2.setBounds(new Rectangle(170, 30, 100, 20));
JLabel label3 = new JLabel("目的IP:端口");
label3.setBounds(new Rectangle(310, 30, 100, 20));
JLabel label4 = new JLabel("包长度");
label4.setBounds(new Rectangle(450, 30, 100, 20));
add(label1);
add(label2);
add(label3);
add(label4);
Button bt1 = new Button("监听");
bt1.addActionListener(new GrabTest());
bt1.setBounds(new Rectangle(220, 0, 70, 25));
ta2.setEditable(true);
ta2.setText(" ");
ta2.setBackground(Color.white);
ta2.setLineWrap(true);
ta3.setEditable(true);
ta3.setText(" ");
ta3.setBackground(Color.white);
ta3.setLineWrap(true);
ta4.setEditable(true);
ta4.setText(" ");
ta4.setBackground(Color.white);
ta4.setLineWrap(true);
ta5.setEditable(true);
ta5.setText(" ");
ta5.setBackground(Color.white);
ta5.setLineWrap(true);
ta2.setBounds(new Rectangle(20, 50, 100, 300));
ta3.setBounds(new Rectangle(122, 50, 150, 300));
ta4.setBounds(new Rectangle(274, 50, 150, 300));
ta5.setBounds(new Rectangle(426, 50, 100, 300));
ta6.setBounds(new Rectangle(20, 352, 506, 90));
ta7.setBounds(new Rectangle(20, 444, 506, 90));
ta8.setBounds(new Rectangle(20, 536, 506, 90));
ta9.setBounds(new Rectangle(20, 628, 506, 90));
ta6.append("TCP数据包解析:"+"\n");
ta7.append("UDP数据包解析:"+"\n");
ta8.append("ARP数据包解析:"+"\n");
ta9.append("ICMP数据包解析:"+"\n");
add(bt1);
add(ta2);
add(ta3);
add(ta4);
add(ta5);
add(ta5);
add(ta6);
add(ta7);
add(ta8);
add(ta9);
setVisible(true);
}
}
class GrabTest implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
//获取本机上的网络接口对象数组
final NetworkInterface[] devices = JpcapCaptor.getDeviceList();
for(int i=0;i
NetworkInterface nc=devices[i];
//创建某个卡口上的抓取对象,最大为2000个
JpcapCaptor jpcap = JpcapCaptor.openDevice(nc, 2000, true, 20);
startCapThread(jpcap);
//Task3.ta.append("开始抓取第"+i+"个卡口上的数据"+"\n");
}
}catch(Exception ef){
ef.printStackTrace();
//Task3.ta.append("启动失败: "+ef);
}
}
//将每个Captor放到独立线程中运行
public static void startCapThread(final JpcapCaptor jpcap ){
JpcapCaptor jp=jpcap;
java.lang.Runnable runner=new Runnable(){
public void run(){
//使用接包处理器循环抓包
jpcap.loopPacket(-1, new TestPacketReceiver());
}
};
new Thread(runner).start();//启动抓包线程
}
}
/**
* 抓包监听器,实现PacketReceiver中的方法并打印出数据包说明
*/
class TestPacketReceiver implements PacketReceiver {
public void receivePacket(Packet packet) {
//Tcp包
if(packet instanceof jpcap.packet.TCPPacket){
Task3.ta2.append("TCP"+"\n");
TCPPacket p=(TCPPacket)packet;
Task3.ta3.append(p.src_ip.toString()+":"+p.src_port+"\n");
Task3.ta4.append(p.dst_ip.toString()+":"+p.dst_port+"\n");
Task3.ta5.append(" "+Integer.toString(p.len));
Task3.ta5.append("\n");
String str = p.toString();
byte []b = str.getBytes();
int count = 0;
for(int i = 0 ; i < b.length ; i++){
int v = b[i]&0xFF;
String string = Integer.toHexString(v);
count++;
if(string.length() < 2 ) string = '0' + string;
if(count%25 == 0){
Task3.ta6.append("\r\n");
}
Task3.ta6.append(string+" ");
}
Task3.ta6.append("\n");
}
//UDP包
else if(packet instanceof jpcap.packet.UDPPacket){
UDPPacket p=(UDPPacket)packet;
Task3.ta2.append("UDP"+"\n");
Task3.ta3.append(p.src_ip.toString()+":"+p.src_port+"\n");
Task3.ta4.append(p.dst_ip.toString()+":"+p.dst_port+"\n");
Task3.ta5.append(" "+Integer.toString(p.len));
Task3.ta5.append("\n");
String str = p.toString();
byte []b = str.getBytes();
int count = 0;
for(int i = 0 ; i < b.length ; i++){
int v = b[i]&0xFF;
String string = Integer.toHexString(v);
count++;
if(string.length() < 2 ) string = '0' + string;
if(count%25 == 0){
Task3.ta7.append("\r\n");
}
Task3.ta7.append(string+" ");
}
Task3.ta7.append("\n");
}
//ARP请求包
else if(packet instanceof jpcap.packet.ARPPacket){
ARPPacket p=(ARPPacket)packet;
//返回发送者的mac地址
Object saa= p.getSenderHardwareAddress();
Object taa=p.getTargetHardwareAddress();
Task3.ta2.append("UDP"+"\n");
Task3.ta3.append(saa.toString()+"\n");
Task3.ta4.append(taa.toString()+"\n");
Task3.ta5.append(" "+Integer.toString(p.len));
Task3.ta5.append("\n");
String str = p.toString();
byte []b = str.getBytes();
int count = 0;
for(int i = 0 ; i < b.length ; i++){
int v = b[i]&0xFF;
String string = Integer.toHexString(v);
count++;
if(string.length() < 2 ) string = '0' + string;
if(count%25 == 0){
Task3.ta8.append("\r\n");
}
Task3.ta8.append(string+" ");
}
Task3.ta8.append("\n");
}
//ICMP包
else if(packet instanceof jpcap.packet.ICMPPacket){
ICMPPacket p=(ICMPPacket)packet;
//ICMP包的路由链
String router_ip="";
for(int i=0;i
router_ip+=" "+p.router_ip[i].getHostAddress();
}
Task3.ta2.append("ICMP"+"\n");
Task3.ta3.append(p.router_ip.toString()+"\n");
Task3.ta4.append(p.redir_ip.toString()+"\n");
Task3.ta5.append(" "+Integer.toString(p.len));
Task3.ta5.append("\n");
String str = p.toString();
byte []b = str.getBytes();
int count = 0;
for(int i = 0 ; i < b.length ; i++){
int v = b[i]&0xFF;
String string = Integer.toHexString(v);
count++;
if(string.length() < 2 ) string = '0' + string;
if(count%25 == 0){
Task3.ta9.append("\r\n");
}
Task3.ta9.append(string+" ");
}
Task3.ta9.append("\n");
}
}
}
这题的难度在于要导入jpcap的包,非常坑爹,几次都失败。