Netcat
   HOME

TheInfoList



OR:

netcat (often abbreviated to nc) is a computer networking utility for reading from and writing to network connections using
TCP TCP may refer to: Science and technology * Transformer coupled plasma * Tool Center Point, see Robot end effector Computing * Transmission Control Protocol, a fundamental Internet standard * Telephony control protocol, a Bluetooth communication s ...
or UDP. The
command Command may refer to: Computing * Command (computing), a statement in a computer language * COMMAND.COM, the default operating system shell and command-line interpreter for DOS * Command key, a modifier key on Apple Macintosh computer keyboards ...
is designed to be a dependable back-end that can be used directly or easily driven by other programs and scripts. At the same time, it is a feature-rich network debugging and investigation tool, since it can produce almost any kind of connection its user could need and has a number of built-in capabilities. Its list of features includes port scanning, transferring files, and port listening: as with any server, it can be used as a backdoor.


Features

The original netcat's features include: * Outbound or inbound connections, TCP or UDP, to or from any ports * Full DNS forward/reverse checking, with appropriate warnings * Ability to use any local source port * Ability to use any locally configured network source address * Built-in port-scanning capabilities, with randomization * Built-in loose source-routing capability * Can read command line arguments from standard input * Slow-send mode, one line every N seconds * Hex dump of transmitted and received data * Optional ability to let another program service establish connections * Optional telnet-options responder Rewrites like GNU's and OpenBSD's support additional features. For example, OpenBSD's nc supports
TLS TLS may refer to: Computing * Transport Layer Security, a cryptographic protocol for secure computer network communication * Thread level speculation, an optimisation on multiprocessor CPUs * Thread-local storage, a mechanism for allocating vari ...
, and GNU netcat natively supports a tunneling mode supporting UDP and TCP (optionally allowing one to be tunneled over the other) in a single command, where other versions may require piping data from one netcat instance to another.


Examples


Opening a raw connection to port 25

nc mail.server.net 25


Performing an HTTP request

printf "GET /index.html HTTP/1.0\r\nHost: info.cern.ch\r\n\r\n" , nc info.cern.ch 80 The full response (including HTTP headers) will be dumped to standard output.


Setting up a one-shot webserver on port 8080 to present the content of a file

, nc -l 8080 The file can then be accessed via a web browser under http://servername:8080/. Netcat only serves the file once to the first client that connects and then exits; it also provides the content length for browsers that expect it. (This should work fine in a LAN, but may potentially fail with any kind of firewall between.) In some versions of netcat like netcat-traditional in Debian 8.6, you need to specify -p before the port number.


Checking whether UDP ports (-u) 80–90 are open on 192.168.0.1 using zero mode I/O (-z)

nc -vzu 192.168.0.1 80-90 Note that UDP tests will always show as "open".


Test whether UDP port is open: simple UDP server and client

This test is useful, if you have shell access to the server that should be tested, but you do not know whether there is a firewall blocking a specific UDP port on the server. On the listening host, i.e. on the server whose port needs to be checked, do the following: nc -l -u -p 4172 On the sending host, do the following – note that servname is the hostname of the listening host: nc -u servname 4172 If text typed on the sending host (type something and hit enter) is displayed also on the listening host, then the UDP port 4172 is open. If it is not open, you will get an error such as "Connection refused". There is a caveat. On some machines,
IPv6 Internet Protocol version 6 (IPv6) is the most recent version of the Internet Protocol (IP), the communications protocol that provides an identification and location system for computers on networks and routes traffic across the Internet. I ...
may be the default IP version to use by netcat. Thus, the host specified by the hostname is contacted using IPv6, and the user might not know about this. Ports may appear closed in the test, even though they would be open when using
IPv4 Internet Protocol version 4 (IPv4) is the fourth version of the Internet Protocol (IP). It is one of the core protocols of standards-based internetworking methods in the Internet and other packet-switched networks. IPv4 was the first version d ...
. This can be difficult to notice and may cause the false impression that the port is blocked, while it is actually open. You can force the use of IPv4 by adding -4 to the options of the nc commands.


Pipe via UDP (-u) with a wait time (-w) of 1 second to "loggerhost" on port 514

echo '<0>message' , nc -w 1 -u loggerhost 514


Port scanning

An uncommon use of netcat is port scanning. Netcat is not considered the best tool for this job, but it can be sufficient (a more advanced tool is nmap) nc -v -n -z -w 1 192.168.1.2 1-1000 The -n parameter here prevents DNS lookup, -z makes nc not receive any data from the server, and -w 1 makes the connection timeout after 1 second of inactivity.


Proxying

Another useful behaviour is using netcat as a proxy. Both ports and hosts can be redirected. Look at this example: nc -l 12345 , nc www.google.com 80 Port 12345 represents the request. This starts a nc server on port 12345 and all the connections get redirected to google.com:80. If a web browser makes a request to nc, the request will be sent to google but the response will not be sent to the web browser. That is because
pipes Pipe(s), PIPE(S) or piping may refer to: Objects * Pipe (fluid conveyance), a hollow cylinder following certain dimension rules ** Piping, the use of pipes in industry * Smoking pipe ** Tobacco pipe * Half-pipe and quarter pipe, semi-circul ...
are unidirectional. This can be worked around with a named pipe to redirect the input and output. $ mkfifo backpipe $ nc -l 12345 0backpipe The -c option may also be used with the ncat implementation: $ ncat -l 12345 -c 'nc www.google.com 80' Using a named pipe is a more reliable method because using -c option provides only a one-shot proxy. Another useful feature is to proxy SSL connections. This way, the traffic can not be viewed in wire sniffing applications such as wireshark. This can be accomplished on UNIXes by utilizing mkfifo, netcat, and openssl. mkfifo tmp mkfifo tmp2 nc -l 8080 -k > tmp < tmp2 & while true; do openssl s_client -connect www.google.com:443 -quiet < tmp > tmp2 done


Making any process a server

netcat can be used to make any process a network server. It can listen on a port and pipe the input it receives to that process. The -e option spawns the executable with its input and output redirected via network socket. For example, it is possible to expose a bourne shell process to remote computers. To do so, on a computer A with IP address 192.168.1.2, run this command: $ nc -l -p 1234 -e /bin/sh Then, from any other computer on the same network, one could run this nc command: $ nc 192.168.1.2 1234 ls -la total 4288 drwxr-xr-x 15 dummy users 4096 2009-02-17 07:47 . drwxr-xr-x 4 dummy users 4096 2009-01-18 21:22 .. -rw------- 1 dummy users 8192 2009-02-16 19:30 .bash_history -rw-r--r-- 1 dummy users 220 2009-01-18 21:04 .bash_logout ... In this way, the -e option can be used to create a rudimentary backdoor. Some administrators perceive this as a risk and thus do not allow netcat on a computer. The version of netcat developed by OpenBSD that is often installed by default on distributions such as Debian and Ubuntu lacks this option due to this potential as a security risk.


ncat

Ncat is a similar tool to netcat provided by Nmap suite. ''"While Ncat isn't built on any code from the “traditional” Netcat (or any other implementation), Ncat is most definitely based on Netcat in spirit and functionality."'' Ncat features includes: ability to chain Ncats together, redirect both TCP and UDP ports to other sites, SSL support, and proxy connections via SOCKS4 or HTTP (CONNECT method) proxies (with optional proxy authentication as well). * Connect to example.org on TCP port 8080. : ncat example.org 8080 * Listen for connections on TCP port 8080. : ncat -l 8080 * Redirect TCP port 8080 on the local machine to host on port 80. : * Bind to TCP port 8081 and attach /bin/bash for the world to access freely. : * Bind a shell to TCP port 8081, limit access to hosts on a local network, and limit the maximum number of simultaneous connections to 3: *: * Connect to smtphost:25 through a SOCKS4 server on port 1080: ncat --proxy socks4host --proxy-type socks4 --proxy-auth user smtphost 25 * Create an HTTP proxy server on localhost port 8888. : ncat -l --proxy-type http localhost 8888 * Send a file over TCP port 9899 from host2 (client) to host1 (server). user@HOST1$ ncat -l 9899 > outputfile user@HOST2$ ncat HOST1 9899 < inputfile * Transfer in the other direction, turning Ncat into a “one file” server. user@HOST1$ ncat -l 9899 < inputfile user@HOST2$ ncat HOST1 9899 > outputfile


Encrypted file transfer

Suppose you have an SSH tunnel, and you want to copy a file to the remote machine. You could just scp it directly, but that opens up another connection. The goal is to re-use the existing connection. You can use ncat to do this: When you SSH in, add in -L 31000:127.0.0.1:31000 (this is port forwarding, sending everything from port 31000 on the remote machine to the same port on the local machine) * On the remote: ncat -lvnp 31000 127.0.0.1 > file * On the local: ncat -v -w 2 127.0.0.1 31000 < file No extra overhead. TCP takes care of error correction. SSH has already encrypted the pipe.


Ports and reimplementations

The original version of netcat was a
Unix Unix (; trademarked as UNIX) is a family of multitasking, multiuser computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...
program. The last version (1.10) was released in March 1996. There are several implementations on
POSIX The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines both the system- and user-level application programming inte ...
systems, including rewrites from scratch like GNU netcat or
OpenBSD OpenBSD is a security-focused operating system, security-focused, free and open-source, Unix-like operating system based on the Berkeley Software Distribution (BSD). Theo de Raadt created OpenBSD in 1995 by fork (software development), forking N ...
netcat, the latter of which supports IPv6 and
TLS TLS may refer to: Computing * Transport Layer Security, a cryptographic protocol for secure computer network communication * Thread level speculation, an optimisation on multiprocessor CPUs * Thread-local storage, a mechanism for allocating vari ...
. The OpenBSD version has been ported to the
FreeBSD FreeBSD is a free and open-source Unix-like operating system descended from the Berkeley Software Distribution (BSD), which was based on Research Unix. The first version of FreeBSD was released in 1993. In 2005, FreeBSD was the most popular ...
base, Windows/Cygwin, and Linux.
Mac OS X macOS (; previously OS X and originally Mac OS X) is a Unix operating system developed and marketed by Apple Inc. since 2001. It is the primary operating system for Apple's Mac computers. Within the market of desktop and lapt ...
comes with netcat installed as of OSX 10.13 or users can use MacPorts to install a variant. A DOS version of ''netcat'' called ''NTOOL'' is included in the
FreeDOS FreeDOS (formerly Free-DOS and PD-DOS) is a free software operating system for IBM PC compatible computers. It intends to provide a complete MS-DOS-compatible environment for running legacy software and supporting embedded systems. FreeDOS ca ...
Package group ''Networking''. It is based on the WatTCP stack and licensed under the European Union Public Licence Version 1.1. Known ports for embedded systems includes versions for
Windows CE Windows Embedded Compact, formerly Windows Embedded CE, Windows Powered and Windows CE, is an operating system subfamily developed by Microsoft as part of its Windows Embedded family of products. Unlike Windows Embedded Standard, which is ba ...
(named "Netcat 4 wince") or for the iPhone.
BusyBox BusyBox is a software suite that provides several Unix utilities in a single executable file. It runs in a variety of POSIX environments such as Linux, Android, and FreeBSD, although many of the tools it provides are designed to work with int ...
includes by default a lightweight version of netcat. Solaris 11 includes netcat implementation based on OpenBSD netcat. Socat is a more complex variant of ''netcat''. It is larger and more flexible and has more options that must be configured for a given task. On February 1, 2016, Santiago Zanella-Beguelin and Microsoft Vulnerability Research issued a security advisory regarding a composite Diffie-Hellman parameter which had been hard-coded into the
OpenSSL OpenSSL is a software library for applications that provide secure communications over computer networks against eavesdropping or need to identify the party at the other end. It is widely used by Internet servers, including the majority of HT ...
implementation of ''socat''. The implausibility that a composite might have been unintentionally introduced where a
prime number A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. A natural number greater than 1 that is not prime is called a composite number. For example, 5 is prime because the only way ...
is required has led to the suspicion of
sabotage Sabotage is a deliberate action aimed at weakening a polity, effort, or organization through subversion, obstruction, disruption, or destruction. One who engages in sabotage is a ''saboteur''. Saboteurs typically try to conceal their identiti ...
to introduce a backdoor software vulnerability. This socat bug affected version 1.7.3.0 and 2.0.0-b8 it was corrected in following releases from 1.7.3.1 and 2.0.0-b9. Cryptcat is a version of ''netcat'' with integrated transport encryption capabilities. In the middle of 2005, Nmap announced another netcat incarnation called Ncat. It features new possibilities such as "Connection Brokering", TCP/UDP Redirection, SOCKS4 client and server support, ability to "Chain" Ncat processes, HTTP CONNECT proxying (and proxy chaining), SSL connect/listen support and IP address/connection filtering. Like Nmap, Ncat is
cross-platform In computing, cross-platform software (also called multi-platform software, platform-agnostic software, or platform-independent software) is computer software that is designed to work in several computing platforms. Some cross-platform software ...
. On some systems, modified versions or similar netcat utilities go by the command name(s) nc, ncat, pnetcat, socat, sock, socket, sbd.


See also

*
List of Unix commands This is a list of Unix commands as specified by IEEE Std 1003.1-2008, which is part of the Single UNIX Specification (SUS). These commands can be found on Unix operating systems and most Unix-like operating systems. List See also * List of G ...
* Nmap *
OpenSSL OpenSSL is a software library for applications that provide secure communications over computer networks against eavesdropping or need to identify the party at the other end. It is widely used by Internet servers, including the majority of HT ...
* Telnet * Plink *
Packet Sender is an open source utility to allow sending and receiving TCP and UDP packets. It also supports TCP connections using SSL, intense traffic generation, HTTP(S) GET/POST requests, and panel generation. It is available for Windows, Mac, and Linux. ...
*
Banner grabbing Banner grabbing is a technique used to gain information about a computer system on a network and the services running on its open ports. Administrators can use this to take inventory of the systems and services on their network. However, an intruder ...


References


External links

* * {{Unix commands Free network management software Network analyzers Port scanners Unix network-related software Windows network-related software Free software programmed in C