<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>

<channel>
	<title>Lior Gradstein's Blog &#187; network</title>
	<atom:link href="http://www.gradstein.info/category/network/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gradstein.info</link>
	<description></description>
	<pubDate>Wed, 17 Dec 2008 11:37:45 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to understand the ARP queries and replies fields with pypcap</title>
		<link>http://www.gradstein.info/python/how-to-understand-the-arp-queries-and-replies-fields-with-pypcap/</link>
		<comments>http://www.gradstein.info/python/how-to-understand-the-arp-queries-and-replies-fields-with-pypcap/#comments</comments>
		<pubDate>Wed, 21 Mar 2007 21:36:00 +0000</pubDate>
		<dc:creator>Lior Gradstein</dc:creator>
		
		<category><![CDATA[network]]></category>

		<category><![CDATA[python]]></category>

	<!-- AutoMeta Start -->
	<category>pc</category>
	<category>packet</category>
	<category></category>
	<category>dpkt ethernet ethernet pkt</category>
	<category>object</category>
	<category>oriented   pypcap</category>
	<category>includes</category>
	<category>a</category>
	<category>huge</category>
	<category>quantity</category>
	<category>cool</category>
	<category>includes</category>
	<category>a</category>
	<category>network</category>
	<category>packet</category>
	<category>capture</category>
	<category>method</category>
	<category>print</category>
	<category>arp</category>
	<category>packet</category>
	<category>received</category>
	<category></category>
	<category>packet data op</category>
	<category>print</category>
	<category>pcapfrom</category>
	<category>socket</category>
	<category>import</category>
	<category>inet_ntoa    def</category>
	<category>ether_decode p</category>
	<category></category>
	<category>ether_decode packet data tha</category>
	<category></category>
	<category>ether_decode packet data sha</category>
	<!-- AutoMeta End -->
	
		<guid isPermaLink="false">http://www.gradstein.info/uncategorized/how-to-understand-the-arp-queries-and-replies-fields-with-pypcap/</guid>
		<description><![CDATA[I had a hard time understanding the function of each field in an ARP packet. The problem is that the fields change of meaning, depending on the opcode field. The two useful ones are for ARP queries (what is the ethernet address of the ip address I&#8217;m giving now) and ARP replies (that ip address [...]]]></description>
			<content:encoded><![CDATA[<p>I had a hard time understanding the function of each field in an ARP packet. The problem is that the fields change of meaning, depending on the opcode field. The two useful ones are for ARP queries (<span style="font-style: italic">what is the ethernet address of the ip address I&#8217;m giving now</span>) and ARP replies (<span style="font-style: italic">that ip address is located at this ethernet address</span>).</p>
<p>So to fix this problem once for all, I decided to write a python script that shows the different field values when an ARP packet is captured.</p>
<p>There are several libraries available to the pythonista to manipulate network packets. The most known is certainly <a href="http://pylibpcap.sourceforge.net/" class="liexternal">pylibpcap</a> which is quite old now, and not really object oriented. It is more an adaptation one-to-one of the C libpcap library, which may be useful for some people.<br />
Another library is <a href="http://code.google.com/p/pypcap/" class="liexternal">pypcap</a>, which is like pylibpcap, but <span style="font-weight: bold">much much</span> more object oriented.<br />
pypcap includes a huge quantity of protocols definitions, so it&#8217;s really cool to use, especially because it also includes a network packet capture method. There is no included method to send packets, but there are examples of how to do this in the test files.<br />
Another possibility is <a href="http://www.secdev.org/projects/scapy/" class="liexternal">scapy</a>, which is an extremely complete program (more a program than a library, even though you can use it as a module). The fact that it&#8217;s not that easy to include scapy in my own program, even though there&#8217;s now a <a href="http://www.secdev.org/projects/scapy/build_your_own_tools.html" class="liexternal">howto</a>. My program doesn&#8217;t need all the bells and whistles given by scapy, so I settled on pypcap.</p>
<p class="code">
<div class="python" style="font-family: monospace;color: #000066; border: 1px solid orange; margin: 5px; padding: 5px; background-color: #ffffff;"><span style="color: #ff7700;font-weight:bold;">import</span> dpkt, pcapfrom <span style="color: #dc143c;">socket</span> <span style="color: #ff7700;font-weight:bold;">import</span> inet_ntoa</p>
<p><span style="color: #ff7700;font-weight:bold;">def</span> ether_decode<span style="color: black;">&#40;</span>p<span style="color: black;">&#41;</span>: &nbsp; <br />
&nbsp; &nbsp;<span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&#8216;:&#8217;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&#8216;%02x&#8217;</span> % <span style="color: #008000;">ord</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>p<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></p>
<p><span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&#8216;__main__&#8217;</span>:<br />
&nbsp; &nbsp;iface_name = <span style="color: #483d8b;">&#8216;eth1&#8242;</span> &nbsp;<span style="color: #808080; font-style: italic;"># Here set your listening interface</span><br />
&nbsp; &nbsp;pc = pcap.<span style="color: black;">pcap</span><span style="color: black;">&#40;</span>iface_name<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp;pc.<span style="color: black;">setfilter</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&#8216;arp&#8217;</span><span style="color: black;">&#41;</span></p>
<p>&nbsp; &nbsp;<span style="color: #ff7700;font-weight:bold;">for</span> ts,pkt <span style="color: #ff7700;font-weight:bold;">in</span> pc:<br />
&nbsp; &nbsp; &nbsp; packet = dpkt.<span style="color: black;">ethernet</span>.<span style="color: black;">Ethernet</span><span style="color: black;">&#40;</span>pkt<span style="color: black;">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;ARP packet received:&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;op=%d&quot;</span> % packet.<span style="color: black;">data</span>.<span style="color: black;">op</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;src=%s&quot;</span> % ether_decode<span style="color: black;">&#40;</span>packet.<span style="color: black;">src</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;dst=%s&quot;</span> % ether_decode<span style="color: black;">&#40;</span>packet.<span style="color: black;">dst</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;spa=%s&quot;</span> % inet_ntoa<span style="color: black;">&#40;</span>packet.<span style="color: black;">data</span>.<span style="color: black;">spa</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;tpa=%s&quot;</span> % inet_ntoa<span style="color: black;">&#40;</span>packet.<span style="color: black;">data</span>.<span style="color: black;">tpa</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;tha=%s&quot;</span> % ether_decode<span style="color: black;">&#40;</span>packet.<span style="color: black;">data</span>.<span style="color: black;">tha</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;sha=%s&quot;</span> % ether_decode<span style="color: black;">&#40;</span>packet.<span style="color: black;">data</span>.<span style="color: #dc143c;">sha</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span></div>
</p>
<p>That&#8217;s it. Now, for example, 192.168.4.3 wants to get 192.168.4.254&#8217;s ethernet address (192.168.4.254 has 00:90:4c:49:00:2a address and 192.168.4.3 has 00:50:70:b4:19:0c), here is the output:</p>
<p class="code">
<div class="python" style="font-family: monospace;color: #000066; border: 1px solid orange; margin: 5px; padding: 5px; background-color: #ffffff;">ARP packet received:op=<span style="color: #ff4500;">1</span> src=<span style="color: #ff4500;">00</span>:<span style="color: #ff4500;">50</span>:<span style="color: #ff4500;">70</span>:b4:<span style="color: #ff4500;">19</span>:0c dst=ff:ff:ff:ff:ff:ff <br />
spa=<span style="color: #ff4500;">192.168</span><span style="color: #ff4500;">.4</span><span style="color: #ff4500;">.3</span> tpa=<span style="color: #ff4500;">192.168</span><span style="color: #ff4500;">.4</span><span style="color: #ff4500;">.254</span> tha=<span style="color: #ff4500;">00</span>:<span style="color: #ff4500;">00</span>:<span style="color: #ff4500;">00</span>:<span style="color: #ff4500;">00</span>:<span style="color: #ff4500;">00</span>:00sha=<span style="color: #ff4500;">00</span>:<span style="color: #ff4500;">50</span>:<span style="color: #ff4500;">70</span>:b4:<span style="color: #ff4500;">19</span>:0c<br />
ARP packet received:op=<span style="color: #ff4500;">2</span> src=<span style="color: #ff4500;">00</span>:<span style="color: #ff4500;">90</span>:4c:<span style="color: #ff4500;">49</span>:<span style="color: #ff4500;">00</span>:2a dst=<span style="color: #ff4500;">00</span>:<span style="color: #ff4500;">50</span>:<span style="color: #ff4500;">70</span>:b4:<span style="color: #ff4500;">19</span>:0c<br />
spa=<span style="color: #ff4500;">192.168</span><span style="color: #ff4500;">.4</span><span style="color: #ff4500;">.254</span> tpa=<span style="color: #ff4500;">192.168</span><span style="color: #ff4500;">.4</span><span style="color: #ff4500;">.3</span> tha=<span style="color: #ff4500;">00</span>:<span style="color: #ff4500;">50</span>:<span style="color: #ff4500;">70</span>:b4:<span style="color: #ff4500;">19</span>:0csha=<span style="color: #ff4500;">00</span>:<span style="color: #ff4500;">90</span>:4c:<span style="color: #ff4500;">49</span>:<span style="color: #ff4500;">00</span>:2a</div></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gradstein.info/python/how-to-understand-the-arp-queries-and-replies-fields-with-pypcap/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
