TExcelTAD

Netcat (Ncat)

Netcat reads and writes raw bytes over TCP or UDP connections. That is all it does, and it is enough to make it the most versatile network tool there is: it can talk to any text protocol by hand, check whether a port is open, grab a service banner, move a file between two machines, or stand in for a server while you test a client. Several implementations exist (the original nc, OpenBSD nc, and Nmap's Ncat, used here); their options differ slightly, so check --help on the system in front of you. This lesson uses Ncat in the sandbox, always on 127.0.0.1, and ends with why defenders treat an unexpected netcat on a server as a red flag.

Talking to a service by hand

The session starts the Python web server on loopback and sends it a raw HTTP request with Ncat. This is banner grabbing: the server's headers reveal its software and version.

bash · try it yourself
python3 -m http.server 8000 --bind 127.0.0.1 >http.log 2>&1 &
for i in $(seq 50); do ss -Htln 'sport = :8000' | grep -q . && break; sleep 0.1; done
printf 'HEAD / HTTP/1.0\r\n\r\n' | ncat 127.0.0.1 8000 | tr -d '\r' | sed -E 's/^Date: .*/Date: <DATE>/'
echo "--- is a port open? (-z: connect, send nothing)"
for p in 8000 8001; do ncat -z 127.0.0.1 $p && echo "$p open" || echo "$p closed"; done
kill %1
HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.12.3
Date: <DATE>
Content-type: text/html; charset=utf-8
Content-Length: 398

--- is a port open? (-z: connect, send nothing)
8000 open
8001 closed

Server: SimpleHTTP/0.6 Python/3.12.3 is exactly the information Nmap's version detection used in the previous lesson, obtained with one line. The same works for SMTP (220 … banner), FTP, SSH (SSH-2.0-…), Redis (PING) and many others.

Moving data

A listener on one side, a client on the other, and anything can flow between them. The session sends a file over a local TCP connection and checks that it arrived intact, then sends a syslog-style line over UDP.

bash · try it yourself
cp $SAMPLES/files/hello.txt .
ncat -l 127.0.0.1 9100 > received.txt &
for i in $(seq 50); do ss -Htln 'sport = :9100' | grep -q . && break; sleep 0.1; done
ncat --send-only 127.0.0.1 9100 < hello.txt
sleep 0.2
sha256sum hello.txt received.txt | cut -c1-16
echo "--- UDP:"
ncat -lu 127.0.0.1 5514 > udp.txt &
sleep 0.3
echo "<14>lab-host: hello over UDP" | ncat -u --send-only 127.0.0.1 5514
sleep 0.3; cat udp.txt
kill %1 2>/dev/null
f751a306e3c158ae
f751a306e3c158ae
--- UDP:
<14>lab-host: hello over UDP

Identical hashes: the file came through unchanged. Nothing in plain netcat protects it, though: anyone on the path could read or alter it. That is what the next option is for.

TLS, and why verification matters

Ncat can wrap the connection in TLS (--ssl). The session starts a TLS listener with the lab certificate, which is valid for lab.test and www.lab.test, and connects to it by IP address twice: once with certificate verification, once without.

bash · try it yourself
ncat -l --ssl --ssl-cert $SAMPLES/pki/lab.test.crt --ssl-key $SAMPLES/pki/lab.test.key 127.0.0.1 9443 >tls1.txt 2>/dev/null &
for i in $(seq 50); do ss -Htln 'sport = :9443' | grep -q . && break; sleep 0.1; done
echo "verified" | ncat --ssl --ssl-verify --ssl-trustfile $SAMPLES/pki/ca.crt --send-only 127.0.0.1 9443 2>&1
echo "exit code: $?"
wait %1 2>/dev/null
ncat -l --ssl --ssl-cert $SAMPLES/pki/lab.test.crt --ssl-key $SAMPLES/pki/lab.test.key 127.0.0.1 9443 >tls2.txt 2>/dev/null &
for i in $(seq 50); do ss -Htln 'sport = :9443' | grep -q . && break; sleep 0.1; done
echo "not verified" | ncat --ssl --send-only 127.0.0.1 9443 2>&1
sleep 0.3
echo "listener received: $(cat tls2.txt)"
kill %1 2>/dev/null
Ncat: Certificate verification error. QUITTING.
exit code: 2
listener received: not verified

The verified connection fails, correctly: the certificate was issued by the trusted lab CA but for lab.test, not for 127.0.0.1, so it doesn't prove the identity of what you connected to. The unverified connection succeeds and is encrypted, but a man-in-the-middle could have presented any certificate. Encryption without verification protects only against passive eavesdroppers. The OpenSSL lesson shows how to connect with the right name.

Netcat in incidents

The features that make netcat useful also make it a favourite of intruders: it can relay connections, transfer stolen data, and attach a shell to a network socket (a "bind shell" or "reverse shell"). This course doesn't show those commands; what matters for a defender is recognising them:

  • Detection: a netcat binary (nc, ncat, netcat, renamed copies) on servers where nobody installed it; a process with a network socket whose child is a shell (bash, sh, cmd.exe, powershell.exe), which EDR tools and Sysmon (process creation with parent) flag; outbound connections from servers to unusual ports on the Internet; listeners on high ports that aren't in the baseline (Ports, Services and the Attack Surface).
  • Prevention: don't install netcat on production servers; application allow-listing; egress filtering so servers can only reach the destinations they need (most reverse shells need an outbound connection); least privilege, so a shell obtained through a vulnerable service has little power.

Pitfalls

  • Different netcats, different flags: -e exists in some builds and not others, -q/-N control closing behaviour on OpenBSD nc, Ncat uses --send-only. Scripts break silently when the implementation changes.
  • Forgetting that a netcat listener accepts anyone who can reach the port. Bind to 127.0.0.1 for local tests; Ncat's --allow restricts sources.
  • Using netcat for transfers of sensitive data without TLS, or with TLS but without --ssl-verify.

Practice

1. How would you check by hand whether a lab SMTP server at 10.10.0.25 offers STARTTLS?

ncat 10.10.0.25 25, wait for the 220 banner, type EHLO lab.test and look for a 250-STARTTLS line, then QUIT. (Within your scope only.) openssl s_client -starttls smtp then tests the TLS part, as in the mail lesson.

2. Why did the verified TLS connection fail although the CA was trusted?

Verification checks two things: that the chain leads to a trusted CA, and that the certificate's names match the host you asked for. The chain was fine; the name 127.0.0.1 isn't in the certificate's subjectAltName.

3. Which single network control defeats most reverse shells, and why?

Egress filtering: a reverse shell is an outbound connection from the compromised host to the attacker. If servers may only connect to known update servers, databases and the like, the connection never leaves.

Test yourself

What does printf 'HEAD / HTTP/1.0\r\n\r\n' | ncat host 80 do?

  1. Scans all ports
  2. Sends a raw HTTP request and prints the server's response headers (banner grabbing)
  3. Starts a web server
  4. Downloads the whole site

Why did ncat --ssl --ssl-verify to 127.0.0.1 fail?

  1. TLS is disabled
  2. The certificate is for lab.test, not for 127.0.0.1
  3. The CA is expired
  4. Ncat can't use TLS on loopback

Which observation on a server most strongly suggests a netcat-based shell?

  1. A cron job
  2. A process with a network socket that has spawned a shell as its child
  3. A DNS query
  4. A TLS handshake to an update server