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
[[285361]] First, let's briefly introduce the...
Tribe has shared information about OpLink twice i...
This question is actually very simple. As long as...
This article is reprinted from the WeChat public ...
A brief discussion on the Internet of Things (I):...
Recently, the communication test of my country...
There was another new change today. The U.S. Dist...
Huawei's 5G development is hindered With the ...
When I look at computer networks, there is always...
The last time the tribe shared information about ...
Technological development is endless, especially ...
A sudden epidemic has plunged the whole country i...
According to foreign media reports, as an industr...
LOCVPS has newly launched a Hong Kong MG (BGP/Int...
Recently, the General Office of the Communist Par...