Original: http://useyourloaf.com/blog/2012/02/07/remote-packet-capture-for-ios-devices.html
Remote Packet Capture for IOS Devices
FEB 7TH, 11:33 PM
I previously posted about using the network Link conditioner to create realistic and "challenging" Network conditions when Testing IOS Apps. In this post I want to highlight another useful network debugging tool which allows the capture network traffic from an IO S device.
Remote Virtual Interfaces
As with the Network Link conditioner your need to use a host MAC computer to perform remote packet capture of an IOS device . The only other requirement are that the device being connected to the host computer via USB. No jailbreaking or hacking of your device is required to get the this to work.
The basic technique is to create a OS X remote virtual network interface that represents the remote network stack of the IOS device. Once You has the virtual interface you can use your favourite network debugging tool such as tcpdump or Wireshark to view The network traffic.
The steps to get the virtual network interface up and running is as follows:
- Plug your IOS device into the USB port of your Mac.
- Use the Xcode Organizer to obtain the UDID of the device (the value of your want is named Identifier):
If you want to capture packets from more devices you can repeat this process with the UDID for each device. You can also with the RVICTL command to list the active devices:
$ rvictl -l
The virtual interfaces is named Rvi0, RVI1, Rvi2, etc. and like all network interfaces is viewable using the Ifconfig Co Mmand:
$ ifconfig rvi0 rvi0: flags=3005<UP,DEBUG,LINK0,LINK1> mtu 0
Finally when is finished you can remove the virtual interface:
$ rvictl -x <UDID>
Using tcpdump
The easiest-to-capture and dump the network traffic is to use the Tcpdump command which are included with OS X. The man page for tcpdump have lots of options if you just want to see the live traffic the following would get you start Ed
$ tcpdump -n -i rvi0
To better illustrate the results I'll use the Twitter Search app I showed in an earlier post to generate a simple HTTP R Equest and Response.
$ tcpdump-n-t-i rvi0-q tcp tcpdump:WARNING:rvi0:That device doesn ' t support promiscuous mode (Biocpromisc : Operation not supported on socket) Tcpdump:WARNING:rvi0:no IPV4 address assigned tcpdump:verbose output Suppre ssed, Use-v OR-VV for full protocol decode listening on rvi0, Link-type Raw (Raw IP), capture size 65535 bytes IP 192.168.1.66.55101 > 192.168.1.64.51712:TCP 117 ip 192.168.1.64.51712 > 192.168.1.66.55101:tcp 0 IP 192.168. 1.64.51712 > 192.168.1.66.55101:tcp 298 IP 192.168.1.66.55101 > 192.168.1.64.51712:tcp 0 IP 192.168.1.66.553 > 199.59.148.201.80:tcp 0 IP 199.59.148.201.80 > 192.168.1.66.55324:tcp 0 IP 192.168.1.66.55324 > 199. 59.148.201.80:TCP 0 IP 192.168.1.66.55324 > 199.59.148.201.80:tcp 269 IP 199.59.148.201.80 > 192.168.1.66.55 324:TCP 0 IP 199.59.148.201.80 > 192.168.1.66.55324:tcp 1428 IP 199.59.148.201.80 > 192.168.1.66.55324:tcp 1428 IP 199.59.148.201.80 > 192.168.1.66.55324:tcp 1428
Note the tcpdump options I am using to cut down some of the noise. THE-T option gets rid of the timestamp on all line,-Q removes some of the packet header information which is not intere Sting and finally we specify that we is only interested in TCP/IP packets.
My Local IP address is 192.168.1.66 and the IP of the remote Twitter server in this case is 199.59.148.201. The HTTP request starts on line 5 where can see an outgoing connection to port 80:
IP 192.168.1.66.55324 > 199.59.148.201.80: tcp 0
The following lines show the search results coming back. Of course, this trace isn't very interesting as we cannot see the contents. You can add-x to the tcpdump command to see the actual packet contents but even that's not always that informative as Yo U need to know what to decode and interpret the packet data. A quick and dirty-on-the-know-dealing with HTTP traffic was to add the-a option-to-get tcpdump to print the PA Cket data in ASCII:
$ tcpdump-n-t-i rvi0-q-A TCP ... Get/search.json?rpp=100&q=apple http/1.1 Host:search.twitter.com user-agent:twittersearch/1.0 cfnetwork/548.0 .4 darwin/11.0.0 Accept: */* accept-language:en-us accept-encoding:gzip, deflate cookie:k=86.168.77.194.580 2087337bc706b connection:keep-alive ... http/1.1 OK cache-control:max-age=15, Must-revalidate, max-age=300 expires:tue, 22:18:05 GMT Co Ntent-type:application/json;charset=utf-8 vary:accept-encoding date:tue, 22:13:06 GMT x-varnish:6 82230572 age:0 via:1.1 varnish server:tfe content-encoding:gzip content-length:12715
This was a minor improvement in so we can now see the HTTP GET request with the query we have using and see the HTTP RESPO NSE But we still cannot easily drop-to-the-the-JSON in the result-to-see-what-Twitter-is-sending-back. For the We need to use a more sophisticated tool than tcpdump.
Using Wireshark
Whilst Tcpdump is a quick and easy-to-see and capture traffic it's not exactly an easy tool to use when you want to f Igure out what's going on. Wireshark is a much easier tool if you want perform deeper packet inspection or if you just prefer your network debugging Tools to has a user interface. Luckily Mac OS x ports is readily available, if you is following along I downloaded and installed version 1.6.5 for OS x 10.6 (Snow Leopard) Intel 64-bit from here.
Once you has Wireshark installed and running you should see a list of available interfaces that it can capture. The one we are interested in was of course our virtual interface rvi0:
Selecting rvi0 switches us to a live capture of the packet data with a lot more information to help us decode and Understa nd what are going on. This can being interesting to watch and see all of the things your IOS device is doing. For the purposes of this example it was useful to apply some filters so we can focus on the HTTP request traffic. The easiest-to-do-is-to-apply a display filter (Analyze-Display Filters ...). There is a number of pre-defined filter expressions including being able to limit the display to HTTP traffic:
Now if create our request it's immediately obvious what's going on as we can clearly see the HTTP GET request and the JS On response:
The central pane of Wireshark allows-to-drill down into the contents of each packet allowing us-see the JSON detail S
The full packet decode are also available in the lower pane if you need to see the whole packet.
Wrapping up
I should say that this post was not a attempt to explain everything involved in debugging network communications using TCP Dump or Wireshark. That is a huge topic and requires some knowledge of the underlying protocols. What I do want to make clear was that tools need to capture and analyse live traffic off your IOS device was Readi Ly available and take just a few minutes to get setup. It isn't something you'll (hopefully) need to use every day but it's well worth have it in your toolbox for those OC Casions when you need to debug network communications.
"Go" ios crawl tcp/udp pack