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