'\" te .\" Copyright (c) 1996 David Sacerdote. All rights reserved. .\" Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. .\" Portions Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved. .TH nc 1 "6 Jun 2012" "SunOS 5.11" "User Commands" .SH NAME nc, netcat \- arbitrary TCP and UDP connections and listens .SH SYNOPSIS .LP .nf \fBnc\fR \fB-h\fR .fi .LP .nf \fBnc\fR [\fB-46dnrtuvz\fR] [\fB-i\fR \fIinterval\fR] [\fB-P\fR \fIproxy_username\fR] [\fB-p\fR \fIport\fR] [\fB-s\fR \fIsource_ip_address\fR] [\fB-T\fR \fIdspc\fR] [\fB-w\fR \fItimeout\fR] [\fB-X\fR \fIproxy_protocol\fR] [\fB-x\fR \fIproxy_address\fR[:\fIport\fR]][\fB-L\fR \fItimeout\fR] [\fB-e\fR \fIprogram\fR] [\fB-b\fR \fIbufsize\fR] [\fB-q\fR \fItimeout\fR] [\fB-m\fR \fIbytes\fR] [\fB-I\fR \fIbufsize\fR][\fB-O\fR \fIbufsize\fR] [\fB-S\fR \fIsla-prop\fR] \fIhostname\fR \fIport_list\fR .fi .LP .nf \fBnc\fR \fB-l\fR [\fB-46DdEFnrtuvzZ\fR] [\fB-i\fR \fIinterval\fR] [\fB-T\fR \fIdspc\fR] [\fB-e\fR \fIprogram\fR] [\fB-b\fR bufsize] [\fB-q\fR \fItimeout\fR] [\fB-R\fR \fIaddress\fR/\fIport\fR[/\fIproto\fR]] [\fB-m\fR \fIbytes\fR] [\fB-L\fR \fItimeout\fR] [\fB-I\fR \fIbufsize\fR] [\fB-O\fR \fIbufsize\fR] [\fB-S\fR \fIsla-prop\fR] [\fIhostname\fR] \fIport\fR .fi .LP .nf \fBnc\fR \fB-l\fR [\fB-46DEFdnrtuvzZ\fR] [\fB-i\fR \fIinterval\fR] [\fB-T\fR \fIdspc\fR] [\fB-e\fR \fIprogram\fR] [\fB-b\fR \fIbufsize\fR] [\fB-q\fR \fItimeout\fR] [\fB-R\fR \fIaddress\fR/\fIport\fR[/\fIproto\fR]] [\fB-m\fR \fIbytes\fR] [\fB-L\fR \fItimeout\fR] [\fB-I\fR \fIbufsize\fR] [\fB-O\fR \fIbufsize\fR] [\fB-S\fR \fIsla-prop\fR] \fB-p\fR \fIport\fR [\fIhostname\fR] .fi .LP .nf \fBnc\fR \fB-U\fR [\fB-Ddtvz\fR] [\fB-i\fR \fIinterval\fR] [\fB-w\fR \fItimeout\fR] [\fB-e\fR \fIprogram\fR] [\fB-b\fR \fIbufsize\fR] [\fB-q\fR \fItimeout\fR] [\fB-m\fR \fIbytes\fR] \fIpath\fR .fi .LP .nf \fBnc\fR \fB-Ul\fR [\fB-46DdktvZ\fR] [\fB-i\fR \fIinterval\fR] [\fB-e\fR \fIprogram\fR] [\fB-b\fR \fIbufsize\fR] [\fB-q\fR \fItimeout\fR] [\fB-R\fR \fIaddress\fR/\fIport\fR[/\fIproto\fR]] [\fB-m\fR \fIbytes\fR] \fIpath\fR .fi .SH DESCRIPTION .sp .LP The \fBnc\fR (or \fBnetcat\fR) utility is used for a variety of tasks associated with TCP or UDP. \fBnc\fR can open TCP connections, send UDP packets, listen on arbitrary TCP and UDP ports, perform port scanning, and deal with both IPv4 and IPv6. Unlike \fBtelnet\fR(1), \fBnc\fR scripts nicely, and separates error messages onto standard error instead of sending them to standard output. .sp .LP The \fBnc\fR command is often used for the following tasks: .RS +4 .TP .ie t \(bu .el o simple TCP proxies .RE .RS +4 .TP .ie t \(bu .el o shell-script based HTTP clients and servers .RE .RS +4 .TP .ie t \(bu .el o network daemon testing .RE .RS +4 .TP .ie t \(bu .el o a SOCKS or HTTP \fBProxyCommand\fR for \fBssh\fR(1) .RE .sp .LP The \fBnc\fR command can also be run as \fBnetcat\fR, using the identical options. .SH OPTIONS .sp .LP The following options are supported: .sp .ne 2 .mk .na \fB\fB-4\fR\fR .ad .sp .6 .RS 4n Force \fBnc\fR to use IPv4 addresses only. .RE .sp .ne 2 .mk .na \fB\fB-6\fR\fR .ad .sp .6 .RS 4n Force \fBnc\fR to use IPv6 addresses only. .RE .sp .ne 2 .mk .na \fB\fB-b\fR \fIbufsize\fR\fR .ad .sp .6 .RS 4n Specify buffer size for read operations. .sp The default value is \fB1024\fR bytes. .RE .sp .ne 2 .mk .na \fB\fB-D\fR\fR .ad .sp .6 .RS 4n Enable debugging on the socket. .RE .sp .ne 2 .mk .na \fB\fB-d\fR\fR .ad .sp .6 .RS 4n Do not attempt to read from \fBstdin\fR. .RE .sp .ne 2 .mk .na \fB\fB-E\fR\fR .ad .sp .6 .RS 4n Use exclusive bind for listening TCP or UDP socket. .sp It is an error to use this option without the \fB-l\fR option. .sp This option does not have any effect when used in conjunction with the \fB-U\fR option. .RE .sp .ne 2 .mk .na \fB\fB-e\fR \fIprogram\fR\fR .ad .sp .6 .RS 4n Execute external program after accepting a connection or making connection. Before the execution \fBstdin,stdout,stderr\fR is redirected to the network descriptor. Only one port can be used with this option. .sp It is an error to use this option in conjunction with the \fB-R\fR, \fB-k\fR, or \fB-i\fR options. .RE .sp .ne 2 .mk .na \fB\fB-F\fR\fR .ad .sp .6 .RS 4n Do not close network socket for writing after seeing \fBEOF\fR on \fBstdin\fR. .RE .sp .ne 2 .mk .na \fB\fB-h\fR\fR .ad .sp .6 .RS 4n Print \fBnc\fR help. .RE .sp .ne 2 .mk .na \fB\fB-I\fR \fIbufsize\fR\fR .ad .sp .6 .RS 4n Set receive (input) socket buffer size. .sp This option does not have any effect when used in conjunction with the \fB-U\fR option. .RE .sp .ne 2 .mk .na \fB\fB-i\fR \fIinterval\fR\fR .ad .sp .6 .RS 4n Specify a delay time of \fIinterval\fR between lines of text sent and received. .sp The interval is specified in seconds, with possible fractions. .sp This option also causes a delay time between connections to multiple ports, and therefore also affects port scan mode. .RE .sp .ne 2 .mk .na \fB\fB-k\fR\fR .ad .sp .6 .RS 4n Force \fBnc\fR to listen for another connection after its current connection is closed. .sp It is an error to use this option without the \fB-l\fR option. .sp It is an error to use this option in conjunction with the \fB-e\fR option. .RE .sp .ne 2 .mk .na \fB\fB-L\fR \fItimeout\fR\fR .ad .sp .6 .RS 4n Linger on close - wait for messages to be sent after network descriptor is closed up to specified timeout in seconds. .RE .sp .ne 2 .mk .na \fB\fB-l\fR\fR .ad .sp .6 .RS 4n Listen for an incoming connection rather than initiate a connection to a remote host. .sp It is an error to use this option in conjunction with the \fB-s\fR or \fB-z\fR options. .sp If the \fB-l\fR option is used with a wildcard socket (no IP address or hostname specified) and without the \fB-4\fR /\fB-6\fR options, it accepts both IPv4 and IPv6 connections. .RE .sp .ne 2 .mk .na \fB\fB-m\fR \fIbyte_count\fR\fR .ad .sp .6 .RS 4n Quit after receiving at least \fBbyte_count\fR bytes. When used with \fB-l\fR option \fBbyte_count\fR is compared to number of bytes received from the client. .sp \fBbyte_count\fR must be greater than \fB0\fR and less than \fBINT_MAX\fR. .RE .sp .ne 2 .mk .na \fB\fB-N\fR \fIfile\fR\fR .ad .sp .6 .RS 4n Specifies file with pattern for UDP port scanning. The contents of this file are used as payload for each emitted UDP packet. .sp It is an error to use this option without the \fB-u\fR and \fB-z\fR options. .RE .sp .ne 2 .mk .na \fB\fB-n\fR\fR .ad .sp .6 .RS 4n Do not do any naming or service lookups on any addresses, hostnames, or ports. .sp Use of this option means that \fIhostname\fR and \fIport\fR arguments are restricted to numeric values. .sp If used with \fB-v\fR option all addresses and ports are printed in numeric form, in addition to the restriction imposed on the arguments. This option does not have any effect when used in conjunction with the \fB-U\fR option. .RE .sp .ne 2 .mk .na \fB\fB-O\fR \fIbufsize\fR\fR .ad .sp .6 .RS 4n Set send (output) socket buffer size. .sp This option does not have any effect when used in conjunction with the \fB-U\fR option. .RE .sp .ne 2 .mk .na \fB\fB-P\fR \fIproxy_username\fR\fR .ad .sp .6 .RS 4n Specify a username (\fIproxy_username\fR) to present to a proxy server that requires authentication. If \fIproxy_username\fR is not specified, authentication is not attempted. Proxy authentication is only supported for \fBHTTP CONNECT\fR proxies at present. .sp It is an error to use this option in conjunction with the \fB-l\fR option. .RE .sp .ne 2 .mk .na \fB\fB-p\fR \fIport\fR\fR .ad .sp .6 .RS 4n When used without \fB-l\fR option, specify the source port \fBnc\fR should use, subject to privilege restrictions and availability. When used with the \fB-l\fR option, set the listen port. .sp This option can be used with \fB-l\fR option only provided global port argument is not specified. .RE .sp .ne 2 .mk .na \fB\fB-q\fR \fItimeout\fR\fR .ad .sp .6 .RS 4n After receiving \fBEOF\fR on \fBstdin\fR, wait for specified number of seconds and quit. .RE .sp .ne 2 .mk .na \fB\fB-R\fR \fIaddr\fR/\fIport\fR[/\fIproto\fR]\fI\fR\fR .ad .sp .6 .RS 4n Perform port redirection to given \fIhost\fR and \fIport\fR. .sp After the connection has been accepted, \fBnc\fR connects to the remote \fIhost\fR/\fIport\fR and passes all data between the client and the remote host. The \fIproto\fR (protocol) part of the redirect specification can be either \fBtcp\fR or \fBudp\fR. If the \fIproto\fR is not specified, \fBredirector\fR uses the same protocol as the server. .sp It is an error to use this option in conjunction with the \fB-z\fR option. .RE .sp .ne 2 .mk .na \fB\fB-r\fR\fR .ad .sp .6 .RS 4n Choose destination ports randomly instead of sequentially within all ports specified by the \fIport_list\fRargument. .sp It is an error to use this option in conjunction with the \fB-l\fR option. .RE .sp .ne 2 .mk .na \fB\fB-s\fR \fIsource_ip_address\fR\fR .ad .sp .6 .RS 4n Specify the IP of the interface which is used to send the packets. .sp It is an error to use this option in conjunction with the \fB-l\fR option. .RE .sp .ne 2 .mk .na \fB\fB-S\fR \fIsla-prop\fR\fR .ad .sp .6 .RS 4n Specify properties of the MAC flow created for the socket. \fIsla-prop\fR is supplied as a comma-separated list of 'name=value' of properties. .sp Currently supported property names are \fBmaxbw\fR, \fBpriority\fR, and \fBinherit\fR. .sp \fBmaxbw\fR and \fBpriority\fR come from the properties defined in flowadm(1M) and denote maximum bandwidth and priority of the flow. Allowed values for \fBmaxbw\fR are integer plus optional suffix, which defaults to Mega. \fBpriority\fR can take values from 'high', 'medium' and 'low'. .sp At least one of \fBmaxbw\fR and \fBpriority\fR need to be specified for flow creation. .sp \fBinherit\fR can take values from 'on' and 'off' with default value of 'off'. By default, an accepted/new socket (returned from accept(3C)) does not inherit the properties of the listener socket. When it is set to 'on', the new socket will inherit the properties of the listener socket. This is useful with \fB-l\fR option when the properties need to be enforced on the new socket. .sp This option requires \fBSYS_FLOW_CONFIG\fR privilege. This option also requires the IP address or the hostname to be specified. .RE .sp .ne 2 .mk .na \fB\fB-T\fR \fIdscp\fR\fR .ad .sp .6 .RS 4n Specify Differentiated Services Code Point for the connection. .sp For IPv4 this specifies the IP Type of Service (ToS) IP header field and the valid values for the argument are the string tokens: \fBlowdelay\fR, \fBthroughput\fR, \fBreliability\fR, or an 8-bit hexadecimal value preceded by \fB0x\fR. .sp For IPv6 (Traffic Class) only hexadecimal value can be used. .RE .sp .ne 2 .mk .na \fB\fB-t\fR\fR .ad .sp .6 .RS 4n Cause \fBnc\fR to send \fIRFC 854\fR \fBDON'T\fR and \fBWON'T\fR responses to \fIRFC 854\fR \fBDO\fR and \fBWILL\fR requests. This makes it possible to use \fBnc\fR to script \fBtelnet\fR sessions. .RE .sp .ne 2 .mk .na \fB\fB-U\fR\fR .ad .sp .6 .RS 4n Specify the use of Unix Domain Sockets. If you specify this option without \fB-l\fR, \fBnc\fR, it becomes \fBAF_UNIX\fR client. If you specify this option with the \fB-l\fR option, a \fBAF_UNIX\fR server is created. .sp Use of this option requires that a single argument of a valid Unix domain path has to be provided to \fBnc\fR, not a host name or port. .RE .sp .ne 2 .mk .na \fB\fB-u\fR\fR .ad .sp .6 .RS 4n Use UDP instead of the default option of TCP. .RE .sp .ne 2 .mk .na \fB\fB-v\fR\fR .ad .sp .6 .RS 4n Specify verbose output. .RE .sp .ne 2 .mk .na \fB\fB-w\fR \fItimeout\fR\fR .ad .sp .6 .RS 4n Silently close the connection if a connection and \fBstdin\fR are idle for more than \fItimeout\fR seconds. .sp The default is no timeout. .sp This option has no effect on the connection establishment phase in client mode or waiting for a connection in server mode. .RE .sp .ne 2 .mk .na \fB\fB-X\fR \fIproxy_protocol\fR\fR .ad .sp .6 .RS 4n Use the specified protocol when talking to the proxy server. Supported protocols are \fB4\fR (\fBSOCKS v.4\fR), \fB5\fR (\fBSOCKS v.5\fR) and \fBconnect\fR (\fBHTTP\fR proxy). If the protocol is not specified, \fBSOCKS v. 5\fR is used. .sp It is an error to use this option in conjunction with the \fB-l\fR option. .RE .sp .ne 2 .mk .na \fB\fB-x\fR \fIproxy_address\fR[:\fIport\fR]\fR .ad .sp .6 .RS 4n Request connection to \fIhostname\fR using a proxy at \fIproxy_address\fR and \fIport\fR. If \fIport\fR is not specified, the well-known port for the proxy protocol is used (\fB1080\fR for \fBSOCKS\fR, \fB3128\fR for \fBHTTP\fR). .sp It is an error to use this option in conjunction with the \fB-l\fR option. .sp This option does not work with numeric representation of IPv6 addresses. .RE .sp .ne 2 .mk .na \fB\fB-Z\fR\fR .ad .sp .6 .RS 4n In listening mode bind to address/port in all zones using the \fBSO_ALLZONES\fR socket option. .sp This option requires \fBSYS_NET_CONFIG\fR privilege. .RE .sp .ne 2 .mk .na \fB\fB-z\fR\fR .ad .sp .6 .RS 4n Perform port scan. For TCP ports (default), connect scan (full 3-way handshake) is tried with no data sent. For UDP (\fB-u\fR) empty UDP packets are sent by default. To specify UDP payload the \fB-N\fR option can be used. .sp The UDP scan mode is estimative, it considers a port to be open if it does not receive negative response (ICMP Destination Port Unreachable message). For this mode the timeout set with the \fB-w\fR option is used to wait for the ICMP messages or data from remote node. With \fB-v\fR any received data is dumped as hexadecimal bytes to \fBstderr\fR. .sp As most of the operating systems employ rate limiting for sending ICMP messages in reaction to input packets, it is necessary to use \fB-i\fR when performing UDP scan otherwise the results is not reliable. .sp It is an error to use this option in conjunction with the \fB-l\fR option. .RE .SH OPERANDS .sp .LP The following operands are supported: .sp .ne 2 .mk .na \fB\fIhostname\fR\fR .ad .RS 13n .rt Specify host name. .sp \fIhostname\fR can be a numerical IP address or a symbolic hostname (unless the \fB-n\fR option is specified). .sp In general, \fIhostname\fR must be specified, unless the \fB-l\fR option is given or \fB-U\fR is used (in which case the argument is a path). If \fIhostname\fR argument is specified with \fB-l\fR option then \fIport\fR argument must be given as well and \fBnc\fR tries to bind to that address and port. If \fIhostname\fR argument is not specified with \fB-l\fR option then \fBnc\fR tries to listen on a wildcard socket for given \fIport\fR. .RE .sp .ne 2 .mk .na \fB\fIpath\fR\fR .ad .RS 13n .rt Specify pathname. .RE .sp .ne 2 .mk .na \fB\fIport\fR\fR .ad .br .na \fB\fIport_list\fR\fR .ad .RS 13n .rt Specify port. .sp \fIport_list\fR can be specified as single integers, ranges or combinations of both. Specify ranges in the form of \fInn-mm\fR. The \fIport_list\fR must have at least one member, but can have multiple ports/ranges separated by commas. .sp In general, a destination port must be specified, unless the \fB-U\fR option is given, in which case a Unix Domain Socket path must be specified instead of \fIhostname\fR. .sp It is an error to use list of ports containing more than one port in conjunction with the -e option. .RE .SH USAGE .SS "Client/Server Model" .sp .LP It is quite simple to build a very basic client/server model using \fBnc\fR. On one console, start \fBnc\fR listening on a specific port for a connection. For example, the command: .sp .in +2 .nf $ nc -l 1234 .fi .in -2 .sp .sp .LP listens on port \fB1234\fR for a connection. On a second console (or a second machine), connect to the machine and port to which \fBnc\fR is listening: .sp .in +2 .nf $ nc 127.0.0.1 1234 .fi .in -2 .sp .sp .LP There should now be a connection between the ports. Anything typed at the second console is concatenated to the first, and vice-versa. After the connection has been set up, \fBnc\fR does not really care which side is being used as a \fBserver\fR and which side is being used as a \fBclient\fR. The connection can be terminated using an \fBEOF\fR (Ctrl/d). .SS "Data Transfer" .sp .LP The example in the previous section can be expanded to build a basic data transfer model. Any information input into one end of the connection is output to the other end, and input and output can be easily captured in order to emulate file transfer. .sp .LP Start by using \fBnc\fR to listen on a specific port, with output captured into a file: .sp .in +2 .nf $ nc -l 1234 > filename.out .fi .in -2 .sp .sp .LP Using a second machine, connect to the listening \fBnc\fR process, feeding it the file which is to be transferred: .sp .in +2 .nf $ nc host.example.com 1234 < filename.in .fi .in -2 .sp .sp .LP After the file has been transferred, the connection closes automatically. .SS "Talking to Servers" .sp .LP It is sometimes useful to talk to servers \fBby hand\fR rather than through a user interface. It can aid in troubleshooting, when it might be necessary to verify what data a server is sending in response to commands issued by the client. .sp .LP For example, to retrieve the home page of a web site: .sp .in +2 .nf $ echo -n "GET / HTTP/1.0\er\en\er\en" | nc host.example.com 80 .fi .in -2 .sp .sp .LP This also displays the headers sent by the web server. They can be filtered, if necessary, by using a tool such as \fBsed\fR(1). .sp .LP More complicated examples can be built up when the user knows the format of requests required by the server. As another example, an email can be submitted to an SMTP server using: .sp .in +2 .nf $ nc localhost 25 << EOF HELO host.example.com MAIL FROM: > /etc/services wwwredir 8080/tcp # WWW redirect EOF # cat << EOF > /tmp/wwwredir.conf wwwredir stream tcp nowait nobody /usr/bin/nc /usr/bin/nc -w 3 realwww 80 EOF # inetconv -i /tmp/wwwredir.conf wwwredir -> /var/svc/manifest/network/wwwredir-tcp.xml Importing wwwredir-tcp.xml ...Done # inetadm -l wwwredir/tcp SCOPE NAME=VALUE name="wwwredir" endpoint_type="stream" proto="tcp" isrpc=FALSE wait=FALSE exec="/usr/bin/nc -w 3 realwww 80" arg0="/usr/bin/nc" user="nobody" default bind_addr="" default bind_fail_max=-1 default bind_fail_interval=-1 default max_con_rate=-1 default max_copies=-1 default con_rate_offline=-1 default failrate_cnt=40 default failrate_interval=60 default inherit_env=TRUE default tcp_trace=TRUE default tcp_wrappers=FALSE .fi .in -2 .sp .SS "Privileges" .sp .LP To bind to a privileged port number \fBnc\fR needs to be granted the \fBnet_privaddr\fR privilege. If Solaris Trusted Extensions are configured and the port \fBnc\fR should listen on is configured as a multi-level port \fBnc\fR also needs the \fBnet_bindmlp\fR privilege. .sp .LP Privileges can be assigned to the user or role directly, by specifying them in the account's default privilege set in \fBuser_attr\fR(4). However, this means that any application that this user or role starts have these additional privileges. To only grant the \fBprivileges\fR(5) when \fBnc\fR is invoked, the recommended approach is to create and assign an \fBrbac\fR(5) rights profile. See \fBEXAMPLES\fR for additional information. .SH EXAMPLES .LP \fBExample 1 \fRUsing \fBnc\fR .sp .LP Open a TCP connection to port \fB42\fR of \fBhost.example.com\fR, using port \fB3141\fR as the source port, with a timeout of \fB5\fR seconds: .sp .in +2 .nf $ nc -p 3141 -w 5 host.example.com 42 .fi .in -2 .sp .sp .LP Open a TCP connection to port \fB7777\fR of \fBhost.example.com\fR, setting a maximum bandwidth of 50Mbps on the socket: .sp .in +2 .nf $ nc -M maxbw=50M host.example.com 7777 .fi .in -2 .sp .sp .LP Open a UDP connection to port \fB53\fR of \fBhost.example.com\fR: .sp .in +2 .nf $ nc -u host.example.com 53 .fi .in -2 .sp .sp .LP Open a TCP connection to port 42 of \fBhost.example.com\fR using \fB10.1.2.3\fR as the IP for the local end of the connection: .sp .in +2 .nf $ nc -s 10.1.2.3 host.example.com 42 .fi .in -2 .sp .sp .LP Use a list of ports and port ranges for a port scan on various ports: .sp .in +2 .nf $ nc -z host.example.com 21-25,53,80,110-120,443 .fi .in -2 .sp .sp .LP Create and listen on a Unix Domain Socket: .sp .in +2 .nf $ nc -lU /var/tmp/dsocket .fi .in -2 .sp .sp .LP Create and listen on a UDP socket with associated port \fB8888\fR: .sp .in +2 .nf $ nc -u -l -p 8888 .fi .in -2 .sp .sp .LP which is the same as: .sp .in +2 .nf $ nc -u -l 8888 .fi .in -2 .sp .sp .LP Create and listen on a TCP socket with associated port \fB2222\fR and bind to address \fB127.0.0.1\fR only: .sp .in +2 .nf $ nc -l 127.0.0.1 2222 .fi .in -2 .sp .sp .LP Create and listen on a TCP socket with associated port \fB2222\fR and create a high priority MAC flow on the listener and the connected sockets: .sp .in +2 .nf $ nc -l -M priority=high,inherit=on host.example.com 2222 .fi .in -2 .sp .sp .LP Connect to TCP port, send some data and terminate the connection with TCP RST segment (instead of classic TCP closing handshake) by setting the linger option and timeout to \fB0\fR: .sp .in +2 .nf $ echo "foo" | nc -L 0 host.example.com 22 .fi .in -2 .sp .sp .LP Perform port redirection to port \fB22\fR on host \fBhost.example.com\fR from local port \fB4545\fR: .sp .in +2 .nf $ nc -R host.example.com/22 -l 4545 .fi .in -2 .sp .sp .LP After that, it should be possible to run \fBssh\fR(1) client and connect to \fBhost.example.com\fR using \fBhost redir.example.com\fR running the above command: .sp .in +2 .nf $ ssh -oStrictHostKeyChecking=no -p 4545 redir.example.com .fi .in -2 .sp .sp .LP It is also possible to let \fBnc\fR listen on TCP port and convert the TCP data stream to UDP (or vice versa): .sp .in +2 .nf $ nc -R host.example.com/53/udp -l 4666 .fi .in -2 .sp .sp .LP Connect to port \fB42\fR of \fBhost.example.com\fR using an HTTP proxy at \fB10.2.3.4\fR, port \fB8080\fR. This example could also be used by \fBssh\fR(1). See the \fBProxyCommand\fR directive in \fBssh_config\fR(4) for more information. .sp .in +2 .nf $ nc -x10.2.3.4:8080 -Xconnect host.example.com 42 .fi .in -2 .sp .sp .LP The same example again, this time enabling proxy authentication with username \fBruser\fR if the proxy requires it: .sp .in +2 .nf $ nc -x10.2.3.4:8080 -Xconnect -Pruser host.example.com 42 .fi .in -2 .sp .sp .LP Basic UDP port scan can be efficiently done like this: .sp .in +2 .nf $ nc -z -w 3 -u -i 0.5 host.example.com 11-100 .fi .in -2 .sp .sp .LP Between each 2 ports it pauses for 0.5 second (thus evading ICMP message rate limiting) and waits up to 3 seconds for reply. If no reply comes then the port might be open. .sp .LP To run \fBnc\fR with the smallest possible set of privileges as a user or role that has additional privileges (such as the default \fBroot\fR account) it can be invoked using \fBppriv\fR(1) as well. For example, limiting it to only run with the privilege to bind to a privileged port: .sp .in +2 .nf $ ppriv -e -sA=basic,!file_link_any,!proc_exec,!proc_fork,\e !proc_info,!proc_session,net_privaddr nc -l 42 .fi .in -2 .sp .sp .LP To allow a user or role to use only \fBnc\fR with the \fBnet_privaddr\fR privilege, a rights profile needs to be created: .sp .in +2 .nf /etc/security/exec_attr Netcat privileged:solaris:cmd:::/usr/bin/nc:privs=net_privaddr /etc/security/prof_attr Netcat privileged:::Allow nc to bind to privileged ports:help=None.html .fi .in -2 .sp .sp .LP Assigning this rights profile using \fBuser_attr\fR(4) permits the user or role to run \fBnc\fR allowing it to listen on any port. To permit a user or role to use \fBnc\fR only to listen on specific ports a wrapper script should be specified in the rights profiles: .sp .in +2 .nf /etc/security/exec_attr Netcat restricted:solaris:cmd:::/usr/bin/nc-restricted:privs=net_privaddr /etc/security/prof_attr Netcat restricted:::Allow nc to bind to privileged ports:help=None.html .fi .in -2 .sp .sp .LP and write a shell script that restricts the permissible options, for example, one that permits one to bind only on ports between \fB42\fR and \fB64\fR (non-inclusive): .sp .in +2 .nf /usr/bin/nc-restricted: #!/bin/sh [ $# -eq 1 ] && [ $1 -gt 42 -a $1 -lt 64 ] && /usr/bin/nc -l -p "$1" .fi .in -2 .sp .sp .LP This grants the extra privileges when the user or role invokes \fBnc\fR using the wrapper script from a profile shell. See \fBpfsh\fR(1), \fBpfksh\fR(1), \fBpfcsh\fR(1), and \fBpfexec\fR(1). .sp .LP Invoking \fBnc\fR directly does not run it with the additional privileges, and neither does invoking the script without using \fBpfexec\fR or a profile shell. .SH ATTRIBUTES .sp .LP See \fBattributes\fR(5) for descriptions of the following attributes: .sp .sp .TS tab() box; cw(2.75i) |cw(2.75i) lw(2.75i) |lw(2.75i) . ATTRIBUTE TYPEATTRIBUTE VALUE _ Availabilitynetwork/netcat _ Interface StabilitySee below. .TE .sp .LP The package name is Committed. The command line syntax is Committed for the \fB-4\fR, \fB-6,\fR \fB-l\fR, \fB-n\fR, \fB-p\fR, \fB-u\fR, and \fB-w\fR options and their arguments (if any). The \fIname\fR and \fIport\fR list arguments are Committed. The port range syntax is Uncommitted. The interface stability level for all other command line options and their arguments is Uncommitted. .SH SEE ALSO .sp .LP \fBcat\fR(1), \fBpfcsh\fR(1), \fBpfexec\fR(1), \fBpfksh\fR(1), \fBpfsh\fR(1), \fBppriv\fR(1), \fBsed\fR(1), \fBssh\fR(1), \fBtelnet\fR(1), \fBinetadm\fR(1M), \fBinetconv\fR(1M), \fBinetd\fR(1M), \fBssh_config\fR(4), \fBuser_attr\fR(4), \fBattributes\fR(5), \fBprivileges\fR(5), \fBrbac\fR(5) .SH AUTHORS .sp .LP The original implementation of \fBnc\fR was written by Hobbit, \fBhobbit@avian.org\fR. .sp .LP \fBnc\fR was rewritten with IPv6 support by Eric Jackson, \fBericj@monkey.org\fR. .SH NOTES .sp .LP If an instance of \fBnc\fR is listening on a wildcard socket (regardless of address family specification) it is still possible to bind another \fBnc\fR process to concrete IP address and accept connections to this address. For example, with the following process running: .sp .in +2 .nf $ nc -4 -l 5656 .fi .in -2 .sp .sp .LP it is possible to run another \fBnc\fR process listening on specific IP address and the same port: .sp .in +2 .nf $ nc -4 -l 10.20.30.40 5656 .fi .in -2 .sp .sp .LP TCP connection to address \fB10.20.30.40\fR and port \fB5656\fR is accepted by the latter process, all TCP connections to port \fB5656\fR and different addresses is accepted by the former process. .sp .LP Also, it is possible to steal IPv4 connections from a process which listens on a wildcard socket (without address family specification) by binding to IPv4 wildcard socket. To suppress this and the behavior described above the \fB-E\fR option could be used.