Open In App

Minimal Web Server Using Netcat

Last Updated : 10 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Netcat is a networking utility that can be used to complete various tasks over TCP and UDP. It can be used to send TCP and UDP packets, also it can listen on the ports, specified for UDP and TCP. This utility is well-known for its versatility, as its application ranges from setting up simple chat servers to building your reverse shell. The interesting part about this utility is that it can be used as a web server as HTTP also relies on TCP.

In this article, we will learn how to harness the power of Netcat to spin up a minimal web server in no time.

How to Install Netcat in Linux?

Netcat has various versions and implementations. Most of them are quite similar but may have some differences in the list of options available. I have used ncat – a reimplementation of netcat, by the creators of NMAP.

Open the terminal and enter the following command in the terminal

sudo apt install ncat -y

This command will install the Ncat package, along with all the dependencies that are required to execute Ncat.

netcat installation

netcat installation

Setting up the netcat server

Step 1: Let’s make Netcat listen on an ephemeral port, say port:8000. This can be done by entering the following command in the terminal.

nc -l -p 8000
  • -l: The ‘-l’ option stands for listening mode.
  • -p: The ‘-p‘ option is meant for specifying the port number to listen to.
nc server listening

nc server listening

Now the server is listening for connection requests on port 8000.

Step 2: Since we have not provided ways to handle HTTP requests, we can only make a TCP connection to this server. We can set this by opening another terminal and entering the below command.

nc -nv 127.0.0.1 8000
  • -n: The ‘-n‘ option tells Netcat not to use a DNS server to perform a DNS resolution.
  • -v: The ‘-v‘ option stands for verbose. It is used to print
  • 127.0.0.1: The first parameter of the command denotes the IPv4 address of the host in which the netcat server is running, which is 127.0.0.1 (localhost), in our case.
  • 8000: The second parameter is the port number in which we are going to connect to the server.
netcat client

netcat client

You will get a similar output if the connection is successful.

How to Draft an HTTP response?

Now that we have made our Netcat server listen to TCP connection requests, Let’s define a way to handle HTTP requests. Any web server must be capable of receiving and acknowledging HTTP requests. A classic HTTP response looks like the one given below.

HTTP/1.1 200 OK\r\n
  • HTTP stands for Hypertext Transfer Protocol.
  • 1.1 is the version of HTTP used.
  • 200 OK is the HTTP status code, which means that the connection is successfully established.
  • \r stands for carriage return and \n is a new line character.

Serving the HTTP response using Netcat

To serve our defined HTTP response using the Netcat server, we are going to make use of a concept called piping. Let’s create a basic web server using NetCat by entering the following command in the terminal.

echo -e 'HTTP/1.1 200 OK\r\n' | nc -lp 8000
netcat web server listening

netcat web server listening

The above command is in the form of ‘command1 | command2’. Command2 should be familiar to you, as we have already discussed it. Command1 is also simple, as it just echoes our HTTP response message.

The ‘-e’ option is used as we have used backslashes in our response, which need to be interpreted in the right way. The output of this echo command is fed as input to the next command, which is our listening netcat server. That’s it, now our Netcat web server is ready to respond to HTTP connections!

How to Connect to Netcat Web Server

We are going to use the conventional way of accessing a web server, using a web browser. Follow the below steps:-

Note: Make sure that the netcat server is listening on a specific port before trying to connect to the server. Keep the terminal in which the server is running, alive.

Step 1: Open the web browser

Firefox is the default web browser in most of the Linux distributions and I’m using the same.

Step 2: Enter URL

In the address bar of the browser, enter the target URL, which is given below:-

http://localhost:8000/
Entering the URL in the web browser's address bar

Entering the URL in the web browser’s address bar

This will load a blank screen on your web browser, as we have not provided any content for the web browser to the server, but only a response message.

1. Viewing the HTTP response

Follow the below steps to check out our HTTP response in the web browser.

Step 1: Open browser developer tools

Right-click anywhere on the browser’s screen and select ‘inspect‘ from the dropdown list. This will open the developer tools of the web browser.

Step 2: Network tab

In the developer tools, click on the ‘Network Tab‘, from the array of tabs at the top.

Step 3: Reload

Reload the webpage using the shortcut key ‘CTRL+R. Now the traffic will be displayed on the network tab of developer tools.

HTTP status code

HTTP status code

Note that the status code 200 is sent from the server and received by the web browser, successfully.

2. HTTP request

At the same time, when you make the connection to the server, the HTTP request sent by the web browser will be displayed by the listening Netcat web server.

http request from a web browser

http request from a web browser

This contains several information such as the language and type of encoding accepted by the web browser.

How to Serve Content to the Web Browser?

We have successfully made a connection via TCP and made our Netcat web server respond to an incoming HTTP request. But till now, we are seeing just blank screens without any content. It’s time to finish building our minimal web server by enabling web content sharing in the form of HTML pages.

Let’s create a simple HTML page to be served. This can be done by creating a file called gfg.html on our machine. The path of this file can be arbitrary, but it is conventional to use the ‘/var/www/html ‘path in Linux to store web pages to be served by web servers. This is the default path used by the Apache web server.

Note: The file name for HTML page is arbitrary. But it should have the extension ‘.html’. In my case, it is gfg.html

Create a new file in the conventional location by entering the following command in the terminal.

sudo nano /var/www/html/gfg.html
  • gfg.html

HTML




<!DOCTYPE html>
<html>
<head>
<title>Minimal Web Server using Netcat</title>
</head>
<body style="background-color: black;">
    <h1 style="color: green; text-align: center;">GeeksForGeeks</h1>
    <h2 style="color: white; text-align: center;">Minimal Web Server using Netcat</h2>
</body>
</html>


Copy and paste the above code in the newly created file – gfg.html. Press ‘CTRL+X’ and then ‘Y’ to save your changes using nano.

1. Serving the HTML page

Now, let’s use the already discussed concept of piping to pipe the HTTP response and our newly created HTML document as the input to our Netcat server. We can do this by typing the following command in the terminal.

 { echo -e 'HTTP/1.1 200 OK\r\n'; cat /var/www/html/gfg.html; } | netcat -l -p 8000

netcat web server listening on port 80000 and ready to serve gfg.html

netcat web server listening on port 80000 and ready to serve gfg.html

  • In the command, we have used braces and semicolons to combine the echo and cat commands.
  • The cat command, when one file is given as input, prints the content of the file as stdout which is fed as stdin to the netcat server, listening on port 8000.
  • Thus, the content of the file – gfg.html will be served by our netcat server, which in turn, gets interpreted and rendered by the web browser as a web page to the client.

2. Viewing served content

While the Netcat server is listening, keep the terminal alive and open a web browser in another window. Connect to the Netcat web server by entering the already mentioned URL – http://localhost:8000/

The following web page will be loaded by the web browser.

Web page served using netcat

Web page served using netcat

Conclusion

It is clear from what we have learned that Netcat is truly versatile and can even be used as a web server. Though it is not recommended to use Netcat as an enterprise-grade web server, it comes in handy in many situations where we need to host something on the web using very less resources. Netcat turns out to be a lifesaver in such cases where we don’t have sophisticated resources for serving web pages.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads