I would like to examine the network traffic being handled by a single process, but simple network captures won't work since I am dealing with such a busy system (lots of other traffic happening at the same time). Is there a way to isolate a tcpdump
or wireshark
capture to the networking traffic of a single specific process? (Using netstat
is insufficient.)
To start and monitor an new process:
To monitor an existing process with a known PID:
-f
is for "follow new processes"-e
defines a filter-s
sets the limit of strings to more then 32-p
takes the process id to attach toI know this thread is a bit old but I think this might help some of you:
If your kernel allows it, capturing the network traffic of a single process is very easily done by running the said process in an isolated network namespace and using wireshark (or other standard networking tools) in the said namespace as well.
The setup might seem a bit complex, but once you understand it and become familiar with it, it will ease your work so much.
So as to do so:
create a test network namespace:
create a pair of virtual network interfaces (veth-a and veth-b):
change the active namespace of the veth-a interface:
configure the IP addresses of the virtual interfaces:
configure the routing in the test namespace:
activate ip_forward and establish a NAT rule to forward the traffic coming in from the namespace you created (you have to adjust the network interface and SNAT ip address):
(You can also use the MASQUERADE rule if you prefer)
finally, you can run the process you want to analyze in the new namespace, and wireshark too:
You'll have to monitor the veth-a interface.
Indeed there is a way, using the Wireshark filters. But you cannot filter directly by process name or PID (because they are not a network quantities).
You should first figure out the protocols and the ports used by your process (the netstat command in the previous comment works well).
Then use Wireshark to filter the inbound (or outbound) port with the one you just retrieve. That should isolate the incoming and outcoming traffic of your process.
That will show the connections an application is making including the port being used.
Just an idea: Is it possible to bind your application to a different IP address? If so, you can use the usual suspects (tcpdump, etc.)
Tools for applications which are not capable of binding to another IP address:
http://freshmeat.net/projects/fixsrcip
http://freshmeat.net/projects/force_bind
I have come to a similar issue and I was able to sort it out based on this answer by ioerror, using NFLOG as described here:
Then you can create run the process in question from a user account that doesn't do anything else - and voila, you have just isolated and captured traffic from a single process.
Just wanted to post back in case it helps anyone.
I wrote a C application that does what is described in the great answer above by felahdab!
See here: nsntrace github repo
You can try tracedump - http://mutrics.iitis.pl/tracedump
It does exactly what you want, you can either give it a process ID or a program to run.
Try running the process you're interested in under strace:
It will give you some very detailed information about what your process is doing. As a process can open up any ports it wants to anywhere, using a predefined filter you may miss something.
Another approach would be to use a stripped-down virtual machine or a test machine on your network, and place your process on it in isolation on this. Then you can just use Wireshark to catch all from that machine. You'll be pretty sure that the traffic you capture will be relevant.
This is a dirty hack but I'd suggest either a divert or a log target with iptables for a given UID. eg:
It might also be worth looking into something like '--log-tcp-sequence', '--log-tcp-options', '--log-ip-options', '--log-uid' for that log target. Though I suspect that will only help you post process a pcap that includes a ton of other data.
The NFLOG target might be useful if you want to flag packets and then certain tagged packets will be sent over a netlink socket to a process of your choosing. I wonder if that would be useful for hacking up something with wireshark and your specific application running as a specific user?