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