cybersecurity
penetration testing
Port scanning is a process by which, through specialized tools, the ports of a computer system are analyzed. By scanning ports it is possible to obtain information such as which ports are open, which are closed, or which are protected by firewalls.
This procedure can be used for different purposes. For example, system administrators use it to find out what services are being offered by the machine or to analyze the status of ports and detect and remedy possible vulnerabilities.
However, as we will see later, open port scanning can also be used for unethical purposes. Just as network administrators can use it to detect and fix vulnerabilities, cybercriminals can use it to exploit such security holes.
The following are the most common port scanning techniques.
connect()
system call. If a connection is established, the port is open; if no connection is established, the port is closed.No, port scanning is not always legal. Port scanning can be used for legitimate purposes. For example, for network and system administrators to check the status of ports, connections, and programs or applications being offered by the computer. It can also be used as a method for scanning for vulnerabilities and security breaches. Administrators can scan ports to create a map of connections and detect possible flaws or vulnerabilities. Therefore, it can be said that port scanning is completely legal, as long as you have permission to access the computer, and it is done for legitimate purposes.
However, it can also be used for unethical purposes. Many attackers employ such techniques to take advantage of these security flaws. In the end, open ports can function as a gateway to a computer, and then perform other malicious activities, such as denial of service attacks.
Following on from the previous point, port scanning attacks are one of the most common methods used by crackers to discover vulnerabilities in a system. The first thing a cybercriminal will do before launching his attack will be a portscan, which will allow him to find out information such as the network architecture, and the services offered on our computers and, of course, to look for holes or security breaches in them. In other words, each open port is a potential gateway to the computer, so it is necessary to take certain security measures.
One of them is not to open more ports than necessary. Normally, the computers already come with a series of predetermined open ports to be able to offer the services and basic functions. It is also advisable to use a firewall or packet filter, which acts as a barrier to protect the computer from intruders. Another recommendation is to always keep computers and systems updated to the latest version since developers usually create patches to correct possible vulnerabilities.
Finally, there is the option of using tools such as PortScanDetector, which allows us to detect when our ports are being scanned. It alerts administrators in cases where more than 10 ports have been scanned, which may indicate that a hacker is trying to access the computer.
Port and service scanning is a common practice in penetration testing and security assessments to identify active services, open ports, and potential vulnerabilities in a system. Here are general steps and some common tools used to perform this type of scan:
Nmap: Versatile port scanning and network mapping tool.
Example of Use:
1nmap -p 1-1000 192.168.1.1
Masscan: High performance port scanner.
Example of use:
1masscan -p1-65535 192.168.1.1
Unicornscan: Network and port scanning tool with a focus on speed and efficiency.
Usage Example:
1unicornscan -mU -Iv 192.168.1.1:a
Hping3: Advanced packet forwarding tool, useful for port scanning.
Example of Use:
1hping3 -S -p 80 192.168.1.1
Netcat (nc): Network utility tool that can be used to scan ports.
Example of use:
1nc -zv 192.168.1.1 1 1-100
It is essential to remember that port and service scanning must be carried out ethically and legally, with the explicit consent of the owner of the target system or network.
Nmap is a completely free and open source utility that allows us to discover networks and hosts, as well as perform security auditing. This program is compatible with Linux, Windows, and also macOS operating systems, but in all of them, it is used through the command line, although we have the possibility of installing ZenMap which is the graphical utility of Nmap to make port scans through the graphical user interface. If you do not want to fight with commands through the console, this GUI could be useful for the first steps with this great program, however, when you have more experience you will surely execute all the commands directly from the terminal.
Nmap allows us to detect hosts on a local network, and also over the Internet, in this way, we can know if those hosts (computers, servers, routers, switches, IoT devices) are currently connected to the Internet or to the local network. This tool also allows you to perform a port scan to different hosts, see what services we have active on those hosts because it will tell us the status of their ports, we can know what operating system is using a particular computer, and can even automate different pentesting tests to check the security of the equipment.
Nmap has different types of port scanning, they can be through TCP segments, UDP datagrams, or ICMP packets, in addition, it allows you to perform scans in a hidden way so that they are difficult to detect by firewalls. Of course, you can perform port scans on specific ports, between port ranges, IP address ranges, and the possibility to use TCP null, FIN, Xmas, and ACK packets as well as SYN, to locate open TCP ports.
Other features provided by this tool are the possibility to make a complete network inventory, and even check if a certain host or service is still up and running. This program was designed to scan many hosts, so if you need to scan multiple targets, you won't have any problems. This program is very flexible, it incorporates dozens of advanced techniques for scanning hosts and ports, in addition, it also allows you to perform audits through NSE (Nmap Search Engine), so it is really powerful.
👉 Nmap has several port states that will appear when doing a port scan. It is critical to know what each Nmap state means because with any port scan, it will return different states.
As you can see, Nmap is a very versatile tool. It can be used for a variety of tasks, mostly related to information security. But it also allows us to perform network administration. Some of the most common uses for Nmap are:
As we have seen, with Nmap we have a tool that specializes in scanning the ports of computer systems, obtaining information about whether they are open, closed, or protected with firewalls. This can be done for a multitude of purposes, such as knowing what services are being offered from a computer by administrators or for security, looking to solve a problem that may come in the form of a vulnerability.
But this can also be used for malicious purposes. Just as administrators can analyze them to look for possible vulnerabilities, an attacker can do it for the same reason, but to take advantage of it to infiltrate the network by exploiting a security breach.
Nmap is a tool that is considered to be highly effective. This is due to a combination of functionality and advanced features, which makes this solution one of the best we can find. It can perform vulnerability scans to identify possible weaknesses in security system configurations. Therefore, if it were not effective, it would not be such a well-known tool. Therefore, we could say that Nmap's fame is a good indicator of the quality that it can provide in a wide variety of different situations.
On the other hand, beyond its effectiveness in identifying devices, services, and vulnerabilities, is its ability to work with different network protocols. Among others, TCP, UDP, ICMP, and IP. All of which can be integrated with other security and analysis tools. These can be tools such as Metasploit or even Wireshark. This makes Nmap a very versatile tool for all types of systems.
Instead, this is not a perfect solution, nor does it represent a security measure against problems. Although it is very effective and powerful, it requires some knowledge on the part of the users who use it. As well as the availability of the necessary skills to use it with all the guarantees, and taking advantage of its performance and functions to the maximum. We must also think that many devices may have some kind of protection against scans by applications such as Nmap, or may even be difficult to detect. But in these cases, it would no longer be a problem of the efficiency of Nmap or its functions, but of the protection measures set up on those devices. In any case, if we are looking for an effective, reliable solution with a good track record, Nmap is one of the best solutions at the moment.
As you can see, NMAP is an open source tool, so it is highly customizable. This makes it a highly adaptable solution to the needs of each user, in all kinds of scenarios. But this is something that is not done lightly, but some factors stand out above others. Starting with the scanning options, where the range of options is very wide. This allows you to manage many parameters and advanced options, which completely modify the operation of NMAP. Customized scanning allows us to have very valuable information of all kinds when performing port analysis, for example.
On the other hand, there are the NSE (Nmap Scripting Engine) scripts. This is one of the most powerful features of NMAP, which allows you to run highly customized scripts during scan time. Since we can create the scripts ourselves, they can be as complex as necessary. And as such it gives us that added customization, which we would not have without this section of NMAP. Everything is done by looking at custom output data, where you can set many parameters. From the output format, generate HTML reports, and the results that we will need to include the analysis that is performed on the network.
Finally, NMAP has a great capacity for integration with other tools. One of the most striking features is the ability to set NMAP as an input for other security tools we use, or even for intrusion detection. Which, again, can be combined with the scripts we mentioned previously. Now that we've seen the main features of Nmap and the port status we have available, let's install and use it.
The first thing we need to do to use this powerful program is to download and install it. In the Nmap download section you can find all the links, binaries, and source code for installation on Windows, Linux, and MacOS operating systems. This program is currently available in all Linux-based operating system repositories, so its installation is really easy. In Kali Linux is already installed, but if your Linux is not installed simply run the installation command followed by "nmap", and you will install the program without difficulties.
1sudo apt install nmap
Nmap is a very advanced and complex program, with dozens of commands and attacks that we will be able to perform, to discover all the hosts that we have in a local home or professional network, in addition, it is also able to detect hosts on the Internet, that is, we will be able to scan one by one any IP address on the Internet and public subnets.
Once we have discovered that the host is online, we can perform a quick port scan and check if it has a firewall filtering all packets, or if we have an open port to exploit a vulnerability. Nmap allows you to use both private and public IPv4 addresses, as well as IPv6 addresses, to scan the ports of any host.
Next, we will see some examples of how to use Nmap at the user level, and also with more advanced commands, this program will allow us to discover with some accuracy what operating system is using the remote host, ideal to get as much information as possible.
If you want to perform a quick port scan on a specific host, type the following command. This basic command will be in charge of scanning the main ports to the defined private or public IP address, a very important detail is that it will not scan all ports, but the most commonly used ones.
1nmap [ip]
For example, if we want to perform a quick scan of the main ports to a host with IP address 192.168.1.1, the command would be as follows:
1nmap 192.168.1.1
The program will return the ports that are open on the target machine.
In the case of wanting to scan every one of the ports we will have to resort to the following command and put a range from port 1 to 65535, in this way, we will be checking if every one of the ports is open, closed, or filtered.
Instead of performing a scan of all ports, we can set a range of ports to check. To do this, we will run:
1> nmap -p [range] [ip]
If we want to perform a port scan from 20 TCP to 200 TCP on the IP address 192.168.1.1, just run the following command:
1nmap -p 20-200 192.168.1.1.1
The program will indicate which ports are open within that range.
Depending on the latency of the connection between your computer and the remote host, and also the number of ports to be scanned, this process could take from several seconds to about 10 minutes.
We can instruct Nmap to detect the operating system. It does this by sending packets and analyzing the way it sends them back, being on each system differently. Along with this, it will perform a scan of ports and services for vulnerabilities. Also, the scan will return useful information. To do this, we must run:
1nmap -A -v [ip]
If we want to perform this scan to the IP address 192.168.1.1 we can run the following command:
1nmap -A -v 192.168.1.1
This operating system detection test is not completely reliable because it depends on many parameters, in some cases the accuracy is very good, especially in differentiating whether it is Windows or Linux, but within the Linux world, it is complicated to know which operating system it is in particular.
This program is complete, so far we have used the basic commands to discover hosts and also to see if you have the ports open, however, this does not stay like that, and we have a great list of commands to squeeze the most out of this tool.
IP addresses or ranges, system names, networks, etc.
Most of the time, when users are just starting to use this tool, they will most likely try the resolution of most problems with the SYN scan type because it is one of the most versatile. But, as they progress and get to know the tool in depth, they will realize that they will expand their range of solutions, and here below we explain a little bit some of the commands that are most useful when scanning ports.
Service and version detection:
Firewall/IDS bypass
Detail and debug level parameters
Other options
Interactive options
Scripts
Output formats
💡 Mainly these are the commands available in Nmap. Before finishing, we must say that Nmap has a multitude of options with which to perform complete network analysis. We can consult all the available options by typing:
1nmap --help
Nmap is undoubtedly a very simple and complete tool to perform network audits, but this does not end here, we also have available Nmap NSE for advanced pentesting.
Other scanning techniques
As we have seen, Nmap offers many parameters to perform a more tailored scan, but there are currently other methods to perform this task which can be interesting. Each of them has its characteristics and can vary in effectiveness.
Thanks to these advanced scanning techniques, we can determine whether ports are open or closed.
Nmap Search Engine also known as Nmap NSE, is a large database with thousands of scripts that will allow us to automate the pentesting of systems and networks. The first step of any pentesting is to perform a port scan, once this port scan is done, we can try to exploit vulnerabilities in the services that are running behind a particular port, for example, we could attack web servers, Samba servers, FTP, SSH servers, DNS servers, check if the different services have known vulnerabilities or directly try to authenticate in them if they have authentication as in FTP or SSH.
NMAP NSE is a set of scripts that will allow us to automate many actions, such as performing brute force attacks on Samba servers, to try to access them and take control, although we could also attack it to perform a denial of service attack and make the service unavailable. The same could be done with FTP servers, SSH servers, and much more, above all, the web servers that must have an open port in the firewall are the ones that must be protected the most to avoid or mitigate the attacks that could be carried out.
For example, if we want to perform a brute force attack, based on a list of users (with a file called users.txt) and with a list of passwords to test (with a file called keys.txt) to an SSH server of a particular computer that has the IP 192.168.25.01, we can put the following command:
1nmap -p 22 --script ssh-brute --script-args userdb=usarios.txt,passdb=claves.txt --script-args ssh-brute.timeout=4s 192.168.25.01
If we want to know if an FTP server has anonymous authentication enabled, we can easily do so by setting the following command:
1nmap -sV -sC -p21 192.168.25.01
If we want to perform a brute force attack on an FTP server to a server with the IP 99.99.99.99.99, we can put the following command:
1nmap -p 21 --script ftp-brute 192.168.25.01
We have a huge amount of scripts within Nmap NSE to check the security of dozens of services because we will not only have the typical Samba, FTP, SSH servers, and more, but we can also attack in a very specific way a web server with special HTTP headers to see if there are vulnerabilities, of course, we will have specific scripts that will allow us to exploit vulnerabilities to the PHP of web servers, of course, we can also exploit vulnerabilities to the different Samba, FTP and SSH servers, that is, known vulnerabilities that have already been fixed, but it is possible that the target still has them operational. For this reason, it is so important to update as soon as possible all the services that we have exposed to the Internet and also services that we have exposed to the local network because malware could exploit a vulnerability and turn the penetration attack into a ransomware attack to encrypt all files and folders.
I recommend visiting the NSE official website where you will find all the scripts currently available in this large database, and you will also get examples of how to use them.
Zenmap is a free tool that we can use to scan ports. We can know which ones we have opened, to avoid problems when using some programs or accessing a server. It is the graphical interface of the popular open-source program Nmap, which allows a complete port scan of any connected computer. It should be noted that this completely free tool is available for different operating systems such as Microsoft Windows, Linux, or macOS. Likewise, it allows users to run different types of port scans. It is ideal for both less experienced and more advanced users.
To start using Zenmap the first thing we have to do is download it. We can do it from the official website of Nmap. There we will find the different versions, depending on the operating system we are using. The installation process is simple, fast, and intuitive. In a few seconds, we will have it ready to use.
When we have installed it we will simply have to execute it. You will see an image like the one above. We can choose a full port scan, quick scan, TCP ports, UDP ports, etc. With Zenmap we can see which ports are open on any device. In the Target section, we have to put the IP address that corresponds to that computer to subsequently perform the scan to show which ones are open. So the first thing we have to do is to know what is the IP address of our computer.
On Windows, this process is very simple. We simply have to go to Start, run the Command Prompt and then ipconfig. It will show us a series of information, among which we can see the default gateway (usually 192.168.1.1.1) as well as the IP address of that computer. When we know which is the address, we will have to put it in the section of Objective, in Zenmap. Then we will have to choose the type of scan we want to perform, as it could be for example a full scan of all TCP ports.
In Zenmap, at the top, we will find different tabs. We will see Nmap Output, Ports/Servers, Topology, Server Details and Scans. All of them provide us with information, as well as being able to choose the type of service on the left side. When we click on Ports/Servers we will see a compilation of all the open and filtered ports on that host. It is necessary to bear in mind that more or less ports will appear depending on the type of scan that we have carried out.
If we click on Scans we will see all the scans we have performed. We can save them to be able to analyze the data on another occasion, as well as to eliminate them so that they do not appear there.
This can be done with any other computer connected to the network. For example, we can also check the open ports and scan our cell phone. We only need to know the IP address. However, keep in mind that this process may take more or less time depending on the type of device.
In short, Zenmap is an interesting program with which we can perform port scans. We can use it on Windows, which is the most widely used operating system on desktop computers, as well as Linux and macOS. Its use is simple and intuitive, and we can have more control over which ports we have opened, especially when we need to know if a computer is going to work properly when using an application that requires certain ports open.
Many companies decide to implement these systems to have a little more control over their networks. But this is something that must be studied carefully, and following some steps so that the whole process is efficient. These steps are:
👉 As we can see, Nmap is a tremendously useful tool, which can give us many facilities when managing networks.