Understanding the -d option of tcpdump
Translation of the original in Catalan. Read it in: CatalàCastellano
[ Original ] According to the man page, tcpdump -d says:
-d Dump the compiled packet-matching code in a human readable form to standard output and stop.
Imagine that we want to see the packets that come from 162.129.225.192 of both TCP and UDP type. The tcp command to do this would be the one I write below and also, thanks to the quoted article, I describe the interpretation of the BPF filter (code that tcpdump generates to apply what we have asked it in the command).
# tcpdump -d src host centernet.jhuccp.org and \( ip proto \\tcp or \\udp \)
(000) ldh [12]
(001) jeq #0x800 jt 2 jf 8
(002) ld [26]
(003) jeq #0xa281e1c0 jt 4 jf 8
(004) ldb [23]
(005) jeq #0x6 jt 7 jf 6
(006) jeq #0x11 jt 7 jf 8
(007) ret #96
(008) ret #0
Interpretation of the code:
We load the 2 most significant bytes (big-endian) with an offset of 12 bytes from the start of the packet. In an ethernet type packet this corresponds to the field of the header where the type and the size are indicated. Once we have these bytes we compare them with 0x0800, which is the code for IPv4 type packets. If it is equal we jump to instruction 8, which returns 0 ( reject this packet ).
Next we load the 4 most significant bytes with an offset of 26bytes from the start of the IPv4 packet over ethernet, these 4 bytes correspond to the origin IP and then we compare them with 0xa281e1c0 (that is, 162.129.225.192). If it is not equal, we jump to instruction 8 ( reject this packet).
Now we load 1 byte with an offset of 23 bytes from the origin of the packet, which corresponds to the transport type that the IPv4 packet contains. If this byte is equal to 6 it is a TCP packet, if it is equal to 7 we jump to instruction 7, which returns 96, meaning accept this packet and get its first 96bytes (accept this packet ang get its first 96 bytes). If it is not equal to 6, we do the same check that it is not equal to 17, which corresponds to the UDP transport code.
What I have commented in this post, let it be known that I have not deduced it myself, but rather I have taken it from the quoted source. But for people who work with networks and Linux environments, like me, it can be very interesting so here it stays in case it can help someone.
2 comments
Comments from the old blog, as they were written. Comments are closed.
Ramon Roca
No em funciona l'enllaç "Original"
—
He intentat recuperar el link, però no hi ha manera de trobar-lo. Perquè el link original el trec d'un lloc que recopilen webs de seguretat i allà han perdut el link cap a la pàgina origen. També ho he buscat pel google però sense sort, ho sento.