nc Command in Linux

Topics Covered

Overview

The nc command in Linux, often referred to as Netcat, is a versatile tool utilized for reading, writing, redirecting data across networks. It creates both TCP and UDP sockets for network communication. It is often termed as 'Swiss-army knife' for TCP/IP due to its flexibility and capability to perform various network related tasks.

Syntax of nc Command in Linux

The basic syntax of the nc command in Linux is as follows:

Where:

  • options: These are flags that modify the behavior of the nc command.
  • hostname: This refers to the network device on which the connection is to be established.
  • port[s]: This is the network port number where nc should connect to or listen at.

Options in nc Command in Linux

  1. -l: Used to specify that nc should listen for an incoming connection rather than initiate a connection.

    For example -

    Output:

    This command makes nc listen on port 1234 for incoming connections.

  2. -v: Used to make the operation more talkative or verbose.

    For example -

    Output:

    This command attempts to connect to 'localhost' on port 1234, and outputs verbose information about the connection.

  3. -z: Makes nc to scan for listening daemons, without sending any data to them.

    For example -

    Output:

    This command checks if the ports from 20 to 25 on 'localhost' are open.

Example Usages

  • Transferring files between machines:

    Output:

    Explanation: The nc command in Linux is used to send 'file_to_send.txt' from the sender machine to the receiver machine.

  • Chatting between two systems:

    Output:

    Explanation: The nc command creates a simple chat system between two hosts.

Tips

  • The nc command can be combined with other commands using pipes for powerful results.

  • Always ensure that the port you are trying to use is not blocked by any firewall rules.

  • For security purposes, avoid running nc with root privileges unless absolutely necessary.

Advanced Use Cases of nc Command in Linux

  • Performing a port scanning:

    Output:

    Explanation: This advanced use of the nc command in Linux scans ports from 20 to 80 on the specified IP address, informing which ones are open and which ones are closed.

  • Setting up a reverse shell:

    Output:

    Explanation: This advanced use of the nc command sets up a reverse shell, allowing the listener to execute commands on the victim's machine. Use with caution and only for legitimate purposes.

  • Performing a banner grabbing:

    Output:

    Explanation: This advanced use of the nc command in Linux grabs the HTTP banner of 'www.google.com'. This technique is often used in reconnaissance stages of penetration testing.

Conclusion

  • The nc command in Linux is a powerful tool for network diagnostics and data transfer.

  • NC can operate in two modes: listen mode and connect mode.

  • NC can be used for file transfers, port scanning, banner grabbing, and even setting up a chat system or a reverse shell.

  • Despite its power, nc should be used responsibly and ethically due to its potential in network exploitation.