Skyler Kaufman | 991ae4d | 2011-04-07 12:30:41 -0700 | [diff] [blame^] | 1 | # Debugging with tcpdump # |
| 2 | |
| 3 | ## Installing tcpdump ## |
| 4 | |
| 5 | ### Pushing the binary to an existing device ### |
| 6 | |
| 7 | Download tcpdump from [tcpdump.org](http://www.tcpdump.org/), then execute: |
| 8 | |
| 9 | adb root |
| 10 | adb remount |
| 11 | adb push /wherever/you/put/tcpdump /system/xbin/tcpdump |
| 12 | adb shell chmod 6755 /data/local/tmp/tcpdump |
| 13 | |
| 14 | ### Including tcpdump in the build image ### |
| 15 | |
| 16 | If you are running your own build, execute: |
| 17 | |
| 18 | mmm external/tcpdump # install the binary in out/.../system/xbin |
| 19 | make snod # build a new system.img that includes it |
| 20 | |
| 21 | Flash the device as usual, for example, `fastboot flashball`. |
| 22 | |
| 23 | If you want to build tcpdump by default, add |
| 24 | |
| 25 | CUSTOM_TARGETS += tcpdump |
| 26 | |
| 27 | to your `buildspec.mk`. |
| 28 | |
| 29 | ## Running tcpdump ## |
| 30 | |
| 31 | You need to have root access on your device. |
| 32 | |
| 33 | ### Batch mode capture ### |
| 34 | |
| 35 | The typical procedure is to capture packets to a file and then examine the file on the desktop, as illustrated below: |
| 36 | |
| 37 | adb shell tcpdump -i any -p -s 0 -w /sdcard/capture.pcap |
| 38 | |
| 39 | - `-i any`: listen on any network interface |
| 40 | - `-p`: disable promiscuous mode (doesn't work anyway) |
| 41 | - `-s 0`: capture the entire packet |
| 42 | - `-w`: write packets to a file (rather than printing to stdout) |
| 43 | |
| 44 | Do whatever you want to capture, then ^C to stop it. |
| 45 | |
| 46 | adb pull /sdcard/capture.pcap . |
| 47 | sudo apt-get install wireshark # or ethereal, if you're still on dapper |
| 48 | wireshark capture.pcap # or ethereal |
| 49 | |
| 50 | Look at your packets and be wise! |
| 51 | |
| 52 | You can run `tcpdump` in the background from an interactive shell or from Terminal. By default, tcpdump captures all traffic without filtering. If you prefer, add an expression like port 80 to the tcpdump command line. |
| 53 | |
| 54 | ### Real time packet monitoring ### |
| 55 | |
| 56 | Execute the following if you would like to watch packets go by rather than capturing them to a file: |
| 57 | |
| 58 | adb shell tcpdump -n -s 0 |
| 59 | |
| 60 | - `-n`: skip DNS lookups |
| 61 | - `-s 0`: capture the entire packet rather than just the header) |
| 62 | |
| 63 | Typical tcpdump options apply. For example, if you want to see HTTP traffic: |
| 64 | |
| 65 | adb shell tcpdump -X -n -s 0 port 80 |
| 66 | |
| 67 | You can also monitor packets with `wireshark` or `ethereal`: |
| 68 | |
| 69 | 1. In one shell, start `tcpdump`: |
| 70 | |
| 71 | adb shell "tcpdump -n -s 0 -w - | nc -l -p 11233" |
| 72 | |
| 73 | 1. In a separate shell, forward data and run ethereal. |
| 74 | |
| 75 | adb forward tcp:11233 tcp:11233 && nc 127.0.0.1 11233 | ethereal -k -S -i - |
| 76 | |
| 77 | Note that you can't restart capture via `ethereal`. If anything goes wrong, you will need to rerun both commands. |
| 78 | |
| 79 | For more immediate output, add `-l` as an option to the tcpdump command. Note that this can cause `adb` to choke (it helps to use a nonzero argument for `-s` to limit the amount of data captured per packet; `-s 100` is sufficient if you just want to see headers). |
| 80 | |
| 81 | ### Disabling encryption ### |
| 82 | |
| 83 | If your service runs over `https`, tcpdump is of limited use. In this case, you can rewrite some service URLs to use `http`, for example: |
| 84 | |
| 85 | vendor/google/tools/override-gservices url:calendar_sync_https_proxy \ |
| 86 | https://www.google.com/calendar rewrite http://android.clients.google.com/proxy/calendar |
| 87 | |
| 88 | # Other network debugging commands # |
| 89 | |
| 90 | ## On the device ## |
| 91 | |
| 92 | - `ifconfig interface`: configure a network interface. Note that unlike Linux, `ifconfig` on Android requires an argument. |
| 93 | |
| 94 | - `netcfg`: lists interfaces and IP addresses |
| 95 | |
| 96 | - `iftop`: like top for network |
| 97 | |
| 98 | - `route`: examine the routing table |
| 99 | |
| 100 | - `netstat`: see active network connections |
| 101 | |
| 102 | - `nc`: `netcat` connection utility |
| 103 | |
| 104 | ## On the desktop ## |
| 105 | |
| 106 | - `curl`: fetch URLs directly to emulate device requests |
| 107 | |