{"id":19460,"date":"2021-10-29T09:39:22","date_gmt":"2021-10-29T13:39:22","guid":{"rendered":"https:\/\/nuxx.net\/blog\/?p=19460"},"modified":"2021-10-29T09:44:34","modified_gmt":"2021-10-29T13:44:34","slug":"network-capture-with-process-name-and-pid-on-macos","status":"publish","type":"post","link":"https:\/\/nuxx.net\/blog\/2021\/10\/29\/network-capture-with-process-name-and-pid-on-macos\/","title":{"rendered":"Network Capture with Process Name and PID on macOS"},"content":{"rendered":"\n<p>There are many times when one wants to see which process is responsible for network traffic on a endpoint. While there are ways to look at which process has an open socket or whatnot, this doesn&#8217;t help with UDP, and it&#8217;s often quite useful to simply do a recording then later see which process is responsible for creating a packet.<\/p>\n\n\n\n<p>On Windows this is very easy using <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows-server\/networking\/technologies\/pktmon\/pktmon\">Pktmon<\/a> \/ netsh trace \/ <a href=\"https:\/\/www.microsoft.com\/en-us\/download\/details.aspx?id=4865\">Network Monitor<\/a> to do a capture, but on macOS it&#8217;s not that straightforward.<\/p>\n\n\n\n<p>Using <code>tcpdump<\/code> with the <code>-k<\/code> argument one can print process name and PID to the console. In this example I&#8217;m filtering on just the host 8.8.8.8 (<a href=\"https:\/\/developers.google.com\/speed\/public-dns\">Google Public DNS<\/a>) then running dig in another window to look up dingleberrypie.com:<\/p>\n\n\n\n<p><code>c0nsumer@myopia ~ % sudo tcpdump -k -i en0 host 8.8.8.8<br>Password:<br>tcpdump: verbose output suppressed, use -v or -vv for full protocol decode<br>listening on en0, link-type EN10MB (Ethernet), capture size 262144 bytes<br><strong>09:17:58.210959 <em>pid dig.76987<\/em> svc BE IP myopia.home.nuxx.net.56248 &gt; dns.google.domain: 10294+ [1au] A? dingleberrypie.com. (47)<\/strong><br>09:17:58.292541 IP dns.google.domain &gt; myopia.home.nuxx.net.56248: 10294 1\/0\/1 A 96.126.107.52 (63)<\/code><\/p>\n\n\n\n<p>The bold line is the packet going out to 8.8.8.8, and the <em><code>pid dig.76987<\/code><\/em> portion shows that it&#8217;s from a dig process, which had process ID 76987.<\/p>\n\n\n\n<p>Unfortunately, <a href=\"https:\/\/osqa-ask.wireshark.org\/questions\/53818\/seeing-pktap-metadata-in-wireshark-gui\/\">due to the non-standard way Apple writes this metadata into files, it&#8217;s not viewable in Wireshark<\/a>. This would be very nice, as it&#8217;d make parsing large captures much easier.<\/p>\n\n\n\n<p>For now it seems the best way to do this is to record the capture to a file, then feed it back into <code>tcpdump<\/code>. Recording this requires using the pktap pseudo interface (see the <code>tcpdump<\/code> man page about the <code>--interface<\/code> argument) to ensure this data is saved into the file. The same capture above, writing to a file called <code>out.cap<\/code>, would be as follows:<\/p>\n\n\n\n<p><code>sudo tcpdump -i pktap,en0 host 8.8.8.8 -w out.cap<\/code><\/p>\n\n\n\n<p>This can then be fed back into tcpdump for parsing\/filtering\/viewing:<\/p>\n\n\n\n<p><code>c0nsumer@myopia ~ % tcpdump -k -r out.cap<br>reading from PCAP-NG file out.cap<br>09:27:10.964758 (en0, proc dig:77619, svc BE, out, so) IP myopia.home.nuxx.net.52983 &gt; dns.google.domain: 56446+ [1au] A? dingleberrypie.com. (47)<br>09:27:11.018809 (en0, proc dig:77619, svc BE, in, so) IP dns.google.domain &gt; myopia.home.nuxx.net.52983: 56446 1\/0\/1 A 96.126.107.52 (63)<br>c0nsumer@myopia ~ %<\/code><\/p>\n\n\n\n<p>In this case it shows both the sending process and the process which received the packet.<\/p>\n\n\n\n<p>When using this for more detailed analysis, I&#8217;ll use the macOS <code>tcpdump<\/code> to grab a very broad capture and then do first-pass filtering before bringing it into <a href=\"https:\/\/www.wireshark.org\/\">Wireshark<\/a> for more detailed analysis. See the <em>PACKET METADATA FILTER<\/em> section of the <code>tcpdump<\/code> man page for details on how to filter on a PID, process name, etc. From the header of this section:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Use packet metadata filter expression to match packets against descriptive information about the packet: interface, process, service type or direction.<\/p><\/blockquote>\n\n\n\n<p> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are many times when one wants to see which process is responsible for network traffic on a endpoint. While there are ways to look&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/nuxx.net\/blog\/2021\/10\/29\/network-capture-with-process-name-and-pid-on-macos\/\">Continue reading<span class=\"screen-reader-text\">Network Capture with Process Name and PID on macOS<\/span><\/a><\/div>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-19460","post","type-post","status-publish","format-standard","hentry","category-computers","entry"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/posts\/19460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/comments?post=19460"}],"version-history":[{"count":3,"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/posts\/19460\/revisions"}],"predecessor-version":[{"id":19464,"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/posts\/19460\/revisions\/19464"}],"wp:attachment":[{"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/media?parent=19460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/categories?post=19460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nuxx.net\/blog\/wp-json\/wp\/v2\/tags?post=19460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}