1 .\" $OpenBSD: nc.1,v 1.45 2007/05/31 19:20:13 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.
35 .Nd arbitrary TCP and UDP connections and listens
39 .Op Fl 46DdEhklnOorStUuvz
40 .Op Fl e Ar IPsec_policy
42 .Op Fl P Ar proxy_username
43 .Op Fl p Ar source_port
44 .Op Fl s Ar source_ip_address
47 .Op Fl X Ar proxy_protocol
49 .Fl x Ar proxy_address Ns Oo : Ns
53 .Op Ar port Ns Bq Ar s
60 utility is used for just about anything under the sun involving TCP
62 It can open TCP connections, send UDP packets, listen on arbitrary
63 TCP and UDP ports, do port scanning, and deal with both IPv4 and
68 scripts nicely, and separates error messages onto standard error instead
69 of sending them to standard output, as
75 .Bl -bullet -offset indent -compact
79 shell-script based HTTP clients and servers
81 network daemon testing
83 a SOCKS or HTTP ProxyCommand for
89 The options are as follows:
94 to use IPv4 addresses only.
98 to use IPv6 addresses only.
100 Enable debugging on the socket.
102 Do not attempt to read from stdin.
106 .Li "-e 'in ipsec esp/transport//require'"
107 .Li "-e 'out ipsec esp/transport//require'"
109 which enables IPsec ESP transport mode in both
112 If IPsec support is available, then one can specify the IPsec policies
113 to be used using the syntax described in
114 .Xr ipsec_set_policy 3 .
115 This flag can be specified up to two times, as typically one policy for
116 each direction is needed.
122 Specifies a delay time interval between lines of text sent and received.
123 Also causes a delay time between connections to multiple ports.
127 to stay listening for another connection after its current connection
129 It is an error to use this option without the
135 should listen for an incoming connection rather than initiate a
136 connection to a remote host.
137 It is an error to use this option in conjunction with the
143 Additionally, any timeouts specified with the
147 Do not do any DNS or service lookups on any specified addresses,
150 Disables the use of TCP options on the socket, by setting the boolean
157 does not terminate on EOF condition on input,
158 but continues until the network side has been closed down.
161 will make it terminate on EOF as well.
162 .It Fl P Ar proxy_username
163 Specifies a username to present to a proxy server that requires authentication.
164 If no username is specified then authentication will not be attempted.
165 Proxy authentication is only supported for HTTP CONNECT proxies at present.
166 .It Fl p Ar source_port
167 Specifies the source port
169 should use, subject to privilege restrictions and availability.
170 It is an error to use this option in conjunction with the
174 Specifies that source and/or destination ports should be chosen randomly
175 instead of sequentially within a range or in the order that the system
178 Enables the RFC 2385 TCP MD5 signature option.
179 .It Fl s Ar source_ip_address
180 Specifies the IP of the interface which is used to send the packets.
181 It is an error to use this option in conjunction with the
185 Specifies IP Type of Service (ToS) for the connection.
186 Valid values are the tokens
190 or an 8-bit hexadecimal value preceded by
195 to send RFC 854 DON'T and WON'T responses to RFC 854 DO and WILL requests.
196 This makes it possible to use
198 to script telnet sessions.
200 Specifies to use Unix Domain Sockets.
202 Use UDP instead of the default option of TCP.
206 give more verbose output.
208 If a connection and stdin are idle for more than
210 seconds, then the connection is silently closed.
213 flag has no effect on the
217 will listen forever for a connection, with or without the
220 The default is no timeout.
221 .It Fl X Ar proxy_protocol
224 should use the specified protocol when talking to the proxy server.
225 Supported protocols are
233 If the protocol is not specified, SOCKS version 5 is used.
235 .Fl x Ar proxy_address Ns Oo : Ns
248 is not specified, the well-known port for the proxy protocol is used (1080
249 for SOCKS, 3128 for HTTPS).
253 should just scan for listening daemons, without sending any data to them.
254 It is an error to use this option in conjunction with the
260 can be a numerical IP address or a symbolic hostname
264 In general, a hostname must be specified,
268 (in which case the local host is used).
271 can be single integers or ranges.
272 Ranges are in the form nn-mm.
274 a destination port must be specified,
278 (in which case a socket must be specified).
279 .Sh CLIENT/SERVER MODEL
280 It is quite simple to build a very basic client/server model using
282 On one console, start
284 listening on a specific port for a connection.
290 is now listening on port 1234 for a connection.
292 .Pq or a second machine ,
293 connect to the machine and port being listened on:
295 .Dl $ nc 127.0.0.1 1234
297 There should now be a connection between the ports.
298 Anything typed at the second console will be concatenated to the first,
300 After the connection has been set up,
302 does not really care which side is being used as a
304 and which side is being used as a
306 The connection may be terminated using an
310 The example in the previous section can be expanded to build a
311 basic data transfer model.
312 Any information input into one end of the connection will be output
313 to the other end, and input and output can be easily captured in order to
314 emulate file transfer.
318 to listen on a specific port, with output captured into a file:
320 .Dl $ nc -l 1234 \*(Gt filename.out
322 Using a second machine, connect to the listening
324 process, feeding it the file which is to be transferred:
326 .Dl $ nc host.example.com 1234 \*(Lt filename.in
328 After the file has been transferred, the connection will close automatically.
329 .Sh TALKING TO SERVERS
330 It is sometimes useful to talk to servers
332 rather than through a user interface.
333 It can aid in troubleshooting,
334 when it might be necessary to verify what data a server is sending
335 in response to commands issued by the client.
336 For example, to retrieve the home page of a web site:
337 .Bd -literal -offset indent
338 $ echo -n "GET / HTTP/1.0\er\en\er\en" | nc host.example.com 80
341 Note that this also displays the headers sent by the web server.
342 They can be filtered, using a tool such as
346 More complicated examples can be built up when the user knows the format
347 of requests required by the server.
348 As another example, an email may be submitted to an SMTP server using:
349 .Bd -literal -offset indent
350 $ nc localhost 25 \*(Lt\*(Lt EOF
351 HELO host.example.com
352 MAIL FROM:\*(Ltuser@host.example.com\*(Gt
353 RCPT TO:\*(Ltuser2@host.example.com\*(Gt
361 It may be useful to know which ports are open and running services on
365 flag can be used to tell
367 to report open ports,
368 rather than initiate a connection.
370 .Bd -literal -offset indent
371 $ nc -z host.example.com 20-30
372 Connection to host.example.com 22 port [tcp/ssh] succeeded!
373 Connection to host.example.com 25 port [tcp/smtp] succeeded!
376 The port range was specified to limit the search to ports 20 \- 30.
378 Alternatively, it might be useful to know which server software
379 is running, and which versions.
380 This information is often contained within the greeting banners.
381 In order to retrieve these, it is necessary to first make a connection,
382 and then break the connection when the banner has been retrieved.
383 This can be accomplished by specifying a small timeout with the
385 flag, or perhaps by issuing a
387 command to the server:
388 .Bd -literal -offset indent
389 $ echo "QUIT" | nc host.example.com 20-30
390 SSH-1.99-OpenSSH_3.6.1p2
392 220 host.example.com IMS SMTP Receiver Version 0.84 Ready
395 Open a TCP connection to port 42 of host.example.com, using port 31337 as
396 the source port, with a timeout of 5 seconds:
398 .Dl $ nc -p 31337 -w 5 host.example.com 42
400 Open a UDP connection to port 53 of host.example.com:
402 .Dl $ nc -u host.example.com 53
404 Open a TCP connection to port 42 of host.example.com using 10.1.2.3 as the
405 IP for the local end of the connection:
407 .Dl $ nc -s 10.1.2.3 host.example.com 42
409 Open a TCP connection to port 42 of host.example.com using IPsec ESP for
410 incoming and outgoing traffic.
412 .Dl $ nc -E host.example.com 42
414 Open a TCP connection to port 42 of host.example.com using IPsec ESP for
415 outgoing traffic only.
417 .Dl $ nc -e 'out ipsec esp/transport//require' host.example.com 42
419 Create and listen on a Unix Domain Socket:
421 .Dl $ nc -lU /var/tmp/dsocket
423 Connect to port 42 of host.example.com via an HTTP proxy at 10.2.3.4,
425 This example could also be used by
431 for more information.
433 .Dl $ nc -x10.2.3.4:8080 -Xconnect host.example.com 42
435 The same example again, this time enabling proxy authentication with username
437 if the proxy requires it:
439 .Dl $ nc -x10.2.3.4:8080 -Xconnect -Pruser host.example.com 42
445 Original implementation by *Hobbit*
446 .Aq hobbit@avian.org .
448 Rewritten with IPv6 support by
449 .An Eric Jackson Aq ericj@monkey.org .
451 UDP port scans will always succeed
452 (i.e. report the port as open),
455 combination of flags relatively useless.