The purpose of this article is to randomly intercept a network data packet and then parse this memory according to the protocol type. Basic knowledge you need to master to study this article:
The installation and use of the packet capture tool are as follows: "Learn how to capture network data in one article" The video tutorial link is as follows: "Teach you how to capture data packets on the network! Essential skills for hackers" 1. Intercept a network data packetUse the packet capture tool to randomly capture a TCP data packet The data packet information parsed by the Collet packet capture tool is as follows: Memory information of the data packet: The data information can be copied directly: 2. Structures usedNext, I will teach you step by step how to parse the information of these data packets. We can find the definition of the protocol header from the Linux kernel Ether Header:
IP header
TCP header
Because the protocol header length is defined according to the standard protocol, So the Ethernet length is 14, the IP header length is 20, and the TCP header length is 20. The memory space corresponding to each protocol header is as follows: 3. Parsing the Ethernet Header
When executing the following statement,
The member correspondence of the structure pointer variable eth is as follows: The final print result is as follows: 4. Parsing IP headerThe idea of parsing the IP header is very simple. That is, you can find the IP header by offsetting the Ethernet header length (14 bytes) from the pkt header. The parsing code is as follows:
IiP The final analysis results are as follows: It can be seen that we have correctly parsed the IP address, and the result is consistent with the data analyzed by the packet capture tool. The protocol field indicates the protocol type following the IP protocol. Common values are as follows: 5. Parsing TCP headerThe idea of finding the TCP header is very simple. That is, you can find the TCP header by offsetting the Ethernet header length (14 bytes) and the IP header length (20 bytes) from the pkt header.
Correspondence between structure and memory The print results are as follows: 6. Learn to print this memory in different formatsIn actual projects, we may not parse standard TCP/IP protocol packets. It may be our own defined protocol data packet, Once you have mastered the above methods, All protocol analysis is at your fingertips! Sometimes we also need to print the content of the data frame sent by the other party. Often we print out all the data in hexadecimal format. This is most conducive to our analysis of data content. 1. Print by bytesThe code is as follows:
2. Analyze a section of memory by short typeWhen we receive data, although we use an unsigned char array, But sometimes the data sent by the other party may be an array of 2 bytes. Then we just need to use a short pointer to point to the head of the memory. Then you can access the data sent by the other party through this pointer. At this time, you must pay attention to the byte order problem. Different scenarios may be different, so we must analyze specific issues specifically. In this example, network byte order data is converted into host byte order. So the byte order needs to be converted.
The results are as follows: Please contact Yikou Linux public account for reprinting this article. |
<<: Server-Speaks-First is a bit of a bummer, protocol detection and opaque ports in Linkerd 2.10
>>: For the first time in 21 years! SpaceX acquires satellite communications startup
It is well known that the holiday shopping period...
Double 11 is coming, HostXen has launched the lat...
As 5G commercialization approaches, the demand fo...
At the end of 2017, the country began to promote ...
Over the past few years, operators and regulators...
On December 4, the 2019 Fourth National Seminar o...
[[426636]] At the 5G Message High-level Forum of ...
Recently, Jimmy Yu, vice president and analyst at...
Shenyang University is one of the universities wi...
[51CTO.com original article] The first time the r...
edgeNAT has announced a promotion for this year...
In the movie "Ready Player One", the pr...
The fifth generation of mobile communication tech...
NFV automation and NFV orchestration have overlap...
5G has been in commercial use for more than a yea...