Using Netcat: some practical commands

Netcat o nc, is a well-known tool for network analysis, also known as the hackers' Swiss army knife, as it has many functionalities, similar to the aforementioned knife. In this post we will explain some of its most basic functionalities with examples:

1.-Netcat as a client-server:

Netcat can be used as a server and left to the listens from a certain port:

$ nc -l 2389

Also, we can use it to conectarnos to a port (2389), recently opened:

$ nc localhost 2389

Now if we write on the side of the also information, will arrive next to the server:

$ nc localhost 2389
HI, server

In the terminal where the server:

$ nc -l 2389
HI, server

We have seen an example of how to use nectar for client-server communication.

2.-Use Netcat to transfer files:

Netcat can be used to transfer files. On the side of also information Suppose we have a file called 'testfile' that contains:

$ cat testfile
hello testfile

and on the side of server we have an empty file called 'test'.

Now we side with the server:

$ nc -l 2389 > test

and we run the client as follows:

cat testfile | nc localhost 2389

When we check the 'test' file in the server:

$ cat test
Hello testfile

We have transferred data from also information al server.

3.-Netcat supports timeouts:

Sometimes when we open a connection we do not want it to remain open indefinitely, so to solve this problem we use the option -w, so that after x amount of seconds the connection between client-server is closed.

Server:

$nc -l 2389

Client:

$ nc -w 10 localhost 2389

The connection will be closed after 10 seconds.

Note: you should not use the option -w with the option -l on the side of server as -w it would have no effect and therefore the connection would remain open indefinitely.

4.-Netcat supports IPV6:

The options -4 y -6 force Netcat that uses the IPv4 or IPv6 protocol respectively.

employee:

$ nc -4 -l 2389

Client:

$ nc -4 localhost 2389

Now, if we run the command netstat, we would see:

$ netstat | grep 2389
tcp 0 0 localhost:2389 localhost:50851 ESTABLISHED
tcp 0 0 localhost:50851 localhost:2389 ESTABLISHED

The first parameter of the above output if it were IPv6 it would show a 6 after the tcp, but as we use IPv4 show us only tcp :)

.

Now, let's force Necati for it to use IPv6:

Server:

$nc -6 -l 2389

Client:

$ nc -6 localhost 2389

Running netstat again we would see:

$ netstat | grep 2389
tcp6 0 0 localhost:2389 localhost:33234 ESTABLISHED
tcp6 0 0 localhost:33234 localhost:2389 ESTABLISHED

We can see how the tcp is now accompanied by a 6, indicating the use of IPv6.

5.-Deactivate the reading by STDIN of Netcat:

This functionality is available through the option -d. In this example we do it on the client side:

Server:

$ nc -l 2389

Client:

$ nc -d localhost 2389
Hi

The Hi text will not be sent to the server since reading via STDIN has been disabled.

6.-Force Netcat to stay awake:

When we have the server running and the also information disconnects, the server also ends:

Server:

$ nc -l 2389

Client:

$ nc localhost 2389
^C

Server:

$ nc -l 2389
$

We could see in the previous example that if the also information closes the connection also the server So what can we do? Our solution is to use the option -k, which forces the server to keep running.

Server:

$ nc -k -l 2389

Client:

$ nc localhost 2389
C^

Server:

$ nc -k -l 2389

We have seen that server keep running even though also information has been disconnected, thanks to the option -k that we add to the server.

7.-Configure Netcat to stay awake after an EOF:

Netcat is configured so that after receiving a EOF(End Of File) terminate the connection, normally this is what happens, but we can modify this default behavior of Netcat adding option -q. This option instructs Netcat It must wait x number of seconds before closing the connection.

Client:

El also information should be started as follows:

nc -q 5 localhost 2389

Now whenever the also information receives an EOF will wait 5 seconds before closing the connection.

8.-Use Netcat over UDP:

Default Netcat uses the protocol for its communication TCP, but we can also use UDP by option -u.

Server:

$ nc -4 -u -l 2389

Client:

$ nc -4 -u localhost 2389

Now also information y server are using the protocol UDP for your communication, we can check this through the command netstat.

$ netstat | grep 2389
udp 0 0 localhost:42634 localhost:2389 ESTABLISHED

Well, during the post we have seen some examples of the use of Netcat, they could appreciate that it is a very versatile tool, hence the hacker's Swiss army knife ;)

, here we present just some of its functionalities, as always if you want more information: man nc, and they will see everything that can be done with this tool. Until the next post and Happy Hacking !!!

Article taken from humans.


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   rafagcg said

    what inventions !!

  2.   proper said

    Just the weekend I was working with this tool, really very good.

    regards

  3.   hexborg said

    A good summary of the things that can be done with netcat. This will come in handy to me from time to time. Thanks a lot.

  4.   Hugo said

    Good summary, thanks for the info.

  5.   nwt_lazaro said

    For what it has served me, Audit a wireless channel or the complete activity of the wireless interface of a ubiquiti AP (those of technology N)
    en
    PC: (192.168.0.1)
    nc -l 1234> packages.raw
    AP: (192.168.0.2)
    tcpdump -i ath0 -w - | nc 192.168.0.1 1234
    Ctrl + C (to end the capture)

    PC:
    open wireshark or any other with pcap-file support and read the file packages.raw

    This has been an immense help to me, and that is why I share it with you

  6.   carlosg said

    Very good blog

    1.    let's use linux said

      Thanks Carlos! A hug!
      Pablo.

  7.   Gustavo said

    Dear User,

    I am new to security levels and I would like to know where I can find the tool, that is, it is a windows complement or it is only functional for LINUX platforms since in my work environment it has served a lot for what I am currently doing

    I appreciate your attention in advance, I look forward to your comments and support

  8.   William said

    Who teaches me to use linux..ubuntu .. in order to learn more about hankin ... xd