SkillPilotAI

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

PlatformHow to get it
WindowsNmap installer or winget install Insecure.Nmap
Linuxapt or dnf: ncat / nmap-ncat
macOSbrew install nmap (ncat included)
Syntaxncat [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.
Terminal
# 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
Output
$ (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

OptionWhat it does
-uUDP instead of TCP
-UUnix domain socket (file path instead of IP)
-4 / -6Force IPv4 / IPv6
--sctpSCTP
--vsockBetween a VM and its host (CID 2 = host)
-s IP / -p portFixed source address / source port
-g / -GIPv4 loose source routing (most routers drop it)
Terminal
# 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 8082
Output
srv$ 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 port

SCTP 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.
Terminal
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 9001
Output
srv$ 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 other

43.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.
TLS check with --ssl-verify (cert CN = srv.lab)
  1. 1.Server: ncat -l -k --ssl --ssl-cert cert.pem --ssl-key key.pem 8444

  2. 2.Client connects by IP 192.168.63.20 with --ssl-verify --ssl-trustfile ca.pem

  3. 3.Cert is trusted but the name (IP) does not match CN srv.lab

  4. 4.Certificate verification error. QUITTING. [exit 2]

  5. 5.Add --ssl-servername srv.lab: name matches

  6. 6.SSL connection up, 'verified hello' received [exit 0]

Terminal
# 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 8444
Output
srv$ 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.
Terminal
# 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 80
Output
HTTP/1.0 200 OK hello from srvNcat: Proxy returned status code 407.[exit 1]HTTP/1.0 200 OK hello from srv

43.7 Command execution and port forwarding

One-line port forward with --sh-exec
  1. 1.Forwarder 192.168.63.5: ncat -l -k 8080 --sh-exec 'ncat 192.168.63.20 80'

  2. 2.Client: curl http://192.168.63.5:8080/

  3. 3.A new inner ncat starts for this connection

  4. 4.Inner ncat carries the request to 192.168.63.20 port 80

  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.
Terminal
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
Output
$ 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 2026

43.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.
Terminal
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'
Output
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 allowed

43.9 Timing, output and misc options

OptionWhat it does
-w 2Connect timeout (TIMEOUT after 2 s for missing 192.168.63.99)
-i 2sIdle timeout: Idle timeout expired (2000 ms)
-d 500msDelay between lines
-o file / -x fileSave session as text / as hex dump (0d 0a = CRLF visible)
--append-outputAppend to the log instead of overwriting
-vvlibnsock debug details
-z / -nOnly check the port / no DNS
-CCRLF line endings (HTTP, SMTP)
--send-only / --recv-onlyOne-way transfer (file copy)
--no-shutdown / -tNo half-close on EOF / answer Telnet negotiation
Output
$ 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.txt

43.10 Exit codes

Exit codeMeaningLab example
0Successncat -z ... 80 && echo open
1Network errorPort 81 refused, 407 from proxy
2Usage / 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.