十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
一、实验内容描述
创新互联公司-专业网站定制、快速模板网站建设、高性价比和平网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式和平网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖和平地区。费用合理售后完善,10年实体公司更值得信赖。
本实验是用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)
{
}
}
}
package com.daidai.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
Configuration conf=new Configuration().configure();
sessionFactory=conf.buildSessionFactory();
return sessionFactory;
}
public static void setSessionFactory(SessionFactory sessionFactory) {
HibernateUtil.sessionFactory = sessionFactory;
}
public static Session getSession(){
return sessionFactory.openSession();
}
}
package com.daidai.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class Util {
private static String Cellphone_Number;
private static String Cellphone_Number1;
private static String Cellphone_Number2;
private static String Fasten_Telephone;
private static String Fasten_Telephone2;
private static String name;
private static String remarks;
public static Sheet rs;
public Util(){}
static{
try{
Workbook rwb=Workbook.getWorkbook(new File("Book1.xls"));
rs=rwb.getSheet(0);
int count=rs.getColumns();
System.out.println(count);
int count1=rs.getRows();
System.out.println(count1);
//获取第一行,第一列的值
//count1行
//count列
for(int i=0;icount1;i++){
for(int j=0;jcount;j++){
Cell c00 = rs.getCell(j, i);
String strc00 = c00.getContents();
//获取第一行,第二列的值
Cell c10 = rs.getCell(1, 0);
String strc10 = c10.getContents();
System.out.println(strc00);
System.out.println(strc10);
}
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[]args){
Util u=new Util();
}
}
?xml version='1.0' encoding='utf-8'?
!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
""
hibernate-configuration
session-factory
property name="connection.driver_class"
com.mysql.jdbc.Driver/property
property name="connection.url"
jdbc:mysql://127.0.0.1:3306/test/property
property name="connection.username"
root/property
property name="connection.password"
daidai/property
!-- SQL dialect 数据库方言--
property name="dialect"
org.hibernate.dialect.MySQLDialect/property
!-- Echo all executed SQL to stdout --
property name="show_sql"true/property
property name="hbm2ddl.auto"create/property
mapping resource="User.hbm.xml"/
/session-factory
/hibernate-configuration
?xml version="1.0" encoding="utf-8"?
!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
""
hibernate-mapping package="com.daidai.entity"
class name="User" table="t_user"
id name="id"
generator class="native"/
/id
property name="name" type="java.lang.String" length="15"/
property name="Cellphone_Number" type="java.lang.String" length="11"/
property name="Cellphone_Number1" type="java.lang.String" length="11"/
property name="Cellphone_Number2" type="java.lang.String" length="11"/
property name="Fasten_Telephone" type="java.lang.String" length="11"/
property name="Fasten_Telephone2" type="java.lang.String" length="11"/
property name="remarks" type="java.lang.String"/
/class
/hibernate-mapping
package com.daidai.dao.impl;
import com.daidai.util.*;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import com.daidai.dao.UserDAO;
import com.daidai.entity.User;
public class UserDAOImpl implements UserDAO {
public Session session=null;
public void save(User user) {
try {
session=HibernateUtil.getSession();
session.getTransaction().begin();
session.save(user);
session.getTransaction().commit();
} catch (HibernateException e) {
session.getTransaction().rollback();
e.printStackTrace();
throw e;
}
}
}
package com.daidai.dao;
import com.daidai.entity.User;
public interface UserDAO {
public void save(User user);
}
1.确保写的类没问题,java程序是先编译,后执行
2.打开cmd,进入该文件所在路径,输入javac -d 类名.java,对该java源文件编译,生成class文件
3.再输入java 类名 ,就可以运行这个程序了。
如果有多个类,需要写个bat批处理文件,即同时运行多个java类、jar包、配置文件、资源文件。
java程序需要设置proxy才能生效:
String url="";
Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP,new InetSocketAddress("127.0.0.1", 8888));
URL serverUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) serverUrl.openConnection(proxy);
抓包工具fidder是一个很轻巧的可以获取浏览器,程序的http,https请求的软件。
百科地址:
官网地址:
firefox的fidder插件
而Java程序需要设置proxy才能生效:
[java] view plain copy
Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP,new InetSocketAddress("127.0.0.1", 8888));
URL serverUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) serverUrl.openConnection(proxy);
详细代码请下载。
[java] view plain copy
package com.zuidaima.http;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
/**
*@author
**/
public class Http302Redirect {
public static void main(String[] args) {
try {
String url = "";
System.out.println("访问地址:" + url);
Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP,
new InetSocketAddress("127.0.0.1", 8888));
URL serverUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) serverUrl
.openConnection(proxy);
conn.setRequestMethod("GET");
conn.addRequestProperty("Accept-Charset", "UTF-8;");
conn.addRequestProperty("User-Agent",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Firefox/3.6.8");
conn.addRequestProperty("Referer", "");
conn.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
StringBuffer buffer = new StringBuffer();
String line = reader.readLine();
while (line != null) {
buffer.append(line).append("\r\n");
line = reader.readLine();
}
reader.close();
conn.disconnect();
System.out.println(buffer);
} catch (Exception e) {
e.printStackTrace();
}
}
}