Netcat (nc) Command Tutorial With Examples

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/netcat-nc-command-tutorial-examples/
PINTEREST
PINTEREST
INSTAGRAM

Netcat is a simple but useful tool used for TCP, UDP, Unix-domain sockets. Netcat can listen or connect specified sockets easily. Netcat is a platform independent command supported by Linux, Unix, Windows, BSD, MacOS etc. Common use cases for netcat are;

  • Simple TCP proxy
  • Shell script based HTTP clients and Servers
  • Network daemon testing
  • A SOCKS or HTTP ProxyCommand for ssh

Syntax

Help

Help information about netcat can be get with -h option like below.

Help
Help

Port Scan

Penetration testers generally uses port scan techniques for information gathering. Nmap is one of the most popular tool to find open ports.

Netcat can provide port scan functionality. The advantage of netcat is simplicity and no library dependency. Single netcat binary is enough for port scan and can be used for all operating systems like Windows, Linux, Unix, MacOS, BSD.

We will use -z options for port scan like below. In this example we will scan ip address 192.168.122.1 which can be also an domain name like poftut.com . The port range is specified as 1-30 .

Port Scan
Port Scan

The screenshot shows detailed output only port 22 is open as we can see.

Verbose Scan

In previous example we have scanned our host in a silent mode. Silent mode is the default mode which means only open ports will be printed to the console. There is option -v which will produce more detailed information . Verbose mode can be also used for banner grabbing purposes.

LEARN MORE  How To Install OwnCloud On CentOS Linux?

TCP Server

Another useful feature of netcat is acting as a TCP server. Netcat can listen for specified TCP port. But as a security measure in Linux systems only privileged users can listen ports between 1-1024 . In this example we will listen TCP ports 30. To give required privileges we use sudo command.

TCP Server
TCP Server

In the example screenshot we see that a client is connected to our server and provided text like ls and test . This client tool can be telnet or netcat too.

TCP Client

In previous example we have examined the TCP server. Netcat also provides client capabilities. To use netcat as a client we should provide host name or IP address and the port information. There is no special option for this. Use following syntax.

TCP Client
TCP Client

In this example we have connected to the localhost ssh port number 22. Ssh server sends us some text about it and waiting for response.

Send Files Trough Netcat

Another useful feature of the netcat is file transfer. As we see previous examples netcat can transfer text easily with a server-client architecture. There is no limit about transfer data. This data can be normal program or a movie. But keep in mind the transfer time will change according to data size. In order to transfer we need to setup a server which is the destination. And in the server configuration  we will redirect the incoming data into a file name myfile.txt

Now we can send the file from client. We will read file thefile.txt in the client side by redirecting to the netcat file like below.

LEARN MORE  Linux stat Command Tutorial With Examples

After the transfer is completed the both server and client side netcat instances will be closed.

Compress Transfers

 

Simple Web Server With Netcat

Netcat have another interesting feature for simple usage. Netcat can be used as a simple web server. Actually web servers are very simple if there is no special configuration requirements. Web servers only send html pages over HTTP protocol. Netcat can also send html code with redirection.

Simple Web Server With Netcat
Simple Web Server With Netcat

In the client side we will use Google Chrome to navigate IP address 192.168.122.239 with port number 4444 

Simple Web Server With Netcat
Simple Web Server With Netcat

Prevent DNS Lookup

While using DNS lookup can be disabled with option -n this will make operations more faster.

Prevent DNS Lookup
Prevent DNS Lookup

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/netcat-nc-command-tutorial-examples/
PINTEREST
PINTEREST
INSTAGRAM

2 Replies to “Netcat (nc) Command Tutorial With Examples

Leave a Reply

Your email address will not be published.