1 .\" $OpenBSD: nc.1,v 1.95 2020/02/12 14:46:36 schwarze 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: February 12 2020 $
33 .Nd arbitrary TCP and UDP connections and listens
36 .Op Fl 46bCDdFhklNnrStUuvZz
42 .Op Fl P Ar proxy_username
43 .Op Fl p Ar source_port
45 .Op Fl s Ar sourceaddr
50 .Op Fl X Ar proxy_protocol
51 .Op Fl x Ar proxy_address Ns Op : Ns Ar port
59 utility is used for just about anything under the sun involving TCP,
63 It can open TCP connections, send UDP packets, listen on arbitrary
64 TCP and UDP ports, do port scanning, and deal with both IPv4 and
69 scripts nicely, and separates error messages onto standard error instead
70 of sending them to standard output, as
76 .Bl -bullet -offset indent -compact
80 shell-script based HTTP clients and servers
82 network daemon testing
84 a SOCKS or HTTP ProxyCommand for
90 The options are as follows:
93 Use IPv4 addresses only.
95 Use IPv6 addresses only.
99 Send CRLF as line-ending. Each line feed (LF) character from the input
100 data is translated into CR+LF before being written to the socket. Line
101 feed characters that are already preceded with a carriage return (CR)
102 are not translated. Received data is not affected.
104 Enable debugging on the socket.
106 Do not attempt to read from stdin.
108 Pass the first connected socket using
111 This is useful in conjunction with
115 perform connection setup with a proxy but then leave the rest of the
116 connection to another program (e.g.\&
129 Specify the size of the TCP receive buffer.
133 seconds between lines of text sent and received.
134 Also causes a delay time between connections to multiple ports.
136 When a connection is completed, listen for another one.
139 When used together with the
141 option, the server socket is not connected and it can receive UDP datagrams from
144 Listen for an incoming connection rather than initiating a
145 connection to a remote host.
150 to listen on can be specified either as non-optional arguments, or with
156 Cannot be used together with
160 Additionally, any timeouts specified with the
164 Set the TTL / hop limit of outgoing packets.
166 Ask the kernel to drop incoming packets whose TTL / hop limit is under
170 the network socket after EOF on the input.
171 Some servers require this to finish their work.
173 Do not perform domain name resolution.
174 If a name cannot be resolved without DNS, an error will be reported.
176 Specify the size of the TCP send buffer.
177 .It Fl P Ar proxy_username
178 Specifies a username to present to a proxy server that requires authentication.
179 If no username is specified then authentication will not be attempted.
180 Proxy authentication is only supported for HTTP CONNECT proxies at present.
181 .It Fl p Ar source_port
182 Specify the source port
184 should use, subject to privilege restrictions and availability.
186 after EOF on stdin, wait the specified number of
190 is negative, wait forever (default). Specifying a non-negative
195 Choose source and/or destination ports randomly
196 instead of sequentially within a range or in the order that the system
199 Enable the RFC 2385 TCP MD5 signature option.
200 .It Fl s Ar sourceaddr
201 Set the source address to send packets from,
202 which is useful on machines with multiple interfaces.
205 datagram sockets, specifies the local temporary socket file
206 to create and use so that datagrams can be received.
207 Cannot be used together with
210 Change the IPv4 TOS/IPv6 traffic class value.
220 or one of the DiffServ Code Points:
222 .Cm af11 No ... Cm af43 ,
223 .Cm cs0 No ... Cm cs7 ;
224 or a number in either hex or decimal.
226 Send RFC 854 DON'T and WON'T responses to RFC 854 DO and WILL requests.
227 This makes it possible to use
229 to script telnet sessions.
234 Cannot be used together with
239 Use UDP instead of TCP.
240 Cannot be used together with
244 sockets, use a datagram socket instead of a stream socket.
247 socket is used, a temporary receiving socket is created in
253 Set the routing table to be used.
255 Produce more verbose output.
256 .It Fl W Ar recvlimit
257 Terminate after receiving
259 packets from the network.
261 Connections which cannot be established or are idle timeout after
266 flag has no effect on the
270 will listen forever for a connection, with or without the
273 The default is no timeout.
274 .It Fl X Ar proxy_protocol
277 when talking to the proxy server.
278 Supported protocols are
286 If the protocol is not specified, SOCKS version 5 is used.
287 .It Fl x Ar proxy_address Ns Op : Ns Ar port
296 is not specified, the well-known port for the proxy protocol is used (1080
297 for SOCKS, 3128 for HTTPS).
298 An IPv6 address can be specified unambiguously by enclosing
301 A proxy cannot be used with any of the options
306 Only scan for listening daemons, without sending any data to them.
307 Cannot be used together with
312 can be a numerical IP address or a symbolic hostname
316 In general, a destination must be specified,
320 (in which case the local host is used).
323 sockets, a destination is required and is the socket path to connect to
329 can be specified as a numeric port number or as a service name.
330 Port ranges may be specified as numeric port numbers of the form
331 .Ar nn Ns - Ns Ar mm .
333 a destination port must be specified,
337 .Sh CLIENT/SERVER MODEL
338 It is quite simple to build a very basic client/server model using
340 On one console, start
342 listening on a specific port for a connection.
348 is now listening on port 1234 for a connection.
350 .Pq or a second machine ,
351 connect to the machine and port being listened on:
353 .Dl $ nc 127.0.0.1 1234
355 There should now be a connection between the ports.
356 Anything typed at the second console will be concatenated to the first,
358 After the connection has been set up,
360 does not really care which side is being used as a
362 and which side is being used as a
364 The connection may be terminated using an
372 option in this netcat, but you still can execute a command after connection
373 being established by redirecting file descriptors. Be cautious here because
374 opening a port and let anyone connected execute arbitrary command on your
375 site is DANGEROUS. If you really need to do this, here is an example:
381 .Dl $ rm -f /tmp/f; mkfifo /tmp/f
382 .Dl $ cat /tmp/f | /bin/sh -i 2>&1 | nc -l 127.0.0.1 1234 > /tmp/f
388 .Dl $ nc host.example.com 1234
389 .Dl $ (shell prompt from host.example.com)
391 By doing this, you create a fifo at /tmp/f and make nc listen at port 1234
392 of address 127.0.0.1 on
396 establishes a connection successfully to that port, /bin/sh gets executed
399 side and the shell prompt is given to
403 When connection is terminated,
407 if you want it keep listening, but if the command quits this option won't
410 running. Also don't forget to remove the file descriptor once you don't need
416 The example in the previous section can be expanded to build a
417 basic data transfer model.
418 Any information input into one end of the connection will be output
419 to the other end, and input and output can be easily captured in order to
420 emulate file transfer.
424 to listen on a specific port, with output captured into a file:
426 .Dl $ nc -l 1234 \*(Gt filename.out
428 Using a second machine, connect to the listening
430 process, feeding it the file which is to be transferred:
432 .Dl $ nc -N host.example.com 1234 \*(Lt filename.in
434 After the file has been transferred, the connection will close automatically.
435 .Sh TALKING TO SERVERS
436 It is sometimes useful to talk to servers
438 rather than through a user interface.
439 It can aid in troubleshooting,
440 when it might be necessary to verify what data a server is sending
441 in response to commands issued by the client.
442 For example, to retrieve the home page of a web site:
443 .Bd -literal -offset indent
444 $ printf "GET / HTTP/1.0\er\en\er\en" | nc host.example.com 80
447 Note that this also displays the headers sent by the web server.
448 They can be filtered, using a tool such as
452 More complicated examples can be built up when the user knows the format
453 of requests required by the server.
454 As another example, an email may be submitted to an SMTP server using:
455 .Bd -literal -offset indent
456 $ nc [\-C] localhost 25 \*(Lt\*(Lt EOF
457 HELO host.example.com
458 MAIL FROM:\*(Ltuser@host.example.com\*(Gt
459 RCPT TO:\*(Ltuser2@host.example.com\*(Gt
467 It may be useful to know which ports are open and running services on
471 flag can be used to tell
473 to report open ports,
474 rather than initiate a connection. Usually it's useful to turn on verbose
475 output to stderr by use this option in conjunction with
480 .Bd -literal -offset indent
481 $ nc \-zv host.example.com 20-30
482 Connection to host.example.com 22 port [tcp/ssh] succeeded!
483 Connection to host.example.com 25 port [tcp/smtp] succeeded!
486 The port range was specified to limit the search to ports 20 \- 30, and is
487 scanned by increasing order (unless the
491 You can also specify a list of ports to scan, for example:
492 .Bd -literal -offset indent
493 $ nc \-zv host.example.com http 20 22-23
494 nc: connect to host.example.com 80 (tcp) failed: Connection refused
495 nc: connect to host.example.com 20 (tcp) failed: Connection refused
496 Connection to host.example.com port [tcp/ssh] succeeded!
497 nc: connect to host.example.com 23 (tcp) failed: Connection refused
500 The ports are scanned by the order you given (unless the
504 Alternatively, it might be useful to know which server software
505 is running, and which versions.
506 This information is often contained within the greeting banners.
507 In order to retrieve these, it is necessary to first make a connection,
508 and then break the connection when the banner has been retrieved.
509 This can be accomplished by specifying a small timeout with the
511 flag, or perhaps by issuing a
513 command to the server:
514 .Bd -literal -offset indent
515 $ echo "QUIT" | nc host.example.com 20-30
516 SSH-1.99-OpenSSH_3.6.1p2
518 220 host.example.com IMS SMTP Receiver Version 0.84 Ready
521 Open a TCP connection to port 42 of host.example.com, using port 31337 as
522 the source port, with a timeout of 5 seconds:
524 .Dl $ nc -p 31337 -w 5 host.example.com 42
526 Open a UDP connection to port 53 of host.example.com:
528 .Dl $ nc -u host.example.com 53
530 Open a TCP connection to port 42 of host.example.com using 10.1.2.3 as the
531 IP for the local end of the connection:
533 .Dl $ nc -s 10.1.2.3 host.example.com 42
535 Create and listen on a
539 .Dl $ nc -lU /var/tmp/dsocket
541 Connect to port 42 of host.example.com via an HTTP proxy at 10.2.3.4,
543 This example could also be used by
549 for more information.
551 .Dl $ nc -x10.2.3.4:8080 -Xconnect host.example.com 42
553 The same example again, this time enabling proxy authentication with username
555 if the proxy requires it:
557 .Dl $ nc -x10.2.3.4:8080 -Xconnect -Pruser host.example.com 42
562 Original implementation by
563 .An *Hobbit* Aq Mt hobbit@avian.org .
565 Rewritten with IPv6 support by
566 .An Eric Jackson Aq Mt ericj@monkey.org .
568 Modified for Debian port by Aron Xu
569 .Aq aron@debian.org .
571 UDP port scans using the
573 combination of flags will always report success irrespective of
574 the target machine's state.
576 in conjunction with a traffic sniffer either on the target machine
577 or an intermediary device,
580 combination could be useful for communications diagnostics.
581 Note that the amount of UDP traffic generated may be limited either
582 due to hardware resources and/or configuration settings.