{"id":1185,"date":"2011-03-18T18:05:20","date_gmt":"2011-03-18T18:05:20","guid":{"rendered":"http:\/\/mccltd.net\/blog\/?p=1185"},"modified":"2011-03-30T07:45:42","modified_gmt":"2011-03-30T06:45:42","slug":"using-tcp-flags-to-filter-using-tcpdump-wireshark","status":"publish","type":"post","link":"http:\/\/darenmatthews.com\/blog\/?p=1185","title":{"rendered":"Using TCP Flags to filter 3-Way Handshake using TCPDUMP \/ Wireshark"},"content":{"rendered":"<p>Matching TCP traffic with particular flag combinations can be a useful way of examining TCP conversations.\u00a0 Refer to the TCP State Machine to understand the context of these flags:<!--more--><\/p>\n<p><a href=\"http:\/\/darenmatthews.com\/blog\/wp-content\/uploads\/2011\/03\/tcp_state_machine.jpg\"><\/a><a href=\"http:\/\/darenmatthews.com\/blog\/wp-content\/uploads\/2011\/03\/tcp_state_machine1.jpg\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1191\" title=\"tcp_state_machine\" src=\"http:\/\/darenmatthews.com\/blog\/wp-content\/uploads\/2011\/03\/tcp_state_machine1.jpg\" alt=\"tcp_state_machine\" width=\"846\" height=\"691\" srcset=\"http:\/\/darenmatthews.com\/blog\/wp-content\/uploads\/2011\/03\/tcp_state_machine1.jpg 846w, http:\/\/darenmatthews.com\/blog\/wp-content\/uploads\/2011\/03\/tcp_state_machine1-300x245.jpg 300w\" sizes=\"(max-width: 846px) 100vw, 846px\" \/><\/a><br \/>\n<strong>USING TCPDUMP TO FILTER ON FLAGS:<\/strong><\/p>\n<p>The flags are defined in the 14th byte of the TCP header.<\/p>\n<blockquote><p>+-+-+-+-+-+-+-+-+<br \/>\n|C|E|U|A|P|R|S|F|<br \/>\n|W|C|R|C|S|S|Y|I|<br \/>\n|R|E|G|K|H|T|N|N|<br \/>\n+-+-+-+-+-+-+-+-+<\/p><\/blockquote>\n<p>In the TCP 3-way handshakes, the exchange between hosts goes like this :<\/p>\n<p>1. Source sends SYN<br \/>\n2. Destination answers with SYN, ACK<br \/>\n3. Source sends ACK<\/p>\n<p>&#8211; If we want to match packets with only the SYN flag set, the 14th byte would have a binary<br \/>\nvalue of 00000010 which equals 2 in decimal.<br \/>\n# tcpdump -i eth1 &#8216;tcp[13] = 2&#8217;<\/p>\n<p>&#8211; Matching SYN, ACK (00010010 or 18 in decimal)<br \/>\n# tcpdump -i eth1 &#8216;tcp[13] = 18&#8217;<\/p>\n<p>&#8211; Matching either SYN only or SYN-ACK datagrams<br \/>\n# tcpdump -i eth1 &#8216;tcp[13] &amp; 2 = 2&#8217;<\/p>\n<p>We used a mask here. It will returns anything with the ACK bit set (thus the SYN-ACK combination as well)<\/p>\n<p>Let&#8217;s assume the following examples (SYN-ACK)<\/p>\n<p>00010010 : SYN-ACK packet<br \/>\n00000010 : mask (2 in decimal)<br \/>\n&#8212;&#8212;&#8211;<br \/>\n00000010 : result (2 in decimal)<\/p>\n<p>Every bits of the mask match !<\/p>\n<p>&#8211; Matching PSH-ACK packets<br \/>\n# tcpdump -i eth1 &#8216;tcp[13] = 24&#8217;<\/p>\n<p>&#8211; Matching any combination containing FIN (FIN usually always comes with an ACK so we either<br \/>\nneed to use a mask or match the combination ACK-FIN)<br \/>\n# tcpdump -i eth1 &#8216;tcp[13] &amp; 1 = 1&#8217;<\/p>\n<p>&#8211; Matching RST flag<br \/>\n# tcpdump -i eth1 &#8216;tcp[13] &amp; 4 = 4&#8217;<\/p>\n<p><strong>USING WIRESHARK:<\/strong><br \/>\nBy simply using &#8220;tcp.flags &amp; [number]&#8221; (without the quotes), you can easily filter interesting parts of the TCP conversation (such as SYN, SYN\/ACK, FIN and FIN\/ACK.<\/p>\n<p>Examples:<br \/>\n<a href=\"http:\/\/darenmatthews.com\/blog\/wp-content\/uploads\/2011\/03\/tcpflags1.jpg\"><\/a><a href=\"http:\/\/darenmatthews.com\/blog\/wp-content\/uploads\/2011\/03\/tcpflags11.jpg\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1187\" title=\"tcpflags1\" src=\"http:\/\/darenmatthews.com\/blog\/wp-content\/uploads\/2011\/03\/tcpflags11.jpg\" alt=\"tcpflags1\" width=\"978\" height=\"595\" srcset=\"http:\/\/darenmatthews.com\/blog\/wp-content\/uploads\/2011\/03\/tcpflags11.jpg 978w, http:\/\/darenmatthews.com\/blog\/wp-content\/uploads\/2011\/03\/tcpflags11-300x182.jpg 300w\" sizes=\"(max-width: 978px) 100vw, 978px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/darenmatthews.com\/blog\/wp-content\/uploads\/2011\/03\/tcpflags2.jpg\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1188\" title=\"tcpflags2\" src=\"http:\/\/darenmatthews.com\/blog\/wp-content\/uploads\/2011\/03\/tcpflags2.jpg\" alt=\"tcpflags2\" width=\"978\" height=\"595\" srcset=\"http:\/\/darenmatthews.com\/blog\/wp-content\/uploads\/2011\/03\/tcpflags2.jpg 978w, http:\/\/darenmatthews.com\/blog\/wp-content\/uploads\/2011\/03\/tcpflags2-300x182.jpg 300w\" sizes=\"(max-width: 978px) 100vw, 978px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/darenmatthews.com\/blog\/wp-content\/uploads\/2011\/03\/tcpflags7.jpg\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1189\" title=\"tcpflags7\" src=\"http:\/\/darenmatthews.com\/blog\/wp-content\/uploads\/2011\/03\/tcpflags7.jpg\" alt=\"tcpflags7\" width=\"978\" height=\"595\" srcset=\"http:\/\/darenmatthews.com\/blog\/wp-content\/uploads\/2011\/03\/tcpflags7.jpg 978w, http:\/\/darenmatthews.com\/blog\/wp-content\/uploads\/2011\/03\/tcpflags7-300x182.jpg 300w\" sizes=\"(max-width: 978px) 100vw, 978px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Matching TCP traffic with particular flag combinations can be a useful way of examining TCP conversations.\u00a0 Refer to the TCP State Machine to understand the context of these flags:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[84],"tags":[51,37],"_links":{"self":[{"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1185"}],"collection":[{"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1185"}],"version-history":[{"count":5,"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1185\/revisions"}],"predecessor-version":[{"id":1244,"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1185\/revisions\/1244"}],"wp:attachment":[{"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1185"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/darenmatthews.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}