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