1*de0e0e4dSAntonio Huete Jimenez.\" $OpenBSD: nc.1,v 1.97 2022/09/11 09:58:06 schwarze Exp $ 2f5b1c8a1SJohn Marino.\" 3f5b1c8a1SJohn Marino.\" Copyright (c) 1996 David Sacerdote 4f5b1c8a1SJohn Marino.\" All rights reserved. 5f5b1c8a1SJohn Marino.\" 6f5b1c8a1SJohn Marino.\" Redistribution and use in source and binary forms, with or without 7f5b1c8a1SJohn Marino.\" modification, are permitted provided that the following conditions 8f5b1c8a1SJohn Marino.\" are met: 9f5b1c8a1SJohn Marino.\" 1. Redistributions of source code must retain the above copyright 10f5b1c8a1SJohn Marino.\" notice, this list of conditions and the following disclaimer. 11f5b1c8a1SJohn Marino.\" 2. Redistributions in binary form must reproduce the above copyright 12f5b1c8a1SJohn Marino.\" notice, this list of conditions and the following disclaimer in the 13f5b1c8a1SJohn Marino.\" documentation and/or other materials provided with the distribution. 14f5b1c8a1SJohn Marino.\" 3. The name of the author may not be used to endorse or promote products 15f5b1c8a1SJohn Marino.\" derived from this software without specific prior written permission 16f5b1c8a1SJohn Marino.\" 17f5b1c8a1SJohn Marino.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18f5b1c8a1SJohn Marino.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19f5b1c8a1SJohn Marino.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20f5b1c8a1SJohn Marino.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21f5b1c8a1SJohn Marino.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22f5b1c8a1SJohn Marino.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23f5b1c8a1SJohn Marino.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24f5b1c8a1SJohn Marino.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25f5b1c8a1SJohn Marino.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26f5b1c8a1SJohn Marino.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27f5b1c8a1SJohn Marino.\" 28*de0e0e4dSAntonio Huete Jimenez.Dd $Mdocdate: September 11 2022 $ 29f5b1c8a1SJohn Marino.Dt NC 1 30f5b1c8a1SJohn Marino.Os 31f5b1c8a1SJohn Marino.Sh NAME 32f5b1c8a1SJohn Marino.Nm nc 33f5b1c8a1SJohn Marino.Nd arbitrary TCP and UDP connections and listens 34f5b1c8a1SJohn Marino.Sh SYNOPSIS 35f5b1c8a1SJohn Marino.Nm nc 36f5b1c8a1SJohn Marino.Op Fl 46cDdFhklNnrStUuvz 37f5b1c8a1SJohn Marino.Op Fl C Ar certfile 38f5b1c8a1SJohn Marino.Op Fl e Ar name 39f5b1c8a1SJohn Marino.Op Fl H Ar hash 40f5b1c8a1SJohn Marino.Op Fl I Ar length 41f5b1c8a1SJohn Marino.Op Fl i Ar interval 42f5b1c8a1SJohn Marino.Op Fl K Ar keyfile 43f5b1c8a1SJohn Marino.Op Fl M Ar ttl 44f5b1c8a1SJohn Marino.Op Fl m Ar minttl 45f5b1c8a1SJohn Marino.Op Fl O Ar length 4672c33676SMaxim Ag.Op Fl o Ar staplefile 47f5b1c8a1SJohn Marino.Op Fl P Ar proxy_username 48f5b1c8a1SJohn Marino.Op Fl p Ar source_port 49f5b1c8a1SJohn Marino.Op Fl R Ar CAfile 50cca6fc52SDaniel Fojt.Op Fl s Ar sourceaddr 51f5b1c8a1SJohn Marino.Op Fl T Ar keyword 52f5b1c8a1SJohn Marino.Op Fl V Ar rtable 5372c33676SMaxim Ag.Op Fl W Ar recvlimit 54f5b1c8a1SJohn Marino.Op Fl w Ar timeout 55f5b1c8a1SJohn Marino.Op Fl X Ar proxy_protocol 56f5b1c8a1SJohn Marino.Op Fl x Ar proxy_address Ns Op : Ns Ar port 5772c33676SMaxim Ag.Op Fl Z Ar peercertfile 58f5b1c8a1SJohn Marino.Op Ar destination 59f5b1c8a1SJohn Marino.Op Ar port 60f5b1c8a1SJohn Marino.Sh DESCRIPTION 61f5b1c8a1SJohn MarinoThe 62f5b1c8a1SJohn Marino.Nm 63f5b1c8a1SJohn Marino(or 64f5b1c8a1SJohn Marino.Nm netcat ) 65f5b1c8a1SJohn Marinoutility is used for just about anything under the sun involving TCP, 66f5b1c8a1SJohn MarinoUDP, or 67f5b1c8a1SJohn Marino.Ux Ns -domain 68f5b1c8a1SJohn Marinosockets. 69f5b1c8a1SJohn MarinoIt can open TCP connections, send UDP packets, listen on arbitrary 70f5b1c8a1SJohn MarinoTCP and UDP ports, do port scanning, and deal with both IPv4 and 71f5b1c8a1SJohn MarinoIPv6. 72f5b1c8a1SJohn MarinoUnlike 73f5b1c8a1SJohn Marino.Xr telnet 1 , 74f5b1c8a1SJohn Marino.Nm 75f5b1c8a1SJohn Marinoscripts nicely, and separates error messages onto standard error instead 76f5b1c8a1SJohn Marinoof sending them to standard output, as 77f5b1c8a1SJohn Marino.Xr telnet 1 78f5b1c8a1SJohn Marinodoes with some. 79f5b1c8a1SJohn Marino.Pp 80f5b1c8a1SJohn MarinoCommon uses include: 81f5b1c8a1SJohn Marino.Pp 82f5b1c8a1SJohn Marino.Bl -bullet -offset indent -compact 83f5b1c8a1SJohn Marino.It 84f5b1c8a1SJohn Marinosimple TCP proxies 85f5b1c8a1SJohn Marino.It 86f5b1c8a1SJohn Marinoshell-script based HTTP clients and servers 87f5b1c8a1SJohn Marino.It 88f5b1c8a1SJohn Marinonetwork daemon testing 89f5b1c8a1SJohn Marino.It 90f5b1c8a1SJohn Marinoa SOCKS or HTTP ProxyCommand for 91f5b1c8a1SJohn Marino.Xr ssh 1 92f5b1c8a1SJohn Marino.It 93f5b1c8a1SJohn Marinoand much, much more 94f5b1c8a1SJohn Marino.El 95f5b1c8a1SJohn Marino.Pp 96f5b1c8a1SJohn MarinoThe options are as follows: 97f5b1c8a1SJohn Marino.Bl -tag -width Ds 98f5b1c8a1SJohn Marino.It Fl 4 9972c33676SMaxim AgUse IPv4 addresses only. 100f5b1c8a1SJohn Marino.It Fl 6 10172c33676SMaxim AgUse IPv6 addresses only. 102f5b1c8a1SJohn Marino.It Fl C Ar certfile 10372c33676SMaxim AgLoad the public key part of the TLS peer certificate from 10472c33676SMaxim Ag.Ar certfile , 10572c33676SMaxim Agin PEM format. 10672c33676SMaxim AgRequires 10772c33676SMaxim Ag.Fl c . 108f5b1c8a1SJohn Marino.It Fl c 10972c33676SMaxim AgUse TLS to connect or listen. 11072c33676SMaxim AgCannot be used together with any of the options 11172c33676SMaxim Ag.Fl FuU . 112f5b1c8a1SJohn Marino.It Fl D 113f5b1c8a1SJohn MarinoEnable debugging on the socket. 114f5b1c8a1SJohn Marino.It Fl d 115f5b1c8a1SJohn MarinoDo not attempt to read from stdin. 116f5b1c8a1SJohn Marino.It Fl e Ar name 11772c33676SMaxim AgOnly accept the TLS peer certificate if it contains the 11872c33676SMaxim Ag.Ar name . 11972c33676SMaxim AgRequires 12072c33676SMaxim Ag.Fl c . 12172c33676SMaxim AgIf not specified, 12272c33676SMaxim Ag.Ar destination 12372c33676SMaxim Agis used. 124f5b1c8a1SJohn Marino.It Fl F 125f5b1c8a1SJohn MarinoPass the first connected socket using 126f5b1c8a1SJohn Marino.Xr sendmsg 2 127f5b1c8a1SJohn Marinoto stdout and exit. 128f5b1c8a1SJohn MarinoThis is useful in conjunction with 129f5b1c8a1SJohn Marino.Fl X 130f5b1c8a1SJohn Marinoto have 131f5b1c8a1SJohn Marino.Nm 132f5b1c8a1SJohn Marinoperform connection setup with a proxy but then leave the rest of the 133f5b1c8a1SJohn Marinoconnection to another program (e.g.\& 134f5b1c8a1SJohn Marino.Xr ssh 1 135f5b1c8a1SJohn Marinousing the 136f5b1c8a1SJohn Marino.Xr ssh_config 5 137f5b1c8a1SJohn Marino.Cm ProxyUseFdpass 138f5b1c8a1SJohn Marinooption). 13972c33676SMaxim AgCannot be used with 14072c33676SMaxim Ag.Fl c 14172c33676SMaxim Agor 14272c33676SMaxim Ag.Fl U . 143f5b1c8a1SJohn Marino.It Fl H Ar hash 14472c33676SMaxim AgOnly accept the TLS peer certificate if its hash returned from 14572c33676SMaxim Ag.Xr tls_peer_cert_hash 3 14672c33676SMaxim Agmatches 14772c33676SMaxim Ag.Ar hash . 14872c33676SMaxim AgRequires 14972c33676SMaxim Ag.Fl c 15072c33676SMaxim Agand cannot be used with 15172c33676SMaxim Ag.Fl T Cm noverify . 152f5b1c8a1SJohn Marino.It Fl h 15372c33676SMaxim AgPrint out the 154f5b1c8a1SJohn Marino.Nm 15572c33676SMaxim Aghelp text and exit. 156f5b1c8a1SJohn Marino.It Fl I Ar length 15772c33676SMaxim AgSpecify the size of the TCP receive buffer. 158f5b1c8a1SJohn Marino.It Fl i Ar interval 15972c33676SMaxim AgSleep for 16072c33676SMaxim Ag.Ar interval 16172c33676SMaxim Agseconds between lines of text sent and received. 162f5b1c8a1SJohn MarinoAlso causes a delay time between connections to multiple ports. 163f5b1c8a1SJohn Marino.It Fl K Ar keyfile 16472c33676SMaxim AgLoad the TLS private key from 16572c33676SMaxim Ag.Ar keyfile , 16672c33676SMaxim Agin PEM format. 16772c33676SMaxim AgRequires 16872c33676SMaxim Ag.Fl c . 169f5b1c8a1SJohn Marino.It Fl k 17072c33676SMaxim AgWhen a connection is completed, listen for another one. 17172c33676SMaxim AgRequires 17272c33676SMaxim Ag.Fl l . 173f5b1c8a1SJohn MarinoWhen used together with the 174f5b1c8a1SJohn Marino.Fl u 175f5b1c8a1SJohn Marinooption, the server socket is not connected and it can receive UDP datagrams from 176f5b1c8a1SJohn Marinomultiple hosts. 177f5b1c8a1SJohn Marino.It Fl l 17872c33676SMaxim AgListen for an incoming connection rather than initiating a 179f5b1c8a1SJohn Marinoconnection to a remote host. 18072c33676SMaxim AgCannot be used together with any of the options 18172c33676SMaxim Ag.Fl psxz . 182f5b1c8a1SJohn MarinoAdditionally, any timeouts specified with the 183f5b1c8a1SJohn Marino.Fl w 184f5b1c8a1SJohn Marinooption are ignored. 185f5b1c8a1SJohn Marino.It Fl M Ar ttl 186f5b1c8a1SJohn MarinoSet the TTL / hop limit of outgoing packets. 187f5b1c8a1SJohn Marino.It Fl m Ar minttl 188f5b1c8a1SJohn MarinoAsk the kernel to drop incoming packets whose TTL / hop limit is under 189f5b1c8a1SJohn Marino.Ar minttl . 190f5b1c8a1SJohn Marino.It Fl N 191f5b1c8a1SJohn Marino.Xr shutdown 2 192f5b1c8a1SJohn Marinothe network socket after EOF on the input. 193f5b1c8a1SJohn MarinoSome servers require this to finish their work. 194f5b1c8a1SJohn Marino.It Fl n 195cca6fc52SDaniel FojtDo not perform domain name resolution. 196cca6fc52SDaniel FojtIf a name cannot be resolved without DNS, an error will be reported. 197f5b1c8a1SJohn Marino.It Fl O Ar length 19872c33676SMaxim AgSpecify the size of the TCP send buffer. 19972c33676SMaxim Ag.It Fl o Ar staplefile 20072c33676SMaxim AgDuring the TLS handshake, load data to be stapled from 20172c33676SMaxim Ag.Ar staplefile , 20272c33676SMaxim Agwhich is expected to contain an OCSP response from an OCSP server in 20372c33676SMaxim AgDER format. 20472c33676SMaxim AgRequires 20572c33676SMaxim Ag.Fl c 20672c33676SMaxim Agand 20772c33676SMaxim Ag.Fl C . 208f5b1c8a1SJohn Marino.It Fl P Ar proxy_username 209f5b1c8a1SJohn MarinoSpecifies a username to present to a proxy server that requires authentication. 210f5b1c8a1SJohn MarinoIf no username is specified then authentication will not be attempted. 211f5b1c8a1SJohn MarinoProxy authentication is only supported for HTTP CONNECT proxies at present. 212f5b1c8a1SJohn Marino.It Fl p Ar source_port 21372c33676SMaxim AgSpecify the source port 214f5b1c8a1SJohn Marino.Nm 215f5b1c8a1SJohn Marinoshould use, subject to privilege restrictions and availability. 21672c33676SMaxim AgCannot be used together with 21772c33676SMaxim Ag.Fl l . 218f5b1c8a1SJohn Marino.It Fl R Ar CAfile 21972c33676SMaxim AgLoad the root CA bundle for TLS certificate verification from 22072c33676SMaxim Ag.Ar CAfile , 22172c33676SMaxim Agin PEM format, instead of 222f5b1c8a1SJohn Marino.Pa /etc/ssl/cert.pem . 22372c33676SMaxim AgRequires 22472c33676SMaxim Ag.Fl c . 225f5b1c8a1SJohn Marino.It Fl r 22672c33676SMaxim AgChoose source and/or destination ports randomly 227f5b1c8a1SJohn Marinoinstead of sequentially within a range or in the order that the system 228f5b1c8a1SJohn Marinoassigns them. 229f5b1c8a1SJohn Marino.It Fl S 23072c33676SMaxim AgEnable the RFC 2385 TCP MD5 signature option. 231cca6fc52SDaniel Fojt.It Fl s Ar sourceaddr 232cca6fc52SDaniel FojtSet the source address to send packets from, 233cca6fc52SDaniel Fojtwhich is useful on machines with multiple interfaces. 234f5b1c8a1SJohn MarinoFor 235f5b1c8a1SJohn Marino.Ux Ns -domain 236f5b1c8a1SJohn Marinodatagram sockets, specifies the local temporary socket file 237f5b1c8a1SJohn Marinoto create and use so that datagrams can be received. 23872c33676SMaxim AgCannot be used together with 239f5b1c8a1SJohn Marino.Fl l 24072c33676SMaxim Agor 24172c33676SMaxim Ag.Fl x . 242f5b1c8a1SJohn Marino.It Fl T Ar keyword 24372c33676SMaxim AgChange the IPv4 TOS/IPv6 traffic class value or the TLS options. 244f5b1c8a1SJohn Marino.Pp 24572c33676SMaxim AgFor TLS options, 24672c33676SMaxim Ag.Ar keyword 24772c33676SMaxim Agmay be one of: 24872c33676SMaxim Ag.Cm noverify , 24972c33676SMaxim Agwhich disables certificate verification; 25072c33676SMaxim Ag.Cm noname , 25172c33676SMaxim Agwhich disables certificate name checking; 25272c33676SMaxim Ag.Cm clientcert , 25372c33676SMaxim Agwhich requires a client certificate on incoming connections; or 25472c33676SMaxim Ag.Cm muststaple , 25572c33676SMaxim Agwhich requires the peer to provide a valid stapled OCSP response 25672c33676SMaxim Agwith the handshake. 25772c33676SMaxim AgThe following TLS options specify a value in the form of a 25872c33676SMaxim Ag.Ar key Ns = Ns Ar value 25972c33676SMaxim Agpair: 26072c33676SMaxim Ag.Cm ciphers , 26172c33676SMaxim Agwhich allows the supported TLS ciphers to be specified (see 26272c33676SMaxim Ag.Xr tls_config_set_ciphers 3 26372c33676SMaxim Agfor further details); 26472c33676SMaxim Ag.Cm protocols , 26572c33676SMaxim Agwhich allows the supported TLS protocols to be specified (see 26672c33676SMaxim Ag.Xr tls_config_parse_protocols 3 26772c33676SMaxim Agfor further details). 26872c33676SMaxim AgSpecifying TLS options requires 26972c33676SMaxim Ag.Fl c . 27072c33676SMaxim Ag.Pp 27172c33676SMaxim AgFor the IPv4 TOS/IPv6 traffic class value, 272f5b1c8a1SJohn Marino.Ar keyword 273f5b1c8a1SJohn Marinomay be one of 27472c33676SMaxim Ag.Cm critical , 27572c33676SMaxim Ag.Cm inetcontrol , 27672c33676SMaxim Ag.Cm lowdelay , 27772c33676SMaxim Ag.Cm netcontrol , 27872c33676SMaxim Ag.Cm throughput , 27972c33676SMaxim Ag.Cm reliability , 280f5b1c8a1SJohn Marinoor one of the DiffServ Code Points: 28172c33676SMaxim Ag.Cm ef , 28272c33676SMaxim Ag.Cm af11 No ... Cm af43 , 28372c33676SMaxim Ag.Cm cs0 No ... Cm cs7 ; 284f5b1c8a1SJohn Marinoor a number in either hex or decimal. 285f5b1c8a1SJohn Marino.It Fl t 28672c33676SMaxim AgSend RFC 854 DON'T and WON'T responses to RFC 854 DO and WILL requests. 287f5b1c8a1SJohn MarinoThis makes it possible to use 288f5b1c8a1SJohn Marino.Nm 289f5b1c8a1SJohn Marinoto script telnet sessions. 290f5b1c8a1SJohn Marino.It Fl U 29172c33676SMaxim AgUse 292f5b1c8a1SJohn Marino.Ux Ns -domain 293f5b1c8a1SJohn Marinosockets. 29472c33676SMaxim AgCannot be used together with any of the options 29572c33676SMaxim Ag.Fl cFx . 296f5b1c8a1SJohn Marino.It Fl u 29772c33676SMaxim AgUse UDP instead of TCP. 29872c33676SMaxim AgCannot be used together with 29972c33676SMaxim Ag.Fl c 30072c33676SMaxim Agor 30172c33676SMaxim Ag.Fl x . 302f5b1c8a1SJohn MarinoFor 303f5b1c8a1SJohn Marino.Ux Ns -domain 304f5b1c8a1SJohn Marinosockets, use a datagram socket instead of a stream socket. 305f5b1c8a1SJohn MarinoIf a 306f5b1c8a1SJohn Marino.Ux Ns -domain 307f5b1c8a1SJohn Marinosocket is used, a temporary receiving socket is created in 308f5b1c8a1SJohn Marino.Pa /tmp 309f5b1c8a1SJohn Marinounless the 310f5b1c8a1SJohn Marino.Fl s 311f5b1c8a1SJohn Marinoflag is given. 312f5b1c8a1SJohn Marino.It Fl V Ar rtable 313f5b1c8a1SJohn MarinoSet the routing table to be used. 314f5b1c8a1SJohn Marino.It Fl v 31572c33676SMaxim AgProduce more verbose output. 31672c33676SMaxim Ag.It Fl W Ar recvlimit 31772c33676SMaxim AgTerminate after receiving 31872c33676SMaxim Ag.Ar recvlimit 31972c33676SMaxim Agpackets from the network. 320f5b1c8a1SJohn Marino.It Fl w Ar timeout 321f5b1c8a1SJohn MarinoConnections which cannot be established or are idle timeout after 322f5b1c8a1SJohn Marino.Ar timeout 323f5b1c8a1SJohn Marinoseconds. 324f5b1c8a1SJohn MarinoThe 325f5b1c8a1SJohn Marino.Fl w 326f5b1c8a1SJohn Marinoflag has no effect on the 327f5b1c8a1SJohn Marino.Fl l 328f5b1c8a1SJohn Marinooption, i.e.\& 329f5b1c8a1SJohn Marino.Nm 330f5b1c8a1SJohn Marinowill listen forever for a connection, with or without the 331f5b1c8a1SJohn Marino.Fl w 332f5b1c8a1SJohn Marinoflag. 333f5b1c8a1SJohn MarinoThe default is no timeout. 334f5b1c8a1SJohn Marino.It Fl X Ar proxy_protocol 33572c33676SMaxim AgUse 33672c33676SMaxim Ag.Ar proxy_protocol 33772c33676SMaxim Agwhen talking to the proxy server. 338f5b1c8a1SJohn MarinoSupported protocols are 33972c33676SMaxim Ag.Cm 4 340f5b1c8a1SJohn Marino(SOCKS v.4), 34172c33676SMaxim Ag.Cm 5 342f5b1c8a1SJohn Marino(SOCKS v.5) 343f5b1c8a1SJohn Marinoand 34472c33676SMaxim Ag.Cm connect 345f5b1c8a1SJohn Marino(HTTPS proxy). 346f5b1c8a1SJohn MarinoIf the protocol is not specified, SOCKS version 5 is used. 347f5b1c8a1SJohn Marino.It Fl x Ar proxy_address Ns Op : Ns Ar port 34872c33676SMaxim AgConnect to 349f5b1c8a1SJohn Marino.Ar destination 350f5b1c8a1SJohn Marinousing a proxy at 351f5b1c8a1SJohn Marino.Ar proxy_address 352f5b1c8a1SJohn Marinoand 353f5b1c8a1SJohn Marino.Ar port . 354f5b1c8a1SJohn MarinoIf 355f5b1c8a1SJohn Marino.Ar port 356f5b1c8a1SJohn Marinois not specified, the well-known port for the proxy protocol is used (1080 357f5b1c8a1SJohn Marinofor SOCKS, 3128 for HTTPS). 35872c33676SMaxim AgAn IPv6 address can be specified unambiguously by enclosing 35972c33676SMaxim Ag.Ar proxy_address 36072c33676SMaxim Agin square brackets. 36172c33676SMaxim AgA proxy cannot be used with any of the options 36272c33676SMaxim Ag.Fl lsuU . 36372c33676SMaxim Ag.It Fl Z Ar peercertfile 36472c33676SMaxim AgSave the peer certificates to 36572c33676SMaxim Ag.Ar peercertfile , 36672c33676SMaxim Agin PEM format. 36772c33676SMaxim AgRequires 36872c33676SMaxim Ag.Fl c . 369f5b1c8a1SJohn Marino.It Fl z 37072c33676SMaxim AgOnly scan for listening daemons, without sending any data to them. 37172c33676SMaxim AgCannot be used together with 37272c33676SMaxim Ag.Fl l . 373f5b1c8a1SJohn Marino.El 374f5b1c8a1SJohn Marino.Pp 375f5b1c8a1SJohn Marino.Ar destination 376f5b1c8a1SJohn Marinocan be a numerical IP address or a symbolic hostname 377f5b1c8a1SJohn Marino(unless the 378f5b1c8a1SJohn Marino.Fl n 379f5b1c8a1SJohn Marinooption is given). 380f5b1c8a1SJohn MarinoIn general, a destination must be specified, 381f5b1c8a1SJohn Marinounless the 382f5b1c8a1SJohn Marino.Fl l 383f5b1c8a1SJohn Marinooption is given 384f5b1c8a1SJohn Marino(in which case the local host is used). 385f5b1c8a1SJohn MarinoFor 386f5b1c8a1SJohn Marino.Ux Ns -domain 387f5b1c8a1SJohn Marinosockets, a destination is required and is the socket path to connect to 388f5b1c8a1SJohn Marino(or listen on if the 389f5b1c8a1SJohn Marino.Fl l 390f5b1c8a1SJohn Marinooption is given). 391f5b1c8a1SJohn Marino.Pp 392f5b1c8a1SJohn Marino.Ar port 39372c33676SMaxim Agcan be specified as a numeric port number or as a service name. 39472c33676SMaxim AgPort ranges may be specified as numeric port numbers of the form 39572c33676SMaxim Ag.Ar nn Ns - Ns Ar mm . 396f5b1c8a1SJohn MarinoIn general, 397f5b1c8a1SJohn Marinoa destination port must be specified, 398f5b1c8a1SJohn Marinounless the 399f5b1c8a1SJohn Marino.Fl U 400f5b1c8a1SJohn Marinooption is given. 401f5b1c8a1SJohn Marino.Sh CLIENT/SERVER MODEL 402f5b1c8a1SJohn MarinoIt is quite simple to build a very basic client/server model using 403f5b1c8a1SJohn Marino.Nm . 404f5b1c8a1SJohn MarinoOn one console, start 405f5b1c8a1SJohn Marino.Nm 406f5b1c8a1SJohn Marinolistening on a specific port for a connection. 407f5b1c8a1SJohn MarinoFor example: 408f5b1c8a1SJohn Marino.Pp 409f5b1c8a1SJohn Marino.Dl $ nc -l 1234 410f5b1c8a1SJohn Marino.Pp 411f5b1c8a1SJohn Marino.Nm 412f5b1c8a1SJohn Marinois now listening on port 1234 for a connection. 413f5b1c8a1SJohn MarinoOn a second console 414f5b1c8a1SJohn Marino.Pq or a second machine , 415f5b1c8a1SJohn Marinoconnect to the machine and port being listened on: 416f5b1c8a1SJohn Marino.Pp 417*de0e0e4dSAntonio Huete Jimenez.Dl $ nc -N 127.0.0.1 1234 418f5b1c8a1SJohn Marino.Pp 419f5b1c8a1SJohn MarinoThere should now be a connection between the ports. 420f5b1c8a1SJohn MarinoAnything typed at the second console will be concatenated to the first, 421f5b1c8a1SJohn Marinoand vice-versa. 422f5b1c8a1SJohn MarinoAfter the connection has been set up, 423f5b1c8a1SJohn Marino.Nm 424f5b1c8a1SJohn Marinodoes not really care which side is being used as a 425f5b1c8a1SJohn Marino.Sq server 426f5b1c8a1SJohn Marinoand which side is being used as a 427f5b1c8a1SJohn Marino.Sq client . 428f5b1c8a1SJohn MarinoThe connection may be terminated using an 429f5b1c8a1SJohn Marino.Dv EOF 430*de0e0e4dSAntonio Huete Jimenez.Pq Sq ^D , 431*de0e0e4dSAntonio Huete Jimenezas the 432*de0e0e4dSAntonio Huete Jimenez.Fl N 433*de0e0e4dSAntonio Huete Jimenezflag was given. 434f5b1c8a1SJohn Marino.Sh DATA TRANSFER 435f5b1c8a1SJohn MarinoThe example in the previous section can be expanded to build a 436f5b1c8a1SJohn Marinobasic data transfer model. 437f5b1c8a1SJohn MarinoAny information input into one end of the connection will be output 438f5b1c8a1SJohn Marinoto the other end, and input and output can be easily captured in order to 439f5b1c8a1SJohn Marinoemulate file transfer. 440f5b1c8a1SJohn Marino.Pp 441f5b1c8a1SJohn MarinoStart by using 442f5b1c8a1SJohn Marino.Nm 443f5b1c8a1SJohn Marinoto listen on a specific port, with output captured into a file: 444f5b1c8a1SJohn Marino.Pp 445*de0e0e4dSAntonio Huete Jimenez.Dl $ nc -l 1234 > filename.out 446f5b1c8a1SJohn Marino.Pp 447f5b1c8a1SJohn MarinoUsing a second machine, connect to the listening 448f5b1c8a1SJohn Marino.Nm 449f5b1c8a1SJohn Marinoprocess, feeding it the file which is to be transferred: 450f5b1c8a1SJohn Marino.Pp 451*de0e0e4dSAntonio Huete Jimenez.Dl $ nc -N host.example.com 1234 < filename.in 452f5b1c8a1SJohn Marino.Pp 453f5b1c8a1SJohn MarinoAfter the file has been transferred, the connection will close automatically. 454f5b1c8a1SJohn Marino.Sh TALKING TO SERVERS 455f5b1c8a1SJohn MarinoIt is sometimes useful to talk to servers 456f5b1c8a1SJohn Marino.Dq by hand 457f5b1c8a1SJohn Marinorather than through a user interface. 458f5b1c8a1SJohn MarinoIt can aid in troubleshooting, 459f5b1c8a1SJohn Marinowhen it might be necessary to verify what data a server is sending 460f5b1c8a1SJohn Marinoin response to commands issued by the client. 461f5b1c8a1SJohn MarinoFor example, to retrieve the home page of a web site: 462f5b1c8a1SJohn Marino.Bd -literal -offset indent 463f5b1c8a1SJohn Marino$ printf "GET / HTTP/1.0\er\en\er\en" | nc host.example.com 80 464f5b1c8a1SJohn Marino.Ed 465f5b1c8a1SJohn Marino.Pp 466f5b1c8a1SJohn MarinoNote that this also displays the headers sent by the web server. 467f5b1c8a1SJohn MarinoThey can be filtered, using a tool such as 468f5b1c8a1SJohn Marino.Xr sed 1 , 469f5b1c8a1SJohn Marinoif necessary. 470f5b1c8a1SJohn Marino.Pp 471f5b1c8a1SJohn MarinoMore complicated examples can be built up when the user knows the format 472f5b1c8a1SJohn Marinoof requests required by the server. 473f5b1c8a1SJohn MarinoAs another example, an email may be submitted to an SMTP server using: 474f5b1c8a1SJohn Marino.Bd -literal -offset indent 475*de0e0e4dSAntonio Huete Jimenez$ nc localhost 25 << EOF 476f5b1c8a1SJohn MarinoHELO host.example.com 477*de0e0e4dSAntonio Huete JimenezMAIL FROM:<user@host.example.com> 478*de0e0e4dSAntonio Huete JimenezRCPT TO:<user2@host.example.com> 479f5b1c8a1SJohn MarinoDATA 480f5b1c8a1SJohn MarinoBody of email. 481f5b1c8a1SJohn Marino\&. 482f5b1c8a1SJohn MarinoQUIT 483f5b1c8a1SJohn MarinoEOF 484f5b1c8a1SJohn Marino.Ed 485f5b1c8a1SJohn Marino.Sh PORT SCANNING 486f5b1c8a1SJohn MarinoIt may be useful to know which ports are open and running services on 487f5b1c8a1SJohn Marinoa target machine. 488f5b1c8a1SJohn MarinoThe 489f5b1c8a1SJohn Marino.Fl z 490f5b1c8a1SJohn Marinoflag can be used to tell 491f5b1c8a1SJohn Marino.Nm 492f5b1c8a1SJohn Marinoto report open ports, 493f5b1c8a1SJohn Marinorather than initiate a connection. 494f5b1c8a1SJohn MarinoFor example: 495f5b1c8a1SJohn Marino.Bd -literal -offset indent 496f5b1c8a1SJohn Marino$ nc -z host.example.com 20-30 497f5b1c8a1SJohn MarinoConnection to host.example.com 22 port [tcp/ssh] succeeded! 498f5b1c8a1SJohn MarinoConnection to host.example.com 25 port [tcp/smtp] succeeded! 499f5b1c8a1SJohn Marino.Ed 500f5b1c8a1SJohn Marino.Pp 501f5b1c8a1SJohn MarinoThe port range was specified to limit the search to ports 20 \- 30. 502f5b1c8a1SJohn Marino.Pp 503f5b1c8a1SJohn MarinoAlternatively, it might be useful to know which server software 504f5b1c8a1SJohn Marinois running, and which versions. 505f5b1c8a1SJohn MarinoThis information is often contained within the greeting banners. 506f5b1c8a1SJohn MarinoIn order to retrieve these, it is necessary to first make a connection, 507f5b1c8a1SJohn Marinoand then break the connection when the banner has been retrieved. 508f5b1c8a1SJohn MarinoThis can be accomplished by specifying a small timeout with the 509f5b1c8a1SJohn Marino.Fl w 510f5b1c8a1SJohn Marinoflag, or perhaps by issuing a 511f5b1c8a1SJohn Marino.Qq Dv QUIT 512f5b1c8a1SJohn Marinocommand to the server: 513f5b1c8a1SJohn Marino.Bd -literal -offset indent 514f5b1c8a1SJohn Marino$ echo "QUIT" | nc host.example.com 20-30 515f5b1c8a1SJohn MarinoSSH-1.99-OpenSSH_3.6.1p2 516f5b1c8a1SJohn MarinoProtocol mismatch. 517f5b1c8a1SJohn Marino220 host.example.com IMS SMTP Receiver Version 0.84 Ready 518f5b1c8a1SJohn Marino.Ed 519f5b1c8a1SJohn Marino.Sh EXAMPLES 520f5b1c8a1SJohn MarinoOpen a TCP connection to port 42 of host.example.com, using port 31337 as 521f5b1c8a1SJohn Marinothe source port, with a timeout of 5 seconds: 522f5b1c8a1SJohn Marino.Pp 523f5b1c8a1SJohn Marino.Dl $ nc -p 31337 -w 5 host.example.com 42 524f5b1c8a1SJohn Marino.Pp 52572c33676SMaxim AgOpen a TCP connection to port 443 of www.example.com, and negotiate TLS with 52672c33676SMaxim Agany supported TLS protocol version and "compat" ciphers: 527f5b1c8a1SJohn Marino.Pp 52872c33676SMaxim Ag.Dl $ nc -cv -T protocols=all -T ciphers=compat www.example.com 443 52972c33676SMaxim Ag.Pp 53072c33676SMaxim AgOpen a TCP connection to port 443 of www.google.ca, and negotiate TLS. 53172c33676SMaxim AgCheck for a different name in the certificate for validation: 53272c33676SMaxim Ag.Pp 53372c33676SMaxim Ag.Dl $ nc -cv -e adsf.au.doubleclick.net www.google.ca 443 534f5b1c8a1SJohn Marino.Pp 535f5b1c8a1SJohn MarinoOpen a UDP connection to port 53 of host.example.com: 536f5b1c8a1SJohn Marino.Pp 537f5b1c8a1SJohn Marino.Dl $ nc -u host.example.com 53 538f5b1c8a1SJohn Marino.Pp 539f5b1c8a1SJohn MarinoOpen a TCP connection to port 42 of host.example.com using 10.1.2.3 as the 540f5b1c8a1SJohn MarinoIP for the local end of the connection: 541f5b1c8a1SJohn Marino.Pp 542f5b1c8a1SJohn Marino.Dl $ nc -s 10.1.2.3 host.example.com 42 543f5b1c8a1SJohn Marino.Pp 544f5b1c8a1SJohn MarinoCreate and listen on a 545f5b1c8a1SJohn Marino.Ux Ns -domain 546f5b1c8a1SJohn Marinostream socket: 547f5b1c8a1SJohn Marino.Pp 548f5b1c8a1SJohn Marino.Dl $ nc -lU /var/tmp/dsocket 549f5b1c8a1SJohn Marino.Pp 550f5b1c8a1SJohn MarinoConnect to port 42 of host.example.com via an HTTP proxy at 10.2.3.4, 551f5b1c8a1SJohn Marinoport 8080. 552f5b1c8a1SJohn MarinoThis example could also be used by 553f5b1c8a1SJohn Marino.Xr ssh 1 ; 554f5b1c8a1SJohn Marinosee the 555f5b1c8a1SJohn Marino.Cm ProxyCommand 556f5b1c8a1SJohn Marinodirective in 557f5b1c8a1SJohn Marino.Xr ssh_config 5 558f5b1c8a1SJohn Marinofor more information. 559f5b1c8a1SJohn Marino.Pp 560f5b1c8a1SJohn Marino.Dl $ nc -x10.2.3.4:8080 -Xconnect host.example.com 42 561f5b1c8a1SJohn Marino.Pp 562f5b1c8a1SJohn MarinoThe same example again, this time enabling proxy authentication with username 563f5b1c8a1SJohn Marino.Dq ruser 564f5b1c8a1SJohn Marinoif the proxy requires it: 565f5b1c8a1SJohn Marino.Pp 566f5b1c8a1SJohn Marino.Dl $ nc -x10.2.3.4:8080 -Xconnect -Pruser host.example.com 42 567f5b1c8a1SJohn Marino.Sh SEE ALSO 568f5b1c8a1SJohn Marino.Xr cat 1 , 569f5b1c8a1SJohn Marino.Xr ssh 1 570f5b1c8a1SJohn Marino.Sh AUTHORS 57172c33676SMaxim AgOriginal implementation by 57272c33676SMaxim Ag.An *Hobbit* Aq Mt hobbit@avian.org . 573f5b1c8a1SJohn Marino.br 574f5b1c8a1SJohn MarinoRewritten with IPv6 support by 575f5b1c8a1SJohn Marino.An Eric Jackson Aq Mt ericj@monkey.org . 576f5b1c8a1SJohn Marino.Sh CAVEATS 577f5b1c8a1SJohn MarinoUDP port scans using the 578f5b1c8a1SJohn Marino.Fl uz 579f5b1c8a1SJohn Marinocombination of flags will always report success irrespective of 580f5b1c8a1SJohn Marinothe target machine's state. 581f5b1c8a1SJohn MarinoHowever, 582f5b1c8a1SJohn Marinoin conjunction with a traffic sniffer either on the target machine 583f5b1c8a1SJohn Marinoor an intermediary device, 584f5b1c8a1SJohn Marinothe 585f5b1c8a1SJohn Marino.Fl uz 586f5b1c8a1SJohn Marinocombination could be useful for communications diagnostics. 587f5b1c8a1SJohn MarinoNote that the amount of UDP traffic generated may be limited either 588f5b1c8a1SJohn Marinodue to hardware resources and/or configuration settings. 589