1 .\" $OpenBSD: nc.1,v 1.60 2012/02/07 12:11:43 lum 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 7 2012 $
33 .Nd arbitrary TCP and UDP connections and listens
37 .Op Fl 46bCDdhklnrStUuvZz
41 .Op Fl P Ar proxy_username
42 .Op Fl p Ar source_port
45 .Op Fl T Ar toskeyword
48 .Op Fl X Ar proxy_protocol
50 .Fl x Ar proxy_address Ns Oo : Ns
61 utility is used for just about anything under the sun involving TCP,
65 It can open TCP connections, send UDP packets, listen on arbitrary
66 TCP and UDP ports, do port scanning, and deal with both IPv4 and
71 scripts nicely, and separates error messages onto standard error instead
72 of sending them to standard output, as
78 .Bl -bullet -offset indent -compact
82 shell-script based HTTP clients and servers
84 network daemon testing
86 a SOCKS or HTTP ProxyCommand for
92 The options are as follows:
97 to use IPv4 addresses only.
101 to use IPv6 addresses only.
105 Send CRLF as line-ending.
107 Enable debugging on the socket.
109 Do not attempt to read from stdin.
115 Specifies the size of the TCP receive buffer.
117 Specifies a delay time interval between lines of text sent and received.
118 Also causes a delay time between connections to multiple ports.
122 to stay listening for another connection after its current connection
124 It is an error to use this option without the
130 should listen for an incoming connection rather than initiate a
131 connection to a remote host.
132 It is an error to use this option in conjunction with the
138 Additionally, any timeouts specified with the
142 Do not do any DNS or service lookups on any specified addresses,
145 Specifies the size of the TCP send buffer.
146 .It Fl P Ar proxy_username
147 Specifies a username to present to a proxy server that requires authentication.
148 If no username is specified then authentication will not be attempted.
149 Proxy authentication is only supported for HTTP CONNECT proxies at present.
150 .It Fl p Ar source_port
151 Specifies the source port
153 should use, subject to privilege restrictions and availability.
155 after EOF on stdin, wait the specified number of seconds and then quit. If
157 is negative, wait forever.
159 Specifies that source and/or destination ports should be chosen randomly
160 instead of sequentially within a range or in the order that the system
163 Enables the RFC 2385 TCP MD5 signature option.
165 Specifies the IP of the interface which is used to send the packets.
168 datagram sockets, specifies the local temporary socket file
169 to create and use so that datagrams can be received.
170 It is an error to use this option in conjunction with the
173 .It Fl T Ar toskeyword
174 Change IPv4 TOS value.
184 or one of the DiffServ Code Points:
188 or a number in either hex or decimal.
192 to send RFC 854 DON'T and WON'T responses to RFC 854 DO and WILL requests.
193 This makes it possible to use
195 to script telnet sessions.
201 Use UDP instead of the default option of TCP.
204 sockets, use a datagram socket instead of a stream socket.
207 socket is used, a temporary receiving socket is created in
213 Set the routing table to be used.
218 give more verbose output.
220 Connections which cannot be established or are idle timeout after
225 flag has no effect on the
229 will listen forever for a connection, with or without the
232 The default is no timeout.
233 .It Fl X Ar proxy_protocol
236 should use the specified protocol when talking to the proxy server.
237 Supported protocols are
245 If the protocol is not specified, SOCKS version 5 is used.
247 .Fl x Ar proxy_address Ns Oo : Ns
260 is not specified, the well-known port for the proxy protocol is used (1080
261 for SOCKS, 3128 for HTTPS).
267 should just scan for listening daemons, without sending any data to them.
268 It is an error to use this option in conjunction with the
274 can be a numerical IP address or a symbolic hostname
278 In general, a destination must be specified,
282 (in which case the local host is used).
285 sockets, a destination is required and is the socket path to connect to
291 can be a single integer or a range of ports.
292 Ranges are in the form nn-mm.
294 a destination port must be specified,
298 .Sh CLIENT/SERVER MODEL
299 It is quite simple to build a very basic client/server model using
301 On one console, start
303 listening on a specific port for a connection.
309 is now listening on port 1234 for a connection.
311 .Pq or a second machine ,
312 connect to the machine and port being listened on:
314 .Dl $ nc 127.0.0.1 1234
316 There should now be a connection between the ports.
317 Anything typed at the second console will be concatenated to the first,
319 After the connection has been set up,
321 does not really care which side is being used as a
323 and which side is being used as a
325 The connection may be terminated using an
333 option in this netcat, but you still can execute a command after connection
334 being established by redirecting file descriptors. Be cautious here because
335 opening a port and let anyone connected execute arbitrary command on your
336 site is DANGEROUS. If you really need to do this, here is an example:
342 .Dl $ rm -f /tmp/f; mkfifo /tmp/f
343 .Dl $ cat /tmp/f | /bin/sh -i 2>&1 | nc -l 127.0.0.1 1234 > /tmp/f
349 .Dl $ nc host.example.com 1234
350 .Dl $ (shell prompt from host.example.com)
352 By doing this, you create a fifo at /tmp/f and make nc listen at port 1234
353 of address 127.0.0.1 on
357 establishes a connection successfully to that port, /bin/sh gets executed
360 side and the shell prompt is given to
364 When connection is terminated,
368 if you want it keep listening, but if the command quits this option won't
371 running. Also don't forget to remove the file descriptor once you don't need
377 The example in the previous section can be expanded to build a
378 basic data transfer model.
379 Any information input into one end of the connection will be output
380 to the other end, and input and output can be easily captured in order to
381 emulate file transfer.
385 to listen on a specific port, with output captured into a file:
387 .Dl $ nc -l 1234 \*(Gt filename.out
389 Using a second machine, connect to the listening
391 process, feeding it the file which is to be transferred:
393 .Dl $ nc host.example.com 1234 \*(Lt filename.in
395 After the file has been transferred, the connection will close automatically.
396 .Sh TALKING TO SERVERS
397 It is sometimes useful to talk to servers
399 rather than through a user interface.
400 It can aid in troubleshooting,
401 when it might be necessary to verify what data a server is sending
402 in response to commands issued by the client.
403 For example, to retrieve the home page of a web site:
404 .Bd -literal -offset indent
405 $ printf "GET / HTTP/1.0\er\en\er\en" | nc host.example.com 80
408 Note that this also displays the headers sent by the web server.
409 They can be filtered, using a tool such as
413 More complicated examples can be built up when the user knows the format
414 of requests required by the server.
415 As another example, an email may be submitted to an SMTP server using:
416 .Bd -literal -offset indent
417 $ nc [\-C] localhost 25 \*(Lt\*(Lt EOF
418 HELO host.example.com
419 MAIL FROM:\*(Ltuser@host.example.com\*(Gt
420 RCPT TO:\*(Ltuser2@host.example.com\*(Gt
428 It may be useful to know which ports are open and running services on
432 flag can be used to tell
434 to report open ports,
435 rather than initiate a connection. Usually it's useful to turn on verbose
436 output to stderr by use this option in conjunction with
441 .Bd -literal -offset indent
442 $ nc \-zv host.example.com 20-30
443 Connection to host.example.com 22 port [tcp/ssh] succeeded!
444 Connection to host.example.com 25 port [tcp/smtp] succeeded!
447 The port range was specified to limit the search to ports 20 \- 30, and is
448 scanned by increasing order.
450 You can also specify a list of ports to scan, for example:
451 .Bd -literal -offset indent
452 $ nc \-zv host.example.com 80 20 22
453 nc: connect to host.example.com 80 (tcp) failed: Connection refused
454 nc: connect to host.example.com 20 (tcp) failed: Connection refused
455 Connection to host.example.com port [tcp/ssh] succeeded!
458 The ports are scanned by the order you given.
460 Alternatively, it might be useful to know which server software
461 is running, and which versions.
462 This information is often contained within the greeting banners.
463 In order to retrieve these, it is necessary to first make a connection,
464 and then break the connection when the banner has been retrieved.
465 This can be accomplished by specifying a small timeout with the
467 flag, or perhaps by issuing a
469 command to the server:
470 .Bd -literal -offset indent
471 $ echo "QUIT" | nc host.example.com 20-30
472 SSH-1.99-OpenSSH_3.6.1p2
474 220 host.example.com IMS SMTP Receiver Version 0.84 Ready
477 Open a TCP connection to port 42 of host.example.com, using port 31337 as
478 the source port, with a timeout of 5 seconds:
480 .Dl $ nc -p 31337 -w 5 host.example.com 42
482 Open a UDP connection to port 53 of host.example.com:
484 .Dl $ nc -u host.example.com 53
486 Open a TCP connection to port 42 of host.example.com using 10.1.2.3 as the
487 IP for the local end of the connection:
489 .Dl $ nc -s 10.1.2.3 host.example.com 42
491 Create and listen on a
495 .Dl $ nc -lU /var/tmp/dsocket
497 Connect to port 42 of host.example.com via an HTTP proxy at 10.2.3.4,
499 This example could also be used by
505 for more information.
507 .Dl $ nc -x10.2.3.4:8080 -Xconnect host.example.com 42
509 The same example again, this time enabling proxy authentication with username
511 if the proxy requires it:
513 .Dl $ nc -x10.2.3.4:8080 -Xconnect -Pruser host.example.com 42
518 Original implementation by *Hobbit*
519 .Aq hobbit@avian.org .
521 Rewritten with IPv6 support by
522 .An Eric Jackson Aq ericj@monkey.org .
524 Modified for Debian port by Aron Xu
525 .Aq aron@debian.org .
527 UDP port scans using the
529 combination of flags will always report success irrespective of
530 the target machine's state.
532 in conjunction with a traffic sniffer either on the target machine
533 or an intermediary device,
536 combination could be useful for communications diagnostics.
537 Note that the amount of UDP traffic generated may be limited either
538 due to hardware resources and/or configuration settings.