1 .\" $OpenBSD: nc.1,v 1.91 2018/09/25 20:05:07 jmc Exp $
3 .\" Copyright (c) 1996 David Sacerdote
4 .\" All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
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
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.
28 .Dd $Mdocdate: September 25 2018 $
33 .Nd arbitrary TCP and UDP connections and listens
36 .Op Fl 46cDdFhklNnrStUuvz
46 .Op Fl o Ar staplefile
47 .Op Fl P Ar proxy_username
48 .Op Fl p Ar source_port
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
65 utility is used for just about anything under the sun involving TCP,
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
75 scripts nicely, and separates error messages onto standard error instead
76 of sending them to standard output, as
82 .Bl -bullet -offset indent -compact
86 shell-script based HTTP clients and servers
88 network daemon testing
90 a SOCKS or HTTP ProxyCommand for
96 The options are as follows:
99 Use IPv4 addresses only.
101 Use IPv6 addresses only.
103 Load the public key part of the TLS peer certificate from
109 Use TLS to connect or listen.
110 Cannot be used together with any of the options
113 Enable debugging on the socket.
115 Do not attempt to read from stdin.
117 Only accept the TLS peer certificate if it contains the
125 Pass the first connected socket using
128 This is useful in conjunction with
132 perform connection setup with a proxy but then leave the rest of the
133 connection to another program (e.g.\&
144 Only accept the TLS peer certificate if its hash returned from
145 .Xr tls_peer_cert_hash 3
150 and cannot be used with
157 Specify the size of the TCP receive buffer.
161 seconds between lines of text sent and received.
162 Also causes a delay time between connections to multiple ports.
164 Load the TLS private key from
170 When a connection is completed, listen for another one.
173 When used together with the
175 option, the server socket is not connected and it can receive UDP datagrams from
178 Listen for an incoming connection rather than initiating a
179 connection to a remote host.
180 Cannot be used together with any of the options
182 Additionally, any timeouts specified with the
186 Set the TTL / hop limit of outgoing packets.
188 Ask the kernel to drop incoming packets whose TTL / hop limit is under
192 the network socket after EOF on the input.
193 Some servers require this to finish their work.
195 Do not do any DNS or service lookups on any specified addresses,
198 Specify the size of the TCP send buffer.
199 .It Fl o Ar staplefile
200 During the TLS handshake, load data to be stapled from
202 which is expected to contain an OCSP response from an OCSP server in
208 .It Fl P Ar proxy_username
209 Specifies a username to present to a proxy server that requires authentication.
210 If no username is specified then authentication will not be attempted.
211 Proxy authentication is only supported for HTTP CONNECT proxies at present.
212 .It Fl p Ar source_port
213 Specify the source port
215 should use, subject to privilege restrictions and availability.
216 Cannot be used together with
219 Load the root CA bundle for TLS certificate verification from
221 in PEM format, instead of
222 .Pa /etc/ssl/cert.pem .
226 Choose source and/or destination ports randomly
227 instead of sequentially within a range or in the order that the system
230 Enable the RFC 2385 TCP MD5 signature option.
232 Send packets from the interface with the
237 datagram sockets, specifies the local temporary socket file
238 to create and use so that datagrams can be received.
239 Cannot be used together with
244 Change the IPv4 TOS/IPv6 traffic class value or the TLS options.
250 which disables certificate verification;
252 which disables certificate name checking;
254 which requires a client certificate on incoming connections; or
256 which requires the peer to provide a valid stapled OCSP response
258 The following TLS options specify a value in the form of a
259 .Ar key Ns = Ns Ar value
262 which allows the supported TLS ciphers to be specified (see
263 .Xr tls_config_set_ciphers 3
264 for further details);
266 which allows the supported TLS protocols to be specified (see
267 .Xr tls_config_parse_protocols 3
268 for further details).
269 Specifying TLS options requires
272 For the IPv4 TOS/IPv6 traffic class value,
281 or one of the DiffServ Code Points:
283 .Cm af11 No ... Cm af43 ,
284 .Cm cs0 No ... Cm cs7 ;
285 or a number in either hex or decimal.
287 Send RFC 854 DON'T and WON'T responses to RFC 854 DO and WILL requests.
288 This makes it possible to use
290 to script telnet sessions.
295 Cannot be used together with any of the options
298 Use UDP instead of TCP.
299 Cannot be used together with
305 sockets, use a datagram socket instead of a stream socket.
308 socket is used, a temporary receiving socket is created in
314 Set the routing table to be used.
316 Produce more verbose output.
317 .It Fl W Ar recvlimit
318 Terminate after receiving
320 packets from the network.
322 Connections which cannot be established or are idle timeout after
327 flag has no effect on the
331 will listen forever for a connection, with or without the
334 The default is no timeout.
335 .It Fl X Ar proxy_protocol
338 when talking to the proxy server.
339 Supported protocols are
347 If the protocol is not specified, SOCKS version 5 is used.
348 .It Fl x Ar proxy_address Ns Op : Ns Ar port
357 is not specified, the well-known port for the proxy protocol is used (1080
358 for SOCKS, 3128 for HTTPS).
359 An IPv6 address can be specified unambiguously by enclosing
362 A proxy cannot be used with any of the options
364 .It Fl Z Ar peercertfile
365 Save the peer certificates to
371 Only scan for listening daemons, without sending any data to them.
372 Cannot be used together with
377 can be a numerical IP address or a symbolic hostname
381 In general, a destination must be specified,
385 (in which case the local host is used).
388 sockets, a destination is required and is the socket path to connect to
394 can be a specified as a numeric port number, or as a service name.
395 Ports may be specified in a range of the form
396 .Ar nn Ns - Ns Ar mm .
398 a destination port must be specified,
402 .Sh CLIENT/SERVER MODEL
403 It is quite simple to build a very basic client/server model using
405 On one console, start
407 listening on a specific port for a connection.
413 is now listening on port 1234 for a connection.
415 .Pq or a second machine ,
416 connect to the machine and port being listened on:
418 .Dl $ nc 127.0.0.1 1234
420 There should now be a connection between the ports.
421 Anything typed at the second console will be concatenated to the first,
423 After the connection has been set up,
425 does not really care which side is being used as a
427 and which side is being used as a
429 The connection may be terminated using an
433 The example in the previous section can be expanded to build a
434 basic data transfer model.
435 Any information input into one end of the connection will be output
436 to the other end, and input and output can be easily captured in order to
437 emulate file transfer.
441 to listen on a specific port, with output captured into a file:
443 .Dl $ nc -l 1234 \*(Gt filename.out
445 Using a second machine, connect to the listening
447 process, feeding it the file which is to be transferred:
449 .Dl $ nc -N host.example.com 1234 \*(Lt filename.in
451 After the file has been transferred, the connection will close automatically.
452 .Sh TALKING TO SERVERS
453 It is sometimes useful to talk to servers
455 rather than through a user interface.
456 It can aid in troubleshooting,
457 when it might be necessary to verify what data a server is sending
458 in response to commands issued by the client.
459 For example, to retrieve the home page of a web site:
460 .Bd -literal -offset indent
461 $ printf "GET / HTTP/1.0\er\en\er\en" | nc host.example.com 80
464 Note that this also displays the headers sent by the web server.
465 They can be filtered, using a tool such as
469 More complicated examples can be built up when the user knows the format
470 of requests required by the server.
471 As another example, an email may be submitted to an SMTP server using:
472 .Bd -literal -offset indent
473 $ nc localhost 25 \*(Lt\*(Lt EOF
474 HELO host.example.com
475 MAIL FROM:\*(Ltuser@host.example.com\*(Gt
476 RCPT TO:\*(Ltuser2@host.example.com\*(Gt
484 It may be useful to know which ports are open and running services on
488 flag can be used to tell
490 to report open ports,
491 rather than initiate a connection.
493 .Bd -literal -offset indent
494 $ nc -z host.example.com 20-30
495 Connection to host.example.com 22 port [tcp/ssh] succeeded!
496 Connection to host.example.com 25 port [tcp/smtp] succeeded!
499 The port range was specified to limit the search to ports 20 \- 30.
501 Alternatively, it might be useful to know which server software
502 is running, and which versions.
503 This information is often contained within the greeting banners.
504 In order to retrieve these, it is necessary to first make a connection,
505 and then break the connection when the banner has been retrieved.
506 This can be accomplished by specifying a small timeout with the
508 flag, or perhaps by issuing a
510 command to the server:
511 .Bd -literal -offset indent
512 $ echo "QUIT" | nc host.example.com 20-30
513 SSH-1.99-OpenSSH_3.6.1p2
515 220 host.example.com IMS SMTP Receiver Version 0.84 Ready
518 Open a TCP connection to port 42 of host.example.com, using port 31337 as
519 the source port, with a timeout of 5 seconds:
521 .Dl $ nc -p 31337 -w 5 host.example.com 42
523 Open a TCP connection to port 443 of www.example.com, and negotiate TLS with
524 any supported TLS protocol version and "compat" ciphers:
526 .Dl $ nc -cv -T protocols=all -T ciphers=compat www.example.com 443
528 Open a TCP connection to port 443 of www.google.ca, and negotiate TLS.
529 Check for a different name in the certificate for validation:
531 .Dl $ nc -cv -e adsf.au.doubleclick.net www.google.ca 443
533 Open a UDP connection to port 53 of host.example.com:
535 .Dl $ nc -u host.example.com 53
537 Open a TCP connection to port 42 of host.example.com using 10.1.2.3 as the
538 IP for the local end of the connection:
540 .Dl $ nc -s 10.1.2.3 host.example.com 42
542 Create and listen on a
546 .Dl $ nc -lU /var/tmp/dsocket
548 Connect to port 42 of host.example.com via an HTTP proxy at 10.2.3.4,
550 This example could also be used by
556 for more information.
558 .Dl $ nc -x10.2.3.4:8080 -Xconnect host.example.com 42
560 The same example again, this time enabling proxy authentication with username
562 if the proxy requires it:
564 .Dl $ nc -x10.2.3.4:8080 -Xconnect -Pruser host.example.com 42
569 Original implementation by
570 .An *Hobbit* Aq Mt hobbit@avian.org .
572 Rewritten with IPv6 support by
573 .An Eric Jackson Aq Mt ericj@monkey.org .
575 UDP port scans using the
577 combination of flags will always report success irrespective of
578 the target machine's state.
580 in conjunction with a traffic sniffer either on the target machine
581 or an intermediary device,
584 combination could be useful for communications diagnostics.
585 Note that the amount of UDP traffic generated may be limited either
586 due to hardware resources and/or configuration settings.