<?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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>anil.org.in &#187; Networking</title>
	<atom:link href="http://anil.org.in/category/technology/networking/feed/" rel="self" type="application/rss+xml" />
	<link>http://anil.org.in</link>
	<description>Stuff I run across.</description>
	<lastBuildDate>Tue, 08 Dec 2009 18:46:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Netcat (nc): &#8220;TCP/IP Swiss Army Knife&#8221;</title>
		<link>http://anil.org.in/2009/05/26/netcat-nc-tcpip-swiss-army-knife/</link>
		<comments>http://anil.org.in/2009/05/26/netcat-nc-tcpip-swiss-army-knife/#comments</comments>
		<pubDate>Tue, 26 May 2009 06:31:28 +0000</pubDate>
		<dc:creator>anil</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://anil.org.in/2009/05/26/netcat-nc-tcpip-swiss-army-knife/</guid>
		<description><![CDATA[Many of us use telnet as a network debugging tool quite often. Just with its hostname and port options we find it very useful. But, I&#8217;m sure that many would recall following situations where we find the telnet client inadequate: To test a UDP service Want to send a pre-prepared set of request/responses to a [...]]]></description>
			<content:encoded><![CDATA[<p>Many of us use <code>telnet</code> as a network debugging tool quite often. Just with its <code>hostname</code> and <code>port</code> options we find it very useful. But, I&#8217;m sure that many would recall following situations where we find the <code>telnet</code> client inadequate:</p>
<ul>
<li>To test a UDP service</li>
<li>Want to send a pre-prepared set of request/responses to a server (to test a service)</li>
<li>Specify a Gateway for the TCP or UDP connection (checking routing and firewalling)</li>
<li>Receive and store dump of the traffic (network and application troubleshooting)</li>
<li>Send a UDP broadcast over the subnet (Network and application testing)</li>
<li>To set the ToS (type of service) flag in IP packet (to test QoS settings)</li>
<li>To copy a file over network without any regular services running</li>
<li>Do a port scan (without opening I/O)</li>
<li>Mimic the request/response sequence of a service/client you want to test</li>
</ul>
<p>There is a less-known tool called <code>nc</code>&#8211;acronym for Netcat&#8211;which was ranked 4th in the latest Top 100 Network Security Tools 2006 conducted by <code>Insecure.org</code> and always in the top 5 in previous years as well. </p>
<p><code>nc</code> can perform all the above and more. As it is rightly assigned with the caption of &#8220;TCP/IP swiss army knife.&#8221; Here is a quick set of commands to use <code>nc</code> for the use of a network administrator/troubleshooter.</p>
<p>Please note that Linux and BSD ports of <code>nc</code> differs in behaviour and syntax. This post is mostly based on the Linux port. I have tried to mention the difference where ever possible.</p>
<ul>
<li><strong>Test a TCP Service:</strong>
<p><code>nc &lt;hostname&gt; &lt;port number&gt;</code><br />
e.g.: <code>nc www.google.com 80</code></p>
<p>If you need to run a shell command after the TCP connection is established, use the &#8220;-c&#8221; option.</p>
<p><code>nc -c wwwclient www.google.com 80</code></p>
<p>There is an important behaviour you need to notice while using  -c and -e (invokes a script) options: the called command is supposed to handle both input and output of  <code>nc</code>. This means that while passing an <code>echo</code> command, for example, with -c option, you cannot expect nc to print the response to standard out. For example, the following command writes nothing to standard out:</p>
<p> nc -c &#8220;echo GET / HTTP/1.0&#8243; www.google.com 80</p>
<p>This is because nc pipes the HTTP response from www.google.com to <code>echo</code>. The -e option has the same behaviour. In the BSD port of <code>nc</code> there is no <code>-c</code> option available. And the <code>-e</code> has a very different meaning.</p>
<p>If you want to make a request to a server in two or more lines (as in the case of, say, HTTP protocol), write them down in a file and cat-pipe that file to NC. For example if you want to find the public IP address of your Internet gateway, create a text file http.txt with the following content:<br />
<code><br />
&lt;code&gt;<br />
GET /ip HTTP/1.0<br />
Host: www.linuxense.com</p>
<p>&lt;/code&gt;<br />
</code><br />
Now issue this command:</p>
<p>cat http.txt|nc www.linuxense.com 80</p>
<p>In certain protocols, such as SMTP, it needs to wait for the server to make a response before nc sends the next request. In such situation the -i option comes handy; it waits for the specified number of seconds before it sends the next line.</p>
<p>Now try this SMTP transaction. Save the following in to a file named, say, smtp.txt<br />
<code><br />
&lt;code&gt;<br />
helo greetings<br />
mail from: &lt;me@myhost&gt;<br />
rcpt to: &lt;you@yourhost&gt;<br />
data<br />
Subject: test from Netcat</p>
<p>Hello,<br />
This is a test from Netcat</p>
<p>See ya!</p>
<p>~me</p>
<p>.</p>
<p>&lt;/code&gt;<br />
</code></p>
<p>Now play out this transaction:</p>
<p><code><br />
cat smtp.txt | nc -i 2 your_smtp_server 25<br />
</code></p>
<p>See the <code>-i</code> in action. </p>
<p>There is a <code>-n</code> option that disables any DNS look up. This is useful if you are providing the IP address of the host and to explicitly say so to <code>nc</code>.</p>
<p>Use <code>-p</code> option to specify a source port. It is even possible to specify a range of port as in <code>80-1024</code> (both inclusive). If you omit the <code>-p</code> option, it will use a random port.</p>
</li>
<li><strong>Emulate TCP Service</strong>
<p>Use the <code>-l</code> flag to instruct <code>nc</code> to stay in listen mode. Use the following to bind <code>nc</code> to port 8000:</p>
<p><code>nc -l 8000</code></p>
<p>Normally <code>nc</code> quits when the remote connection closes. To make it stay listening for another connection, use <code>-k</code> (found to work only in BSD port). </p>
<p>Here <code>-p</code> option to specify the port it is listening to is allowed in Linux. In the BSD port, this is illegal.
</li>
<li><strong>Emulate a UDP Service</strong>
<p>Use the <code>-u </code> option, <code>nc</code> will turn the protocol to UDP. Almost all options which are valid for TCP mode (default mode) are valid with this option too.</p>
<p>Emulate a DNS service:</p>
<p><code>nc -u -l -p 53</code> (note that all parameters are similar to those in TCP mode. The option <code>-u</code> makes all the difference).</p>
<p>Here, the <code>-p</code> is a required option in Linux where us in BSD it is not permitted.</p>
<p>Now, run a dig command at it:</p>
<p><code>dig @localhost www.linuxense.com</code></p>
<p>You will see a partially readable line being printed on the console and this is how a DNS query looks like. Capture this into a file:</p>
<p><code>nc -u -i -p 53 > dns-query.txt</code> </p>
<p>And playback it at a functional DNS server and see the output:</p>
<p><code>cat dns-query.txt |nc -u mydnsserver 53</code></p>
</li>
<li>
<strong>Specifying Source IP Address:</strong></p>
<p>If you are on your gateway and you want to specify the source IP address of packets leaving the gateway, use the <code>-s</code> option. For example:</p>
<p><code>nc -s 192.168.1.1 remotehost 80</code>
</li>
<li><strong>Run a Zero I/O Port Scan</strong>
<p>See this example:<br />
<code>nc -z -v remotehost 80</nc></p>
<p>The <code>-z</code> runs <code>nc</code> in zero I/O scan mode. <code>-v</code> option is to turn on the verbose mode.</p>
<p>It is also possible to specify a range of IP addresses as follows (both IP addresses are inclusive):</p>
<p><code>nc -zv remotehost 80-1024</code>
</li>
<li><strong>Set "Type of Service" (ToS) Flag</strong>
<p>The flag <code>-x </code> allows to set the IP ToS flag. Possible values are "Minimize-Delay", "Maximize-Throughput", Maximize-Reliability", "Minimize-Cost".</p>
</li>
<li><strong>Set Up a Service Gateway/Proxy</strong>
<p>This is a quick and trivial service gateway you can try out in a few seconds. You device more sophisticated gateways with <code>nc</code>. BSD port of <code>nc</code> provides built-in support for this functionality with <code>-X</code> and <code>connect</code> verb.
</ul>
<p>If you enjoy reading this post and liked <code>nc</code>, please post here the <code>nc</code> tricks and hacks you invent.</p>
<p>--<br />
Related post:<br />
<a href="http://anil.org.in/2008/07/06/learn-ip-networking/">Learn IP Networking</a></p>
<p>Further reading:<br />
<a href="http://www.manpagez.com/man/1/nc/">BSD nc man page</a><br />
<a href="http://netcat.sourceforge.net/">GNU Netcat official homepage</a> </p>
]]></content:encoded>
			<wfw:commentRss>http://anil.org.in/2009/05/26/netcat-nc-tcpip-swiss-army-knife/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
