Annotation of src/usr.bin/nc/nc.1, Revision 1.70
1.70 ! jmc 1: .\" $OpenBSD: nc.1,v 1.69 2015/09/11 21:07:01 beck Exp $
1.1 deraadt 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: .\"
1.70 ! jmc 28: .Dd $Mdocdate: September 11 2015 $
1.3 aaron 29: .Dt NC 1
1.4 deraadt 30: .Os
1.1 deraadt 31: .Sh NAME
32: .Nm nc
1.28 jmc 33: .Nd arbitrary TCP and UDP connections and listens
1.2 deraadt 34: .Sh SYNOPSIS
1.1 deraadt 35: .Nm nc
1.31 jmc 36: .Bk -words
1.70 ! jmc 37: .Op Fl 46cDdFhklNnrStUuvz
! 38: .Op Fl C Ar certfile
! 39: .Op Fl e Ar name
! 40: .Op Fl H Ar hash
1.47 jmc 41: .Op Fl I Ar length
1.1 deraadt 42: .Op Fl i Ar interval
1.70 ! jmc 43: .Op Fl K Ar keyfile
1.47 jmc 44: .Op Fl O Ar length
1.42 djm 45: .Op Fl P Ar proxy_username
1.28 jmc 46: .Op Fl p Ar source_port
1.70 ! jmc 47: .Op Fl R Ar CAfile
1.57 jeremy 48: .Op Fl s Ar source
1.69 beck 49: .Op Fl T Ar keyword
1.54 guenther 50: .Op Fl V Ar rtable
1.6 aaron 51: .Op Fl w Ar timeout
1.33 djm 52: .Op Fl X Ar proxy_protocol
1.28 jmc 53: .Oo Xo
54: .Fl x Ar proxy_address Ns Oo : Ns
1.53 schwarze 55: .Ar port Oc
56: .Xc Oc
1.57 jeremy 57: .Op Ar destination
1.48 sobrado 58: .Op Ar port
1.31 jmc 59: .Ek
1.1 deraadt 60: .Sh DESCRIPTION
61: The
1.6 aaron 62: .Nm
1.1 deraadt 63: (or
64: .Nm netcat )
1.57 jeremy 65: utility is used for just about anything under the sun involving TCP,
66: UDP, or
67: .Ux Ns -domain
68: sockets.
1.13 ericj 69: It can open TCP connections, send UDP packets, listen on arbitrary
70: TCP and UDP ports, do port scanning, and deal with both IPv4 and
71: IPv6.
1.7 aaron 72: Unlike
1.1 deraadt 73: .Xr telnet 1 ,
1.6 aaron 74: .Nm
1.1 deraadt 75: scripts nicely, and separates error messages onto standard error instead
1.6 aaron 76: of sending them to standard output, as
1.24 pvalchev 77: .Xr telnet 1
1.6 aaron 78: does with some.
1.1 deraadt 79: .Pp
80: Common uses include:
1.7 aaron 81: .Pp
82: .Bl -bullet -offset indent -compact
1.1 deraadt 83: .It
1.3 aaron 84: simple TCP proxies
1.1 deraadt 85: .It
1.28 jmc 86: shell-script based HTTP clients and servers
1.1 deraadt 87: .It
1.13 ericj 88: network daemon testing
1.1 deraadt 89: .It
1.33 djm 90: a SOCKS or HTTP ProxyCommand for
91: .Xr ssh 1
92: .It
1.1 deraadt 93: and much, much more
94: .El
95: .Pp
96: The options are as follows:
97: .Bl -tag -width Ds
1.13 ericj 98: .It Fl 4
99: Forces
100: .Nm
101: to use IPv4 addresses only.
102: .It Fl 6
103: Forces
104: .Nm
105: to use IPv6 addresses only.
1.70 ! jmc 106: .It Fl C Ar certfile
1.69 beck 107: Specifies the filename from which the public key part of the TLS
1.70 ! jmc 108: certificate is loaded, in PEM format.
! 109: May only be used with TLS.
1.69 beck 110: .It Fl c
1.70 ! jmc 111: If using a TCP socket to connect or listen, use TLS.
! 112: Illegal if not using TCP sockets.
1.32 markus 113: .It Fl D
114: Enable debugging on the socket.
1.29 tedu 115: .It Fl d
116: Do not attempt to read from stdin.
1.69 beck 117: .It Fl e Ar name
1.70 ! jmc 118: Specify the name that must be present in the peer certificate when using TLS.
1.69 beck 119: Illegal if not using TLS.
1.64 djm 120: .It Fl F
121: Pass the first connected socket using
122: .Xr sendmsg 2
123: to stdout and exit.
124: This is useful in conjunction with
125: .Fl X
126: to have
127: .Nm
128: perform connection setup with a proxy but then leave the rest of the
1.65 jmc 129: connection to another program (e.g.\&
1.64 djm 130: .Xr ssh 1
131: using the
132: .Xr ssh_config 5
1.68 tobias 133: .Cm ProxyUseFdpass
1.64 djm 134: option).
1.70 ! jmc 135: .It Fl H Ar hash
1.69 beck 136: Specifies the required hash string of the peer certificate when using TLS.
137: The string format required is that used by
138: .Xr tls_peer_cert_hash 3 .
139: Illegal if not using TLS, and may not be used with -T noverify.
1.13 ericj 140: .It Fl h
141: Prints out
142: .Nm
143: help.
1.47 jmc 144: .It Fl I Ar length
1.46 djm 145: Specifies the size of the TCP receive buffer.
1.13 ericj 146: .It Fl i Ar interval
1.1 deraadt 147: Specifies a delay time interval between lines of text sent and received.
148: Also causes a delay time between connections to multiple ports.
1.70 ! jmc 149: .It Fl K Ar keyfile
1.69 beck 150: Specifies the filename from which the private key
1.70 ! jmc 151: is loaded in PEM format.
! 152: May only be used with TLS.
1.13 ericj 153: .It Fl k
154: Forces
155: .Nm
1.21 ericj 156: to stay listening for another connection after its current connection
1.13 ericj 157: is completed.
1.28 jmc 158: It is an error to use this option without the
159: .Fl l
160: option.
1.61 haesbaer 161: When used together with the
162: .Fl u
163: option, the server socket is not connected and it can receive UDP datagrams from
164: multiple hosts.
1.1 deraadt 165: .It Fl l
1.13 ericj 166: Used to specify that
1.6 aaron 167: .Nm
1.13 ericj 168: should listen for an incoming connection rather than initiate a
1.7 aaron 169: connection to a remote host.
1.28 jmc 170: It is an error to use this option in conjunction with the
171: .Fl p ,
172: .Fl s ,
173: or
174: .Fl z
175: options.
1.36 jmc 176: Additionally, any timeouts specified with the
1.35 jmc 177: .Fl w
1.36 jmc 178: option are ignored.
1.62 sthen 179: .It Fl N
180: .Xr shutdown 2
181: the network socket after EOF on the input.
182: Some servers require this to finish their work.
1.1 deraadt 183: .It Fl n
1.21 ericj 184: Do not do any DNS or service lookups on any specified addresses,
185: hostnames or ports.
1.47 jmc 186: .It Fl O Ar length
187: Specifies the size of the TCP send buffer.
1.42 djm 188: .It Fl P Ar proxy_username
189: Specifies a username to present to a proxy server that requires authentication.
190: If no username is specified then authentication will not be attempted.
191: Proxy authentication is only supported for HTTP CONNECT proxies at present.
1.28 jmc 192: .It Fl p Ar source_port
1.1 deraadt 193: Specifies the source port
1.6 aaron 194: .Nm
1.1 deraadt 195: should use, subject to privilege restrictions and availability.
1.28 jmc 196: It is an error to use this option in conjunction with the
197: .Fl l
198: option.
1.70 ! jmc 199: .It Fl R Ar CAfile
! 200: Specifies the filename from which the root CA bundle for certificate
! 201: verification is loaded, in PEM format.
! 202: Illegal if not using TLS.
! 203: The default is
1.69 beck 204: .Pa /etc/ssl/cert.pem .
1.1 deraadt 205: .It Fl r
1.13 ericj 206: Specifies that source and/or destination ports should be chosen randomly
207: instead of sequentially within a range or in the order that the system
1.21 ericj 208: assigns them.
1.28 jmc 209: .It Fl S
210: Enables the RFC 2385 TCP MD5 signature option.
1.57 jeremy 211: .It Fl s Ar source
1.3 aaron 212: Specifies the IP of the interface which is used to send the packets.
1.56 jeremy 213: For
214: .Ux Ns -domain
215: datagram sockets, specifies the local temporary socket file
216: to create and use so that datagrams can be received.
1.28 jmc 217: It is an error to use this option in conjunction with the
218: .Fl l
219: option.
1.69 beck 220: .It Fl T Ar keyword
221: Change IPv4 TOS value or TLS options.
222: For TLS options
223: .Ar keyword
224: may be one of
225: .Ar tlslegacy ,
1.70 ! jmc 226: which allows legacy TLS protocols;
1.69 beck 227: .Ar noverify ,
1.70 ! jmc 228: which disables certificate verification;
1.69 beck 229: .Ar noname ,
1.70 ! jmc 230: which disables certificate name checking; or
! 231: .Ar clientcert ,
! 232: which requires a client certificate on incoming connections.
1.69 beck 233: It is illegal to specify TLS options if not using TLS.
1.70 ! jmc 234: .Pp
1.69 beck 235: For IPv4 TOS value
236: .Ar keyword
1.58 haesbaer 237: may be one of
238: .Ar critical ,
239: .Ar inetcontrol ,
240: .Ar lowdelay ,
241: .Ar netcontrol ,
242: .Ar throughput ,
243: .Ar reliability ,
244: or one of the DiffServ Code Points:
245: .Ar ef ,
246: .Ar af11 ... af43 ,
247: .Ar cs0 ... cs7 ;
248: or a number in either hex or decimal.
1.1 deraadt 249: .It Fl t
250: Causes
1.6 aaron 251: .Nm
1.25 jmc 252: to send RFC 854 DON'T and WON'T responses to RFC 854 DO and WILL requests.
1.7 aaron 253: This makes it possible to use
1.6 aaron 254: .Nm
1.7 aaron 255: to script telnet sessions.
1.28 jmc 256: .It Fl U
1.51 sobrado 257: Specifies to use
1.52 sobrado 258: .Ux Ns -domain
259: sockets.
1.1 deraadt 260: .It Fl u
1.13 ericj 261: Use UDP instead of the default option of TCP.
1.56 jeremy 262: For
263: .Ux Ns -domain
264: sockets, use a datagram socket instead of a stream socket.
265: If a
266: .Ux Ns -domain
267: socket is used, a temporary receiving socket is created in
268: .Pa /tmp
269: unless the
270: .Fl s
271: flag is given.
1.54 guenther 272: .It Fl V Ar rtable
273: Set the routing table to be used.
1.1 deraadt 274: .It Fl v
1.13 ericj 275: Have
1.6 aaron 276: .Nm
1.13 ericj 277: give more verbose output.
1.26 jmc 278: .It Fl w Ar timeout
1.59 fgsch 279: Connections which cannot be established or are idle timeout after
1.26 jmc 280: .Ar timeout
1.59 fgsch 281: seconds.
1.26 jmc 282: The
283: .Fl w
284: flag has no effect on the
285: .Fl l
286: option, i.e.\&
287: .Nm
288: will listen forever for a connection, with or without the
289: .Fl w
290: flag.
291: The default is no timeout.
1.43 jmc 292: .It Fl X Ar proxy_protocol
1.28 jmc 293: Requests that
294: .Nm
1.33 djm 295: should use the specified protocol when talking to the proxy server.
296: Supported protocols are
297: .Dq 4
298: (SOCKS v.4),
299: .Dq 5
300: (SOCKS v.5)
301: and
302: .Dq connect
303: (HTTPS proxy).
304: If the protocol is not specified, SOCKS version 5 is used.
1.28 jmc 305: .It Xo
306: .Fl x Ar proxy_address Ns Oo : Ns
307: .Ar port Oc
308: .Xc
1.19 jakob 309: Requests that
310: .Nm
311: should connect to
1.57 jeremy 312: .Ar destination
1.33 djm 313: using a proxy at
1.28 jmc 314: .Ar proxy_address
315: and
316: .Ar port .
317: If
318: .Ar port
1.33 djm 319: is not specified, the well-known port for the proxy protocol is used (1080
320: for SOCKS, 3128 for HTTPS).
1.1 deraadt 321: .It Fl z
322: Specifies that
1.6 aaron 323: .Nm
1.13 ericj 324: should just scan for listening daemons, without sending any data to them.
1.28 jmc 325: It is an error to use this option in conjunction with the
326: .Fl l
327: option.
328: .El
1.35 jmc 329: .Pp
1.57 jeremy 330: .Ar destination
1.35 jmc 331: can be a numerical IP address or a symbolic hostname
332: (unless the
333: .Fl n
334: option is given).
1.57 jeremy 335: In general, a destination must be specified,
1.35 jmc 336: unless the
337: .Fl l
338: option is given
339: (in which case the local host is used).
1.57 jeremy 340: For
341: .Ux Ns -domain
342: sockets, a destination is required and is the socket path to connect to
343: (or listen on if the
344: .Fl l
345: option is given).
1.35 jmc 346: .Pp
1.48 sobrado 347: .Ar port
348: can be a single integer or a range of ports.
1.35 jmc 349: Ranges are in the form nn-mm.
350: In general,
351: a destination port must be specified,
352: unless the
353: .Fl U
1.57 jeremy 354: option is given.
1.28 jmc 355: .Sh CLIENT/SERVER MODEL
356: It is quite simple to build a very basic client/server model using
357: .Nm .
358: On one console, start
359: .Nm
360: listening on a specific port for a connection.
361: For example:
362: .Pp
363: .Dl $ nc -l 1234
364: .Pp
365: .Nm
366: is now listening on port 1234 for a connection.
367: On a second console
368: .Pq or a second machine ,
369: connect to the machine and port being listened on:
370: .Pp
371: .Dl $ nc 127.0.0.1 1234
372: .Pp
373: There should now be a connection between the ports.
374: Anything typed at the second console will be concatenated to the first,
375: and vice-versa.
376: After the connection has been set up,
377: .Nm
378: does not really care which side is being used as a
379: .Sq server
380: and which side is being used as a
381: .Sq client .
382: The connection may be terminated using an
383: .Dv EOF
384: .Pq Sq ^D .
385: .Sh DATA TRANSFER
386: The example in the previous section can be expanded to build a
387: basic data transfer model.
388: Any information input into one end of the connection will be output
389: to the other end, and input and output can be easily captured in order to
390: emulate file transfer.
391: .Pp
392: Start by using
393: .Nm
394: to listen on a specific port, with output captured into a file:
395: .Pp
396: .Dl $ nc -l 1234 \*(Gt filename.out
397: .Pp
398: Using a second machine, connect to the listening
399: .Nm
400: process, feeding it the file which is to be transferred:
401: .Pp
1.66 jmc 402: .Dl $ nc -N host.example.com 1234 \*(Lt filename.in
1.28 jmc 403: .Pp
404: After the file has been transferred, the connection will close automatically.
405: .Sh TALKING TO SERVERS
406: It is sometimes useful to talk to servers
407: .Dq by hand
408: rather than through a user interface.
409: It can aid in troubleshooting,
410: when it might be necessary to verify what data a server is sending
411: in response to commands issued by the client.
412: For example, to retrieve the home page of a web site:
1.40 jmc 413: .Bd -literal -offset indent
1.55 guenther 414: $ printf "GET / HTTP/1.0\er\en\er\en" | nc host.example.com 80
1.40 jmc 415: .Ed
1.28 jmc 416: .Pp
417: Note that this also displays the headers sent by the web server.
418: They can be filtered, using a tool such as
419: .Xr sed 1 ,
420: if necessary.
421: .Pp
422: More complicated examples can be built up when the user knows the format
423: of requests required by the server.
424: As another example, an email may be submitted to an SMTP server using:
425: .Bd -literal -offset indent
426: $ nc localhost 25 \*(Lt\*(Lt EOF
427: HELO host.example.com
1.44 jmc 428: MAIL FROM:\*(Ltuser@host.example.com\*(Gt
429: RCPT TO:\*(Ltuser2@host.example.com\*(Gt
1.28 jmc 430: DATA
431: Body of email.
432: \&.
433: QUIT
434: EOF
435: .Ed
436: .Sh PORT SCANNING
437: It may be useful to know which ports are open and running services on
438: a target machine.
439: The
440: .Fl z
441: flag can be used to tell
1.22 markus 442: .Nm
1.39 jmc 443: to report open ports,
444: rather than initiate a connection.
1.28 jmc 445: For example:
446: .Bd -literal -offset indent
1.39 jmc 447: $ nc -z host.example.com 20-30
1.28 jmc 448: Connection to host.example.com 22 port [tcp/ssh] succeeded!
449: Connection to host.example.com 25 port [tcp/smtp] succeeded!
450: .Ed
451: .Pp
452: The port range was specified to limit the search to ports 20 \- 30.
453: .Pp
454: Alternatively, it might be useful to know which server software
455: is running, and which versions.
456: This information is often contained within the greeting banners.
457: In order to retrieve these, it is necessary to first make a connection,
458: and then break the connection when the banner has been retrieved.
459: This can be accomplished by specifying a small timeout with the
460: .Fl w
461: flag, or perhaps by issuing a
462: .Qq Dv QUIT
463: command to the server:
464: .Bd -literal -offset indent
465: $ echo "QUIT" | nc host.example.com 20-30
466: SSH-1.99-OpenSSH_3.6.1p2
467: Protocol mismatch.
468: 220 host.example.com IMS SMTP Receiver Version 0.84 Ready
469: .Ed
1.1 deraadt 470: .Sh EXAMPLES
1.37 jmc 471: Open a TCP connection to port 42 of host.example.com, using port 31337 as
1.28 jmc 472: the source port, with a timeout of 5 seconds:
473: .Pp
1.37 jmc 474: .Dl $ nc -p 31337 -w 5 host.example.com 42
1.69 beck 475: .Pp
1.70 ! jmc 476: Open a TCP connection to port 443 of www.google.ca, and negotiate TLS.
! 477: Check for a different name in the certificate for validation.
1.69 beck 478: .Pp
479: .Dl $ nc -v -c -e adsf.au.doubleclick.net www.google.ca 443
1.28 jmc 480: .Pp
1.37 jmc 481: Open a UDP connection to port 53 of host.example.com:
1.28 jmc 482: .Pp
1.37 jmc 483: .Dl $ nc -u host.example.com 53
1.28 jmc 484: .Pp
1.37 jmc 485: Open a TCP connection to port 42 of host.example.com using 10.1.2.3 as the
1.28 jmc 486: IP for the local end of the connection:
487: .Pp
1.37 jmc 488: .Dl $ nc -s 10.1.2.3 host.example.com 42
1.28 jmc 489: .Pp
1.51 sobrado 490: Create and listen on a
1.52 sobrado 491: .Ux Ns -domain
1.57 jeremy 492: stream socket:
1.28 jmc 493: .Pp
494: .Dl $ nc -lU /var/tmp/dsocket
1.33 djm 495: .Pp
1.37 jmc 496: Connect to port 42 of host.example.com via an HTTP proxy at 10.2.3.4,
1.38 jmc 497: port 8080.
498: This example could also be used by
499: .Xr ssh 1 ;
500: see the
501: .Cm ProxyCommand
502: directive in
503: .Xr ssh_config 5
504: for more information.
1.33 djm 505: .Pp
1.37 jmc 506: .Dl $ nc -x10.2.3.4:8080 -Xconnect host.example.com 42
1.42 djm 507: .Pp
508: The same example again, this time enabling proxy authentication with username
509: .Dq ruser
510: if the proxy requires it:
511: .Pp
512: .Dl $ nc -x10.2.3.4:8080 -Xconnect -Pruser host.example.com 42
1.1 deraadt 513: .Sh SEE ALSO
1.38 jmc 514: .Xr cat 1 ,
515: .Xr ssh 1
1.15 smart 516: .Sh AUTHORS
517: Original implementation by *Hobbit*
1.63 schwarze 518: .Aq Mt hobbit@avian.org .
1.28 jmc 519: .br
520: Rewritten with IPv6 support by
1.63 schwarze 521: .An Eric Jackson Aq Mt ericj@monkey.org .
1.39 jmc 522: .Sh CAVEATS
1.60 lum 523: UDP port scans using the
1.39 jmc 524: .Fl uz
1.60 lum 525: combination of flags will always report success irrespective of
526: the target machine's state.
527: However,
528: in conjunction with a traffic sniffer either on the target machine
529: or an intermediary device,
530: the
531: .Fl uz
532: combination could be useful for communications diagnostics.
533: Note that the amount of UDP traffic generated may be limited either
534: due to hardware resources and/or configuration settings.