本文目錄一覽:
網絡抓包,怎麼用Java程序登錄百度
一、實驗內容描述
本實驗是用java實現的網絡抓包程序,在windows環境下安裝winpcap4.0和jpcap6.0後,下載eclipse和jigloo插件(一種在eclipse底下作圖形化開發的工具),將其安裝好,然後就可以進行java的網絡抓包圖形化開發了。
二、原理與關鍵技術
網絡抓包技術原理
網絡層上有各種各樣的數據包,它們以不同的幀格式在網絡層上進行傳輸,但是在傳輸時它們都遵循相同的格式,即有相同的長度,如果一種協議的幀格式達不到這種長度,就讓其補齊,以達到我們的要求。
網絡抓包關鍵技術
無論是在windows操作系統下還是在linux操作系統下,要想捕獲網絡上的數據包,必須要對網卡進行控制,因為本機的數據報從網絡上來到本機是通過網卡然後再保存到本地緩衝區上的,所以要抓獲網包就必須調用網卡驅動中的對外函數,在linux系統中有net.h文件,可以調用net.h文件中的函數來操作網卡,可以直接編程實現,但為了更方便的使用,可以安裝一個叫libpcap的軟件,這樣調用函數更好用,而在windows系統中,因為源代碼不對外公開,所以要安裝一個叫winpcap的軟件,這樣用C或VC++就可以實現了,但因為我用的是java語言來實現的,所以無論是在哪個系統都要安裝一個叫jpcap的軟件,它本身就把底層的函數又封裝了一下,這樣就可以讓java來使用了。
三、設計與實現
1.基於java的設計方案
我的這個網絡抓包程序是圖形化操作界面,在菜單欄點擊抓包按鈕後選擇網卡和過濾字還有最長字長,點擊開始,然後就可以開始抓包了,在主界面中就會顯示出一行又一行的數據,這些數據就是抓獲到的數據包。
具體實現
安裝winpcap4.0和jpcap6.0
2.下載eclipse3.3和jigloo,jigloo是eclipse底下的插件,是用來支持eclipse底下的java 圖形化開發的。
3.編寫java抓包程序:
4.建立三個文件,一個主程序,一個抓包程序,一個抓包選項程序對話框程序
5.第一個程序:主程序如下
package netcap;
import java.awt.event.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.packet.*;
import java.util.*;
import java.sql.Timestamp;
/**
* This code was edited or generated using CloudGarden’s Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* 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
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class JFrameMain extends javax.swing.JFrame implements ActionListener{
private JMenuItem exitMenuItem;
private JSeparator jSeparator2;
private JMenuItem saveAsMenuItem;
private JMenuItem saveMenuItem;
private JMenuItem stopMenuItem;
private JMenuItem 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) {
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(“/u6293/u5305”);
Menu.setPreferredSize(new java.awt.Dimension(35, 21));
{
startMenuItem = 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);
}
{
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”);
exitMenuItem.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(“首部內容”);
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”);
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 if(cmd.equals(“exit”)){
System.exit(0);
}
}
public void dealPacket( Packet packet )
{
try
{
Vector 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(((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(int i=0;ipacket.header.length;i++){
strtmp += Byte.toString(packet.header[i]);
}
r.addElement(strtmp); //首部內容
strtmp = “”;
for(int i=0;ipacket.data.length;i++){
strtmp += Byte.toString(packet.data[i]);
}
r.addElement(strtmp); //數據內容
rows.addElement(r);
tabledisplay.addNotify();
}
catch( Exception e)
{
}
}
}
抓包(百度百科)
抓包(packet capture)就是將 網絡傳輸 發送與接收的 數據包 進行截獲、重發、編輯、轉存等操作,也用來檢查網絡安全。抓包也經常被用來進行數據截取等。
Java套接字socket,只提供了建立套接字服務端,和連接該服務端。至於抓包,需要javaAPI以外的工具。
java能實現抓包分析嗎
可以,我做過,首先你得安裝winpcap,然後下載jpcap的jar包,jar包有一些封裝好的函數,直接調用就可以抓局域網內的包
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/282550.html