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