NcatNcat: Your General-Purpose Network Connector
Ncat is a general-purpose command-line tool for reading, writing,
redirecting, and encrypting data across a network. It aims to be
your network Swiss Army
knife, handling a wide variety of security testing and
administration tasks. Ncat is suitable for interactive use or as a
network-connected back end for other tools. Ncat can:
Act as a simple TCP/UDP/SCTP/SSL client for interacting
with web servers, telnet servers, mail servers, and other TCP/IP
network services. Often the best way to understand a service (for
fixing problems, finding security flaws, or testing custom commands)
is to interact with it using Ncat. This lets you you control every character
sent and view the raw, unfiltered responses.Act as a simple TCP/UDP/SCTP/SSL server for offering
services to clients, or simply to understand what existing clients
are up to by capturing every byte they send.Redirect or proxy TCP/UDP/SCTP traffic to other ports or
hosts. This can be done using simple redirection (everything sent
to a port is automatically relayed somewhere else you specify in
advance) or by acting as a SOCKS or HTTP proxy so clients
specify their own destinations. In client mode, Ncat can
connect to destinations through a chain of anonymous or
authenticated proxies.Run on all major operating systems. We distribute
Linux, Windows, and Mac OS X binaries, and Ncat compiles on most
other systems. A trusted tool must be available
whenever you need it, no matter what computer you're
using.Encrypt communication with SSL, and transport it over IPv4 or IPv6.Act as a network gateway for execution of system
commands, with I/O redirected to the network. It was designed to work
like the Unix utility cat, but for the
network.Act as a connection broker, allowing two (or far
more) clients to connect to each other through a third (brokering)
server. This enables multiple machines hidden behind NAT gateways
to communicate with each other, and also enables the simple Ncat
chat mode.These capabilities become even more powerful and versatile
when combined.
Ncat is our modern reinvention of the venerable Netcat (nc) tool released by Hobbit in 1996. While Ncat is similar to Netcat in spirit, they don't share any source code. Instead, Ncat makes use of Nmap's well optimized and tested networking
libraries. Compatibility with the original Netcat and some well known variants is maintained
where it doesn't conflict with Ncat's enhancements or cause
usability problems. Ncat adds many capabilities not found in
Hobbit's original nc, including SSL support, proxy connections,
IPv6, and connection brokering.
The original nc contained a simple port
scanner, but we omitted that from Ncat because we have a preferred
tool for that function.
This guide starts with examples of basic Ncat usage, then moves on to more advanced features. Those are followed by practical sections which use examples to demonstrate how Ncat can solve common real-world problems. A few neat Ncat tricks are covered as well.
Basic usage
Ncat always operates in one of two basic modes:
connect modeconnect mode and
listen mode.listen mode
In connect mode, Ncat initiates a connection (or sends UDP data) to a service that is
listening somewhere. For those familiar with socket programming,
connect mode is like using the connect function.
In listen mode, Ncat waits for an incoming connection (or data receipt), like using the
bind and listen functions.
You can think of connect mode as client mode and listen
mode as server mode.
To use Ncat in connect mode, run
ncat hostporthost may be a hostname or IP
address, and
port is a port number. Listen mode is the
same, with the addition of the
(Ncat option> option (or
its (Ncat
option)
alias):
ncat --listen hostportncat -l hostport
In listen mode, host controls the address
on which Ncat listens; if you omit it, Ncat will bind to all local interfaces (INADDR_ANY). If the port number is omitted, Ncat uses its
default port
31337.default port of Ncat
Typically only privileged
(root)privileged users
users may bind to a port number lower than
1024.portsreservedreserved ports
A listening TCP server normally accepts only one connection and will
exit after the client disconnects. Combined with the
(Ncat option)
option, Ncat accepts multiple concurrent connections up
to the connection limit. With (or
for short), the server receives everything sent by
any of its clients, and anything the server sends is sent to all of
them.
By default, Ncat uses TCP. The option
(Ncat option)
or
(Ncat option)
enables UDP instead,
and
(Ncat option)
enables SCTP.SCTPin Ncat
Ncat listens on both IPv4 and IPv6, and connects to either address family as well. The
(Ncat option)
option forces IPv6-only, and
(Ncat option)
forces IPv4-only. See for more details.
The rest of this guide documents all the Ncat options through
descriptions and examples. For a quick summary of options at any time,
run
ncat --help (Ncat option)
or man ncat.
A Connect Mode Example
A good way to start learning about Ncat (and network protocols in
general) is to connect to a network service and talk with it. In
this case we use Ncat to manually retrieve a web page from an HTTP
server, just as web browsers do in the background when you visit a
web site.
shows a (truncated) sample session. Try it yourself!
Text in bold is what you type; everything else is what comes
back. The blank line after the GET line is
required—just hit enter twice.
Ncat as a web browserGET HTTP method (Ncat option)example of
$ ncat -C scanme.nmap.org 80GET / HTTP/1.0
HTTP/1.1 200 OK
Date: Thu, 05 Feb 2009 15:31:40 GMT
Server: Apache/2.2.2 (Fedora)
Last-Modified: Mon, 19 May 2008 04:49:49 GMT
ETag: "fc8c91-2e3-44d8e17edd540"
Accept-Ranges: bytes
Content-Length: 739
Connection: close
Content-Type: text/html; charset=UTF-8
<html>
<head>
<title>Go ahead and ScanMe!</title>
</head>
Here we have instructed Ncat to connect to the host
scanme.nmap.orgscanme.nmap.org
on port 80, the port for HTTP. The option turns
on CRLFCRLF replacement,
which replaces any line endings you type with CRLF. CRLF line
endings are required by many protocols, including HTTP, though many servers will accept a plain newline (LF) character.
GET / HTTP/1.0 requests the root document of
the server; we are retrieving the same document named by the URL
http://scanme.nmap.org:80/. The web server responds with a status code
(HTTP/1.1 200 OK), followed by the
HTTP header and the text of the web page. If you try this with other
web servers, note that many of them are actually virtual hosts and you
will need to send the Host header field. See
RFC 2616RFC 2616 for
more information about HTTP.
A Listen Mode Example
So much for using Ncat as a web browser. What about a web server?
That's possible too; it just takes a bit of preparation. The first
step is to create the document to serve. Create a text file called
hello.http with these contents: