Este é um arquivo PCAP de análise simples escrito por você mesmo, o que é conveniente para a leitura de arquivos PCAP.
A cópia do código é a seguinte:
InputStream IS = DataParser.class.getClassLoader (). GetResourceasStream ("Baidu_cdr.pcap");
Pcap pcap = pcapparser.unpack (IS);
is.close ();
byte [] t = pcap.getData (). get (0) .getContent ();
byte [] data = arrays.copyOfRange (t, 42, t.Length);
PcApparser.java
A cópia do código é a seguinte:
pacote com.hylanda.pcap;
importar java.io.ioException;
importar java.io.inputStream;
importar java.util.arraylist;
importar java.util.list;
/**
* @Author Zhouqisheng
*
*/
classe pública pcApparser {
public static pcap Unplack (inputStream) lança ioexception {
Pcap pcap = null;
byte [] buffer_4 = novo byte [4];
byte [] buffer_2 = novo byte [2];
PCAP = new PCAP ();
PCAPHEADER Cabeçalho = new PCApheader ();
int m = is.read (buffer_4);
if (m! = 4) {
retornar nulo;
}
reversebytearray (buffer_4);
header.setMagic (byteArrayToint (buffer_4, 0));
m = is.read (buffer_2);
reversebytearray (buffer_2);
header.setMagor_version (bytearraytoshort (buffer_2, 0));
m = is.read (buffer_2);
reversebytearray (buffer_2);
header.setminor_version (bytearraytoshort (buffer_2, 0));
m = is.read (buffer_4);
reversebytearray (buffer_4);
header.setTimeZone (bytearraytoint (buffer_4, 0));
m = is.read (buffer_4);
reversebytearray (buffer_4);
Header.SetSigFlags (BYTEARRAYTION (BUFFER_4, 0));
m = is.read (buffer_4);
reversebytearray (buffer_4);
Header.SetsNaplen (BYTEARRAYTION (BUFFER_4, 0));
m = is.read (buffer_4);
reversebytearray (buffer_4);
header.setLinkType (byteArrayToint (buffer_4, 0));
pcap.setheader (cabeçalho);
Lista <CapData> Datalist = new ArrayList <CapData> ();
while (m> 0) {
Dados pcapdata = novo pcapData ();
m = is.read (buffer_4);
if (m <0) {
quebrar;
}
reversebytearray (buffer_4);
data.setTime_s (bytearraytoint (buffer_4, 0));
m = is.read (buffer_4);
reversebytearray (buffer_4);
data.settime_ms (bytearraytoint (buffer_4, 0));
m = is.read (buffer_4);
reversebytearray (buffer_4);
Data.setPlength (BYTEARRAYTONT (Buffer_4, 0));
m = is.read (buffer_4);
reversebytearray (buffer_4);
Data.setLength (BYTEARRAYTONT (Buffer_4, 0));
byte [] content = new byte [data.getPlength ()];
m = is.read (content);
data.setContent (Content);
datalist.add (dados);
}
pcap.setData (Datalist);
return pcap;
}
private static int bytearraytoint (byte [] b, int offset) {
int valor = 0;
for (int i = 0; i <4; i ++) {
int shift = (4 - 1 - i) * 8;
valor + = (b [i + deslocamento] e 0x000000FF) << Shift;
}
valor de retorno;
}
BytearToShort de BytearToShort privado (byte [] B, Int Offset) {
valor curto = 0;
for (int i = 0; i <2; i ++) {
int shift = (2 - 1 - i) * 8;
valor + = (b [i + deslocamento] e 0x000000FF) << Shift;
}
valor de retorno;
}
/**
* Inverta a matriz
* @param arr
*/
private estático vazio reversebytearray (byte [] arr) {
temp byte;
int n = arr.length;
for (int i = 0; i <n/2; i ++) {
temp = arr [i];
arr [i] = arr [n-1-i];
arr [n-1-i] = temp;
}
}
}
Pcap.java
A cópia do código é a seguinte:
/**
*
*/
pacote com.hylanda.pcap;
importar java.util.list;
/**
* @Author Zhouqisheng
*
*/
classe pública PCAP {
Cabeçalho privado de PCApheader;
Lista privada <CapData> Dados;
public PCApheader getheader () {
Cabeçalho de retorno;
}
public void setheader (cabeçalho da PCApheader) {
this.Header = cabeçalho;
}
Lista pública <CapData> getData () {
retornar dados;
}
public void SetData (List <CapData> dados) {
this.data = dados;
}
@Override
public string tostring () {
StringBuilder S = new StringBuilder ();
s.append ("cabeçalho {/n");
S.Append (Header.ToString ());
S.Append ("}/n");
S.Append ("contagem de peças de dados ="). Anexe (data.size ());
retornar s.toString ();
}
}
Pcapdata.java
A cópia do código é a seguinte:
pacote com.hylanda.pcap;
/**
* @Author Zhouqisheng
* Cabeçalho do pacote
*/
classe pública pcapdata {
private int time_s; // Timestamp (segundos)
private int time_ms; // timestamp (sutil)
Plenagem privada int; // Comprimento do pacote
comprimento privado int; // comprimento real
Byte privado [] conteúdo; // dados
public int gettime_s () {
retorno time_s;
}
public void Settime_s (int time_s) {
this.time_s = time_s;
}
public int gettime_ms () {
retornar time_ms;
}
public void settime_ms (int time_ms) {
this.time_ms = time_ms;
}
public int getPlength () {
pilha de retorno;
}
public void SetPlength (Int Plength) {
this.Plength = Plength;
}
public int getLength () {
comprimento de retorno;
}
public void SetLength (int length) {
this.length = comprimento;
}
public byte [] getContent () {
retornar conteúdo;
}
public void setContent (Byte [] content) {
this.content = content;
}
@Override
public string tostring () {
StringBuilder S = new StringBuilder ();
s.append ("time_s ="). Anexe (this.time_s);
s.append ("/ntime_ms ="). Append (this.time_ms);
S.Append ("/NPLENGEN ="). Append (this.Plength);
s.append ("/nlength ="). Append (this.length);
retornar nulo;
}
}
Pcapheader.java
A cópia do código é a seguinte:
pacote com.hylanda.pcap;
/**
* @Author Zhouqisheng
* Cabeçalho de arquivo PCAP
*/
classe pública PCApheader {
private int mágica; // cabeçalho de reconhecimento de arquivo, 0XA1B2C3D4
Private Short Magor_version; // versão principal
Private Short Minor_version; // versão menor
private int fuzne; // horário padrão local
private int sigflags; // a precisão dos registros de data e hora
private int snaplen; // comprimento máximo de armazenamento
/**
* 0 dispositivos BSD Loopback, exceto para o OpenBSD posterior
1 Ethernet e dispositivos Linux Loopback
6 802.5 anel de token
7 ArcNet
8 deslizamento
9 ppp
10 FDDI
100 LLC/ATM encapsulado em SNAP
101 "IP bruto", sem link
102 Slip BSD/OS
103 BSD/OS PPP
104 Cisco HDLC
105 802.11
108 Posterior Dispositivos OpenBSD Loopback (com o AF_VALUE na ordem de bytes de rede)
113 Captura especial Linux "cozida"
114 LocalTalk
*/
private int linktype; // tipo de link
public int getMagic () {
retornar magia;
}
public void SetMagic (int mágico) {
this.Magic = Magic;
}
public Short getMagor_version () {
retornar magor_version;
}
public void setMagor_version (Short Magor_version) {
this.magor_version = magor_version;
}
public Short getMinor_version () {
retornar minor_version;
}
public void setminor_version (curto minor_version) {
this.minor_version = Minor_version;
}
public int getTimeZone () {
retorno do tempo;
}
public void setTimeZone (int timebone) {
this.TimeZone = TimeZone;
}
public int getSigflags () {
retornar sigflags;
}
public void Setsigflags (int sigflags) {
this.sigflags = sigflags;
}
public int getSNaplen () {
retornar snaplen;
}
public void SetsNaplen (int snaplen) {
this.snaplen = snapplen;
}
public int getLinkType () {
retornar linkType;
}
public void setLinkType (int linkType) {
this.linkType = linkType;
}
@Override
public string tostring () {
StringBuilder S = new StringBuilder ();
S.Append ("Magic ="). Append ("0x" + Integer.ToHexString (this.Magic));
s.append ("/nmagor_version ="). Append (this.magor_version);
s.append ("/nminor_version ="). Append (this.minor_version);
S.Append ("/nTimeZone ="). Append (this.TimeZone);
s.append ("/nsigflags ="). Append (this.sigflags);
s.append ("/nsnaplen ="). Append (this.snaplen);
s.append ("/nlinkType ="). Append (this.linktype);
retornar s.toString ();
}
}