java的网络抓包技巧.docx
- 文档编号:14105200
- 上传时间:2023-06-20
- 格式:DOCX
- 页数:20
- 大小:20.28KB
java的网络抓包技巧.docx
《java的网络抓包技巧.docx》由会员分享,可在线阅读,更多相关《java的网络抓包技巧.docx(20页珍藏版)》请在冰点文库上搜索。
java的网络抓包技巧
基于java的网络抓包方法
一、实验内容描述
本实验是用java实现的网络抓包程序,在windows环境下安装winpcap4.0和jpcap6.0后,下载eclipse和jigloo插件(一种在eclipse底下作图形化开发的工具),将其安装好,然后就可以进行java的网络抓包图形化开发了。
二、原理与关键技术
2.1网络抓包技术原理
网络层上有各种各样的数据包,它们以不同的帧格式在网络层上进行传输,但是在传输时它们都遵循相同的格式,即有相同的长度,如果一种协议的帧格式达不到这种长度,就让其补齐,以达到我们的要求。
2.2网络抓包关键技术
无论是在windows操作系统下还是在linux操作系统下,要想捕获网络上的数据包,必须要对网卡进行控制,因为本机的数据报从网络上来到本机是通过网卡然后再保存到本地缓冲区上的,所以要抓获网包就必须调用网卡驱动中的对外函数,在linux系统中有net.h文件,可以调用net.h文件中的函数来操作网卡,可以直接编程实现,但为了更方便的使用,可以安装一个叫libpcap的软件,这样调用函数更好用,而在windows系统中,因为源代码不对外公开,所以要安装一个叫winpcap的软件,这样用C或VC++就可以实现了,但因为我用的是java语言来实现的,所以无论是在哪个系统都要安装一个叫jpcap的软件,它本身就把底层的函数又封装了一下,这样就可以让java来使用了。
三、设计与实现
3.1基于java的设计方案
我的这个网络抓包程序是图形化操作界面,在菜单栏点击抓包按钮后选择网卡和过滤字还有最长字长,点击开始,然后就可以开始抓包了,在主界面中就会显示出一行又一行的数据,这些数据就是抓获到的数据包。
3.2具体实现
1、安装winpcap4.0和jpcap6.0
2、下载eclipse3.3和jigloo,jigloo是eclipse底下的插件,是用来支持eclipse底下的java图形化开发的。
3、编写java抓包程序:
建立三个文件,一个主程序,一个抓包程序,一个抓包选项程序对话框程序
第一个程序:
主程序如下
packagenetcap。
importjava.awt.event.ActionEvent。
importjava.awt.event.ActionListener。
importjavax.swing.JSeparator。
importjavax.swing.JMenuItem。
importjavax.swing.JMenu。
importjavax.swing.JMenuBar。
importjava.awt.*。
importjava.awt.event.*。
importjavax.swing.*。
importjavax.swing.table.*。
importnetcap.*。
importjpcap.*。
importjpcap.packet.*。
importjava.util.*。
importjava.sql.Timestamp。
publicclassJFrameMainextendsjavax.swing.JFrameimplementsActionListener{
privateJMenuItemexitMenuItem。
privateJSeparatorjSeparator2。
privateJMenuItemsaveAsMenuItem。
privateJMenuItemsaveMenuItem。
privateJMenuItemstopMenuItem。
privateJMenuItemstartMenuItem。
privateJMenuMenu。
privateJMenuBarjMenuBar1。
JTabletabledisplay=null。
Vectorrows,columns。
DefaultTableModeltabModel。
JScrollPanescrollPane。
JLabelstatusLabel。
Netcaptorcaptor=newNetcaptor()。
/**
*Auto-generatedmainmethodtodisplaythisJFrame
*/
publicstaticvoidmain(String[]args){
JFrameMaininst=newJFrameMain()。
inst.setVisible(true)。
}
publicJFrameMain(){
super()。
initGUI()。
}
privatevoidinitGUI(){
try{
setSize(400,300)。
{
jMenuBar1=newJMenuBar()。
setJMenuBar(jMenuBar1)。
{
Menu=newJMenu()。
jMenuBar1.add(Menu)。
Menu.setText("\u6293\u5305")。
Menu.setPreferredSize(newjava.awt.Dimension(35,21))。
{
startMenuItem=newJMenuItem()。
Menu.add(startMenuItem)。
startMenuItem.setText("开始")。
startMenuItem.setActionCommand("start")。
startMenuItem.addActionListener(this)。
}
{
stopMenuItem=newJMenuItem()。
Menu.add(stopMenuItem)。
stopMenuItem.setText("停止")。
stopMenuItem.setActionCommand("stop")。
stopMenuItem.addActionListener(this)。
}
{
saveMenuItem=newJMenuItem()。
Menu.add(saveMenuItem)。
saveMenuItem.setText("保存")。
}
{
saveAsMenuItem=newJMenuItem()。
Menu.add(saveAsMenuItem)。
saveAsMenuItem.setText("保存为...")。
}
{
jSeparator2=newJSeparator()。
Menu.add(jSeparator2)。
}
{
exitMenuItem=newJMenuItem()。
Menu.add(exitMenuItem)。
exitMenuItem.setText("Exit")。
exitMenuItem.setActionCommand("exit")。
exitMenuItem.addActionListener(this)。
}
}
}
rows=newVector()。
columns=newVector()。
columns.addElement("数据报时间")。
columns.addElement("源IP地址")。
columns.addElement("目的IP地址")。
columns.addElement("首部长度")。
columns.addElement("数据长度")。
columns.addElement("是否分段")。
columns.addElement("分段偏移量")。
columns.addElement("首部内容")。
columns.addElement("数据内容")。
tabModel=newDefaultTableModel()。
tabModel.setDataVector(rows,columns)。
tabledisplay=newJTable(tabModel)。
scrollPane=newJScrollPane(tabledisplay)。
this.getContentPane().add(newJScrollPane(tabledisplay),BorderLayout.CENTER)。
statusLabel=newJLabel("06610班张琛雨066100583")。
this.getContentPane().add(statusLabel,BorderLayout.SOUTH)。
}catch(Exceptione){
e.printStackTrace()。
}
}
publicvoidactionPerformed(ActionEventevent){
Stringcmd=event.getActionCommand()。
if(cmd.equals("start")){
captor.capturePacketsFromDevice()。
captor.setJFrame(this)。
}
elseif(cmd.equals("stop")){
captor.stopCapture()。
}
elseif(cmd.equals("exit")){
System.exit(0)。
}
}
publicvoiddealPacket(Packetpacket)
{
try
{
Vectorr=newVector()。
Stringstrtmp。
Timestamptimestamp=newTimestamp((packet.sec*1000)+(packet.usec/1000))。
r.addElement(timestamp.toString())。
//数据报时间
r.addElement(((IPPacket)packet).src_ip.toString())。
//源IP地址
r.addElement(((IPPacket)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(inti=0。
i i++){ strtmp+=Byte.toString(packet.header[i])。 } r.addElement(strtmp)。 //首部内容 strtmp=""。 for(inti=0。 i i++){ strtmp+=Byte.toString(packet.data[i])。 } r.addElement(strtmp)。 //数据内容 rows.addElement(r)。 tabledisplay.addNotify()。 } catch(Exceptione) { } } } 在这里定义了一个向量r,当有数据包产生时,便将数据包赋值给r,rows.AddElement(r)语句便将r添加到向量rows中,然后tabledisplay.addNotify()。 语句就会刷新界面将新的数据包显示出来。 第二个程序: 抓包 packagenetcap。 importjava.io.File。 importjava.util.Vector。 importjavax.swing.JFileChooser。 importjavax.swing.JOptionPane。 importjpcap.JpcapCaptor。 importjpcap.PacketReceiver。 importjpcap.JpcapWriter。 importjpcap.packet.Packet。 publicclassNetcaptor{ JpcapCaptorjpcap=null。 JFrameMainframe。 publicvoidsetJFrame(JFrameMainframe){ this.frame=frame。 } publicvoidcapturePacketsFromDevice(){ if(jpcap! =null) jpcap.close()。 jpcap=Jcapturedialog.getJpcap(frame)。 if(jpcap! =null){ startCaptureThread()。 } } privateThreadcaptureThread。 privatevoidstartCaptureThread(){ if(captureThread! =null) return。 captureThread=newThread(newRunnable(){ publicvoidrun(){ while(captureThread! =null){ jpcap.processPacket(1,handler)。 } } })。 captureThread.setPriority(Thread.MIN_PRIORITY)。 captureThread.start()。 } voidstopcaptureThread(){ captureThread=null。 } publicvoidstopCapture(){ System.out.println (2)。 stopcaptureThread()。 } privatePacketReceiverhandler=newPacketReceiver(){ publicvoidreceivePacket(Packetpacket){ //System.out.println(packet)。 frame.dealPacket(packet)。 } }。 } 定义一个抓包对象JpcapCaptorjpcap=null。 但点击开始时调用privatevoidstartCaptureThread()方法开始抓包,jpcap.processPacket(1,handler)。 语句能够反复调用handler所指向的方法,这个方法中定义的packet就是网络上抓到的数据包,经过frame.dealPacket(packet)。 就可以显示在主界面上。 程序三: 抓包选项 packagenetcap。 importjavax.swing.JFrame。 importjpcap.*。 importjava.awt.*。 importjava.awt.event.*。 importjavax.swing.*。 /** *ThiscodewaseditedorgeneratedusingCloudGarden'sJigloo *SWT/SwingGUIBuilder,whichisfreefornon-commercial *use.IfJiglooisbeingusedcommercially(ie,byacorporation, *companyorbusinessforanypurposewhatever)thenyou *shouldpurchasealicenseforeachdeveloperusingJigloo. *Pleasevisitfordetails. *UseofJiglooimpliesacceptanceoftheselicensingterms. *ACOMMERCIALLICENSEHASNOTBEENPURCHASEDFOR *THISMACHINE,SOJIGLOOORTHISCODECANNOTBEUSED *LEGALLYFORANYCORPORATEORCOMMERCIALPURPOSE. */ publicclassJcapturedialogextendsjavax.swing.JDialogimplementsActionListener{ /** *Auto-generatedmainmethodtodisplaythisJDialog */ staticJpcapCaptorjpcap=null。 privateJRadioButtonwholeRadioButton。 privateJPanelbuttonPanel。 privateJButtoncancelButton。 privateJButtonokButton。 privateJRadioButtonuserRadioButton。 privateJRadioButtonheadRadioButton。 privateJPanelnetPanel。 privateJTextFieldcaplenTextField。 privateJPanelcaplenPanel。 privateJTextFieldTextField。 privateJPanelfilterPanel。 privateJCheckBoxCheckBox。 privateJComboBoxnetJComboBox。 privateJPaneljPanel_east。 privateJPaneljPanel_west。 NetworkInterface[]devices。 publicstaticvoidmain(String[]args){ JFrameframe=newJFrame()。 Jcapturedialoginst=newJcapturedialog(frame)。 inst.setVisible(true)。 } publicJcapturedialog(JFrameframe){ super(frame,"选择要检测的网卡并设置参数",true)。 try{ BoxLayoutthisLayout=newBoxLayout( getContentPane(), javax.swing.BoxLayout.X_AXIS)。 getContentPane().setLayout(thisLayout)。 { jPanel_west=newJPanel()。 jPanel_west.setLayout(newBoxLayout(jPanel_west,BoxLayout.Y_AXIS))。 getContentPane().add(jPanel_west)。 { netPanel=newJPanel()。 FlowLayoutnetPanelLayout=newFlowLayout()。 netPanelLayout.setAlignOnBaseline(true)。 netPanel.setBorder(BorderFactory.createTitledBorder("选择网卡"))。 netPanel.setAlignmentX(Component.LEFT_ALIGNMENT)。 jPanel_west.add(netPanel)。 netPanel.setLayout(netPanelLayout)。 //netPanel.setPreferredSize(newjava.awt.Dimension(239,56))。 { devices=JpcapCaptor.getDeviceList()。 if(devices==null){ JOptionPane.showMessageDialog(frame,"没有找到网卡")。 dispose()。 return。 } else{ String[]names=newString[devices.length]。 for(inti=0。 i i++){ names[i]=(devices[i].description==null? devices[i].name: devices[i].description)。 } netJComboBox=newJComboBox(names)。 } netPanel.add(netJComboBox)。 } } { CheckBox=newJCheckBox()。 jPanel_west.add(CheckBox)。 FlowLayoutCheckBoxLayout=newFlowLayout()。 CheckBoxLayout.setAlignOnBaseline(true)。 CheckBox.setText("\u662f\u5426\u8bbe\u7f6e\u4e3a\u6df7\u6742\u6a21\u5f0f")。 CheckBox.setLayout(null)。 } { filterPanel=newJPanel()。 filterPanel.setBorder(BorderFactory.createTitledBorder("捕获过滤器"))。 filterPanel.setAlignmentX(Component.LEFT_ALIGNMENT)。 FlowLayoutfilterPanelLayout=newFlowLayout()。 filterPanelLayout.setAlignment(FlowLayout.LEFT)。 filterPanelLayout.setAlignOnBaseline(true)。 jPanel_west.add(filterPanel)。 filter
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 网络 技巧
![提示](https://static.bingdoc.com/images/bang_tan.gif)