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