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