基于java的网络抓包方法

上传人:痛*** 文档编号:194412727 上传时间:2023-03-13 格式:DOCX 页数:26 大小:42.63KB
收藏 版权申诉 举报 下载
基于java的网络抓包方法_第1页
第1页 / 共26页
基于java的网络抓包方法_第2页
第2页 / 共26页
基于java的网络抓包方法_第3页
第3页 / 共26页
资源描述:

《基于java的网络抓包方法》由会员分享,可在线阅读,更多相关《基于java的网络抓包方法(26页珍藏版)》请在装配图网上搜索。

1、基于java的网络抓包方法基于java的网络抓包方法 一、实验内容描述本实验是用java实现的网络抓包程序,在windows环境下安装winpcap4.0和jpcap6.0后,下载eclipse和jigloo插件,将其安装好,然后就可以进行java的网络抓包图形化开发了。二、原理与关键技术2.1 网络抓包技术原理网络层上有各种各样的数据包,它们以不同的帧格式在网络层上进行传输,但是在传输时它们都遵循相同的格式,即有相同的长度,如果一种协议的帧格式达不到这种长度,就让其补齐,以达到我们的要求。2.2 网络抓包关键技术无论是在windows操作系统下还是在linux操作系统下,要想捕获网络上的数据

2、包,必须要对网卡进行控制,因为本机的数据报从网络上来到本机是通过网卡然后再保存到本地缓冲区上的,所以要抓获网包就必须调用网卡驱动中的对外函数,在linux系统中有net.h文件,可以调用net.h文件中的函数来操作网卡,可以直接编程实现,但为了更方便的使用,可以安装一个叫libpcap的软件,这样调用函数更好用,而在windows系统中,因为源代码不对外公开,所以要安装一个叫winpcap的软件,这样用C或VC+就可以实现了,但因为我用的是java语言来实现的,所以无论是在哪个系统都要安装一个叫jpcap的软件,它本身就把底层的函数又封装了一下,这样就可以让java来使用了。三、设计与实现3.

3、1 基于java的设计方案我的这个网络抓包程序是图形化操作界面,在菜单栏点击抓包按钮后选择网卡和过滤字还有最长字长,点击开始,然后就可以开始抓包了,在主界面中就会显示出一行又一行的数据,这些数据就是抓获到的数据包。3.2 具体实现1、安装winpcap4.0和jpcap6.02、下载eclipse3.3和jigloo,jigloo是eclipse底下的插件,是用来支持eclipse底下的java 图形化开发的。3、编写java抓包程序:建立三个文件,一个主程序,一个抓包程序,一个抓包选项程序对话框程序第一个程序:主程序如下package netcap;import java.awt.event

4、.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JSeparator;import javax.swing.JMenuItem;import javax.swing.JMenu;import javax.swing.JMenuBar;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.table.*;import netcap.*;import jpcap.*;import jpcap.pack

5、et.*;import java.util.*;import java.sql.Timestamp;public class JFrameMain extends javax.swing.JFrame implements ActionListenerprivate JMenuItem exitMenuItem;private JSeparator jSeparator2;private JMenuItem saveAsMenuItem;private JMenuItem saveMenuItem;private JMenuItem stopMenuItem;private JMenuItem

6、 startMenuItem;private JMenu Menu;private JMenuBar jMenuBar1;JTable tabledisplay = null;Vector rows,columns;DefaultTableModel tabModel;JScrollPane scrollPane;JLabel statusLabel;Netcaptor captor = new Netcaptor;/* Auto-generated main method to display this JFrame*/public static void main(String args)

7、 JFrameMain inst = new JFrameMain;inst.setVisible(true);public JFrameMain super;initGUI;private void initGUI try setSize(400, 300);jMenuBar1 = new JMenuBar;setJMenuBar(jMenuBar1);Menu = new JMenu;jMenuBar1.add(Menu);Menu.setText(u6293u5305);Menu.setPreferredSize(new java.awt.Dimension(35, 21);startM

8、enuItem = new JMenuItem;Menu.add(startMenuItem);startMenuItem.setText(开始);startMenuItem.setActionCommand(start);startMenuItem.addActionListener(this);stopMenuItem = new JMenuItem;Menu.add(stopMenuItem);stopMenuItem.setText(停止);stopMenuItem.setActionCommand(stop);stopMenuItem.addActionListener(this);

9、saveMenuItem = new JMenuItem;Menu.add(saveMenuItem);saveMenuItem.setText(保存);saveAsMenuItem = new JMenuItem;Menu.add(saveAsMenuItem);saveAsMenuItem.setText(保存为 .);jSeparator2 = new JSeparator;Menu.add(jSeparator2);exitMenuItem = new JMenuItem;Menu.add(exitMenuItem);exitMenuItem.setText(Exit);exitMen

10、uItem.setActionCommand(exit);exitMenuItem.addActionListener(this);rows=new Vector;columns= new Vector;columns.addElement(数据报时间);columns.addElement(源IP地址);columns.addElement(目的IP地址);columns.addElement(首部长度);columns.addElement(数据长度);columns.addElement(是否分段);columns.addElement(分段偏移量);columns.addElement

11、(首部内容);columns.addElement(数据内容);tabModel=new DefaultTableModel;tabModel.setDataVector(rows,columns);tabledisplay = new JTable( tabModel );scrollPane= new JScrollPane(tabledisplay);this.getContentPane.add( new JScrollPane(tabledisplay),BorderLayout.CENTER);statusLabel=new JLabel(06610班 张琛雨 066100583)

12、;this.getContentPane.add(statusLabel,BorderLayout.SOUTH); catch (Exception e) e.printStackTrace;public void actionPerformed(ActionEvent event)String cmd=event.getActionCommand;if(cmd.equals(start)captor.capturePacketsFromDevice;captor.setJFrame(this);else if(cmd.equals(stop)captor.stopCapture;else i

13、f(cmd.equals(exit)System.exit(0);public void dealPacket( Packet packet )tryVector r=new Vector;String strtmp;Timestamp timestamp = new Timestamp(packet.sec * 1000) + (packet.usec / 1000); r.addElement( timestamp.toString ); /数据报时间r.addElement(IPPacket)packet).src_ip.toString); /源IP地址r.addElement(IPP

14、acket)packet).dst_ip.toString); /目的IP地址r.addElement( packet.header.length ); /首部长度r.addElement( packet.data.length ); /数据长度r.addElement( (IPPacket)packet).dont_frag = true ? 分段 : 不分段 ); /是否不分段r.addElement( (IPPacket)packet).offset ); /数据长度strtmp = ;for(int i=0;i<packet.header.length;i+) strtmp +=

15、Byte.toString(packet.headeri);r.addElement(strtmp); /首部内容strtmp = ;for(int i=0;i<packet.data.length;i+) strtmp += Byte.toString(packet.datai);r.addElement(strtmp); /数据内容rows.addElement(r);tabledisplay.addNotify;catch( Exception e)在这里定义了一个向量r,当有数据包产生时,便将数据包赋值给r,rows.AddElement语句便将r添加到向量rows中,然后tabl

16、edisplay.addNotify;语句就会刷新界面将新的数据包显示出来。第二个程序:抓包package netcap;import java.io.File;import java.util.Vector;import javax.swing.JFileChooser;import javax.swing.JOptionPane;import jpcap.JpcapCaptor;import jpcap.PacketReceiver;import jpcap.JpcapWriter;import jpcap.packet.Packet;public class Netcaptor Jpca

17、pCaptor jpcap = null;JFrameMain frame;public void setJFrame(JFrameMain frame)this.frame=frame;public void capturePacketsFromDevice if(jpcap!=null)jpcap.close;jpcap = Jcapturedialog.getJpcap(frame);if (jpcap != null) startCaptureThread;private Thread captureThread;private void startCaptureThreadif(ca

18、ptureThread != null)return;captureThread = new Thread(new Runnablepublic void runwhile(captureThread != null)jpcap.processPacket(1, handler););captureThread.setPriority(Thread.MIN_PRIORITY);captureThread.start;void stopcaptureThreadcaptureThread = null;public void stopCaptureSystem.out.println(2);st

19、opcaptureThread;private PacketReceiver handler=new PacketReceiverpublic void receivePacket(Packet packet) /System.out.println(packet);frame.dealPacket(packet);定义一个抓包对象JpcapCaptor jpcap = null;但点击开始时调用private void startCaptureThread方法开始抓包,jpcap.processPacket(1, handler);语句能够反复调用handler所指向的方法,这个方法中定义的

20、packet就是网络上抓到的数据包,经过frame.dealPacket(packet);就可以显示在主界面上。程序三:抓包选项package netcap;import javax.swing.JFrame;import jpcap.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;/* This code was edited or generated using CloudGardens Jigloo* SWT/Swing GUI Builder, which is free for non-commerci

21、al* use. If Jigloo is being used commercially (ie, by a corporation,* company or business for any purpose whatever) then you* should purchase a license for each developer using Jigloo.* Please visit for details.* Use of Jigloo implies acceptance of these licensing terms.* A COMMERCIAL LICENSE HAS NO

22、T BEEN PURCHASED FOR* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.*/public class Jcapturedialog extends javax.swing.JDialog implements ActionListener /* Auto-generated main method to display this JDialog*/static JpcapCaptor jpcap=null;private

23、JRadioButton wholeRadioButton;private JPanel buttonPanel;private JButton cancelButton;private JButton okButton;private JRadioButton userRadioButton;private JRadioButton headRadioButton;private JPanel netPanel;private JTextField caplenTextField;private JPanel caplenPanel;private JTextField TextField;

24、private JPanel filterPanel;private JCheckBox CheckBox;private JComboBox netJComboBox;private JPanel jPanel_east;private JPanel jPanel_west;NetworkInterface devices;public static void main(String args) JFrame frame = new JFrame;Jcapturedialog inst = new Jcapturedialog(frame);inst.setVisible(true);pub

25、lic Jcapturedialog(JFrame frame) super(frame,选择要检测的网卡并设置参数,true);try BoxLayout thisLayout = new BoxLayout(getContentPane,javax.swing.BoxLayout.X_AXIS);getContentPane.setLayout(thisLayout);jPanel_west = new JPanel;jPanel_west.setLayout(new BoxLayout(jPanel_west,BoxLayout.Y_AXIS);getContentPane.add(jP

26、anel_west);netPanel = new JPanel;FlowLayout netPanelLayout = new FlowLayout;netPanelLayout.setAlignOnBaseline(true);netPanel.setBorder(BorderFactory.createTitledBorder(选择网卡);netPanel.setAlignmentX(Component.LEFT_ALIGNMENT);jPanel_west.add(netPanel);netPanel.setLayout(netPanelLayout);/ netPanel.setPr

27、eferredSize(new java.awt.Dimension(239, 56);devices = JpcapCaptor.getDeviceList;if(devices = null)JOptionPane.showMessageDialog(frame, 没有找到网卡);dispose;return;elseString names = new Stringdevices.length;for(int i=0;i < names.length;i+)namesi = (devicesi.description = null?devicesi.name:devicesi.des

28、cription);netJComboBox = new JComboBox(names);netPanel.add(netJComboBox); CheckBox = new JCheckBox;jPanel_west.add(CheckBox);FlowLayout CheckBoxLayout = new FlowLayout;CheckBoxLayout.setAlignOnBaseline(true);CheckBox.setText(u662fu5426u8bbeu7f6eu4e3au6df7u6742u6a21u5f0f);CheckBox.setLayout(null);fil

29、terPanel = new JPanel;filterPanel.setBorder(BorderFactory.createTitledBorder(捕获过滤器);filterPanel.setAlignmentX(Component.LEFT_ALIGNMENT);FlowLayout filterPanelLayout = new FlowLayout;filterPanelLayout.setAlignment(FlowLayout.LEFT);filterPanelLayout.setAlignOnBaseline(true);jPanel_west.add(filterPanel

30、);filterPanel.setLayout(filterPanelLayout);TextField = new JTextField(20);filterPanel.add(TextField);jPanel_east = new JPanel;jPanel_east.setLayout(new BoxLayout(jPanel_east,BoxLayout.Y_AXIS);getContentPane.add(jPanel_east);caplenPanel = new JPanel;caplenPanel.setBorder(BorderFactory.createTitledBor

31、der(最长字长);caplenPanel.setAlignmentX(Component.LEFT_ALIGNMENT);jPanel_east.add(caplenPanel);caplenPanel.setLayout(new BoxLayout(caplenPanel,BoxLayout.Y_AXIS);caplenTextField = new JTextField(20);caplenPanel.add(caplenTextField);caplenTextField.setText(1514);caplenTextField.setEnabled(false);wholeRadi

32、oButton = new JRadioButton;FlowLayout userRadioButtonLayout = new FlowLayout;userRadioButtonLayout.setAlignOnBaseline(true);caplenPanel.add(wholeRadioButton);wholeRadioButton.setText(u6574u4e2au6570u636eu62a5);wholeRadioButton.setSelected(true);wholeRadioButton.addActionListener(this);headRadioButto

33、n = new JRadioButton;caplenPanel.add(headRadioButton);headRadioButton.setText(u4ec5u9996u90e8);headRadioButton.addActionListener(this);userRadioButton = new JRadioButton;caplenPanel.add(userRadioButton);userRadioButton.setText(u5176u4ed6u90e8u5206);userRadioButton.addActionListener(this);ButtonGroup

34、 group=new ButtonGroup;group.add(wholeRadioButton);wholeRadioButton.setActionCommand(Whole);group.add(headRadioButton);headRadioButton.setActionCommand(Head);group.add(userRadioButton);userRadioButton.setActionCommand(user);buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT);/ buttonPanel.setL

35、ayout(new BoxLayout(buttonPanel,BoxLayout.X_AXIS);jPanel_east.add(buttonPanel);okButton = new JButton;buttonPanel.add(okButton);FlowLayout cancelButtonLayout = new FlowLayout;cancelButtonLayout.setAlignOnBaseline(true);okButton.setText(u786eu5b9a);okButton.setActionCommand(ok);okButton.addActionList

36、ener(this);cancelButton = new JButton;buttonPanel.add(cancelButton);cancelButton.setText(u53d6u6d88);cancelButton.setActionCommand(cancel);cancelButton.addActionListener(this);/ buttonPanel.setAlignmentX(Component.RIGHT_ALIGNMENT);getContentPane.setLayout(new BoxLayout(getContentPane,BoxLayout.X_AXI

37、S);getContentPane.add(jPanel_west);getContentPane.add(jPanel_east);pack; catch (Exception e) e.printStackTrace;public void actionPerformed(ActionEvent evt)String cmd=evt.getActionCommand;if(cmd.equals(Whole)caplenTextField.setText(1514);caplenTextField.setEnabled(false);else if(cmd.equals(Head)caple

38、nTextField.setText(68);caplenTextField.setEnabled(false);else if(cmd.equals(user)caplenTextField.setText();caplenTextField.setEnabled(true);caplenTextField.requestFocus;else if(cmd.equals(ok)tryint caplen=Integer.parseInt(caplenTextField.getText);if(caplen<68 | caplen>1514)JOptionPane.showMessag

39、eDialog(null,捕获长度必须介于 68 和 1514之间);return;jpcap=JpcapCaptor.openDevice(devicesnetJComboBox.getSelectedIndex,caplen,CheckBox.isSelected,50);if(TextField.getText!=null & TextField.getText.length>0)jpcap.setFilter(TextField.getText,true);catch(NumberFormatException e)JOptionPane.showMessageDialog(nul

40、l,捕获长度必须是正整数);catch(java.io.IOException e)JOptionPane.showMessageDialog(null,e.toString);jpcap=null;finallydispose;else if(cmd.equals(cancel)dispose;public static JpcapCaptor getJpcap(JFrame parent)new Jcapturedialog(parent).setVisible(true);return jpcap;这一部分主要是界面操作,根据jigloo插件对不同的按钮和文本框还有其他的组件设置监听操作

41、,以激发不同的函数操作,主要是devices = JpcapCaptor.getDeviceList;语句和jpcap=JpcapCaptor.openDevice(devicesnetJComboBox.getSelectedIndex,caplen,CheckBox.isSelected,50);语句要选择一下监听的网卡,比如说笔记本就有两个网卡,一个无线一个有线,选择一下就会监听相应的网卡。函数publicstatic JpcapCaptor getJpcap(JFrame parent)new Jcapturedialog(parent).setVisible(true);returnjpcap;返回jpcap,这个jpcap就是对应的选择上的网卡对象,接下来就从对应的网卡对象jpcap上不断得到数据包。

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!