1.\" $OpenBSD: nc.1,v 1.45 2007/05/31 19:20:13 jmc Exp $ 2.\" 3.\" Copyright (c) 1996 David Sacerdote 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. The name of the author may not be used to endorse or promote products 15.\" derived from this software without specific prior written permission 16.\" 17.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27.\" 28.\" $FreeBSD$ 29.\" 30.Dd $Mdocdate$ 31.Dt NC 1 32.Os 33.Sh NAME 34.Nm nc 35.Nd arbitrary TCP and UDP connections and listens 36.Sh SYNOPSIS 37.Nm nc 38.Bk -words 39.Op Fl 46DEdhklnoOrStUuvz 40.Op Fl e Ar IPsec_policy 41.Op Fl i Ar interval 42.Op Fl P Ar proxy_username 43.Op Fl p Ar source_port 44.Op Fl s Ar source_ip_address 45.Op Fl T Ar ToS 46.Op Fl w Ar timeout 47.Op Fl X Ar proxy_protocol 48.Oo Xo 49.Fl x Ar proxy_address Ns Oo : Ns 50.Ar port Oc Oc 51.Xc 52.Op Ar hostname 53.Op Ar port Ns Bq Ar s 54.Ek 55.Sh DESCRIPTION 56The 57.Nm 58(or 59.Nm netcat ) 60utility is used for just about anything under the sun involving TCP 61or UDP. 62It can open TCP connections, send UDP packets, listen on arbitrary 63TCP and UDP ports, do port scanning, and deal with both IPv4 and 64IPv6. 65Unlike 66.Xr telnet 1 , 67.Nm 68scripts nicely, and separates error messages onto standard error instead 69of sending them to standard output, as 70.Xr telnet 1 71does with some. 72.Pp 73Common uses include: 74.Pp 75.Bl -bullet -offset indent -compact 76.It 77simple TCP proxies 78.It 79shell-script based HTTP clients and servers 80.It 81network daemon testing 82.It 83a SOCKS or HTTP ProxyCommand for 84.Xr ssh 1 85.It 86and much, much more 87.El 88.Pp 89The options are as follows: 90.Bl -tag -width Ds 91.It Fl 4 92Forces 93.Nm 94to use IPv4 addresses only. 95.It Fl 6 96Forces 97.Nm 98to use IPv6 addresses only. 99.It Fl D 100Enable debugging on the socket. 101.It Fl d 102Do not attempt to read from stdin. 103.It Fl h 104Prints out 105.Nm 106help. 107.It Fl E 108Shortcut for 109.Qo 110.Li "-e 'in ipsec esp/transport//require'" 111.Li "-e 'out ipsec esp/transport//require'" 112.Qc , 113which enables IPsec ESP transport mode in both 114directions. 115.It Fl e 116If IPsec support is available, then one can specify the IPsec policies 117to be used using the syntax described in 118.Xr ipsec_set_policy 3 . 119This flag can be specified up to two times, as typically one policy for 120each direction is needed. 121.It Fl i Ar interval 122Specifies a delay time interval between lines of text sent and received. 123Also causes a delay time between connections to multiple ports. 124.It Fl k 125Forces 126.Nm 127to stay listening for another connection after its current connection 128is completed. 129It is an error to use this option without the 130.Fl l 131option. 132.It Fl l 133Used to specify that 134.Nm 135should listen for an incoming connection rather than initiate a 136connection to a remote host. 137It is an error to use this option in conjunction with the 138.Fl p , 139.Fl s , 140or 141.Fl z 142options. 143Additionally, any timeouts specified with the 144.Fl w 145option are ignored. 146.It Fl n 147Do not do any DNS or service lookups on any specified addresses, 148hostnames or ports. 149.\" 150.It Fl o 151.Dq Once-only mode . 152By default, 153.Nm 154does not terminate on EOF condition on input, 155but continues until the network side has been closed down. 156Specifying 157.Fl o 158will make it terminate on EOF as well. 159.It Fl o 160Disables the use of TCP options on the socket, by setting the boolean 161TCP_NOOPT 162socket option. 163.It Fl P Ar proxy_username 164Specifies a username to present to a proxy server that requires authentication. 165If no username is specified then authentication will not be attempted. 166Proxy authentication is only supported for HTTP CONNECT proxies at present. 167.It Fl p Ar source_port 168Specifies the source port 169.Nm 170should use, subject to privilege restrictions and availability. 171It is an error to use this option in conjunction with the 172.Fl l 173option. 174.It Fl r 175Specifies that source and/or destination ports should be chosen randomly 176instead of sequentially within a range or in the order that the system 177assigns them. 178.It Fl S 179Enables the RFC 2385 TCP MD5 signature option. 180.It Fl s Ar source_ip_address 181Specifies the IP of the interface which is used to send the packets. 182It is an error to use this option in conjunction with the 183.Fl l 184option. 185.It Fl T Ar ToS 186Specifies IP Type of Service (ToS) for the connection. 187Valid values are the tokens 188.Dq lowdelay , 189.Dq throughput , 190.Dq reliability , 191or an 8-bit hexadecimal value preceded by 192.Dq 0x . 193.It Fl t 194Causes 195.Nm 196to send RFC 854 DON'T and WON'T responses to RFC 854 DO and WILL requests. 197This makes it possible to use 198.Nm 199to script telnet sessions. 200.It Fl U 201Specifies to use Unix Domain Sockets. 202.It Fl u 203Use UDP instead of the default option of TCP. 204.It Fl v 205Have 206.Nm 207give more verbose output. 208.It Fl w Ar timeout 209If a connection and stdin are idle for more than 210.Ar timeout 211seconds, then the connection is silently closed. 212The 213.Fl w 214flag has no effect on the 215.Fl l 216option, i.e.\& 217.Nm 218will listen forever for a connection, with or without the 219.Fl w 220flag. 221The default is no timeout. 222.It Fl X Ar proxy_protocol 223Requests that 224.Nm 225should use the specified protocol when talking to the proxy server. 226Supported protocols are 227.Dq 4 228(SOCKS v.4), 229.Dq 5 230(SOCKS v.5) 231and 232.Dq connect 233(HTTPS proxy). 234If the protocol is not specified, SOCKS version 5 is used. 235.It Xo 236.Fl x Ar proxy_address Ns Oo : Ns 237.Ar port Oc 238.Xc 239Requests that 240.Nm 241should connect to 242.Ar hostname 243using a proxy at 244.Ar proxy_address 245and 246.Ar port . 247If 248.Ar port 249is not specified, the well-known port for the proxy protocol is used (1080 250for SOCKS, 3128 for HTTPS). 251.It Fl z 252Specifies that 253.Nm 254should just scan for listening daemons, without sending any data to them. 255It is an error to use this option in conjunction with the 256.Fl l 257option. 258.El 259.Pp 260.Ar hostname 261can be a numerical IP address or a symbolic hostname 262(unless the 263.Fl n 264option is given). 265In general, a hostname must be specified, 266unless the 267.Fl l 268option is given 269(in which case the local host is used). 270.Pp 271.Ar port Ns Op Ar s 272can be single integers or ranges. 273Ranges are in the form nn-mm. 274In general, 275a destination port must be specified, 276unless the 277.Fl U 278option is given 279(in which case a socket must be specified). 280.Sh CLIENT/SERVER MODEL 281It is quite simple to build a very basic client/server model using 282.Nm . 283On one console, start 284.Nm 285listening on a specific port for a connection. 286For example: 287.Pp 288.Dl $ nc -l 1234 289.Pp 290.Nm 291is now listening on port 1234 for a connection. 292On a second console 293.Pq or a second machine , 294connect to the machine and port being listened on: 295.Pp 296.Dl $ nc 127.0.0.1 1234 297.Pp 298There should now be a connection between the ports. 299Anything typed at the second console will be concatenated to the first, 300and vice-versa. 301After the connection has been set up, 302.Nm 303does not really care which side is being used as a 304.Sq server 305and which side is being used as a 306.Sq client . 307The connection may be terminated using an 308.Dv EOF 309.Pq Sq ^D . 310.Sh DATA TRANSFER 311The example in the previous section can be expanded to build a 312basic data transfer model. 313Any information input into one end of the connection will be output 314to the other end, and input and output can be easily captured in order to 315emulate file transfer. 316.Pp 317Start by using 318.Nm 319to listen on a specific port, with output captured into a file: 320.Pp 321.Dl $ nc -l 1234 \*(Gt filename.out 322.Pp 323Using a second machine, connect to the listening 324.Nm 325process, feeding it the file which is to be transferred: 326.Pp 327.Dl $ nc host.example.com 1234 \*(Lt filename.in 328.Pp 329After the file has been transferred, the connection will close automatically. 330.Sh TALKING TO SERVERS 331It is sometimes useful to talk to servers 332.Dq by hand 333rather than through a user interface. 334It can aid in troubleshooting, 335when it might be necessary to verify what data a server is sending 336in response to commands issued by the client. 337For example, to retrieve the home page of a web site: 338.Bd -literal -offset indent 339$ echo -n "GET / HTTP/1.0\er\en\er\en" | nc host.example.com 80 340.Ed 341.Pp 342Note that this also displays the headers sent by the web server. 343They can be filtered, using a tool such as 344.Xr sed 1 , 345if necessary. 346.Pp 347More complicated examples can be built up when the user knows the format 348of requests required by the server. 349As another example, an email may be submitted to an SMTP server using: 350.Bd -literal -offset indent 351$ nc localhost 25 \*(Lt\*(Lt EOF 352HELO host.example.com 353MAIL FROM:\*(Ltuser@host.example.com\*(Gt 354RCPT TO:\*(Ltuser2@host.example.com\*(Gt 355DATA 356Body of email. 357\&. 358QUIT 359EOF 360.Ed 361.Sh PORT SCANNING 362It may be useful to know which ports are open and running services on 363a target machine. 364The 365.Fl z 366flag can be used to tell 367.Nm 368to report open ports, 369rather than initiate a connection. 370For example: 371.Bd -literal -offset indent 372$ nc -z host.example.com 20-30 373Connection to host.example.com 22 port [tcp/ssh] succeeded! 374Connection to host.example.com 25 port [tcp/smtp] succeeded! 375.Ed 376.Pp 377The port range was specified to limit the search to ports 20 \- 30. 378.Pp 379Alternatively, it might be useful to know which server software 380is running, and which versions. 381This information is often contained within the greeting banners. 382In order to retrieve these, it is necessary to first make a connection, 383and then break the connection when the banner has been retrieved. 384This can be accomplished by specifying a small timeout with the 385.Fl w 386flag, or perhaps by issuing a 387.Qq Dv QUIT 388command to the server: 389.Bd -literal -offset indent 390$ echo "QUIT" | nc host.example.com 20-30 391SSH-1.99-OpenSSH_3.6.1p2 392Protocol mismatch. 393220 host.example.com IMS SMTP Receiver Version 0.84 Ready 394.Ed 395.Sh EXAMPLES 396Open a TCP connection to port 42 of host.example.com, using port 31337 as 397the source port, with a timeout of 5 seconds: 398.Pp 399.Dl $ nc -p 31337 -w 5 host.example.com 42 400.Pp 401Open a UDP connection to port 53 of host.example.com: 402.Pp 403.Dl $ nc -u host.example.com 53 404.Pp 405Open a TCP connection to port 42 of host.example.com using 10.1.2.3 as the 406IP for the local end of the connection: 407.Pp 408.Dl $ nc -s 10.1.2.3 host.example.com 42 409.Pp 410Open a TCP connection to port 42 of host.example.com using IPsec ESP for 411incoming and outgoing traffic. 412.Pp 413.Dl $ nc -E host.example.com 42 414.Pp 415Open a TCP connection to port 42 of host.example.com using IPsec ESP for 416outgoing traffic only. 417.Pp 418.Dl $ nc -e 'out ipsec esp/transport//require' host.example.com 42 419.Pp 420Create and listen on a Unix Domain Socket: 421.Pp 422.Dl $ nc -lU /var/tmp/dsocket 423.Pp 424Connect to port 42 of host.example.com via an HTTP proxy at 10.2.3.4, 425port 8080. 426This example could also be used by 427.Xr ssh 1 ; 428see the 429.Cm ProxyCommand 430directive in 431.Xr ssh_config 5 432for more information. 433.Pp 434.Dl $ nc -x10.2.3.4:8080 -Xconnect host.example.com 42 435.Pp 436The same example again, this time enabling proxy authentication with username 437.Dq ruser 438if the proxy requires it: 439.Pp 440.Dl $ nc -x10.2.3.4:8080 -Xconnect -Pruser host.example.com 42 441.Sh SEE ALSO 442.Xr cat 1 , 443.Xr ssh 1 , 444.Xr tcp 4 445.Sh AUTHORS 446Original implementation by *Hobbit* 447.Aq hobbit@avian.org . 448.br 449Rewritten with IPv6 support by 450.An Eric Jackson Aq ericj@monkey.org . 451.Sh CAVEATS 452UDP port scans will always succeed 453(i.e. report the port as open), 454rendering the 455.Fl uz 456combination of flags relatively useless. 457