import nc (LibreSSL 2.5.4)
[unleashed.git] / bin / nc / nc.1
blobb1f96488ab0c055b5d8fbf1bc72f7a1cd07b4b3e
1 .\"     $OpenBSD: nc.1,v 1.82 2017/02/09 20:15:59 jca 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 .Dd $Mdocdate: February 9 2017 $
29 .Dt NC 1
30 .Os
31 .Sh NAME
32 .Nm nc
33 .Nd arbitrary TCP and UDP connections and listens
34 .Sh SYNOPSIS
35 .Nm nc
36 .Op Fl 46cDdFhklNnrStUuvz
37 .Op Fl C Ar certfile
38 .Op Fl e Ar name
39 .Op Fl H Ar hash
40 .Op Fl I Ar length
41 .Op Fl i Ar interval
42 .Op Fl K Ar keyfile
43 .Op Fl M Ar ttl
44 .Op Fl m Ar minttl
45 .Op Fl O Ar length
46 .Op Fl o Ar staplefile
47 .Op Fl P Ar proxy_username
48 .Op Fl p Ar source_port
49 .Op Fl R Ar CAfile
50 .Op Fl s Ar source
51 .Op Fl T Ar keyword
52 .Op Fl V Ar rtable
53 .Op Fl w Ar timeout
54 .Op Fl X Ar proxy_protocol
55 .Op Fl x Ar proxy_address Ns Op : Ns Ar port
56 .Op Ar destination
57 .Op Ar port
58 .Sh DESCRIPTION
59 The
60 .Nm
61 (or
62 .Nm netcat )
63 utility is used for just about anything under the sun involving TCP,
64 UDP, or
65 .Ux Ns -domain
66 sockets.
67 It can open TCP connections, send UDP packets, listen on arbitrary
68 TCP and UDP ports, do port scanning, and deal with both IPv4 and
69 IPv6.
70 Unlike
71 .Xr telnet 1 ,
72 .Nm
73 scripts nicely, and separates error messages onto standard error instead
74 of sending them to standard output, as
75 .Xr telnet 1
76 does with some.
77 .Pp
78 Common uses include:
79 .Pp
80 .Bl -bullet -offset indent -compact
81 .It
82 simple TCP proxies
83 .It
84 shell-script based HTTP clients and servers
85 .It
86 network daemon testing
87 .It
88 a SOCKS or HTTP ProxyCommand for
89 .Xr ssh 1
90 .It
91 and much, much more
92 .El
93 .Pp
94 The options are as follows:
95 .Bl -tag -width Ds
96 .It Fl 4
97 Forces
98 .Nm
99 to use IPv4 addresses only.
100 .It Fl 6
101 Forces
103 to use IPv6 addresses only.
104 .It Fl C Ar certfile
105 Specifies the filename from which the public key part of the TLS
106 certificate is loaded, in PEM format.
107 May only be used with TLS.
108 .It Fl c
109 If using a TCP socket to connect or listen, use TLS.
110 Illegal if not using TCP sockets.
111 .It Fl D
112 Enable debugging on the socket.
113 .It Fl d
114 Do not attempt to read from stdin.
115 .It Fl e Ar name
116 Specify the name that must be present in the peer certificate when using TLS.
117 Illegal if not using TLS.
118 .It Fl F
119 Pass the first connected socket using
120 .Xr sendmsg 2
121 to stdout and exit.
122 This is useful in conjunction with
123 .Fl X
124 to have
126 perform connection setup with a proxy but then leave the rest of the
127 connection to another program (e.g.\&
128 .Xr ssh 1
129 using the
130 .Xr ssh_config 5
131 .Cm ProxyUseFdpass
132 option).
133 .It Fl H Ar hash
134 Specifies the required hash string of the peer certificate when using TLS.
135 The string format required is that used by
136 .Xr tls_peer_cert_hash 3 .
137 Illegal if not using TLS, and may not be used with -T noverify.
138 .It Fl h
139 Prints out
141 help.
142 .It Fl I Ar length
143 Specifies the size of the TCP receive buffer.
144 .It Fl i Ar interval
145 Specifies a delay time interval between lines of text sent and received.
146 Also causes a delay time between connections to multiple ports.
147 .It Fl K Ar keyfile
148 Specifies the filename from which the private key
149 is loaded in PEM format.
150 May only be used with TLS.
151 .It Fl k
152 Forces
154 to stay listening for another connection after its current connection
155 is completed.
156 It is an error to use this option without the
157 .Fl l
158 option.
159 When used together with the
160 .Fl u
161 option, the server socket is not connected and it can receive UDP datagrams from
162 multiple hosts.
163 .It Fl l
164 Used to specify that
166 should listen for an incoming connection rather than initiate a
167 connection to a remote host.
168 It is an error to use this option in conjunction with the
169 .Fl p ,
170 .Fl s ,
172 .Fl z
173 options.
174 Additionally, any timeouts specified with the
175 .Fl w
176 option are ignored.
177 .It Fl M Ar ttl
178 Set the TTL / hop limit of outgoing packets.
179 .It Fl m Ar minttl
180 Ask the kernel to drop incoming packets whose TTL / hop limit is under
181 .Ar minttl .
182 .It Fl N
183 .Xr shutdown 2
184 the network socket after EOF on the input.
185 Some servers require this to finish their work.
186 .It Fl n
187 Do not do any DNS or service lookups on any specified addresses,
188 hostnames or ports.
189 .It Fl O Ar length
190 Specifies the size of the TCP send buffer.
191 .It Fl o Ar staplefile
192 Specifies the filename from which to load data to be stapled
193 during the TLS handshake.
194 The file is expected to contain an OCSP response from an OCSP server in
195 DER format.
196 May only be used with TLS and when a certificate is being used.
197 .It Fl P Ar proxy_username
198 Specifies a username to present to a proxy server that requires authentication.
199 If no username is specified then authentication will not be attempted.
200 Proxy authentication is only supported for HTTP CONNECT proxies at present.
201 .It Fl p Ar source_port
202 Specifies the source port
204 should use, subject to privilege restrictions and availability.
205 It is an error to use this option in conjunction with the
206 .Fl l
207 option.
208 .It Fl R Ar CAfile
209 Specifies the filename from which the root CA bundle for certificate
210 verification is loaded, in PEM format.
211 Illegal if not using TLS.
212 The default is
213 .Pa /etc/ssl/cert.pem .
214 .It Fl r
215 Specifies that source and/or destination ports should be chosen randomly
216 instead of sequentially within a range or in the order that the system
217 assigns them.
218 .It Fl S
219 Enables the RFC 2385 TCP MD5 signature option.
220 .It Fl s Ar source
221 Specifies the IP of the interface which is used to send the packets.
223 .Ux Ns -domain
224 datagram sockets, specifies the local temporary socket file
225 to create and use so that datagrams can be received.
226 It is an error to use this option in conjunction with the
227 .Fl l
228 option.
229 .It Fl T Ar keyword
230 Change IPv4 TOS value or TLS options.
231 For TLS options
232 .Ar keyword
233 may be one of
234 .Ar tlsall ;
235 which allows the use of all supported TLS protocols and ciphers,
236 .Ar noverify ;
237 which disables certificate verification;
238 .Ar noname ,
239 which disables certificate name checking;
240 .Ar clientcert ,
241 which requires a client certificate on incoming connections; or
242 .Ar muststaple ,
243 which requires the peer to provide a valid stapled OCSP response
244 with the handshake.
245 It is illegal to specify TLS options if not using TLS.
247 For IPv4 TOS value
248 .Ar keyword
249 may be one of
250 .Ar critical ,
251 .Ar inetcontrol ,
252 .Ar lowdelay ,
253 .Ar netcontrol ,
254 .Ar throughput ,
255 .Ar reliability ,
256 or one of the DiffServ Code Points:
257 .Ar ef ,
258 .Ar af11 ... af43 ,
259 .Ar cs0 ... cs7 ;
260 or a number in either hex or decimal.
261 .It Fl t
262 Causes
264 to send RFC 854 DON'T and WON'T responses to RFC 854 DO and WILL requests.
265 This makes it possible to use
267 to script telnet sessions.
268 .It Fl U
269 Specifies to use
270 .Ux Ns -domain
271 sockets.
272 .It Fl u
273 Use UDP instead of the default option of TCP.
275 .Ux Ns -domain
276 sockets, use a datagram socket instead of a stream socket.
277 If a
278 .Ux Ns -domain
279 socket is used, a temporary receiving socket is created in
280 .Pa /tmp
281 unless the
282 .Fl s
283 flag is given.
284 .It Fl V Ar rtable
285 Set the routing table to be used.
286 .It Fl v
287 Have
289 give more verbose output.
290 .It Fl w Ar timeout
291 Connections which cannot be established or are idle timeout after
292 .Ar timeout
293 seconds.
295 .Fl w
296 flag has no effect on the
297 .Fl l
298 option, i.e.\&
300 will listen forever for a connection, with or without the
301 .Fl w
302 flag.
303 The default is no timeout.
304 .It Fl X Ar proxy_protocol
305 Requests that
307 should use the specified protocol when talking to the proxy server.
308 Supported protocols are
309 .Dq 4
310 (SOCKS v.4),
311 .Dq 5
312 (SOCKS v.5)
314 .Dq connect
315 (HTTPS proxy).
316 If the protocol is not specified, SOCKS version 5 is used.
317 .It Fl x Ar proxy_address Ns Op : Ns Ar port
318 Requests that
320 should connect to
321 .Ar destination
322 using a proxy at
323 .Ar proxy_address
325 .Ar port .
327 .Ar port
328 is not specified, the well-known port for the proxy protocol is used (1080
329 for SOCKS, 3128 for HTTPS).
330 An IPv6 address can be specified unambiguously by enclosing
331 .Ar proxy_address
332 in square brackets.
333 .It Fl z
334 Specifies that
336 should just scan for listening daemons, without sending any data to them.
337 It is an error to use this option in conjunction with the
338 .Fl l
339 option.
342 .Ar destination
343 can be a numerical IP address or a symbolic hostname
344 (unless the
345 .Fl n
346 option is given).
347 In general, a destination must be specified,
348 unless the
349 .Fl l
350 option is given
351 (in which case the local host is used).
353 .Ux Ns -domain
354 sockets, a destination is required and is the socket path to connect to
355 (or listen on if the
356 .Fl l
357 option is given).
359 .Ar port
360 can be a specified as a numeric port number, or as a service name.
361 Ports may be specified in a range of the form nn-mm.
362 In general,
363 a destination port must be specified,
364 unless the
365 .Fl U
366 option is given.
367 .Sh CLIENT/SERVER MODEL
368 It is quite simple to build a very basic client/server model using
369 .Nm .
370 On one console, start
372 listening on a specific port for a connection.
373 For example:
375 .Dl $ nc -l 1234
378 is now listening on port 1234 for a connection.
379 On a second console
380 .Pq or a second machine ,
381 connect to the machine and port being listened on:
383 .Dl $ nc 127.0.0.1 1234
385 There should now be a connection between the ports.
386 Anything typed at the second console will be concatenated to the first,
387 and vice-versa.
388 After the connection has been set up,
390 does not really care which side is being used as a
391 .Sq server
392 and which side is being used as a
393 .Sq client .
394 The connection may be terminated using an
395 .Dv EOF
396 .Pq Sq ^D .
397 .Sh DATA TRANSFER
398 The example in the previous section can be expanded to build a
399 basic data transfer model.
400 Any information input into one end of the connection will be output
401 to the other end, and input and output can be easily captured in order to
402 emulate file transfer.
404 Start by using
406 to listen on a specific port, with output captured into a file:
408 .Dl $ nc -l 1234 \*(Gt filename.out
410 Using a second machine, connect to the listening
412 process, feeding it the file which is to be transferred:
414 .Dl $ nc -N host.example.com 1234 \*(Lt filename.in
416 After the file has been transferred, the connection will close automatically.
417 .Sh TALKING TO SERVERS
418 It is sometimes useful to talk to servers
419 .Dq by hand
420 rather than through a user interface.
421 It can aid in troubleshooting,
422 when it might be necessary to verify what data a server is sending
423 in response to commands issued by the client.
424 For example, to retrieve the home page of a web site:
425 .Bd -literal -offset indent
426 $ printf "GET / HTTP/1.0\er\en\er\en" | nc host.example.com 80
429 Note that this also displays the headers sent by the web server.
430 They can be filtered, using a tool such as
431 .Xr sed 1 ,
432 if necessary.
434 More complicated examples can be built up when the user knows the format
435 of requests required by the server.
436 As another example, an email may be submitted to an SMTP server using:
437 .Bd -literal -offset indent
438 $ nc localhost 25 \*(Lt\*(Lt EOF
439 HELO host.example.com
440 MAIL FROM:\*(Ltuser@host.example.com\*(Gt
441 RCPT TO:\*(Ltuser2@host.example.com\*(Gt
442 DATA
443 Body of email.
445 QUIT
448 .Sh PORT SCANNING
449 It may be useful to know which ports are open and running services on
450 a target machine.
452 .Fl z
453 flag can be used to tell
455 to report open ports,
456 rather than initiate a connection.
457 For example:
458 .Bd -literal -offset indent
459 $ nc -z host.example.com 20-30
460 Connection to host.example.com 22 port [tcp/ssh] succeeded!
461 Connection to host.example.com 25 port [tcp/smtp] succeeded!
464 The port range was specified to limit the search to ports 20 \- 30.
466 Alternatively, it might be useful to know which server software
467 is running, and which versions.
468 This information is often contained within the greeting banners.
469 In order to retrieve these, it is necessary to first make a connection,
470 and then break the connection when the banner has been retrieved.
471 This can be accomplished by specifying a small timeout with the
472 .Fl w
473 flag, or perhaps by issuing a
474 .Qq Dv QUIT
475 command to the server:
476 .Bd -literal -offset indent
477 $ echo "QUIT" | nc host.example.com 20-30
478 SSH-1.99-OpenSSH_3.6.1p2
479 Protocol mismatch.
480 220 host.example.com IMS SMTP Receiver Version 0.84 Ready
482 .Sh EXAMPLES
483 Open a TCP connection to port 42 of host.example.com, using port 31337 as
484 the source port, with a timeout of 5 seconds:
486 .Dl $ nc -p 31337 -w 5 host.example.com 42
488 Open a TCP connection to port 443 of www.google.ca, and negotiate TLS.
489 Check for a different name in the certificate for validation.
491 .Dl $  nc -v -c -e adsf.au.doubleclick.net www.google.ca 443
493 Open a UDP connection to port 53 of host.example.com:
495 .Dl $ nc -u host.example.com 53
497 Open a TCP connection to port 42 of host.example.com using 10.1.2.3 as the
498 IP for the local end of the connection:
500 .Dl $ nc -s 10.1.2.3 host.example.com 42
502 Create and listen on a
503 .Ux Ns -domain
504 stream socket:
506 .Dl $ nc -lU /var/tmp/dsocket
508 Connect to port 42 of host.example.com via an HTTP proxy at 10.2.3.4,
509 port 8080.
510 This example could also be used by
511 .Xr ssh 1 ;
512 see the
513 .Cm ProxyCommand
514 directive in
515 .Xr ssh_config 5
516 for more information.
518 .Dl $ nc -x10.2.3.4:8080 -Xconnect host.example.com 42
520 The same example again, this time enabling proxy authentication with username
521 .Dq ruser
522 if the proxy requires it:
524 .Dl $ nc -x10.2.3.4:8080 -Xconnect -Pruser host.example.com 42
525 .Sh SEE ALSO
526 .Xr cat 1 ,
527 .Xr ssh 1
528 .Sh AUTHORS
529 Original implementation by *Hobbit*
530 .Aq Mt hobbit@avian.org .
532 Rewritten with IPv6 support by
533 .An Eric Jackson Aq Mt ericj@monkey.org .
534 .Sh CAVEATS
535 UDP port scans using the
536 .Fl uz
537 combination of flags will always report success irrespective of
538 the target machine's state.
539 However,
540 in conjunction with a traffic sniffer either on the target machine
541 or an intermediary device,
543 .Fl uz
544 combination could be useful for communications diagnostics.
545 Note that the amount of UDP traffic generated may be limited either
546 due to hardware resources and/or configuration settings.