Jump to content

Using netcat (nc)


Recommended Posts

netcat is known as the swiss army of network tools found in linux.  It's used for monitoring, testing and sending data across network connections and its free.

By default, netcat (nc) operates by initiating a TCP connection to a remote host.  The basic syntax is:

nc [options] host port

This attempts a TCP connection to the defined host on the port number specified.  Something very similar to the old telnet command. Keep in mind your connection is entirely unencrypted.

If you prefer to test a UDP connection instead of TCP, you can use the -u option

nc -u host port

Sometimes you prefer to test a range of ports

nc host firstport-lastport

Of course this is typically used with other flags.


Netcat for Port Scanning

nmap is a better tool for this sort of thing but you can use netcat to do this also to find what ports are open.

We simply specify a range of ports to scan along with the -z option which performs a scan instead of trying to initiate a connection.

For example, we can scan all ports up to 1000 by issuing:

nc -z -v domain.com 1-1000

Notice I also threw in the -v option to tell netcat to provide more verbose information.

You would get an output like the following:

nc: connect to domain.com port 1 (tcp) failed: Connection refused
nc: connect to domain.com port 2 (tcp) failed: Connection refused
nc: connect to domain.com port 3 (tcp) failed: Connection refused
nc: connect to domain.com port 4 (tcp) failed: Connection refused
nc: connect to domain.com port 5 (tcp) failed: Connection refused
nc: connect to domain.com port 6 (tcp) failed: Connection refused
nc: connect to domain.com port 7 (tcp) failed: Connection refused
. . .
Connection to domain.com 22 port [tcp/ssh] succeeded!
. . .

If you know the IP address you want to scan on instead of domain name it will go much faster if you don't need to resolve the address using DNS, but you have to include in the command to not use DNS to resolve.  Something like this

nc -z -n -v 1-1000

You can also start filtering on the results like this...

nc -z -n -v 1-1000 2>&1 | grep succeeded

Now it will only show successful connections on the successful port.


usage: nc [-46bCDdhjklnrStUuvZz] [-I length] [-i interval] [-O length]
          [-P proxy_username] [-p source_port] [-q seconds] [-s source]
          [-T toskeyword] [-V rtable] [-w timeout] [-X proxy_protocol]
          [-x proxy_address[:port]] [destination] [port]
     The options are as follows:

     -4      Forces nc to use IPv4 addresses only.
     -6      Forces nc to use IPv6 addresses only.
     -b      Allow broadcast.
     -C      Send CRLF as line-ending.
     -D      Enable debugging on the socket.
     -d      Do not attempt to read from stdin.
     -h      Prints out nc help.
     -I length
             Specifies the size of the TCP receive buffer.
     -i interval
             Specifies a delay time interval between lines of text sent and received.  Also causes a delay time between connections
             to multiple ports.
     -k      Forces nc to stay listening for another connection after its current connection is completed.  It is an error to use
             this option without the -l option.
     -l      Used to specify that nc should listen for an incoming connection rather than initiate a connection to a remote host.  It
             is an error to use this option in conjunction with the -p, -s, or -z options.  Additionally, any timeouts specified with
             the -w option are ignored.
     -n      Do not do any DNS or service lookups on any specified addresses, hostnames or ports.
     -O length
             Specifies the size of the TCP send buffer.
     -P proxy_username
             Specifies a username to present to a proxy server that requires authentication.  If no username is specified then
             authentication will not be attempted.  Proxy authentication is only supported for HTTP CONNECT proxies at present.
     -p source_port
             Specifies the source port nc should use, subject to privilege restrictions and availability.
     -q seconds
             after EOF on stdin, wait the specified number of seconds and then quit. If seconds is negative, wait forever.
     -r      Specifies that source and/or destination ports should be chosen randomly instead of sequentially within a range or in
             the order that the system assigns them.
     -S      Enables the RFC 2385 TCP MD5 signature option.
     -s source
             Specifies the IP of the interface which is used to send the packets.  For UNIX-domain datagram sockets, specifies the
             local temporary socket file to create and use so that datagrams can be received.  It is an error to use this option in
             conjunction with the -l option.
     -T toskeyword
             Change IPv4 TOS value.  toskeyword may be one of critical, inetcontrol, lowcost, lowdelay, netcontrol, throughput,
             reliability, or one of the DiffServ Code Points: ef, af11 ... af43, cs0 ... cs7; or a number in either hex or decimal.
     -t      Causes nc to send RFC 854 DON'T and WON'T responses to RFC 854 DO and WILL requests.  This makes it possible to use nc
             to script telnet sessions.
     -U      Specifies to use UNIX-domain sockets.
     -u      Use UDP instead of the default option of TCP.  For UNIX-domain sockets, use a datagram socket instead of a stream
             socket.  If a UNIX-domain socket is used, a temporary receiving socket is created in /tmp unless the -s flag is given.
     -V rtable
             Set the routing table to be used.  The default is 0.
     -v      Have nc give more verbose output.
     -w timeout
             Connections which cannot be established or are idle timeout after timeout seconds.  The -w flag has no effect on the -l
             option, i.e. nc will listen forever for a connection, with or without the -w flag.  The default is no timeout.
     -X proxy_protocol
             Requests that nc should use the specified protocol when talking to the proxy server.  Supported protocols are â4â
             v.4), â5âconnectâ
     -x proxy_address[:port]
             Requests that nc should connect to destination using a proxy at proxy_address and port.  If port is not specified, the
             well-known port for the proxy protocol is used (1080 for SOCKS, 3128 for HTTPS).
     -Z      DCCP mode.
     -z      Specifies that nc should just scan for listening daemons, without sending any data to them.  It is an error to use this
             option in conjunction with the -l option.

     destination can be a numerical IP address or a symbolic hostname (unless the -n option is given).  In general, a destination
     must be specified, unless the -l option is given (in which case the local host is used).  For UNIX-domain sockets, a destination
     is required and is the socket path to connect to (or listen on if the -l option is given).

     port can be a single integer or a range of ports.  Ranges are in the form nn-mm.  In general, a destination port must be speciâ
     fied, unless the -U option is given.


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...