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