{"id":3701,"date":"2016-02-09T19:13:45","date_gmt":"2016-02-09T17:13:45","guid":{"rendered":"http:\/\/www.laurentmarot.fr\/wordpress\/?p=3701"},"modified":"2020-02-18T19:18:32","modified_gmt":"2020-02-18T17:18:32","slug":"ttcpdump-advanced-filtering-pour-memoire","status":"publish","type":"post","link":"https:\/\/www.laurentmarot.fr\/wordpress\/?p=3701","title":{"rendered":"tcpdump advanced filtering (pour m\u00e9moire)"},"content":{"rendered":"<p><strong>TCPDUMP ADVANCED FILTERS<\/strong><br \/>\n===========================<\/p>\n<p>Merci \u00e0 Sebastien Wains <code><\/code><br \/>\nhttp:\/\/www.wains.be<\/p>\n<p>$Id: tcpdump_advanced_filters.txt 36 2013-06-16 13:05:04Z sw $<\/p>\n<p><span style=\"text-decoration: underline;\">Notes :<\/span><br \/>\nJ&rsquo;ai l&rsquo;habitude de sp\u00e9cifier l&rsquo;interface sur laquelle \u00e9couter. C&rsquo;est l&rsquo;option <em>-i<\/em> que l&rsquo;on retrouve dans tous les exemples suivants.<br \/>\nChacune des r\u00e8gles suivantes a \u00e9t\u00e9 test\u00e9e depuis mon laptop connect\u00e9 \u00e0 un r\u00e9seau wifi au travers de eth1.<br \/>\nN&rsquo;h\u00e9sitez pas \u00e0 m&rsquo;envoyer vos commentaires et suggestions ou pour corriger des erreurs.<br \/>\nJe sais que je suis vraiment affreux pour expliquer les choses, alors n&rsquo;h\u00e9sitez pas \u00e0 me solliciter si tout n&rsquo;est pas clair.<\/p>\n<p>J&rsquo;essaierai de mettre \u00e0 jour ce document avec de nouvelles r\u00e8gles utiles.<\/p>\n<p>Avant de commencer avec les filtres avanc\u00e9s, revoyons la syntaxe basique de tcpdump<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Syntaxe basique :<\/strong><\/span><\/p>\n<p><strong>Filtrage sur hosts :<\/strong><br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>&#8211; filtre tout trafic relatif \u00e0 192.168.1.1 en destination ou source<br \/>\n<code># tcpdump -i eth1 host 192.168.1.1<\/code><\/p>\n<p>&#8211; As source only<br \/>\n<code># tcpdump -i eth1 src host 192.168.1.1<\/code><\/p>\n<p>&#8211; As destination only<br \/>\n<code># tcpdump -i eth1 dst host 192.168.1.1<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Filtering ports :<\/strong><br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>&#8211; Match any traffic involving port 25 as source or destination<br \/>\n<code># tcpdump -i eth1 port 25<\/code><\/p>\n<p>&#8211; Source<br \/>\n<code># tcpdump -i eth1 src port 25<\/code><\/p>\n<p>&#8211; Destination<br \/>\n<code># tcpdump -i eth1 dst port 25<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Network filtering :<\/strong><br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p># tcpdump -i eth1 net 192.168<br \/>\n# tcpdump -i eth1 src net 192.168<br \/>\n# tcpdump -i eth1 dst net 192.168<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Protocol filtering :<\/strong><br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p># tcpdump -i eth1 arp<br \/>\n# tcpdump -i eth1 ip<\/p>\n<p># tcpdump -i eth1 tcp<br \/>\n# tcpdump -i eth1 udp<br \/>\n# tcpdump -i eth1 icmp<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Let&rsquo;s combine expressions :<\/strong><\/span><\/p>\n<p>Negation : ! or \u00ab\u00a0not\u00a0\u00bb (without the quotes)<br \/>\nConcatanate : &amp;&amp; or \u00ab\u00a0and\u00a0\u00bb<br \/>\nAlternate : || or \u00ab\u00a0or\u00a0\u00bb<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>&#8211; This rule will match any TCP traffic on port 80 (web) with 192.168.1.254 or 192.168.1.200 as destination host<br \/>\n<code># tcpdump -i eth1 '((tcp) and (port 80) and ((dst host 192.168.1.254) or (dst host 192.168.1.200)))'<\/code><\/p>\n<p>&#8211; Will match any ICMP traffic involving the destination with physical\/MAC address 00:01:02:03:04:05<br \/>\n<code># tcpdump -i eth1 '((icmp) and ((ether dst host 00:01:02:03:04:05)))'<\/code><\/p>\n<p>&#8211; Will match any traffic for the destination network 192.168 except destination host 192.168.1.200<br \/>\n<code># tcpdump -i eth1 '((tcp) and ((dst net 192.168) and (not dst host 192.168.1.200)))'<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>Advanced header filtering :<br \/>\n===========================<\/p>\n<p>Before we continue, we need to know how to filter out info from headers<\/p>\n<p>proto[x:y] : will start filtering from byte x for y bytes. ip[2:2] would filter bytes 3 and 4 (first byte begins by 0)<br \/>\nproto[x:y] &amp; z = 0 : will match bits set to 0 when applying mask z to proto[x:y]<br \/>\nproto[x:y] &amp; z !=0 : some bits are set when applying mask z to proto[x:y]<br \/>\nproto[x:y] &amp; z = z : every bits are set to z when applying mask z to proto[x:y]<br \/>\nproto[x:y] = z : p[x:y] has exactly the bits set to z<\/p>\n<p>Operators : &gt;, &lt;, &gt;=, &lt;=, =, !=<\/p>\n<p>This may not be clear in the first place but you&rsquo;ll find examples below involving these.<\/p>\n<p>Of course, it is important to know what the protocol headers look like before diving into more advanced filters.<\/p>\n<p><strong>IP header<\/strong><br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>|<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td colspan=\"20\">0<\/td>\n<td colspan=\"20\">1<\/td>\n<td colspan=\"20\">2<\/td>\n<td colspan=\"7\">3<\/td>\n<\/tr>\n<tr>\n<td colspan=\"2\">0<\/td>\n<td colspan=\"2\">1<\/td>\n<td colspan=\"2\">2<\/td>\n<td colspan=\"2\">3<\/td>\n<td colspan=\"2\">4<\/td>\n<td colspan=\"2\">5<\/td>\n<td colspan=\"2\">6<\/td>\n<td colspan=\"2\">7<\/td>\n<td colspan=\"2\">8<\/td>\n<td colspan=\"2\">9<\/td>\n<td colspan=\"2\">0<\/td>\n<td colspan=\"2\">1<\/td>\n<td colspan=\"2\">2<\/td>\n<td colspan=\"2\">3<\/td>\n<td colspan=\"2\">4<\/td>\n<td colspan=\"2\">5<\/td>\n<td colspan=\"2\">6<\/td>\n<td colspan=\"2\">7<\/td>\n<td colspan=\"2\">8<\/td>\n<td colspan=\"2\">9<\/td>\n<td colspan=\"2\">0<\/td>\n<td colspan=\"2\">1<\/td>\n<td colspan=\"2\">2<\/td>\n<td colspan=\"2\">3<\/td>\n<td colspan=\"2\">4<\/td>\n<td colspan=\"2\">5<\/td>\n<td colspan=\"2\">6<\/td>\n<td colspan=\"2\">7<\/td>\n<td colspan=\"2\">8<\/td>\n<td colspan=\"2\">9<\/td>\n<td colspan=\"2\">0<\/td>\n<td colspan=\"3\">1<\/td>\n<\/tr>\n<tr>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<\/tr>\n<tr>\n<td>|<\/td>\n<td colspan=\"7\" align=\"center\">Version<\/td>\n<td>|<\/td>\n<td colspan=\"7\" align=\"center\">IHL<\/td>\n<td>|<\/td>\n<td colspan=\"21\" align=\"center\">Type of Service<\/td>\n<td>|<\/td>\n<td colspan=\"25\" align=\"center\">Total Length<\/td>\n<td>|<\/td>\n<\/tr>\n<tr>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<\/tr>\n<tr>\n<td>|<\/td>\n<td colspan=\"23\" align=\"center\">Identification<\/td>\n<td>|<\/td>\n<td colspan=\"7\" align=\"center\">Flags<\/td>\n<td>|<\/td>\n<td colspan=\"31\" align=\"center\">Fragment Offset<\/td>\n<td>|<\/td>\n<\/tr>\n<tr>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>|<\/td>\n<td colspan=\"23\" align=\"center\">Time to Live<\/td>\n<td>|<\/td>\n<td colspan=\"18\" align=\"center\">Protocol<\/td>\n<td>|<\/td>\n<td colspan=\"20\" align=\"center\">Header Checksum<\/td>\n<td>|<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<\/tr>\n<tr>\n<td>|<\/td>\n<td colspan=\"63\" align=\"center\">Source Address<\/td>\n<td>|<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>|<\/td>\n<td colspan=\"63\" align=\"center\">Destination Address<\/td>\n<td>|<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<\/tr>\n<tr>\n<td>|<\/td>\n<td colspan=\"32\" align=\"center\">Options<\/td>\n<td>|<\/td>\n<td colspan=\"30\" align=\"center\">Padding (optional)<\/td>\n<td>|<\/td>\n<td colspan=\"10\">&lt;&#8211;<\/td>\n<\/tr>\n<tr>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>|<\/td>\n<td colspan=\"63\" align=\"center\">DATA&#8230;<\/td>\n<td>|<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td>&#8211;<\/td>\n<td>+<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>I&rsquo;ll consider we are only working with the IPv4 protocol suite for these examples.<br \/>\nIn an ideal world, every field would fit inside one byte. This is not the case, of course.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Are IP options set ?<\/strong><\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>Let&rsquo;s say we want to know if the IP header has options set. We can&rsquo;t just try to filter out the 21st byte because if no options are set, data start at the 21st byte. We know a \u00ab\u00a0normal\u00a0\u00bb header is usually 20 bytes (160 bits) long. With options set, the header is longer than that. The IP header has the header length field which we will filter here to know if the header is longer than 20 bytes.<\/p>\n<p>+-+-+-+-+-+-+-+-+<\/p>\n<p>|\u00a0 Version\u00a0 |\u00a0\u00a0 IHL \u00a0 |<\/p>\n<p>+-+-+-+-+-+-+-+-+<\/p>\n<p>Usually the first byte has a value of 01000101 in binary. Anyhow, we need to divide the first byte in half&#8230;<br \/>\n0100 = 4 in decimal.<br \/>\nThis is the IP version.<br \/>\n0101 = 5 in decimal.<br \/>\nThis is the number of blocks of 32 bits in the headers.<\/p>\n<p>&nbsp;<\/p>\n<p>5 x 32 bits = 160 bits or 20 bytes. The second half of the first byte would be bigger than 5 if the header had IP options set. We have two ways of dealing with that kind of filters.<\/p>\n<p>1. Either try to match a value bigger than 01000101.<\/p>\n<p>This would trigger matches for IPv4 traffic with IP options set, but ALSO any IPv6 traffic !<\/p>\n<p>In decimal 01000101 equals 69. Let&rsquo;s recap how to calculate in decimal. 0 : 0 \\ 1 : 2^6 = 64 \\ First field (IP version) 0 : 0 \/ 0 : 0 \/ &#8211; 0 : 0 \\ 1 : 2^2 = 4 \\ Second field (Header length) 0 : 0 \/ 1 : 2^0 = 1 \/ 64 + 4 + 1 = 69 The first field in the IP header would usually have a decimal value of 69. If we had IP options set, we would probably have 01000110 (IPv4 = 4 + header = 6), which in decimal equals 70. This rule should do the job : # tcpdump -i eth1 &lsquo;ip[0] &gt; 69&rsquo;<\/p>\n<p>Somehow, the proper way is to mask the first half\/field of the first byte, because as mentionned earlier,<br \/>\nthis filter would match any IPv6 traffic.<\/p>\n<p>2. The proper\/right way : \u00ab\u00a0masking\u00a0\u00bb the first half of the byte<\/p>\n<p>0100 0101 : 1<sup>st<\/sup> byte originally<br \/>\n0000 1111 : mask (0xf in hex or 15 in decimal). 0 will mask the values while 1 will keep the values intact.<br \/>\n=========<br \/>\n0000 0101 : final result<\/p>\n<p>You should see the mask as a power switch. 1 means on\/enabled, 0 means off\/disabled.<\/p>\n<p>The correct filter :<\/p>\n<p>In binary<br \/>\n# tcpdump -i eth1 &lsquo;ip[0] &amp; 15 &gt; 5&rsquo;<\/p>\n<p>or<\/p>\n<p>In hexadecimal<br \/>\n# tcpdump -i eth1 &lsquo;ip[0] &amp; 0xf &gt; 5&rsquo;<\/p>\n<p>I use hex masks.<\/p>\n<p>Recap.. That&rsquo;s rather simple, if you want to :<br \/>\n&#8211; keep the last 4 bits intact, use 0xf (binary 00001111)<br \/>\n&#8211; keep the first 4 bits intact, use 0xf0 (binary 11110000)<\/p>\n<p><strong>DF bit (don&rsquo;t fragment) set ?<\/strong><br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>Let&rsquo;s now trying to know if we have fragmentation occuring, which is not desirable. Fragmentation occurs<br \/>\nwhen a the MTU of the sender is bigger than the path MTU on the path to destination.<\/p>\n<p>Fragmentation info can be found in the 7th and 8th byte of the IP header.<\/p>\n<p>+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+<br \/>\n|Flags| Fragment Offset |<br \/>\n+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+<\/p>\n<p>Bit 0: reserved, must be zero<br \/>\nBit 1: (DF) 0 = May Fragment, 1 = Don&rsquo;t Fragment.<br \/>\nBit 2: (MF) 0 = Last Fragment, 1 = More Fragments.<\/p>\n<p>The fragment offset field is only used when fragmentation occurs.<\/p>\n<p>If we want to match the DF bit (don&rsquo;t fragment bit, to avoid IP fragmentation) :<\/p>\n<p>The 7th byte would have a value of :<br \/>\n01000000 or 64 in decimal<\/p>\n<p># tcpdump -i eth1 &lsquo;ip[6] = 64&rsquo;<\/p>\n<p><strong>Matching fragmentation ?<\/strong><br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>&#8211; Matching MF (more fragment set) ? This would match the fragmented datagrams but wouldn&rsquo;t match the last<br \/>\nfragment (which has the 2nd bit set to 0).<br \/>\n# tcpdump -i eth1 &lsquo;ip[6] = 32&rsquo;<\/p>\n<p>The last fragment have the first 3 bits set to 0&#8230; but has data in the fragment offset field.<\/p>\n<p>&#8211; Matching the fragments and the last fragments<br \/>\n# tcpdump -i eth1 &lsquo;((ip[6:2] &gt; 0) and (not ip[6] = 64))&rsquo;<\/p>\n<p>A bit of explanations :<br \/>\n\u00ab\u00a0ip[6:2] &gt; 0\u00a0\u00bb would return anything with a value of at least 1<br \/>\nWe don&rsquo;t want datagrams with the DF bit set though.. the reason of the \u00ab\u00a0not ip[6] = 64\u00a0\u00bb<\/p>\n<p>If you want to test fragmentation use something like :<br \/>\nping -M want -s 3000 192.168.1.1<\/p>\n<p>Matching datagrams with low TTL<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<p>The TTL field is located in the 9th byte and fits perfectly into 1 byte.<br \/>\nThe maximum decimal value of the TTL field is thus 255 (11111111 in binary).<\/p>\n<p>This can be verified :<br \/>\n$ ping -M want -s 3000 -t 256 192.168.1.200<br \/>\nping: ttl 256 out of range<\/p>\n<p>+-+-+-+-+-+-+-+-+<br \/>\n| Time to Live |<br \/>\n+-+-+-+-+-+-+-+-+<\/p>\n<p>We can try to find if someone on our network is using traceroute by using something like this on the gateway :<br \/>\n# tcpdump -i eth1 &lsquo;ip[8] &lt; 5&rsquo; Matching packets longer than X bytes &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; Where X is 600 bytes # tcpdump -i eth1 &lsquo;ip[2:2] &gt; 600&rsquo;<\/p>\n<p>More IP filtering<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>We could imagine filtering source and destination addresses directly in decimal addressing.<br \/>\nWe could also match the protocol by filtering the 10th byte.<\/p>\n<p>It would be pointless anyhow, because tcpdump makes it already easy to filter out that kind of info.<\/p>\n<p>TCP header<br \/>\n&#8212;&#8212;&#8212;-<\/p>\n<p>0 1 2 3<br \/>\n0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1<br \/>\n+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+<br \/>\n| Source Port | Destination Port |<br \/>\n+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+<br \/>\n| Sequence Number |<br \/>\n+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+<br \/>\n| Acknowledgment Number |<br \/>\n+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+<br \/>\n| Data | |C|E|U|A|P|R|S|F| |<br \/>\n| Offset| Res. |W|C|R|C|S|S|Y|I| Window |<br \/>\n| | |R|E|G|K|H|T|N|N| |<br \/>\n+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+<br \/>\n| Checksum | Urgent Pointer |<br \/>\n+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+<br \/>\n| Options | Padding |<br \/>\n+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+<br \/>\n| data |<br \/>\n+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+<\/p>\n<p>&#8211; Matching any TCP traffic with a source port &gt; 1024<br \/>\n# tcpdump -i eth1 &lsquo;tcp[0:2] &gt; 1024&rsquo;<\/p>\n<p>&#8211; Matching TCP traffic with particular flag combinations<\/p>\n<p>The flags are defined in the 14th byte of the TCP header.<\/p>\n<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>\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 &lsquo;tcp[13] = 2&rsquo;<\/p>\n<p>&#8211; Matching SYN, ACK (00010010 or 18 in decimal)<br \/>\n# tcpdump -i eth1 &lsquo;tcp[13] = 18&rsquo;<\/p>\n<p>&#8211; Matching either SYN only or SYN-ACK datagrams<br \/>\n# tcpdump -i eth1 &lsquo;tcp[13] &amp; 2 = 2&rsquo;<\/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&rsquo;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 &lsquo;tcp[13] = 24&rsquo;<\/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 &lsquo;tcp[13] &amp; 1 = 1&rsquo;<\/p>\n<p>&#8211; Matching RST flag<br \/>\n# tcpdump -i eth1 &lsquo;tcp[13] &amp; 4 = 4&rsquo;<\/p>\n<p>Actually, there&rsquo;s an easier way to filter flags :<br \/>\n# tcpdump -i eth1 &lsquo;tcp[tcpflags] == tcp-ack&rsquo;<\/p>\n<p>&#8211; Matching all packages with TCP-SYN or TCP-FIN set :<br \/>\n# tcpdump &lsquo;tcp[tcpflags] &amp; (tcp-syn|tcp-fin) != 0<\/p>\n<p>By looking at the TCP state machine diagram (http:\/\/www.wains.be\/pub\/networking\/tcp_state_machine.jpg)<br \/>\nwe can find the different flag combinations we may want to analyze.<\/p>\n<p>Ideally, a socket in ACK_WAIT mode should not have to send a RST. It means the 3 way handshake has not completed.<br \/>\nWe may want to analyze that kind of traffic.<\/p>\n<p>Matching SMTP data :<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>I will make a filter that will match any packet containing the \u00ab\u00a0MAIL\u00a0\u00bb command from SMTP exchanges.<\/p>\n<p>I use something like http:\/\/www.easycalculation.com\/ascii-hex.php to convert values from ASCII to hexadecimal.<\/p>\n<p>\u00ab\u00a0MAIL\u00a0\u00bb in hex is 0x4d41494c<\/p>\n<p>The rule would be :<\/p>\n<p># tcpdump -i eth1 &lsquo;((port 25) and (tcp[20:4] = 0x4d41494c))&rsquo;<\/p>\n<p>It will check the bytes 21 to 24. \u00ab\u00a0MAIL\u00a0\u00bb is 4 bytes\/32 bits long..<\/p>\n<p>This rule would not match packets with IP options set.<\/p>\n<p>This is an example of packet (a spam, of course) :<\/p>\n<p># tshark -V -i eth0 &lsquo;((port 25) and (tcp[20:4] = 0x4d41494c))&rsquo;<br \/>\nCapturing on eth0<br \/>\nFrame 1 (92 bytes on wire, 92 bytes captured)<br \/>\nArrival Time: Sep 25, 2007 00:06:10.875424000<br \/>\n[Time delta from previous packet: 0.000000000 seconds]<br \/>\n[Time since reference or first frame: 0.000000000 seconds]<br \/>\nFrame Number: 1<br \/>\nPacket Length: 92 bytes<br \/>\nCapture Length: 92 bytes<br \/>\n[Frame is marked: False]<br \/>\n[Protocols in frame: eth:ip:tcp:smtp]<br \/>\nEthernet II, Src: Cisco_X (00:11:5c:X), Dst: 3Com_X (00:04:75:X)<br \/>\nDestination: 3Com_X (00:04:75:X)<br \/>\nAddress: 3Com_X (00:04:75:X)<br \/>\n&#8230;. &#8230;0 &#8230;. &#8230;. &#8230;. &#8230;. = IG bit: Individual address (unicast)<br \/>\n&#8230;. ..0. &#8230;. &#8230;. &#8230;. &#8230;. = LG bit: Globally unique address (factory default)<br \/>\nSource: Cisco_X (00:11:5c:X)<br \/>\nAddress: Cisco_X (00:11:5c:X)<br \/>\n&#8230;. &#8230;0 &#8230;. &#8230;. &#8230;. &#8230;. = IG bit: Individual address (unicast)<br \/>\n&#8230;. ..0. &#8230;. &#8230;. &#8230;. &#8230;. = LG bit: Globally unique address (factory default)<br \/>\nType: IP (0x0800)<br \/>\nInternet Protocol, Src: 62.163.X (62.163.X), Dst: 192.168.X (192.168.X)<br \/>\nVersion: 4<br \/>\nHeader length: 20 bytes<br \/>\nDifferentiated Services Field: 0x00 (DSCP 0x00: Default; ECN: 0x00)<br \/>\n0000 00.. = Differentiated Services Codepoint: Default (0x00)<br \/>\n&#8230;. ..0. = ECN-Capable Transport (ECT): 0<br \/>\n&#8230;. &#8230;0 = ECN-CE: 0<br \/>\nTotal Length: 78<br \/>\nIdentification: 0x4078 (16504)<br \/>\nFlags: 0x04 (Don&rsquo;t Fragment)<br \/>\n0&#8230; = Reserved bit: Not set<br \/>\n.1.. = Don&rsquo;t fragment: Set<br \/>\n..0. = More fragments: Not set<br \/>\nFragment offset: 0<br \/>\nTime to live: 118<br \/>\nProtocol: TCP (0x06)<br \/>\nHeader checksum: 0x08cb [correct]<br \/>\n[Good: True]<br \/>\n[Bad : False]<br \/>\nSource: 62.163.X (62.163.X)<br \/>\nDestination: 192.168.X (192.168.XX)<br \/>\nTransmission Control Protocol, Src Port: 4760 (4760), Dst Port: smtp (25), Seq: 0, Ack: 0, Len: 38<br \/>\nSource port: 4760 (4760)<br \/>\nDestination port: smtp (25)<br \/>\nSequence number: 0 (relative sequence number)<br \/>\n[Next sequence number: 38 (relative sequence number)]<br \/>\nAcknowledgement number: 0 (relative ack number)<br \/>\nHeader length: 20 bytes<br \/>\nFlags: 0x18 (PSH, ACK)<br \/>\n0&#8230; &#8230;. = Congestion Window Reduced (CWR): Not set<br \/>\n.0.. &#8230;. = ECN-Echo: Not set<br \/>\n..0. &#8230;. = Urgent: Not set<br \/>\n&#8230;1 &#8230;. = Acknowledgment: Set<br \/>\n&#8230;. 1&#8230; = Push: Set<br \/>\n&#8230;. .0.. = Reset: Not set<br \/>\n&#8230;. ..0. = Syn: Not set<br \/>\n&#8230;. &#8230;0 = Fin: Not set<br \/>\nWindow size: 17375<br \/>\nChecksum: 0x6320 [correct]<br \/>\n[Good Checksum: True]<br \/>\n[Bad Checksum: False]<br \/>\nSimple Mail Transfer Protocol<br \/>\nCommand: MAIL FROM:\\r\\n<br \/>\nCommand: MAIL<br \/>\nRequest parameter: FROM:<\/p>\n<p>Matching HTTP data :<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>Let&rsquo;s make a filter that will find any packets containing GET requests<br \/>\nThe HTTP request will begin by :<\/p>\n<p>GET \/ HTTP\/1.1\\r\\n (16 bytes counting the carriage return but not the backslashes !)<\/p>\n<p>If no IP options are set.. the GET command will use the byte 20, 21 and 22<br \/>\nUsually, options will take 12 bytes (12nd byte indicates the header length, which should report 32 bytes).<br \/>\nSo we should match bytes 32, 33 and 34 (1st byte = byte 0).<\/p>\n<p>Tcpdump is only able to match data size of either 1, 2 or 4 bytes, we will take the following ASCII<br \/>\ncharacter following the GET command (a space)<\/p>\n<p>\u00ab\u00a0GET \u00a0\u00bb in hex : 47455420<\/p>\n<p># tcpdump -i eth1 &lsquo;tcp[32:4] = 0x47455420&rsquo;<\/p>\n<p>Matching HTTP data (exemple taken from tcpdump man page) :<\/p>\n<p># tcpdump -i eth1 &lsquo;tcp port 80 and (((ip[2:2] &#8211; ((ip[0]&amp;0xf)&lt;&lt;2)) &#8211; ((tcp[12]&amp;0xf0)&gt;&gt;2)) != 0)&rsquo;<\/p>\n<p>+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+<br \/>\nip[2:2] = | Total Length |<br \/>\n+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+<\/p>\n<p>+-+-+-+-+-+-+-+-+<br \/>\nip[0] = |Version| IHL |<br \/>\n+-+-+-+-+-+-+-+-+<\/p>\n<p>+-+-+-+-+-+-+-+-+<br \/>\nip[0]&amp;0xf = |# # # #| IHL | &lt;&#8211; that&rsquo;s right, we masked the version bits with 0xf or 00001111 in binary +-+-+-+-+-+-+-+-+ +-+-+-+-+ | Data | tcp[12] = | Offset| | | +-+-+-+-+ So what we are doing here is \u00ab\u00a0(IP total length &#8211; IP header length &#8211; TCP header length) != 0\u00a0\u00bb We are matching any packet that contains data. We are taking the IHL (total IP lenght Matching other interesting TCP things : &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; SSH connection (on any port) : We will be looking for the reply given by the SSH server. OpenSSH usually replies with something like \u00ab\u00a0SSH-2.0-OpenSSH_3.6.1p2\u00a0\u00bb. The first 4 bytes (SSH-) have an hex value of 0x5353482D. # tcpdump -i eth1 &lsquo;tcp[(tcp[12]&gt;&gt;2):4] = 0x5353482D&rsquo;<\/p>\n<p>If we want to find any connection made to older version of OpenSSH (version 1, which are insecure and subject to MITM attacks) :<br \/>\nThe reply from the server would be something like \u00ab\u00a0SSH-1.99..\u00a0\u00bb<\/p>\n<p># tcpdump -i eth1 &lsquo;(tcp[(tcp[12]&gt;&gt;2):4] = 0x5353482D) and (tcp[((tcp[12]&gt;&gt;2)+4):2] = 0x312E)&rsquo;<\/p>\n<p>Explanation of &gt;&gt;2 can be found below in the reference section.<\/p>\n<p>UDP header<br \/>\n&#8212;&#8212;&#8212;-<\/p>\n<p>0 7 8 15 16 23 24 31<br \/>\n+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+<br \/>\n| Source | Destination |<br \/>\n| Port | Port |<br \/>\n+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+<br \/>\n| | |<br \/>\n| Length | Checksum |<br \/>\n+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+<br \/>\n| |<br \/>\n| DATA &#8230; |<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<\/p>\n<p>Nothing really interesting here.<\/p>\n<p>If we want to filter ports we would use something like :<br \/>\n# tcpdump -i eth1 udp dst port 53<\/p>\n<p>ICMP header<br \/>\n&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>See different ICMP messages :<br \/>\nhttp:\/\/img292.imageshack.us\/my.php?image=icmpmm6.gif<\/p>\n<p>We will usually filter the type (1 byte) and code (1 byte) of the ICMP messages.<\/p>\n<p>Here are common ICMP types :<\/p>\n<p>0 Echo Reply [RFC792]<br \/>\n3 Destination Unreachable [RFC792]<br \/>\n4 Source Quench [RFC792]<br \/>\n5 Redirect [RFC792]<br \/>\n8 Echo [RFC792]<br \/>\n11 Time Exceeded [RFC792]<\/p>\n<p>We may want to filter ICMP messages type 4, these kind of messages are sent in case of congestion of the network.<br \/>\n# tcpdump -i eth1 &lsquo;icmp[0] = 4&rsquo;<\/p>\n<p>If we want to find the ICMP echo replies only, having an ID of 500. By looking at the image with all the ICMP packet description<br \/>\nwe see the ICMP echo reply have the ID spread across the 5th and 6th byte. For some reason, we have to filter out with the value in hex.<\/p>\n<p># tcpdump -i eth0 &lsquo;(icmp[0] = 0) and (icmp[4:2] = 0x1f4)&rsquo;<\/p>\n<p>References<br \/>\n&#8212;&#8212;&#8212;-<\/p>\n<p>tcpdump man page : http:\/\/www.tcpdump.org\/tcpdump_man.html<br \/>\nConversions : http:\/\/easycalculation.com\/hex-converter.php<br \/>\nFiltering HTTP requests : http:\/\/www.wireshark.org\/tools\/string-cf.html<br \/>\nFiltering data regardless of TCP options : http:\/\/www.wireshark.org\/lists\/wireshark-users\/201003\/msg00024.html<\/p>\n<p>Just in case the post disappears, here&rsquo;s a copy of the last URL :<\/p>\n<p>From: Sake Blok &lt;sake@xxxxxxxxxx&gt;<br \/>\nDate: Wed, 3 Mar 2010 22:42:29 +0100<br \/>\nOr if your capturing device is capable of interpreting tcpdump style filters (or more accurately, BPF style filters), you could use:<\/p>\n<p>tcp[(((tcp[12:1] &amp; 0xf0) &gt;&gt; 2) + 8):2] = 0x2030<\/p>\n<p>Which in English would be:<br \/>\n&#8211; take the upper 4 bits of the 12th octet in the tcp header ( tcp[12:1] &amp; 0xf0 )<br \/>\n&#8211; multiply it by four ( (tcp[12:1] &amp; 0xf0)&gt;&gt;2 ) which should give the tcp header length<br \/>\n&#8211; add 8 ( ((tcp[12:1] &amp; 0xf0) &gt;&gt; 2) + 8 ) gives the offset into the tcp header of the space before the first octet of the response code<br \/>\n&#8211; now take two octets from the tcp stream, starting at that offset ( tcp[(((tcp[12:1] &amp; 0xf0) &gt;&gt; 2) + 8):2] )<br \/>\n&#8211; and verify that they are \u00a0\u00bb 0&Prime; ( = 0x2030 )<\/p>\n<p>Of course this can give you false positives, so you might want to add a test for \u00ab\u00a0HTTP\u00a0\u00bb and the start of the tcp payload with:<\/p>\n<p>tcp[((tcp[12:1] &amp; 0xf0) &gt;&gt; 2):4] = 0x48545450<\/p>\n<p>resulting in the filter:<\/p>\n<p>tcp[((tcp[12:1] &amp; 0xf0) &gt;&gt; 2):4] = 0x48545450 and tcp[(((tcp[12:1] &amp; 0xf0) &gt;&gt; 2) + 8):2] = 0x2030<\/p>\n<p>A bit cryptic, but it works, even when TCP options are present (which would mess up a fixed offset into the tcp data).<\/p>\n<p>Cheers,<br \/>\nSake<\/p>\n<p>The end ?<br \/>\n&#8212;&#8212;&#8212;<\/p>\n<p>Please send more useful recipes !<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>TCPDUMP ADVANCED FILTERS =========================== Merci \u00e0 Sebastien Wains http:\/\/www.wains.be $Id: tcpdump_advanced_filters.txt 36 2013-06-16 13:05:04Z sw $ Notes : J&rsquo;ai l&rsquo;habitude de sp\u00e9cifier l&rsquo;interface sur laquelle \u00e9couter. C&rsquo;est l&rsquo;option -i que l&rsquo;on retrouve dans tous les exemples suivants. Chacune des r\u00e8gles suivantes a \u00e9t\u00e9 test\u00e9e depuis mon laptop connect\u00e9 \u00e0 un r\u00e9seau wifi au travers de [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,39],"tags":[],"_links":{"self":[{"href":"https:\/\/www.laurentmarot.fr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/3701"}],"collection":[{"href":"https:\/\/www.laurentmarot.fr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laurentmarot.fr\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laurentmarot.fr\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laurentmarot.fr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3701"}],"version-history":[{"count":50,"href":"https:\/\/www.laurentmarot.fr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/3701\/revisions"}],"predecessor-version":[{"id":4390,"href":"https:\/\/www.laurentmarot.fr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/3701\/revisions\/4390"}],"wp:attachment":[{"href":"https:\/\/www.laurentmarot.fr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laurentmarot.fr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laurentmarot.fr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}