<p>In this episode we'll talk about filtering and dissecting packet traces and streams and introduce diffing. Remember that most tools have very flexible options for a variety of use cases. So check their manpages. Each man page also has multiple examples of how to use each tool.</p>
<h3 id="counting-packets">Counting Packets</h3>
<ul>
<li>Lets start with grabbing a trace from the unit tests:</li>
</ul>
<pre><code>   $ mkdir /tmp/packets
   $ cd /tmp/packets
   $ cp /path/to/onics/tests/data/packets/sample.xpkt .
</code></pre>
<ul>
<li>Lets see what we have inside. First, lets see how many packets there are. We'll use a new tool 'pcount'.</li>
</ul>
<pre><code>   $ pcount sample.xpkt

   90 total packets and 19082 total bytes.
</code></pre>
<ul>
<li>Good thing we looked first. Don't want to walk through all the packets.</li>
</ul>
<h3 id="scanning-packet-flows">Scanning Packet Flows</h3>
<ul>
<li><p>Well, lets look at the connections or &quot;flows&quot; in the trace. We'll do this by using the 'nftrk' command for &quot;network flow tracker&quot;.</p></li>
<li><p>Like 'pcount' this utility (and many or most ONICS utilities), this program can run on a live stream or a trace file. We'll run:</p></li>
</ul>
<pre><code>   $ nftrk -dt sample.xpkt | grep END
</code></pre>
<p>and get:</p>
<pre><code>   |FLOW END|IP:ca=192.168.0.43,sa=224.0.0.251,proto=2|Start=1565446184.543,
   End=1565446184.544,Dur=0.001|SENT:1,60|
   ...
   |FLOW END|IP:ca=192.168.0.7,sa=192.168.0.255,proto=17,cpt=631,spt=631|
   Start=1565446184.543,End=1565446184.544,Dur=0.001|SENT:3,660|
</code></pre>
<ul>
<li><p>'nftrk' tracks flows giving events like the start and end of each flow or connection. We just want a summary of all the connections so we just grep for 'END' (all caps).</p></li>
<li><p>We could just as easily have grepped for START, but this way we get the final number of packets sent and received on each connection. If we just want a count of the connections we can do:</p></li>
</ul>
<pre><code>   $ nftrk -dt sample.xpkt | grep START | wc -l
</code></pre>
<p>and that tells us that there are 10 flows in the trace.</p>
<h3 id="basic-filtering">Basic Filtering</h3>
<ul>
<li>Ok, so 90 packets, in 10 flows totalling ~19000 bytes. Lets now see about filtering the connection so we just get the TCP packets.</li>
</ul>
<pre><code>   $ pflt tcp sample.xpkt tcponly.xpkt

   $ pcount tcponly.xpkt
   73 total packets and 17184 total bytes.

   $ nftrk -dt tcponly.xpkt | grep END | wc -l
   2
</code></pre>
<ul>
<li>We could have been super fancy and done:</li>
</ul>
<pre><code>   $ pflt tcp sample.xpkt |
     pcount -p |
     nftrk -t 2&gt;/tmp/flows &gt; tcponly.xpkt &amp;&amp;
     echo -n &quot;Number of flows &quot; &amp;&amp;
     grep END /tmp/flows | wc -l &amp;&amp;
     rm -f /tmp/flows
</code></pre>
<ul>
<li>Ok, enough of that. Anyway, now we have a trace file with only the TCP connections. Running</li>
</ul>
<pre><code>   $ nftrk -dt /tmp/tcponly.xpkt | grep END
   |FLOW END|IP:ca=192.168.0.4,sa=192.168.0.7,proto=6,cpt=38859,spt=22|
   Start=1566073862.612,End=1566073862.613,Dur=0.000|C2S:25,4561|S2C:30,5124|
   |FLOW END|IP:ca=192.168.0.4,sa=64.233.169.147,proto=6,cpt=35071,spt=80|
   Start=1566073862.613,End=1566073862.613,Dur=0.000|C2S:9,704|S2C:9,6795|
</code></pre>
<p>Shows that the server ports are 22 and 80 for the two connections. That's SSH and HTTP.</p>
<ul>
<li><p>The patterns we can use to filter packets are pretty standard across most of the ONICS tools.</p></li>
<li><p>We'll discuss this is more detail in a future podcast. But if you want to see the kinds of fields you can match on go to</p></li>
</ul>
<pre><code>   $ man onics_proto
</code></pre>
<h3 id="extracting-ranges-of-packets">Extracting Ranges of Packets</h3>
<ul>
<li>What if we wanted to just grab specific packets out of the trace file? Say we wanted packets 3-6. For that we would run:</li>
</ul>
<pre><code>   $ pxtr 3,6 sample.xpkt pkts-3-to-6.xpkt
</code></pre>
<ul>
<li>Alternately we could ask for all packets from the 7th packet to the first TCP packet. We match using the same types of matching conditions as with pflt, but we must enclose them in {}s.</li>
</ul>
<pre><code>   $ pxtr &quot;7,{tcp}&quot; sample.xpkt | xpktdump
</code></pre>
<ul>
<li>Lets say we just wanted to drop packets 5-10 from the stream. There are several ways to do this in ONICS, but using pxtr, the way we would do it would be:</li>
</ul>
<pre><code>   $ pxtr 1,4 sample.xpkt &gt; not-5-to-10.xpkt
   $ pxtr 11,NONE sample.xpkt &gt;&gt; not-5-to-10.xpkt
</code></pre>
<ul>
<li>Maybe I should add another option to pxtr to invert the boundary conditions. It's a tradeoff between having the tools do one thing and one thing well and supporting a potentially common use case.</li>
</ul>
<h3 id="differences-between-traces">Differences Between Traces</h3>
<ul>
<li>Finally, lets look at one tool that I really like. Let's see the difference between the original stream and the one that we just created:</li>
</ul>
<pre><code>   $ pdiff sample.xpkt not-5-to-10.xpkt | less
</code></pre>
<ul>
<li>Sure enough that shows us that packets 5-10 were dropped from the stream. If we do the reverse</li>
</ul>
<pre><code>   $ pdiff -v not-5-to-10.xpkt sample.xpkt | less
</code></pre>
<p>it describes the sample.xpkt from the perspective of starting with not-5-to-10.xpkt and inserting a bunch of packets into the middle.</p>
<h3 id="conclusion">Conclusion</h3>
<ul>
<li>In this podcast we looked at a few tools to help analyze and dissect packet traces or packet streams.</li>
<li>Next time we'll look at some of the more powerful pattern matching we can apply and</li>
</ul>
