packet, each packet will have a description header:struct PCAP_PKTHDR { struct timeval ts; /* Time stamp */ bpf_u_int32 Caplen; /* Length of portion present because tcpdump can set the-s parameter to specify the size of the fetch, this field represents the actual fetched packet length */ bpf_u_int32 len; /* Length This packet the field indicates the natural length of the packet */};This structure describes the time information and length information of the packet fetch, which
Pcap is a data packet capture library, which is used by many software as a data packet capture tool. Wireshark also uses the pcap library to capture data packets. The packets captured by pcap are not the original network byte streams, but are assembled to form a new data format.The file format of a data packet captured by pca
Tags: http OS AR for SP strong file data Div Pcap is a data packet capture library, which is used by many software as a data packet capture tool. Wireshark also uses the pcap library to capture data packets. The packets captured by pcap are not the original network byte streams, but are assembled to form a new data format.
The file format of a data packet captur
-py2.7.rarThe following code shows how to use Scapy to complete pcap reading and writing, and provides a picklablepacket class for solving the problem of scapy parsing packet cannot be serialized, the code is as follows:Importscapy fromScapy.allImport* fromScapy.utilsImportPcapreader, PcapwriterImportGzip,zlib,cpickleclassPicklablepacket:" "A container for scapy packets so can be pickled (in contrast to scapy pakcets themselves)" " def __init__(sel
function can continuously capture Ethernet packets, CNT is the number of captures, and callback is the processing function. How to Write this processing function? You can see pcap_3.c. What is the USER parameter? Don't ask me. I don't know either. /* Pcap_3.c */# include # Include # Include # Include # Include # Include # Include # Include
/* Callback function that is passed to pcap_loop (...) and called each time
* A packet is recieved */
Void my_c
Through the TCP/IP protocol learning, I wrote a can achieve the Pcap file in the IPV4 TCP stream extraction, as well as extract the specified TCP stream, in order to learn, did not adopt the third party package parsing pcap, but the analysis of the bytes stream, the core idea is: If you want to extract TCP Content, in the lower-level IPV4 protocol to determine whether the protocol is TCP, and then determine
, char *);INT (* stats_op) (pcap_t *, struct pcap_stat *);Void (* close_op) (pcap_t *);
/* If the BPF filtering Code cannot be executed in the kernel, save it and run it in the user space */Struct bpf_program fcode;
/* Function call error message buffer */Char errbuf [pcap_errbuf_size + 1];/* Number of Data Link types supported and changeable by the current device */Int dlt_count;/* List of changeable Data Link types, which is not used in Linux */Int
HTTP packets in the PCAP package can be easily parsed using scapy, scapy_httpScapy_http can be downloaded in https://github.com/invernizzi/scapy-http, the address is also given a simple example program, according to this sample program I modified an output PCAP packet in the source destination address of the HTTP packet, The payload applet is as follows:where P is a packet, scapy_http divides it into:Ethern
PCAP_VERSION_MAJOR 2
Minor: 2-byte version # define PCAP_VERSION_MINOR 4
Thiszone: The 4-byte time zone is not used. Currently, all values are 0.
Sigfigs: The 4-byte exact timestamp is not used and is currently 0
Snaplen: the maximum length of a 4-byte packet capture. If you want to capture the entire packet, set it to 0x0000ffff (65535 ),
Tcpdump-s 0 sets this parameter. The default value is 68 bytes.
Linktype: 4-byte link types are generally 1: ethernet | magic | major | minor | thiszone | si
This is a simple analysis of their own pcap file, easy to read Pcap file, we refer to the use of the bar
Copy Code code as follows:
InputStream is = DataParser.class.getClassLoader (). getResourceAsStream ("Baidu_cdr.pcap");
Pcap Pcap = Pcapparser.unpack (IS);Is.close ();byte[] t = pcap.getdata (
The following describes the default *. pcap file storage format of ethereal.
Description of each field in the pcap File Header 24B:Magic: 4b: 0x1a 2B 3C 4d: used to mark the start of the file Major: 2b, 0x02 00: The main version number of the current file minor: 2b, 0x04 00 current file minor version number thiszone:
Step 1: download the relevant source code
Pycap: http://code.google.com/p/pypcap/
Sendpkt: http://code.google.com/p/sendpkt/
Dpkt: http://code.google.com/p/dpkt/
WDP http://www.winpcap.org/install/bin/WpdPack_4_0_2.zip
Mingw: http://www.mingw.org/
Step 2: Compile
1. decompress the two files in pycap and WDP to the same directory. Note:2. Create a setup. cfg file in the decompressed pycap folder with the following content:
[Build]Compiler = mingw32
3. Start Compilation
C: \
Note that this is the 1.0 series of tcpcopy.
First, please understand the 1.0 series architecture related content:http://blog.csdn.net/wangbin579/article/details/8949315
http://blog.csdn.net/wangbin579/article/details/8950282
having understood the rationale, let's explain how to use the Pcap interface to send request packets
in the online system, the following methods are used to compile tcpcopy./configure--
byte sequence (hbo,host byte order): Different machine HBO is not the same, related to CPU design, the order of data is determined by the CPU, and operating system independent.such as the Intel x86 structure, the short type number 0x1234 is expressed as a decimal, the int type number 0x12345678 is expressed as 78 56 34 12such as the IBM Power PC structure, the short type
two may be different. If the-S 0 parameter is used in the tcpdump command, 8-11 bytes and 12-15 bytes are equal.
From the end of the data packet header to the number of bytes specified in the length, it is the link layer data packet actually transmitted in the network. Next, it is the next data packet header.
4. Link Layer data
The link layer data packet format is related to the transmission method. for LAN shared Internet access, it is rfc894 E
Pcap file format Analysis
I. Basic Format:File header data packet ......
Ii. File Header structure:Sturct pcap_file_header {DWORD magic; Word version_major; Word version_minor; DWORD thiszone; DWORD sigfigs; DWORD snaplen; DWORD linktype;} Description: 1. identifier: 32-bit, the value of this flag is 0xa1b2c3d4 in hexadecimal notation. A 32-bit magic number, the magic n
user message cannot be placed in a sctp group, the message can be divided into several data blocks.
Sctp Header
The sctp public grouping header contains the source port number, destination port number, verification tag, and checksum)
1. Source Port Number (16 bits)
The source port number identifies the sctp Port
Preface
The data structure has already been defined, so it's time to formally parse the Pcap file.Note: The following only the core code, the project all the code will be at the end of the article to give the download link Resolution pcap file
1 Read the entire Pcap file to memory
FileInputStream FIS = null;
try {
fis = new FileInputStream (
Installation method One, sudo apt-get install Libpcap-devInstallation Method II,Go to http://www.tcpdump.org/to download the latest libpcap.tar.gz packAfter decompression./configure here I did not add--frefix=path results to see belowMakeMake installDuring the installation process./configure when the error, no lex we use flex instead, yum install flex canError in make will not find the command YACC we use Bison instead, yum install Bison can--------------error when you remember to use make clean
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.