1.\" $OpenBSD: nc.1,v 1.60 2012/02/07 12:11:43 lum 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.Dd $Mdocdate: February 7 2012 $ 29.Dt NC 1 30.Os 31.Sh NAME 32.Nm nc 33.Nd arbitrary TCP and UDP connections and listens 34.Sh SYNOPSIS 35.Nm nc 36.Bk -words 37.Op Fl 46DdhklnrStUuvz 38.Op Fl I Ar length 39.Op Fl i Ar interval 40.Op Fl O Ar length 41.Op Fl P Ar proxy_username 42.Op Fl p Ar source_port 43.Op Fl s Ar source 44.Op Fl T Ar toskeyword 45.Op Fl V Ar rtable 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 51.Xc Oc 52.Op Ar destination 53.Op Ar port 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, 61UDP, or 62.Ux Ns -domain 63sockets. 64It can open TCP connections, send UDP packets, listen on arbitrary 65TCP and UDP ports, do port scanning, and deal with both IPv4 and 66IPv6. 67Unlike 68.Xr telnet 1 , 69.Nm 70scripts nicely, and separates error messages onto standard error instead 71of sending them to standard output, as 72.Xr telnet 1 73does with some. 74.Pp 75Common uses include: 76.Pp 77.Bl -bullet -offset indent -compact 78.It 79simple TCP proxies 80.It 81shell-script based HTTP clients and servers 82.It 83network daemon testing 84.It 85a SOCKS or HTTP ProxyCommand for 86.Xr ssh 1 87.It 88and much, much more 89.El 90.Pp 91The options are as follows: 92.Bl -tag -width Ds 93.It Fl 4 94Forces 95.Nm 96to use IPv4 addresses only. 97.It Fl 6 98Forces 99.Nm 100to use IPv6 addresses only. 101.It Fl D 102Enable debugging on the socket. 103.It Fl d 104Do not attempt to read from stdin. 105.It Fl h 106Prints out 107.Nm 108help. 109.It Fl I Ar length 110Specifies the size of the TCP receive buffer. 111.It Fl i Ar interval 112Specifies a delay time interval between lines of text sent and received. 113Also causes a delay time between connections to multiple ports. 114.It Fl k 115Forces 116.Nm 117to stay listening for another connection after its current connection 118is completed. 119It is an error to use this option without the 120.Fl l 121option. 122.It Fl l 123Used to specify that 124.Nm 125should listen for an incoming connection rather than initiate a 126connection to a remote host. 127It is an error to use this option in conjunction with the 128.Fl p , 129.Fl s , 130or 131.Fl z 132options. 133Additionally, any timeouts specified with the 134.Fl w 135option are ignored. 136.It Fl n 137Do not do any DNS or service lookups on any specified addresses, 138hostnames or ports. 139.It Fl O Ar length 140Specifies the size of the TCP send buffer. 141.It Fl P Ar proxy_username 142Specifies a username to present to a proxy server that requires authentication. 143If no username is specified then authentication will not be attempted. 144Proxy authentication is only supported for HTTP CONNECT proxies at present. 145.It Fl p Ar source_port 146Specifies the source port 147.Nm 148should use, subject to privilege restrictions and availability. 149It is an error to use this option in conjunction with the 150.Fl l 151option. 152.It Fl r 153Specifies that source and/or destination ports should be chosen randomly 154instead of sequentially within a range or in the order that the system 155assigns them. 156.It Fl S 157Enables the RFC 2385 TCP MD5 signature option. 158.It Fl s Ar source 159Specifies the IP of the interface which is used to send the packets. 160For 161.Ux Ns -domain 162datagram sockets, specifies the local temporary socket file 163to create and use so that datagrams can be received. 164It is an error to use this option in conjunction with the 165.Fl l 166option. 167.It Fl T Ar toskeyword 168Change IPv4 TOS value. 169.Ar toskeyword 170may be one of 171.Ar critical , 172.Ar inetcontrol , 173.Ar lowdelay , 174.Ar netcontrol , 175.Ar throughput , 176.Ar reliability , 177or one of the DiffServ Code Points: 178.Ar ef , 179.Ar af11 ... af43 , 180.Ar cs0 ... cs7 ; 181or a number in either hex or decimal. 182.It Fl t 183Causes 184.Nm 185to send RFC 854 DON'T and WON'T responses to RFC 854 DO and WILL requests. 186This makes it possible to use 187.Nm 188to script telnet sessions. 189.It Fl U 190Specifies to use 191.Ux Ns -domain 192sockets. 193.It Fl u 194Use UDP instead of the default option of TCP. 195For 196.Ux Ns -domain 197sockets, use a datagram socket instead of a stream socket. 198If a 199.Ux Ns -domain 200socket is used, a temporary receiving socket is created in 201.Pa /tmp 202unless the 203.Fl s 204flag is given. 205.It Fl V Ar rtable 206Set the routing table to be used. 207The default is 0. 208.It Fl v 209Have 210.Nm 211give more verbose output. 212.It Fl w Ar timeout 213Connections which cannot be established or are idle timeout after 214.Ar timeout 215seconds. 216The 217.Fl w 218flag has no effect on the 219.Fl l 220option, i.e.\& 221.Nm 222will listen forever for a connection, with or without the 223.Fl w 224flag. 225The default is no timeout. 226.It Fl X Ar proxy_protocol 227Requests that 228.Nm 229should use the specified protocol when talking to the proxy server. 230Supported protocols are 231.Dq 4 232(SOCKS v.4), 233.Dq 5 234(SOCKS v.5) 235and 236.Dq connect 237(HTTPS proxy). 238If the protocol is not specified, SOCKS version 5 is used. 239.It Xo 240.Fl x Ar proxy_address Ns Oo : Ns 241.Ar port Oc 242.Xc 243Requests that 244.Nm 245should connect to 246.Ar destination 247using a proxy at 248.Ar proxy_address 249and 250.Ar port . 251If 252.Ar port 253is not specified, the well-known port for the proxy protocol is used (1080 254for SOCKS, 3128 for HTTPS). 255.It Fl z 256Specifies that 257.Nm 258should just scan for listening daemons, without sending any data to them. 259It is an error to use this option in conjunction with the 260.Fl l 261option. 262.El 263.Pp 264.Ar destination 265can be a numerical IP address or a symbolic hostname 266(unless the 267.Fl n 268option is given). 269In general, a destination must be specified, 270unless the 271.Fl l 272option is given 273(in which case the local host is used). 274For 275.Ux Ns -domain 276sockets, a destination is required and is the socket path to connect to 277(or listen on if the 278.Fl l 279option is given). 280.Pp 281.Ar port 282can be a single integer or a range of ports. 283Ranges are in the form nn-mm. 284In general, 285a destination port must be specified, 286unless the 287.Fl U 288option is given. 289.Sh CLIENT/SERVER MODEL 290It is quite simple to build a very basic client/server model using 291.Nm . 292On one console, start 293.Nm 294listening on a specific port for a connection. 295For example: 296.Pp 297.Dl $ nc -l 1234 298.Pp 299.Nm 300is now listening on port 1234 for a connection. 301On a second console 302.Pq or a second machine , 303connect to the machine and port being listened on: 304.Pp 305.Dl $ nc 127.0.0.1 1234 306.Pp 307There should now be a connection between the ports. 308Anything typed at the second console will be concatenated to the first, 309and vice-versa. 310After the connection has been set up, 311.Nm 312does not really care which side is being used as a 313.Sq server 314and which side is being used as a 315.Sq client . 316The connection may be terminated using an 317.Dv EOF 318.Pq Sq ^D . 319.Sh DATA TRANSFER 320The example in the previous section can be expanded to build a 321basic data transfer model. 322Any information input into one end of the connection will be output 323to the other end, and input and output can be easily captured in order to 324emulate file transfer. 325.Pp 326Start by using 327.Nm 328to listen on a specific port, with output captured into a file: 329.Pp 330.Dl $ nc -l 1234 \*(Gt filename.out 331.Pp 332Using a second machine, connect to the listening 333.Nm 334process, feeding it the file which is to be transferred: 335.Pp 336.Dl $ nc host.example.com 1234 \*(Lt filename.in 337.Pp 338After the file has been transferred, the connection will close automatically. 339.Sh TALKING TO SERVERS 340It is sometimes useful to talk to servers 341.Dq by hand 342rather than through a user interface. 343It can aid in troubleshooting, 344when it might be necessary to verify what data a server is sending 345in response to commands issued by the client. 346For example, to retrieve the home page of a web site: 347.Bd -literal -offset indent 348$ printf "GET / HTTP/1.0\er\en\er\en" | nc host.example.com 80 349.Ed 350.Pp 351Note that this also displays the headers sent by the web server. 352They can be filtered, using a tool such as 353.Xr sed 1 , 354if necessary. 355.Pp 356More complicated examples can be built up when the user knows the format 357of requests required by the server. 358As another example, an email may be submitted to an SMTP server using: 359.Bd -literal -offset indent 360$ nc localhost 25 \*(Lt\*(Lt EOF 361HELO host.example.com 362MAIL FROM:\*(Ltuser@host.example.com\*(Gt 363RCPT TO:\*(Ltuser2@host.example.com\*(Gt 364DATA 365Body of email. 366\&. 367QUIT 368EOF 369.Ed 370.Sh PORT SCANNING 371It may be useful to know which ports are open and running services on 372a target machine. 373The 374.Fl z 375flag can be used to tell 376.Nm 377to report open ports, 378rather than initiate a connection. 379For example: 380.Bd -literal -offset indent 381$ nc -z host.example.com 20-30 382Connection to host.example.com 22 port [tcp/ssh] succeeded! 383Connection to host.example.com 25 port [tcp/smtp] succeeded! 384.Ed 385.Pp 386The port range was specified to limit the search to ports 20 \- 30. 387.Pp 388Alternatively, it might be useful to know which server software 389is running, and which versions. 390This information is often contained within the greeting banners. 391In order to retrieve these, it is necessary to first make a connection, 392and then break the connection when the banner has been retrieved. 393This can be accomplished by specifying a small timeout with the 394.Fl w 395flag, or perhaps by issuing a 396.Qq Dv QUIT 397command to the server: 398.Bd -literal -offset indent 399$ echo "QUIT" | nc host.example.com 20-30 400SSH-1.99-OpenSSH_3.6.1p2 401Protocol mismatch. 402220 host.example.com IMS SMTP Receiver Version 0.84 Ready 403.Ed 404.Sh EXAMPLES 405Open a TCP connection to port 42 of host.example.com, using port 31337 as 406the source port, with a timeout of 5 seconds: 407.Pp 408.Dl $ nc -p 31337 -w 5 host.example.com 42 409.Pp 410Open a UDP connection to port 53 of host.example.com: 411.Pp 412.Dl $ nc -u host.example.com 53 413.Pp 414Open a TCP connection to port 42 of host.example.com using 10.1.2.3 as the 415IP for the local end of the connection: 416.Pp 417.Dl $ nc -s 10.1.2.3 host.example.com 42 418.Pp 419Create and listen on a 420.Ux Ns -domain 421stream socket: 422.Pp 423.Dl $ nc -lU /var/tmp/dsocket 424.Pp 425Connect to port 42 of host.example.com via an HTTP proxy at 10.2.3.4, 426port 8080. 427This example could also be used by 428.Xr ssh 1 ; 429see the 430.Cm ProxyCommand 431directive in 432.Xr ssh_config 5 433for more information. 434.Pp 435.Dl $ nc -x10.2.3.4:8080 -Xconnect host.example.com 42 436.Pp 437The same example again, this time enabling proxy authentication with username 438.Dq ruser 439if the proxy requires it: 440.Pp 441.Dl $ nc -x10.2.3.4:8080 -Xconnect -Pruser host.example.com 42 442.Sh SEE ALSO 443.Xr cat 1 , 444.Xr ssh 1 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 using the 453.Fl uz 454combination of flags will always report success irrespective of 455the target machine's state. 456However, 457in conjunction with a traffic sniffer either on the target machine 458or an intermediary device, 459the 460.Fl uz 461combination could be useful for communications diagnostics. 462Note that the amount of UDP traffic generated may be limited either 463due to hardware resources and/or configuration settings. 464