Network capture is a basic skill, and has certain requirements for the mastery of network protocols. The implementation of network capture on iOS can be used with Charles (for HTTP and HTTPS), tcpdump (Fast Analysis network packet), and Wireshare. Before writing an introductory article about tcpdump grab bag, compared with tcpdump, Wireshark provides rich GUI interaction, and can analyze all network protocols, key or free, master Wireshark can deal with most of the scenarios that need to analyze the network protocol.
Installation Initial knowledge
Wireshark provides Mac version, can be downloaded from the official website installation, to this blog up to the latest version should be 2.2.1. The first interface opened after installation is as follows:
Wireshark in the first interface to the current system contains the network card listed, directly click on any one can start to listen to all network traffic through the card.
When we connect the iphone to the MacBook via USB, Wireshark does not directly monitor the network traffic through the iphone and requires a system program on our Mac system to create a virtual network card that maps to the iphone. Enter the following command in the terminal:
The format is rvictl-s [device Udid], the device udid can be obtained through itunes or itools, after the command Wireshark can immediately identify the newly added RVI0 network card, that is, in the highlighted section, double-click the Rvi0 this item, Wireshare then enters the following interface to start listening for all traffic on the iphone device. rvictl-x[device Udid] Disconnects after use is complete.
At this point, the launch of any app on the iphone, as long as there is network traffic generated, the corresponding network package will be shown in the Wireshark list above.
Wireshark's traffic monitoring interface is divided into four blocks, from top to bottom the first part (labeled 1) is the toolbar, through the toolbar we can control the behavior of monitoring, such as start to grasp the packet, stop grasping the packet, start to grab the packet again, and jump between the package and so on. There is an input box at the bottom of the toolbar that allows us to manually enter the filter conditions for the package, which is very important for skilled use of the Wireshark, which will be explained in detail later.
The second part (labeled 2) is the historical Traffic Pack List display interface, which shows the beginning of the capture package, all traffic through our iphone devices. The different packages of the list interface have different colors, wireshark the type of package by color, and it is useful to quickly identify target traffic for a particular scene, which is also explained later.
The third part (labeled 3) is a single package details display panel, we selected in the second part of the network package will be in this part of the structure in the form of readable text, to correctly read this part of the information needs to have a certain grasp of the TCP/IP protocol.
Part IV (labeled 4) is a single package of binary flow information display panel, this part of the information presented is the original package of data, but also a network package contains the content of the real show, we in the third part of the multi-selected protocol header, will be in this part in the form of synchronous highlighting is marked out. This part of the show is to let us make a visual judgment of the real content of the package, can be specific to a single byte.
After a preliminary understanding of the four main panels, you can try to start analyzing the network packets, before you start analyzing the network packets, you should have a rough idea of the network packets.
Network Package Structure
When we first learned about network protocols, both the OSI seven-layer model and the classic TCP/IP five-layer structure were presented in the form of the left part of the.
This is a classic layered architecture, and indeed conforms to the idea of network protocol design, but it can not express the real inclusion of the network package. The right part is a package structure that I've drawn. In my opinion, this onion-like structure is more in line with the true form of the network packet. Application is the most inner layer of payload, except for the application layer, the other layers are using their own layer of protocol header+ the layer of payload. Can be expressed in the following formula:
TCP Layer = TCP Header + application Payload
IP Layer = IP Header + TCP Payload
...
We analyze each network packet to understand the abstract meaning that each package represents, and then concatenate the associated packages to form a complete network session.
Analysis Package Structure
For iOS programmers, most of our traffic analysis is focused on HTTP or TCP-based socket long connections. From this level, the three layers closest to us are the application layer (HTTP), the Transport layer (TCP or UDP), the network layer (IP).
For the application layer is mainly the HTTP protocol learning, for HTTP request and response format reading, such as the expression of an HTTP request packet:
Packet details panel to conform to the HTTP protocol, the header of each field is clearly listed, reading is very convenient.
Transport Layer We apply more TCP, this layer of reading is mainly the study of TCP header:
A typical TCP header is typically 20 bytes long, and you can analyze most of the TCP traffic by learning the 20 bytes one at a time.
The network layer analysis is mainly for the IP Header,header structure as follows:
This is the IP header of the 13th byte of each filed understanding, for us to analyze the start and end of TCP traffic is particularly useful, the typical IPV4 header is also 20 bytes, comb the IP packet can be analyzed again.
So the key to the analysis of package structure lies in the learning of three knowledge Points: HTTP header, TCP header, IP header, so there seems to be not many things;)
Filter Packages with filter
The biggest difference between using Wireshark and using Charles is that Charles captures only HTTP traffic, while Wireshark captures all the traffic through the target NIC, which can swell into hard-to-read quantities within seconds. So at this point we need to use filter to do the packet filter, the more the filter rule is determined, the more the interference information is removed, the faster the analysis.
Wireshark filter is divided into two types, one for capture filter and the other for display filter.
Capture filter appears in the initial interface, there is an input box above the NIC list, allowing us to enter capture filter, and once a specific capture rule is entered, Wireshark only captures traffic packets that conform to the rule.
Display filter appears in the traffic monitoring interface, there is an input box below the toolbar, allows us to enter the display Filter,display filter just from the interface to filter out the non-conforming package, Wireshark actually listen to these packets, Once the display filter is removed, all packages will appear in the same interface.
The rules for Capture filter are consistent with the filter syntax that we normally use with tcpdump, for example, in order to monitor only HTTP traffic, we can first select the RVI0 network card in the initialization interface and enter it in the Capture filter input box:
//只捕获HTTP流量port 80 or port 443
After the carriage return, Wireshark began to monitor all the HTTP and HTTPS traffic on our iphone, very simply, and we could use other capture filter to catch certain traffic, such as to analyze the DNS parsing process, which can be used:
//只捕获DNS流量port 53
For example, to capture only the traffic associated with a particular server:
//只捕获和特定主机的流量host 171.10.191.10
The syntax of Display filter is custom-defined by Wireshark, and cannot be mixed with the syntax of capture filter. For example, we just want to see a host of traffic, you can use the following display Filter:
ip.addr==171.10.191.10
If you only look at http or HTTPS traffic, you can use:
tcp.port == 80 || tcp.port == 443
More grammar rules can be viewed Wireshark official documentation, Wireshark actually provides convenient UI action to help us write the display filter, which has an expression button at the far right of the display filter input box. After clicking, the following screen can be displayed:
The syntax of Display filter is essentially a relational description, and we can enter the protocols we are interested in, such as HTTP, in search, Then choose our conditions like Http.host in the expanded protocol, and finally set relation and value to generate a display filter condition.
Package Color Rules
Wireshark Most of the time the number of packets captured is far more than we are interested in the number, and the various connected packages are mixed together, in order to facilitate our identification of different connection sessions, Wireshark by default use a shading rule to help us to do packet type differentiation.
Specific rules can be view->coloring through the menu ... View, the default rules are as follows:
Here's a little tip, as shown, I'm just wrapping the protocols I'm interested in, focusing on the HTTP,TCP,UDP package, which makes the analysis more intuitive. For example, according to the rules, the Sync packet in the TCP three handshake is grayed out, so I can quickly locate the start packet location of the TCP connection in the package:
Of course, the color of the package can also be customized according to their own visual habits, I am personally accustomed to the sync bag and fin package set a highlight color, easy to determine the beginning and end of an HTTP session.
Traffic tracking
Wireshark By default, the traffic for different network connections is mixed together to show that even if the packages for different protocols are shaded, it is still not convenient to view the traffic for a particular connection in isolation, and we can achieve this by Wireshark.
Way One: Follow Stream
When we select a package, the right-click menu has an option that allows us to list the full flow of the current package, such as:
Wireshark supports our common four types of STREAM,TCP,UDP,HTTP,SSL. For example, after we select follow TCP stream, we can get the following detailed analysis output (sample to monitor the iphone's traffic):
A complete presentation of the connection traffic between the iphone and the server, including how many packages the iphone sends, how many packages the server has back, and the traffic on the iphone upstream and downstream, as well as the traffic codec selection, text search functionality, and more.
Way two: Flow Graph
Flow graph can be generated from the menu Statistics->flow graph, so that we can get another form of traffic rendering:
Unlike follow stream, we get a complete flow of traffic, from which we can see network traffic flowing from 10.136.66.127 (my iphone IP address) to multiple servers, including DNS resolution and SSL security handshake. Of course, we can also do further filtering in the operating area below, and you can use display filter for further traffic positioning.
Follow stream is more suitable for analyzing traffic to a single server address, and flow graph is more suitable for analyzing the overall network behavior of an app, from starting with DNS resolution to interacting with multiple servers.
In fact, under the statistics menu there are more chart Analysis mode, can be based on different analysis objectives to choose, such as statistics->http->requests can get the following by the host classified HTTP request analysis diagram, And the fee for Charles's presentation was similar.
HTTPS packet Analysis
After the introduction of the use of the method to actually analyze the traffic under HTTPS. I use Wireshark on my iphone to capture the results of the app network request:
When I use the follow TCP stream, a full HTTPS session traffic is filtered out separately, and the first step is to analyze the package list interface.
By highlighting the color of the session's real sync package, you can quickly locate the three packets generated by the TCP three handshake at the beginning of the HTTP connection:
Sync: iphone sends sync.
Sync+ack: Server sends Sync+ack.
ack: iPhone ack.
After three handshakes, SSL Handshake,ssl handshake is divided into the following steps:
Client Hello
This package is the starting packet for the SSL handshake, and the client (my iphone) carries some key information that the current session relies on: The TLS version used (currently tls1.2), the last session ID (which can be avoided if the session is reused), A suite of cryptographic algorithms supported by the client (which can be seen from 22 suites).
Server Hello
Server Hello this package with some information on this side of the server, such as the TLS version selected by the server, or with a reusable session ID to avoid a handshake, pick a cipher in the cipher suites that the client passed in. Suite for subsequent secure calls, and more.
Server Issued Certificate
The server also sends its own certificate, as shown in:
As can be seen from the Package list interface, Certificate (size of 2,407 bytes) This packet was split into 2 packets because it exceeded 1440 bytes, so we can see [TCP segment of a reassembled PDU] in package info. We often see the reassembled PDU when we use Wireshark to grab the bag, which is because the package is too large to exceed the MSS and needs to be split into two to send.
The next few packages are client and server that negotiate the last used key based on the information exchanged above.
Server Key Exchange
Client Key Exchange
Change Cipher Spec
...
Send Application Data
Each package contains detailed content analysis related to the asymmetric encryption algorithm of the relevant knowledge, here do not expand, using Wireshark can be the whole HTTPS handshake process very clear display, interested students can read this article.
Of course, most of the time we need to analyze the specific contents of HTTPS traffic on the iphone, Wireshark although the RSA private key is supported, but we can not directly obtain the iphone device on the use of the private key, in this scenario we generally use the MITM (man in The middle) man-in-the-middle attack to crack the HTTPS packet content, the fee tool Charles can be implemented by proxy way to achieve this function, the free version of the Grab kit tool Mitmproxy also can, Charles's use of the tutorial more, Later we will write a mitmproxy tutorial on how to use the hack to debug HTTPS traffic.
Conclusion
Wireshark introduced here, there are now many ways to grab a packet on the iphone, there are tcpdump and Wireshark for all protocols, and Charles and Mitmproxy for HTTP, regardless of which tool you use, The premise is that we need to have a comprehensive understanding of the network protocols, so while learning to use these tools, we should continue to deeply learn the network protocol knowledge.
MAC Wireshark Grab Pack iOS