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