ncat
Ncat is the Nmap project's reimplementation of netcat. It behaves the same on Windows, Linux and macOS and adds SSL/TLS, proxy support, command execution (port forwarding) and allow/deny access control.
43.1 What it does, where it runs
| Platform | How to get it |
|---|---|
| Windows | Nmap installer or winget install Insecure.Nmap |
| Linux | apt or dnf: ncat / nmap-ncat |
| macOS | brew install nmap (ncat included) |
| Syntax | ncat [options] [hostname] [port] |
43.2 Two modes: connect and listen
- Listen mode: ncat -l port waits for an incoming connection (server).
- Connect mode: ncat host port opens the connection (client).
- No port with -l: default port 31337. An address after -l binds to that IP only.
- Without -k the listener exits after the first connection.
# srv 192.168.63.20ncat -l -v 8080# cli 192.168.63.10(echo hello server; sleep 1) | ncat -v 192.168.63.20 8080# no port given: default 31337ncat -l# bind to one address onlyncat -l 192.168.63.20 8081ss -ltn$ (echo hello server; sleep 1) | ncat -v 192.168.63.20 8080Ncat: Version 7.992 ( https://nmap.org/ncat )Ncat: Connected to 192.168.63.20:8080.Ncat: 13 bytes sent, 0 bytes received in 1.00 seconds.srv$ cat /tmp/m1.txtNcat: Listening on [::]:8080Ncat: Listening on 0.0.0.0:8080Ncat: Connection from 192.168.63.10:42890.hello server$ ss -ltn | grep -E 'State|31337|8081'State Recv-Q Send-Q Local Address:Port Peer Address:PortLISTEN 0 10 192.168.63.20:8081 0.0.0.0:*LISTEN 0 10 0.0.0.0:31337 0.0.0.0:*LISTEN 0 10 [::]:31337 [::]:*43.3 Protocol and connect options
| Option | What it does |
|---|---|
| -u | UDP instead of TCP |
| -U | Unix domain socket (file path instead of IP) |
| -4 / -6 | Force IPv4 / IPv6 |
| --sctp | SCTP |
| --vsock | Between a VM and its host (CID 2 = host) |
| -s IP / -p port | Fixed source address / source port |
| -g / -G | IPv4 loose source routing (most routers drop it) |
# srvncat -u -l 5140ncat -l -U /tmp/app.sock# cliecho '<14>syslog test' | ncat -u 192.168.63.20 5140ncat -4 -z -v 192.168.63.20 80ncat --sctp -z -v 192.168.63.20 5000ncat --vsock -z 2 1234echo from fixed port | ncat -s 192.168.63.10 -p 40000 192.168.63.20 8082srv$ cat /tmp/udp.txt<14>syslog test$ ncat --sctp -z -v 192.168.63.20 5000Ncat: Connection refused.$ ncat --vsock -z 2 1234libnsock nsock_make_socket(): Socket trouble: Operation not permittedsrv$ cat /tmp/c3.txtNcat: Connection from 192.168.63.10:40000.from fixed portSCTP was refused because nothing listens on port 5000; vsock only works between a VM and its host, so inside a container it gives a permission error.
43.4 Listen mode: -k, --chat, --broker
- -k (keep open): the listener keeps accepting new connections.
- --chat: small chat server, every client gets an ID like <user5>.
- --broker: forwards messages to all clients, without IDs.
- -m N: maximum simultaneous connections.
ncat -l -k -v 8083echo one | ncat 192.168.63.20 8083echo two | ncat 192.168.63.20 8083echo three | ncat 192.168.63.20 8083ncat -l --chat 9000ncat -l --broker 9001srv$ cat /tmp/k.txtNcat: Connection from 192.168.63.10:58112.oneNcat: Connection from 192.168.63.10:58128.twoNcat: Connection from 192.168.63.10:58134.threecli$ cat /tmp/chat-cli.txt<announce> 192.168.63.10:56872 is connected as <user5>.<announce> already connected: nobody.<announce> 192.168.63.66:36804 is connected as <user6>.<user6> hi from other43.5 SSL / TLS
- --ssl on a listener with no certificate: ncat generates a temporary 2048-bit RSA key.
- --ssl on the client encrypts but does not verify: a self-signed cert gives a warning and the connection still comes up.
- --ssl-verify makes the check strict; --ssl-trustfile trusts a CA/cert file.
- --ssl-servername sets the name to match (SNI); --ssl-cert / --ssl-key give your own certificate; --ssl-ciphers, --ssl-alpn tune TLS.
- Step 1
1.Server: ncat -l -k --ssl --ssl-cert cert.pem --ssl-key key.pem 8444
- Step 2
2.Client connects by IP 192.168.63.20 with --ssl-verify --ssl-trustfile ca.pem
- Step 3
3.Cert is trusted but the name (IP) does not match CN srv.lab
- Step 4
4.Certificate verification error. QUITTING. [exit 2]
- Step 5
5.Add --ssl-servername srv.lab: name matches
- Step 6
6.SSL connection up, 'verified hello' received [exit 0]
# srv: no certificate givenncat -l -k --ssl -v 8443 -c 'echo secret over TLS'sleep 1 | ncat --ssl -v 192.168.63.20 8443sleep 1 | ncat --ssl-verify -v 192.168.63.20 8443# srv: own certificate, CN srv.labncat -l -k --ssl --ssl-cert /tmp/cert.pem --ssl-key /tmp/key.pem 8444 -c 'echo verified hello'sleep 1 | ncat --ssl-verify --ssl-trustfile /tmp/ca.pem 192.168.63.20 8444sleep 1 | ncat -v --ssl-verify --ssl-trustfile /tmp/ca.pem --ssl-servername srv.lab 192.168.63.20 8444srv$ cat /tmp/ssl.txtNcat: Generating a temporary 2048-bit RSA key. Use --ssl-key and --ssl-cert to use a permanent one.Ncat: Listening on 0.0.0.0:8443$ sleep 1 | ncat --ssl -v 192.168.63.20 8443Ncat: Subject: CN=localhostNcat: Certificate verification failed (self-signed certificate).Ncat: SSL connection to 192.168.63.20:8443.secret over TLS$ sleep 1 | ncat --ssl-verify -v 192.168.63.20 8443Ncat: Certificate verification failed (self-signed certificate).Ncat: I/O error.$ sleep 1 | ncat --ssl-verify --ssl-trustfile /tmp/ca.pem 192.168.63.20 8444Ncat: Certificate verification error. QUITTING.[exit 2]$ sleep 1 | ncat -v --ssl-verify --ssl-trustfile /tmp/ca.pem --ssl-servername srv.lab 192.168.63.20 8444Ncat: SSL connection to 192.168.63.20:8444.verified hello[exit 0]43.6 Proxy options
- ncat -l --proxy-type http turns ncat itself into an HTTP proxy server.
- Client: --proxy host:port --proxy-type http|socks4|socks5; default ports 3128 (HTTP) and 1080 (SOCKS).
- --proxy-auth user:pass (or env NCAT_PROXY_AUTH); without it an auth proxy returns 407 Proxy Authentication Required.
- --proxy-dns local | remote (default) | both | none decides who resolves the destination name.
# proxy 192.168.63.5ncat -l -k -v --proxy-type http 3128ncat -l -k --proxy-type http --proxy-auth joe:secret 3129# cli(printf 'GET / HTTP/1.0\r\n\r\n'; sleep 1) | ncat --proxy 192.168.63.5:3128 --proxy-type http 192.168.63.20 80(...) | ncat --proxy 192.168.63.5:3129 192.168.63.20 80(...) | ncat --proxy 192.168.63.5:3129 --proxy-auth joe:secret 192.168.63.20 80HTTP/1.0 200 OK hello from srvNcat: Proxy returned status code 407.[exit 1]HTTP/1.0 200 OK hello from srv43.7 Command execution and port forwarding
- Step 1
1.Forwarder 192.168.63.5: ncat -l -k 8080 --sh-exec 'ncat 192.168.63.20 80'
- Step 2
2.Client: curl http://192.168.63.5:8080/
- Step 3
3.A new inner ncat starts for this connection
- Step 4
4.Inner ncat carries the request to 192.168.63.20 port 80
- Step 5
5.Reply 'hello from srv' comes back on the same path
- --sh-exec / -c: run a command through a shell for each connection.
- --exec / -e: run a program by its full path (e.g. /bin/date).
- --lua-exec: run a Lua script.
- The program gets NCAT_REMOTE_ADDR, NCAT_REMOTE_PORT, NCAT_LOCAL_ADDR, NCAT_LOCAL_PORT, NCAT_PROTO.
- Never expose --exec on an open network: it becomes a remote shell.
ncat -l -k 8080 --sh-exec 'ncat 192.168.63.20 80'curl -s http://192.168.63.5:8080/ncat -l -k 7000 --sh-exec 'echo peer $NCAT_REMOTE_ADDR:$NCAT_REMOTE_PORT local $NCAT_LOCAL_ADDR:$NCAT_LOCAL_PORT proto $NCAT_PROTO'ncat -l -k 7001 --exec /bin/date$ curl -s http://192.168.63.5:8080/hello from srv$ ncat 192.168.63.20 7000 </dev/nullpeer 192.168.63.10:55674 local 192.168.63.20:7000 proto TCP$ ncat 192.168.63.20 7001 </dev/nullSun Nov 8 18:09:39 UTC 202643.8 Access control
- --allow IP: only these hosts may connect; --deny IP: block these.
- --allowfile / --denyfile read the IP list from a file.
- A denied client still completes the TCP handshake (it sees Connected) but ncat closes it at once: 0 bytes received.
ncat -l -k -v 7100 --allow 192.168.63.10 -c 'echo welcome'echo 192.168.63.66 > /tmp/denied.txtncat -l -k 7101 --denyfile /tmp/denied.txt -c 'echo welcome'cli$ ncat 192.168.63.20 7100 </dev/nullwelcomeother$ ncat -v 192.168.63.20 7100 </dev/nullNcat: Connected to 192.168.63.20:7100.Ncat: 0 bytes sent, 0 bytes received in 0.17 seconds.srv$ cat /tmp/acl.txtNcat: Connection from 192.168.63.66:54904.Ncat: New connection denied: not allowed43.9 Timing, output and misc options
| Option | What it does |
|---|---|
| -w 2 | Connect timeout (TIMEOUT after 2 s for missing 192.168.63.99) |
| -i 2s | Idle timeout: Idle timeout expired (2000 ms) |
| -d 500ms | Delay between lines |
| -o file / -x file | Save session as text / as hex dump (0d 0a = CRLF visible) |
| --append-output | Append to the log instead of overwriting |
| -vv | libnsock debug details |
| -z / -n | Only check the port / no DNS |
| -C | CRLF line endings (HTTP, SMTP) |
| --send-only / --recv-only | One-way transfer (file copy) |
| --no-shutdown / -t | No half-close on EOF / answer Telnet negotiation |
$ time ncat -w 2 -v 192.168.63.99 80Ncat: TIMEOUT.real 0m 2.24s$ head -3 /tmp/s.hex0000 47 45 54 20 2f 20 48 54 54 50 2f 31 2e 30 0d 0a GET./.HTTP/1.0..0010 0d 0a ..0000 48 54 54 50 2f 31 2e 30 20 32 30 30 20 4f 4b 0d HTTP/1.0.200.OK.cli$ md5sum /etc/os-releasec17ab4d0cf771965682630394a9691e1 /etc/os-releasesrv$ md5sum /tmp/got.txtc17ab4d0cf771965682630394a9691e1 /tmp/got.txt43.10 Exit codes
| Exit code | Meaning | Lab example |
|---|---|---|
| 0 | Success | ncat -z ... 80 && echo open |
| 1 | Network error | Port 81 refused, 407 from proxy |
| 2 | Usage / fatal error | --bogus-option, SSL name mismatch |
In scripts treat only exit 0 as success.
43.11 Common mistakes
- Forgetting -k: the second client gets Connection refused because the listener already exited.
- Thinking --ssl means secure: it encrypts but accepts any certificate; use --ssl-verify with a trusted cert.
- Connecting by IP to a cert issued for a name: use --ssl-servername.
- Leaving --exec / --sh-exec listening on an open network.
43.12 Interview tips
- nc vs ncat: nc is simple and differs per OS; ncat (Nmap) is the same on all OS with SSL, proxy and ACL.
- --ssl encrypts, --ssl-verify also checks the certificate and name.
- ncat -l -k 8080 --sh-exec 'ncat host 80' is a one-line port forwarder.
- Exit codes: 0 success, 1 network error, 2 usage/fatal error.