8888 loader: rename STAND to _STANDALONE
[unleashed.git] / bin / nc / nc.1
blobe10d385a142dee38bb822c36c035faa5647b7e8a
1 .\"     $OpenBSD: nc.1,v 1.88 2017/11/28 16:59:10 jsing 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: November 28 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 recvlimit
54 .Op Fl w Ar timeout
55 .Op Fl X Ar proxy_protocol
56 .Op Fl x Ar proxy_address Ns Op : Ns Ar port
57 .Op Fl Z Ar peercertfile
58 .Op Ar destination
59 .Op Ar port
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
101 to use IPv4 addresses only.
102 .It Fl 6
103 Forces
105 to use IPv6 addresses only.
106 .It Fl C Ar certfile
107 Specifies the filename from which the public key part of the TLS
108 certificate is loaded, in PEM format.
109 May only be used with TLS.
110 .It Fl c
111 If using a TCP socket to connect or listen, use TLS.
112 Illegal if not using TCP sockets.
113 .It Fl D
114 Enable debugging on the socket.
115 .It Fl d
116 Do not attempt to read from stdin.
117 .It Fl e Ar name
118 Specify the name that must be present in the peer certificate when using TLS.
119 Illegal if not using TLS.
120 .It Fl F
121 Pass the first connected socket using
122 .Xr sendmsg 2
123 to stdout and exit.
124 This is useful in conjunction with
125 .Fl X
126 to have
128 perform connection setup with a proxy but then leave the rest of the
129 connection to another program (e.g.\&
130 .Xr ssh 1
131 using the
132 .Xr ssh_config 5
133 .Cm ProxyUseFdpass
134 option).
135 .It Fl H Ar hash
136 Specifies the required hash string of the peer certificate when using TLS.
137 The string format required is that used by
138 .Xr tls_peer_cert_hash 3 .
139 Illegal if not using TLS, and may not be used with -T noverify.
140 .It Fl h
141 Prints out
143 help.
144 .It Fl I Ar length
145 Specifies the size of the TCP receive buffer.
146 .It Fl i Ar interval
147 Specifies a delay time interval between lines of text sent and received.
148 Also causes a delay time between connections to multiple ports.
149 .It Fl K Ar keyfile
150 Specifies the filename from which the private key
151 is loaded in PEM format.
152 May only be used with TLS.
153 .It Fl k
154 Forces
156 to stay listening for another connection after its current connection
157 is completed.
158 It is an error to use this option without the
159 .Fl l
160 option.
161 When used together with the
162 .Fl u
163 option, the server socket is not connected and it can receive UDP datagrams from
164 multiple hosts.
165 .It Fl l
166 Used to specify that
168 should listen for an incoming connection rather than initiate a
169 connection to a remote host.
170 It is an error to use this option in conjunction with the
171 .Fl p ,
172 .Fl s ,
174 .Fl z
175 options.
176 Additionally, any timeouts specified with the
177 .Fl w
178 option are ignored.
179 .It Fl M Ar ttl
180 Set the TTL / hop limit of outgoing packets.
181 .It Fl m Ar minttl
182 Ask the kernel to drop incoming packets whose TTL / hop limit is under
183 .Ar minttl .
184 .It Fl N
185 .Xr shutdown 2
186 the network socket after EOF on the input.
187 Some servers require this to finish their work.
188 .It Fl n
189 Do not do any DNS or service lookups on any specified addresses,
190 hostnames or ports.
191 .It Fl O Ar length
192 Specifies the size of the TCP send buffer.
193 .It Fl o Ar staplefile
194 Specifies the filename from which to load data to be stapled
195 during the TLS handshake.
196 The file is expected to contain an OCSP response from an OCSP server in
197 DER format.
198 May only be used with TLS and when a certificate is being used.
199 .It Fl P Ar proxy_username
200 Specifies a username to present to a proxy server that requires authentication.
201 If no username is specified then authentication will not be attempted.
202 Proxy authentication is only supported for HTTP CONNECT proxies at present.
203 .It Fl p Ar source_port
204 Specifies the source port
206 should use, subject to privilege restrictions and availability.
207 It is an error to use this option in conjunction with the
208 .Fl l
209 option.
210 .It Fl R Ar CAfile
211 Specifies the filename from which the root CA bundle for certificate
212 verification is loaded, in PEM format.
213 Illegal if not using TLS.
214 The default is
215 .Pa /etc/ssl/cert.pem .
216 .It Fl r
217 Specifies that source and/or destination ports should be chosen randomly
218 instead of sequentially within a range or in the order that the system
219 assigns them.
220 .It Fl S
221 Enables the RFC 2385 TCP MD5 signature option.
222 .It Fl s Ar source
223 Specifies the IP of the interface which is used to send the packets.
225 .Ux Ns -domain
226 datagram sockets, specifies the local temporary socket file
227 to create and use so that datagrams can be received.
228 It is an error to use this option in conjunction with the
229 .Fl l
230 option.
231 .It Fl T Ar keyword
232 Change IPv4 TOS value or TLS options.
233 For TLS options
234 .Ar keyword
235 may be one of:
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 The following TLS options specify a value in the form of a key=value pair:
246 .Ar ciphers ,
247 which allows the supported TLS ciphers to be specified (see
248 .Xr tls_config_set_ciphers 3
249 for further details);
250 .Ar protocols ,
251 which allows the supported TLS protocols to be specified (see
252 .Xr tls_config_parse_protocols 3
253 for further details).
254 It is illegal to specify TLS options if not using TLS.
256 For IPv4 TOS value
257 .Ar keyword
258 may be one of
259 .Ar critical ,
260 .Ar inetcontrol ,
261 .Ar lowdelay ,
262 .Ar netcontrol ,
263 .Ar throughput ,
264 .Ar reliability ,
265 or one of the DiffServ Code Points:
266 .Ar ef ,
267 .Ar af11 ... af43 ,
268 .Ar cs0 ... cs7 ;
269 or a number in either hex or decimal.
270 .It Fl t
271 Causes
273 to send RFC 854 DON'T and WON'T responses to RFC 854 DO and WILL requests.
274 This makes it possible to use
276 to script telnet sessions.
277 .It Fl U
278 Specifies to use
279 .Ux Ns -domain
280 sockets.
281 .It Fl u
282 Use UDP instead of the default option of TCP.
284 .Ux Ns -domain
285 sockets, use a datagram socket instead of a stream socket.
286 If a
287 .Ux Ns -domain
288 socket is used, a temporary receiving socket is created in
289 .Pa /tmp
290 unless the
291 .Fl s
292 flag is given.
293 .It Fl V Ar rtable
294 Set the routing table to be used.
295 .It Fl v
296 Have
298 give more verbose output.
299 .It Fl W Ar recvlimit
300 Terminate after receiving
301 .Ar recvlimit
302 packets from the network.
303 .It Fl w Ar timeout
304 Connections which cannot be established or are idle timeout after
305 .Ar timeout
306 seconds.
308 .Fl w
309 flag has no effect on the
310 .Fl l
311 option, i.e.\&
313 will listen forever for a connection, with or without the
314 .Fl w
315 flag.
316 The default is no timeout.
317 .It Fl X Ar proxy_protocol
318 Requests that
320 should use the specified protocol when talking to the proxy server.
321 Supported protocols are
322 .Dq 4
323 (SOCKS v.4),
324 .Dq 5
325 (SOCKS v.5)
327 .Dq connect
328 (HTTPS proxy).
329 If the protocol is not specified, SOCKS version 5 is used.
330 .It Fl x Ar proxy_address Ns Op : Ns Ar port
331 Requests that
333 should connect to
334 .Ar destination
335 using a proxy at
336 .Ar proxy_address
338 .Ar port .
340 .Ar port
341 is not specified, the well-known port for the proxy protocol is used (1080
342 for SOCKS, 3128 for HTTPS).
343 An IPv6 address can be specified unambiguously by enclosing
344 .Ar proxy_address
345 in square brackets.
346 .It Fl Z Ar peercertfile
347 Specifies the filename in which the peer supplied certificates will be saved
348 in PEM format.
349 May only be used with TLS.
350 .It Fl z
351 Specifies that
353 should just scan for listening daemons, without sending any data to them.
354 It is an error to use this option in conjunction with the
355 .Fl l
356 option.
359 .Ar destination
360 can be a numerical IP address or a symbolic hostname
361 (unless the
362 .Fl n
363 option is given).
364 In general, a destination must be specified,
365 unless the
366 .Fl l
367 option is given
368 (in which case the local host is used).
370 .Ux Ns -domain
371 sockets, a destination is required and is the socket path to connect to
372 (or listen on if the
373 .Fl l
374 option is given).
376 .Ar port
377 can be a specified as a numeric port number, or as a service name.
378 Ports may be specified in a range of the form nn-mm.
379 In general,
380 a destination port must be specified,
381 unless the
382 .Fl U
383 option is given.
384 .Sh CLIENT/SERVER MODEL
385 It is quite simple to build a very basic client/server model using
386 .Nm .
387 On one console, start
389 listening on a specific port for a connection.
390 For example:
392 .Dl $ nc -l 1234
395 is now listening on port 1234 for a connection.
396 On a second console
397 .Pq or a second machine ,
398 connect to the machine and port being listened on:
400 .Dl $ nc 127.0.0.1 1234
402 There should now be a connection between the ports.
403 Anything typed at the second console will be concatenated to the first,
404 and vice-versa.
405 After the connection has been set up,
407 does not really care which side is being used as a
408 .Sq server
409 and which side is being used as a
410 .Sq client .
411 The connection may be terminated using an
412 .Dv EOF
413 .Pq Sq ^D .
414 .Sh DATA TRANSFER
415 The example in the previous section can be expanded to build a
416 basic data transfer model.
417 Any information input into one end of the connection will be output
418 to the other end, and input and output can be easily captured in order to
419 emulate file transfer.
421 Start by using
423 to listen on a specific port, with output captured into a file:
425 .Dl $ nc -l 1234 \*(Gt filename.out
427 Using a second machine, connect to the listening
429 process, feeding it the file which is to be transferred:
431 .Dl $ nc -N host.example.com 1234 \*(Lt filename.in
433 After the file has been transferred, the connection will close automatically.
434 .Sh TALKING TO SERVERS
435 It is sometimes useful to talk to servers
436 .Dq by hand
437 rather than through a user interface.
438 It can aid in troubleshooting,
439 when it might be necessary to verify what data a server is sending
440 in response to commands issued by the client.
441 For example, to retrieve the home page of a web site:
442 .Bd -literal -offset indent
443 $ printf "GET / HTTP/1.0\er\en\er\en" | nc host.example.com 80
446 Note that this also displays the headers sent by the web server.
447 They can be filtered, using a tool such as
448 .Xr sed 1 ,
449 if necessary.
451 More complicated examples can be built up when the user knows the format
452 of requests required by the server.
453 As another example, an email may be submitted to an SMTP server using:
454 .Bd -literal -offset indent
455 $ nc localhost 25 \*(Lt\*(Lt EOF
456 HELO host.example.com
457 MAIL FROM:\*(Ltuser@host.example.com\*(Gt
458 RCPT TO:\*(Ltuser2@host.example.com\*(Gt
459 DATA
460 Body of email.
462 QUIT
465 .Sh PORT SCANNING
466 It may be useful to know which ports are open and running services on
467 a target machine.
469 .Fl z
470 flag can be used to tell
472 to report open ports,
473 rather than initiate a connection.
474 For example:
475 .Bd -literal -offset indent
476 $ nc -z host.example.com 20-30
477 Connection to host.example.com 22 port [tcp/ssh] succeeded!
478 Connection to host.example.com 25 port [tcp/smtp] succeeded!
481 The port range was specified to limit the search to ports 20 \- 30.
483 Alternatively, it might be useful to know which server software
484 is running, and which versions.
485 This information is often contained within the greeting banners.
486 In order to retrieve these, it is necessary to first make a connection,
487 and then break the connection when the banner has been retrieved.
488 This can be accomplished by specifying a small timeout with the
489 .Fl w
490 flag, or perhaps by issuing a
491 .Qq Dv QUIT
492 command to the server:
493 .Bd -literal -offset indent
494 $ echo "QUIT" | nc host.example.com 20-30
495 SSH-1.99-OpenSSH_3.6.1p2
496 Protocol mismatch.
497 220 host.example.com IMS SMTP Receiver Version 0.84 Ready
499 .Sh EXAMPLES
500 Open a TCP connection to port 42 of host.example.com, using port 31337 as
501 the source port, with a timeout of 5 seconds:
503 .Dl $ nc -p 31337 -w 5 host.example.com 42
505 Open a TCP connection to port 443 of www.example.com, and negotiate TLS with
506 any supported TLS protocol version and "compat" ciphers:
508 .Dl $ nc -cv -T protocols=all -T ciphers=compat www.example.com 443
510 Open a TCP connection to port 443 of www.google.ca, and negotiate TLS.
511 Check for a different name in the certificate for validation:
513 .Dl $ nc -cv -e adsf.au.doubleclick.net www.google.ca 443
515 Open a UDP connection to port 53 of host.example.com:
517 .Dl $ nc -u host.example.com 53
519 Open a TCP connection to port 42 of host.example.com using 10.1.2.3 as the
520 IP for the local end of the connection:
522 .Dl $ nc -s 10.1.2.3 host.example.com 42
524 Create and listen on a
525 .Ux Ns -domain
526 stream socket:
528 .Dl $ nc -lU /var/tmp/dsocket
530 Connect to port 42 of host.example.com via an HTTP proxy at 10.2.3.4,
531 port 8080.
532 This example could also be used by
533 .Xr ssh 1 ;
534 see the
535 .Cm ProxyCommand
536 directive in
537 .Xr ssh_config 5
538 for more information.
540 .Dl $ nc -x10.2.3.4:8080 -Xconnect host.example.com 42
542 The same example again, this time enabling proxy authentication with username
543 .Dq ruser
544 if the proxy requires it:
546 .Dl $ nc -x10.2.3.4:8080 -Xconnect -Pruser host.example.com 42
547 .Sh SEE ALSO
548 .Xr cat 1 ,
549 .Xr ssh 1
550 .Sh AUTHORS
551 Original implementation by *Hobbit*
552 .Aq Mt hobbit@avian.org .
554 Rewritten with IPv6 support by
555 .An Eric Jackson Aq Mt ericj@monkey.org .
556 .Sh CAVEATS
557 UDP port scans using the
558 .Fl uz
559 combination of flags will always report success irrespective of
560 the target machine's state.
561 However,
562 in conjunction with a traffic sniffer either on the target machine
563 or an intermediary device,
565 .Fl uz
566 combination could be useful for communications diagnostics.
567 Note that the amount of UDP traffic generated may be limited either
568 due to hardware resources and/or configuration settings.