If both end-points of a socket are on local system, network traffic will be seen on loopback interface even if applications are using non-loopback interface (e.g. eth0, wlan0…). Capturing data over loopback is quite obvious. But here I am discussing that applications are using one of the external interfaces (e.g. eth0, eth1, wlan0 …..).
Since both end-points are on local system, kernel will shunt the traffic and not send it to the wire. The data will be delivered internally by queuing it to the read queue of other end-point. So we cannot capture the traffic on that particular interface, but this traffic is visible on loopback interface. Lets see an example.
I use netcat for setting up our test client and server program. nc -kl 9090
will run server on all interfaces on port 9090. And nc 10.1.1.100 9090
will setup a client. Here 10.1.1.100 is the external IP of my system(wlan0). Now instead of using the interface name associated with that IP (in my case wlan0), we have to use loopback interface lo
to capture the traffic as below.
tcpdump -i lo tcp port 9090
Now anything that is typed on the client terminal when sent will be seen by tcpdump. Problem solved.