1 .\" $OpenBSD: nc.1,v 1.48 2008/09/19 13:24:41 sobrado 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.
35 .Nd arbitrary TCP and UDP connections and listens
39 .Op Fl 46DdEhklnorStUuvz
40 .Op Fl e Ar IPsec_policy
45 .Op Fl P Ar proxy_username
46 .Op Fl p Ar source_port
47 .Op Fl s Ar source_ip_address
50 .Op Fl X Ar proxy_protocol
52 .Fl x Ar proxy_address Ns Oo : Ns
63 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.
103 Enable debugging on the socket.
105 Do not attempt to read from stdin.
109 .Li "-e 'in ipsec esp/transport//require'"
110 .Li "-e 'out ipsec esp/transport//require'"
112 which enables IPsec ESP transport mode in both
115 If IPsec support is available, then one can specify the IPsec policies
116 to be used using the syntax described in
117 .Xr ipsec_set_policy 3 .
118 This flag can be specified up to two times, as typically one policy for
119 each direction is needed.
125 Specifies the size of the TCP receive buffer.
127 Specifies a delay time interval between lines of text sent and received.
128 Also causes a delay time between connections to multiple ports.
132 to stay listening for another connection after its current connection
134 It is an error to use this option without the
140 should listen for an incoming connection rather than initiate a
141 connection to a remote host.
142 It is an error to use this option in conjunction with the
148 Additionally, any timeouts specified with the
152 Do not do any DNS or service lookups on any specified addresses,
155 Disables the use of TCP options on the socket, by setting the boolean
159 Specifies the size of the TCP send buffer.
165 does not terminate on EOF condition on input,
166 but continues until the network side has been closed down.
169 will make it terminate on EOF as well.
170 .It Fl P Ar proxy_username
171 Specifies a username to present to a proxy server that requires authentication.
172 If no username is specified then authentication will not be attempted.
173 Proxy authentication is only supported for HTTP CONNECT proxies at present.
174 .It Fl p Ar source_port
175 Specifies the source port
177 should use, subject to privilege restrictions and availability.
178 It is an error to use this option in conjunction with the
182 Specifies that source and/or destination ports should be chosen randomly
183 instead of sequentially within a range or in the order that the system
186 Enables the RFC 2385 TCP MD5 signature option.
187 .It Fl s Ar source_ip_address
188 Specifies the IP of the interface which is used to send the packets.
189 It is an error to use this option in conjunction with the
193 Specifies IP Type of Service (ToS) for the connection.
194 Valid values are the tokens
198 or an 8-bit hexadecimal value preceded by
203 to send RFC 854 DON'T and WON'T responses to RFC 854 DO and WILL requests.
204 This makes it possible to use
206 to script telnet sessions.
208 Specifies to use Unix Domain Sockets.
210 Use UDP instead of the default option of TCP.
214 give more verbose output.
216 If a connection and stdin are idle for more than
218 seconds, then the connection is silently closed.
221 flag has no effect on the
225 will listen forever for a connection, with or without the
228 The default is no timeout.
229 .It Fl X Ar proxy_protocol
232 should use the specified protocol when talking to the proxy server.
233 Supported protocols are
241 If the protocol is not specified, SOCKS version 5 is used.
243 .Fl x Ar proxy_address Ns Oo : Ns
256 is not specified, the well-known port for the proxy protocol is used (1080
257 for SOCKS, 3128 for HTTPS).
261 should just scan for listening daemons, without sending any data to them.
262 It is an error to use this option in conjunction with the
268 can be a numerical IP address or a symbolic hostname
272 In general, a hostname must be specified,
276 (in which case the local host is used).
279 can be a single integer or a range of ports.
280 Ranges are in the form nn-mm.
282 a destination port must be specified,
286 (in which case a socket must be specified).
287 .Sh CLIENT/SERVER MODEL
288 It is quite simple to build a very basic client/server model using
290 On one console, start
292 listening on a specific port for a connection.
298 is now listening on port 1234 for a connection.
300 .Pq or a second machine ,
301 connect to the machine and port being listened on:
303 .Dl $ nc 127.0.0.1 1234
305 There should now be a connection between the ports.
306 Anything typed at the second console will be concatenated to the first,
308 After the connection has been set up,
310 does not really care which side is being used as a
312 and which side is being used as a
314 The connection may be terminated using an
318 The example in the previous section can be expanded to build a
319 basic data transfer model.
320 Any information input into one end of the connection will be output
321 to the other end, and input and output can be easily captured in order to
322 emulate file transfer.
326 to listen on a specific port, with output captured into a file:
328 .Dl $ nc -l 1234 \*(Gt filename.out
330 Using a second machine, connect to the listening
332 process, feeding it the file which is to be transferred:
334 .Dl $ nc host.example.com 1234 \*(Lt filename.in
336 After the file has been transferred, the connection will close automatically.
337 .Sh TALKING TO SERVERS
338 It is sometimes useful to talk to servers
340 rather than through a user interface.
341 It can aid in troubleshooting,
342 when it might be necessary to verify what data a server is sending
343 in response to commands issued by the client.
344 For example, to retrieve the home page of a web site:
345 .Bd -literal -offset indent
346 $ echo -n "GET / HTTP/1.0\er\en\er\en" | nc host.example.com 80
349 Note that this also displays the headers sent by the web server.
350 They can be filtered, using a tool such as
354 More complicated examples can be built up when the user knows the format
355 of requests required by the server.
356 As another example, an email may be submitted to an SMTP server using:
357 .Bd -literal -offset indent
358 $ nc localhost 25 \*(Lt\*(Lt EOF
359 HELO host.example.com
360 MAIL FROM:\*(Ltuser@host.example.com\*(Gt
361 RCPT TO:\*(Ltuser2@host.example.com\*(Gt
369 It may be useful to know which ports are open and running services on
373 flag can be used to tell
375 to report open ports,
376 rather than initiate a connection.
378 .Bd -literal -offset indent
379 $ nc -z host.example.com 20-30
380 Connection to host.example.com 22 port [tcp/ssh] succeeded!
381 Connection to host.example.com 25 port [tcp/smtp] succeeded!
384 The port range was specified to limit the search to ports 20 \- 30.
386 Alternatively, it might be useful to know which server software
387 is running, and which versions.
388 This information is often contained within the greeting banners.
389 In order to retrieve these, it is necessary to first make a connection,
390 and then break the connection when the banner has been retrieved.
391 This can be accomplished by specifying a small timeout with the
393 flag, or perhaps by issuing a
395 command to the server:
396 .Bd -literal -offset indent
397 $ echo "QUIT" | nc host.example.com 20-30
398 SSH-1.99-OpenSSH_3.6.1p2
400 220 host.example.com IMS SMTP Receiver Version 0.84 Ready
403 Open a TCP connection to port 42 of host.example.com, using port 31337 as
404 the source port, with a timeout of 5 seconds:
406 .Dl $ nc -p 31337 -w 5 host.example.com 42
408 Open a UDP connection to port 53 of host.example.com:
410 .Dl $ nc -u host.example.com 53
412 Open a TCP connection to port 42 of host.example.com using 10.1.2.3 as the
413 IP for the local end of the connection:
415 .Dl $ nc -s 10.1.2.3 host.example.com 42
417 Open a TCP connection to port 42 of host.example.com using IPsec ESP for
418 incoming and outgoing traffic.
420 .Dl $ nc -E host.example.com 42
422 Open a TCP connection to port 42 of host.example.com using IPsec ESP for
423 outgoing traffic only.
425 .Dl $ nc -e 'out ipsec esp/transport//require' host.example.com 42
427 Create and listen on a Unix Domain Socket:
429 .Dl $ nc -lU /var/tmp/dsocket
431 Connect to port 42 of host.example.com via an HTTP proxy at 10.2.3.4,
433 This example could also be used by
439 for more information.
441 .Dl $ nc -x10.2.3.4:8080 -Xconnect host.example.com 42
443 The same example again, this time enabling proxy authentication with username
445 if the proxy requires it:
447 .Dl $ nc -x10.2.3.4:8080 -Xconnect -Pruser host.example.com 42
455 Original implementation by *Hobbit*
456 .Aq hobbit@avian.org .
458 Rewritten with IPv6 support by
459 .An Eric Jackson Aq ericj@monkey.org .
461 UDP port scans will always succeed
462 (i.e. report the port as open),
465 combination of flags relatively useless.