pcap包解析.doc_第1页
pcap包解析.doc_第2页
pcap包解析.doc_第3页
pcap包解析.doc_第4页
pcap包解析.doc_第5页
已阅读5页,还剩24页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

项目从pcap文件中析取所有TCP会话与UDP会话张中秋(U201217502 ,853950423, tel1205班) 执笔 100% 签字摘要:本项目主要针对的是对于单一网络节点,.pcap文件是二进制格式的网络轨迹(Network trace)文件,记录了网络通信过程的数据包信息。csv(comma separated values)是一种用逗号,分隔的文本文件,类似于excel文件。.pcap文件是二进制文件,需要从中提取信息并以csv的格式进行存储。可以使用tshark实现对.pcap文件进行分析。关键词: TCP/UDP协议,数据包,文件,IP包1. 引言要对整个pcap文件进行操作,就必须先对pcap文件进行解析,析取pcap文件中所有的TCP会话和UDP会话。HTTP使用TCP而不是UDP的原因在于(打开)一个网页必须传送很多数据,而TCP协议提供传输控制,按顺序组织数据,和错误纠正。所以在做http重组之前,对pcap文件中TCP会话与UDP会话的提取非常的关键。2. 项目问题分析与解决问题的算法Pcap文件结构:(1) 基本格式:文件头 数据包头 数据报数据包头数据报.(2) 文件头: 图1 pcap文件头a、标识位:32位的,这个标识位的值是16进制的 0xa1b2c3d4。b、主版本号:16位, 默认值为0x2。c、副版本号:16位,默认值为0x04。d、区域时间:32位,实际上该值并未使用,因此可以将该位设置为0。e、精确时间戳:32位,实际上该值并未使用,因此可以将该值设置为0。f、数据包最大长度:32位,该值设置所抓获的数据包的最大长度,如果所有数据包都要抓获,将该值设置为65535;例如:想获取数据包的前64字节,可将该值设置为64。g、链路层类型:32位, 数据包的链路层包头决定了链路层的类型。(3) pcap数据包头:图2 pcap数据包头a、时间戳,包括:秒计时:32位,一个UNIX格式的精确到秒时间值,用来记录数据包抓获的时间,记录方式是记录从格林尼治时间的1970年1月1日 00:00:00 到抓包时经过的秒数;微秒计时:32位, 抓取数据包时的微秒值。b、数据包长度:32位 ,标识所抓获的数据包保存在pcap文件中的实际长度,以字节为单位。c、数据包实际长度: 所抓获的数据包的真实长度,如果文件中保存不是完整的数据包,那么这个值可能要比前面的数据包长度的值大。(4) pcap数据: 即Packet(通常就是链路层的数据帧)具体内容,长度就是Caplen,这个长度的后面,就是当前PCAP文件中存放的下一个Packet数据包,也就是说:PCAP文件里面并没有规定捕获的Packet数据包之间有什么间隔字符串,下一组数据在文件中的起始位置。我们需要靠第一个Packet包确定。(5) 读取时进制转换问题:package PcapFinally;import java.util.List;public class Pcap private PcapHeader header;private List data;public PcapHeader getHeader()return header;public void setHeader(PcapHeader header)this.header=header;public List getData()return data;public void setData(List data)this.data=data;public void ottString()System.out.println(data part count=+data.size();package PcapFinally;import java.io.BufferedWriter;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;public class PcapData public static String source_adrress;/数据包头文件public static int time_s;/时间戳(秒)public static int time_ms;/时间戳(微秒)public static int pLength;/抓包长度public static int length;/实际长度public static byte content;/数据public String tai;/以太网帧 14字节 不作处直接读取十四个字节/ip包public String version_ihl;/ip包中的版本号家包头长度 1字节public String tos;/服务类型 8字节public Short totalLen;/总长度 2字节public Short identification;/用于柱状数据包的标识符 2字节public Short flags_fOffset;/控制表示+数据偏移 2字节public String timeToLive;/生命周期 1字节public static String protocol;/协议类型 1字节private short headerChc;/头校验码 2字节public static String source_address;/原地址 4字节public static String des_address;/目的地址 4字节private String options_padding;/ 4字节、/根据version_ihl中的地位数,确定ip的位数/tcp结构public static String source_port;/源端口 2字节public static String des_port;/目的端口 2字节private String seq_number;/ 序号 大小端原因,高低位4个8bit的存放顺序是反的,intel使用小端模式 4字节private String ack_number;/确认号,大小端原因,高低位4个8bit的存放顺序是反的,intel使用小端模式 4字节private String info_ctrl;/Data Offset (4 bits), Reserved (6 bits), Control bits (6 bits),intel使用小端模式 2字节private String window;/窗口 2字节private String checksum;/ 校验和 2字节private String urgent_pointer;/ 紧急指针 2字节/udp结构public String usour_port;/源端口 2字节public String udes_port;/目的端口 2字节private String ulength;/udp长度 2 字节private String ucrc;/udp校验码 2字节public int getTime_s()return time_s;public void setTime_s(int time_s)this.time_s=time_s;public int getTime_ms()return time_ms;public void setTime_ms(int time_ms)this.time_ms=time_ms;public int getpLength()return pLength;public void setpLength(int pLength)this.pLength=pLength;public int getLength()return length;public void setLength(int length)this.length=length;public byte getContent()return content;public void setContent(byte content)this.content=content;public String getiTai()return tai;public void setiTai(String tai )this.tai=tai;public String getVersion_ihl()return version_ihl;public void setVersion_ihl(String version_ihl)this.version_ihl=version_ihl;public String getTos()return tos;public void setTos(String tos)this.tos=tos;public short getTotalLen()return totalLen;public void setTotalLen(short totalLen)this.totalLen=totalLen;public short getIdentification()return identification;public void setIdentification(short identification)this.identification=identification;public short getFlags_fOffset()return flags_fOffset;public void setFlags_fOffset(short flags_fOffset)this.flags_fOffset=flags_fOffset;public String getTimeToLive()return timeToLive;public void setTimeToLive(String timeToLive)this.timeToLive=timeToLive;public String getProtocol()return protocol;public void setProtocol(String protocol)tocol=protocol;public short getHeaderChc()return headerChc;public void setHeaderChc(short headerChc)this.headerChc=headerChc;public String getSource_address()return source_address;public void setSource_address(String source_address)this.source_address=source_address;public String getDes_adress()return des_address;public void setDes_adress(String des_address)this.des_address=des_address;public String getOptions_padding()return options_padding;public void setOptions_padding(String options_padding)this.options_padding=options_padding;public String getSource_port()return source_port;public void setSource_port(String source_port)this.source_port=source_port;public String getDes_port()return des_port;public void setDes_port(String des_port)this.des_port=des_port;public String getSeq_number()return seq_number;public void setSeq_number(String seq_number)this.seq_number=seq_number;public String getAck_number()return ack_number;public void setAck_number(String ack_number)this.ack_number=ack_number;public String getInfo_ctrl()return info_ctrl;public void setInfo_ctrl(String info_ctrl)_ctrl=info_ctrl;public String getWindow()return window;public void setWindow(String window)this.window=window;public String getChecksum()return checksum;public void setChecksum(String checksum)this.checksum=checksum;public String getUrgent_pointer()return urgent_pointer;public void setUrgent_pointer(String urgent_pointer)this.urgent_pointer=urgent_pointer;public String getUsour_port()return usour_port;public void setUsour_port(String usour_port)this.usour_port=usour_port;public String getUdes_port()return usour_port;public void setUdes_port(String udes_port)this.udes_port=udes_port;public String getUlength()return ulength;public void setUlength(String ulength)this.ulength=ulength;public String getUcrc()return ucrc;public void setUcrc(String ucrc)this.ucrc=ucrc;private String content2Str(byte content)char chars = new charcontent.length;for(int i = 0;i content.length; i+)charsi = (char) contenti;return String.valueOf(chars);public void ttString() throws IOException System.out.println(time_s=+this.time_s);System.out.println(ntime_ms=+this.time_ms);System.out.println(npLength=+this.pLength);System.out.println(nlength=+this.length);System.out.println(ncontent=+content2Str(this.content);System.out.println(niTai=+this.tai);System.out.println(nversion_ihl=+this.version_ihl);System.out.println(ntos=+this.tos);System.out.println(ntotal_length=+this.totalLen);System.out.println(nidentification=+this.identification);System.out.println(nflags_offset=+this.flags_fOffset);System.out.println(ntimeToLive=+this.timeToLive);System.out.println(nprptocol=+tocol);System.out.println(nHeaderChecksum=+this.headerChc);System.out.println(nsource_address=+this.source_address);System.out.println(nDes_address=+this.des_address);System.out.println(noption_padding=+this.options_padding);System.out.println(nsour_port=+this.source_port);System.out.println(ndes_port=+this.des_port);System.out.println(nseq_numer=+this.seq_number);System.out.println(nack_number=+this.ack_number);System.out.println(ninfo_ctrl=+_ctrl);System.out.println(nwindow=+this.window);System.out.println(nchecksum=+this.checksum);System.out.println(nurgent_pointer=+this.urgent_pointer);System.out.println(n#);public String ooString() StringBuilder s = new StringBuilder(); s.append(time_s=).append(this.time_s); s.append(ntime_ms=).append(this.time_ms); s.append(npLength=).append(this.pLength); s.append(nlength=).append(this.length); s.append(ncontent=).append(this.content); s.append(niTai=).append(this.tai); s.append(nversion_ihl=).append(this.tai);System.out.println(nversion_ihl=+this.version_ihl);System.out.println(ntos=+this.tos);System.out.println(ntotal_length=+this.totalLen);System.out.println(nidentification=+this.identification);System.out.println(nflags_offset=+this.flags_fOffset);System.out.println(ntimeToLive=+this.timeToLive);System.out.println(nprptocol=+tocol);System.out.println(nHeaderChecksum=+this.headerChc);System.out.println(nsource_address=+this.source_address);System.out.println(nDes_address=+this.des_address);System.out.println(noption_padding=+this.options_padding);System.out.println(nsour_port=+this.source_port);System.out.println(ndes_port=+this.des_port);System.out.println(nseq_numer=+this.seq_number);System.out.println(nack_number=+this.ack_number);System.out.println(ninfo_ctrl=+_ctrl);System.out.println(nwindow=+this.window);System.out.println(nchecksum=+this.checksum);System.out.println(nurgent_pointer=+this.urgent_pointer); return null; package PcapFinally;public class PcapHeader private int magic;/文件识别头,为1a2b3c4dprivate short magor_version;/主要版本private short minor_version;/次要版本private int timezone;/当地标准时间private int sigflags;/时间戳的精度private int snaplen;/最大的存储长度private int linktype;/链路类型public int getMagic()return magic;public void setMagic(int magic)this.magic=magic;public short getMagor_version()return magor_version;public void setMagor_version(short magor_version)this.magor_version=magor_version;public short getMinor_version()return minor_version;public void setMinor_version(short minor_version)this.minor_version=minor_version;public int getTimezone()return timezone;public void setTimezone(int timezone)this.timezone=timezone;public int getSigflags()return sigflags;public void setSigflags(int sigflags)this.sigflags=sigflags;public int getSnaplen()return snaplen;public void setSnaplen(int snaplen)this.snaplen=snaplen;public int getLinktype()return linktype;public void setLinktype(int linktype)this.linktype=linktype;public void otString()System.out.println(magic=+0x+Integer.toHexString(this.magic);System.out.println(nmagor_version=+this.magor_version);System.out.println(nminor_version=+this.minor_version);System.out.println(ntimezone=+this.timezone);System.out.println(nsigflags=+this.sigflags);System.out.println(nsnaplen=+this.snaplen);System.out.println(nlinktype=+this.linktype);public String toString()StringBuilder sbr=new StringBuilder();sbr.append(magic=).append(0x+Integer.toHexString(this.magic);return sbr.toString();package PcapFinally;import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.ArrayList;import java.util.List;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;public class PcapParser static File file; JFrame jframe=new JFrame(五元组分包算法之按钮一);JPanel jpanela=new JPanel();JPanel jpanelb=new JPanel();JPanel jpanelc=new JPanel();JTextField selectpthJtf=new JTextField(40);JTextField outputpthJtf=new JTextField(40);JButton selectbtn=new JButton(选择原文件);JLabel outputbtn=new JLabel(输出文件路径);JButton jiexibao=new JButton(五元组分包);static JTextArea area=new JTextArea(22,22);JScrollPane jspane=new JScrollPane(area);public void inint()jpanela.add(jspane);jpanelb.add(selectpthJtf);jpanelb.add(selectbtn);jpanelb.add(outputpthJtf);jpanelb.add(outputbtn);jpanelc.add(jiexibao);jframe.add(jpanela,BorderLayout.NORTH);jframe.add(jpanelb,BorderLayout.CENTER);jframe.add(jpanelc,BorderLayout.SOUTH);area.setText(解析过程:);selectbtn.addActionListener(new ActionListener()Overridepublic void actionPerformed(ActionEvent arg0) / TODO Auto-generated method stubJFileChooser jchoose = new JFileChooser();/ jchoose.showDialog(null, 确定);if (jchoose.showDialog(null, 确定) = 1) return;file = jchoose.getSelectedFile();selectpthJtf.setText(jchoose.getSelectedFile().toString(););jiexibao.addActionListener(new ActionListener()Overridepublic void actionPerformed(ActionEvent arg0) / TODO Auto-generated method stubtry dealWith();outputpthJtf.setText(五元组分包后的文件在本程序的项目中,或者在JDK文件下面,五元组命名中,6代表tcp协议,7代表Udp协议); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace(););jframe.pack();jframe.setVisible(true);jframe.setSize(666,555);private static void reverseByteArray(byte arr)byte temp;int n=arr.length;for(int i=0;in/2;i+)temp=arri;arri=arrn-1-i;arrn-1-i=temp;private static int byteArrayToInt(byte b,int offset)int value=0;for(int i=0;i4;i+)int shift=(4-1-i)*8;value+=(bi+offset&0x000000FF)shift;return value;private static short byteArrayToShort(byteb,int offset)short value=0;for(int i=0;i2;i+)int shift=(2-1-i)*8;value+=(bi+offset&0x000000FF)shift;return value;public static void main(String args) throws IOException / TODO Auto-generated method stubPcapParser pp=new PcapParser();pp.inint();private static void dealWith() throws IOException FileInputStream fis=new FileInputStream(file);byte buffer_4=new byte4;byte buffer_2=new byte2;PcapHeader header=new PcapHeader();Pcap pcap=new Pcap();int m=fis.read(buffer_4);if(m!=4)return;reverseByteArray(buffer_4);header.setMagic(byteArrayToInt(buffer_4, 0);m=fis.read(buffer_2);reverseByteArray(buffer_2);header.setMagor_version(byteArrayToShort(buffer_2,0);m=fis.read(buffer_2);reverseByteArray(buffer_2);header.setMinor_version(byteArrayToShort(buffer_2,0);m=fis.read(buffer_4); reverseByteArray(buffer_4);header.setTimezone(byteArrayToInt(buffer_4,0);m=fis.read(buffer_4);reverseByteArray(buffer_4);header.setSigflags(byteArrayToInt(buffer_4,0);m=fis.read(buffer_4);reverseByteArray(buffer_4);header.setSnaplen(byteArrayToInt(buffer_4,0);m=fis.read(buffer_4);reverseByteArray(buffer_4);header.setLinktype(byteArrayToInt(buffer_4,0);header.otString();pcap.setHeader(header);List dataList=new ArrayList();while (m0)PcapData data=new PcapData();m=fis.read(buffer_4);if(m0)break;reverseByteArray(buffer_4);data.setTime_s(byteArrayToInt(buffer_4,0);m=fis.read(buffer_4);reverseByteArray(buffer_4);data.setTime_ms(byteArrayToInt(buffer_4,0);m=fis.read(buffer_4);reverseByteArray(buffer_4);data.setpLength(byteArrayToInt(buffer_4,0);m=fis.read(buffer_4);reverseByteArray(buffer_4);data.setLength(byteArrayToInt(buffer_4,0);byte content=new bytedata.getpLength(); m=fis.read(content);data.setContent(content);byte ti=new byte14;for(int i=0;i14;i+)tii=contenti;data.setiTai(ti.toString();byte ver_ihla=new byte1;for(int i=0;i1;i+)int b=i+14

温馨提示

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

评论

0/150

提交评论