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