Unit-3 Enumeration and Port Scanning
Unit-3 Enumeration and Port Scanning
Host Discovery -Scanning for Open Ports and Services -Types of Port
Scanning -TCP Three-way handshake- TCP Flags -Port Status Types- TCP
SYN Scan- TCP Connect Scan- NULL, FIN and XMAS -TCP ACK Scan-
Responses- UDP Port Scan- Scanning a vulnerable host- Performing an
IDLE scan with NMAP -Service Version Detection- OS Fingerprinting.
1. **Open Ports:** Open ports are those that are actively listening for
incoming network connections. They are the primary target for ethical
hackers as they provide potential entry points into a system. Services
and applications often run on open ports, and vulnerabilities in these
services can be exploited.
2. **Closed Ports:** Closed ports are not actively listening for incoming
connections. This usually means that there is no service or application
running on these ports. While they may not be direct entry points,
ethical hackers might still scan them to gather information about the
target system.
3. **Filtered Ports:** Filtered ports are those where a firewall or some
other network filtering device is actively blocking access. Ethical hackers
need to identify filtered ports as these can sometimes indicate that the
organization has taken security measures to protect specific services.
4. **Stealth Ports:** Stealth ports are ports that are configured not to
respond to network scans. This can be done intentionally to make it
more challenging for hackers to discover open ports. Ethical hackers
often try to identify stealth ports and understand why they are hidden.
To determine the status of ports on a target system during ethical
hacking, various network scanning techniques are used, including:
- **Port Scanning:** Tools like Nmap are commonly used to scan a
target's ports and determine their status (open, closed, or filtered).
- **Banner Grabbing:** This technique involves connecting to open
ports to retrieve banners or service information. It can help in
identifying the exact services running on open ports, which can be
useful for further exploitation.
- **TCP/UDP Scanning:** Ethical hackers may perform both TCP and
UDP port scans since different services often use different transport
protocols.
- **Version Detection:** Some scanning tools can even attempt to
identify the specific versions of services running on open ports, which
can be critical for finding known vulnerabilities associated with those
versions.
It's important to note that ethical hacking should always be conducted
with proper authorization and within the boundaries defined by the
organization being tested. Unauthorized port scanning or any form of
network intrusion is illegal and unethical. Ethical hackers should have
clear rules of engagement and permissions before conducting any
testing.
TCP SYN SCAN
TCP SYN scan is a most popular and default scan in Nmap because it
performs quickly compare to other scan types and it is also less likely to
block from firewalls. Another reason is that when it comes to states
open, closed and filtered, TCP SYN scan gives a clear definition. Main
concept behind this scan is TCP three-way handshake. TCP SYN scan
required raw-packet privileges that needs root access.
Open state :
What happened there is that Nmap tries to establish a connection
between scanme.nmap.org by sending TCP SYN packet. In this situation
server sends a SYN/ACK packets to establish the connection. This is the
result that Nmap uses to determine whether the port is open. Nmap
reset the connection at the end.
Close state:
TCP SYN packet is sends to the server as the last time and what happned
here is server directly reject the connection with RST packet due to the
closed port.
filtered port:
In the filtered state, Server doesn't send a reply back, not even a RST
packet to terminate the connection. Most accurate reason can be a
firewall on the server side blocks reply packets. So Nmap decides this
type of ports as filtered.
Another things is that default SYN scan is not enough for slip through a
network with firewalls and intrusion prevention systems. It needs more
improved techniques.
TCP Connect Scan
In the Nmap TCP connection scan, Nmap asks its underlying Operating
network to establish a connection with the target server by issuing the
“connect” system call. But the problem with this scan is that it takes
time to complete and it require to generate more packets to obtain
information. In the other hand, targets are more likely to allow the
connection because it tries to establish a connection with target same
as network enabled applications like web browsers.
Open state :
First two steps are exactly the same as TCP SYN scan and instead of
sending a reset(RST) packet, TCP Connect Scan sends a ACK packet and
establish the connection. After the establish the connection, it resets
the connection.
Other steps of SYN Connect scan is same as SYN scan which is
mentioned in the above section.
So the difference between these two scan types is TCP Connect scan
establish a full connection with the target but SYN scan completes only a
half of the connection with target.
There are some other scanning techniques in Nmap which are not used
as much as the one mentioned in previous lessons. However, in some
cases, you may need to find some other ways to be able to discover the
sensitive hosts in a network.
📜 In the figure above, there are three types of scans which is the null
scan, fin scan and xmas scan. The common feature of these three
scanning methods is that they send packets to the target systems in
which SYN, ACK and RST flags are not set.
▶️ Null scan (-sN) does not set any bits. For instance, TCP flag header is 0.
▶️ Fin scan (-sF) sets just the TCP FIN bit.
▶️ Xmas scan (-sX) sets the FIN, PSH/Push , and URG flags lighting up the
packet like a Christmas tree.
📁There are two rules defined in RFC standards about such packets:
1. The first rule is if the destination port state is CLOSED, and
incoming segment not containing a RST causes a RST to be sent in
response.
2. The second rule is packets sent to OPEN ports without the SYN,
RST or ACK bits set are dropped.
How Nmap interprets responses to a NULL, FIN, or Xmas scan probe
These three scan types are exactly the same in behavior except for the
TCP flags set in probe packets. Responses are treated as shown in Figure
above.
If the RST packet is received, the port is considered closed. While no
response means that it is open or filtered. In addition, if an ICMP
unreachable error is received, the port is marked as filtered. Thus, with
the result from these types of scans, you can find out if a port is closed
or not.
TCP ACK Scan (-sA):
⭐️The ACK scan probe packet has only ACK flag set.When scanning
unfiltered systems, both open and closed port will return a RST packet.
Nmap then labels them as unfiltered.
⭐️This mean that the ports are reachable by the ACK packet. However,
whether they are open or closed is undetermined.
⭐️Ports that don’t respond or send certain ICMP error messages back
are labelled as filtered.
In the figure above, the result illustrated that the state of port is
unfiltered instead of normal scan that result in open, closed or filtered.
Idle Scan (-sL):
⚡️ Idle scan as an advance scan method/technique that allows for a truly
blind TCP port scan of the target. Truly blind TCP port scan means
that no packets are sent to the target from your real IP address.
⚡️ IDS systems will display the scan as coming from the zombie machine
that you specified.
The idle scan is based on three facts:
As you already know, one way to determine whether TCP port is
open or closed. The target machine will responded with a
SYN/ACK packet if the port is open and RST if the port is closed.
A machine that receives unexpected SYN/ACK packet will
responded with RST and an unexpected RST will be ignored.
Every IP packet on the internet has a fragment identification
number (IP ID). Since many operating systems simply increment
this number for each packet they send. Probing for the IP ID can
tell an attacker how many packets have been sent since the last
probe.
Open Port:
Let’s see what happen in an idle scan if the target port is open.
Steps:
1. First step is to probe the IP ID of the zombie system. The
attacker/scanner will sends a SYN/ACK to the zombie. Then, since
the zombie does not expect the packet, it sends back a RST with
IP ID.
2. The second steps is to forge a SYN packet from the zombie to the
target system. The target then sends a SYN/ACK in response to
the SYN and appears to be coming from the zombie. Since the
zombie does not expect the packet, it sends back a
RST. This increments its IP ID in the process.
3. Third step is to probe the zombie’s IP ID again. The
attacker/scanner a SYN/ACK to zombie again. Then, the RST
packet of the zombie has an IP ID which is increased by two since
the first step.
Concluded that port is open.
Closed Port:
Now let’s see what happen in an idle scan if the target port is closed.
1. The first step is to probe the IP ID of the zombie system.
Attacker/Scanner sends a SYN/ACK to the zombie. Since the
zombie does not expect the packet, it sends back a RST with an IP
ID.
2. Second step is to forge a SYN packet from the zombie to the
target system. The target sends a RST as the port is closed in
response to the SYN and that appears to be coming from the
zombie. The zombie ignores the unexpected RST. Thus, its IP ID
does not change.
3. Third step is to probe the zombie’s IP ID again. The
attacker/scanner sends a SYN/ACK to the zombie again. The RST
packet of the zombie has an IP ID which is increased by 1 since
the first step.
Concluded that port is closed/not open.
Filtered Port:
Let’s see what happen in an idle scan if the target port is filtered.
1. The first step is to probe the IP ID of the zombie system. The
attacker/scanner sends a SYN/ACK to the zombie. Since zombie
does not expect the packet, it sends back a RST with an IP ID.
2. Second step is to forge a SYN packet from the zombie and send to
the target system. The target filtering its port, ignores the SYN
that appears to come from the zombie. The zombie is unaware
that anything happened so its IP ID remains the same.
3. The third step is to probe zombie’s IP ID again.The
attacker/scanner sends a SYN/ACK to the zombie again. The RST
packet of the zombie’s IP ID which has increased by 1 since the
first step.
Concluded that port is closed/not open. Form the attacker’s point of
view, the filtered port is indistinguishable from a closed port. WHY ❓ in
both cases the IP ID is increased only by one.
Let’s perform an idle scan:
In order to be able to perform an idle scan, we first need to have a
zombie computer on the network which has incremental IP ID
sequencing. Fortunately, we have an Nmap script to help us find the
computer that is appropriate to become a zombie.
First let find the scripts that start with ipid with the command in figure
above.
Responses
■ If there is no response, this means that the firewall is stateful and it’s
filtering your packets.
■ If you receive a reset packet, it means that the packet reached the
destination.
The capture from Wireshark also gives a better insight into the TCP
ACK scan.
UDP scanning is a process in which we scan for the UDP services that are
being deployed on the target system or are currently in a running state.
UDP is a connectionless protocol, hence it is hard to probe as compared
to TCP.
UDP scan works by sending a UDP packet to every targeted port. For
most ports, this packet will be empty (no payload), but for a few of the
more common ports, a protocol-specific payload will be sent. Based on
the response, the port is assigned to one of four states. The result table
looks like this in a Nmap UDP scan:
State Response
For UDP scanning, the service sends a generic UDP packet and waits for
a response. If there is no response, the port is assumed to be open and
a UDP packet specific to the service on that port is sent to detect the
service. If an ICMP error packet is returned, the port is considered
closed.
SERVICE VERSION DETECTION
Syntax
Version Detection is used with the -sV command, and it allows the user
to collect information about the port. This can include the version
number, the service type, the operating system, the hostname, etc.
--allports
--version-intensity
--version-light
--version-all
--version-trace
Example
The following example demonstrates the use of Version Detection
using scanme.nmap.org (a safe test host):
This outputs:
OS DETECTION