Ports scan
Single host fast ports and services scan using masscan and nmap
Basic ports scan
ping + netcat
The ping
and netcat
utilities can be used to quickly enumerate accessible servers and their open ports from a compromised host. Both utilities can be uploaded, if not already available on the compromised host, as standalone binaries. Note that some statically linked version of netcat
may be detected as malicious agent by anti-viral solutions.
The following one-liners can be used to conduct an ICMP
echo sweep using the built-in ping
utility:
netcat
can be used to conduct a basic TCP
or UDP
ports scan, with no banner grabbing or version probing:
Combining ping
and netcat
, the following bash one-liners can be used to do a ping
sweep followed by a basic port scan using netcat
on the hosts responding to the ICMP
echo requests:
PowerShell
PowerShell can be used to conduct a very slow and basic ports scan using the Net.Sockets.TcpClient
or Test-Netconnection
built-ins.
Note that for each and every inaccessible ports Test-NetConnection
will perform an ICMP
echo request (ping) to the targeted host, tremendously slowing down the ports scanning process.
The PowerSploit
's Invoke-Portscan
cmdlet, supporting nmap
-like arguments, can be used for a faster ports scan from a compromised host using PowerShell:
Asynchronous and stateless ports scan
masscan
or RustScan
(or Unicornscan
, ZMap
, etc.) can be used to conduct fast asynchronous and stateless ports scan on targets supporting high inbound network bandwidth. masscan
/ RustScan
's ports scan speed can be combined with nmap
's services detection probes to rapidly conduct a large network ports and services scan.
Note however that the trade-off for the speed achieved using such tools is less precision, and potentially missed open ports.
masscan
masscan
uses a default rate of 100 packets/second and supports nmap
like options.
RustScan
rustscan
uses a default rate of 3000 packets/second, which may interfere with the target host(s) operational activity, and scans all TCP
65535 ports.
Ports and services scan with nmap
The nmap
("Network Mapper") tool is the most popular, versatile, and robust port scanners to date. It has been actively developed for over a decade, and has numerous features beyond port scanning.
nmap
uses raw IP packets to determine what hosts are available on the network, what services (application name and version) those hosts are offering, what operating systems (and OS versions) they are running, what type of packet filters/firewalls are in use, and dozens of other characteristics.
In addition to the classic command-line nmap
executable, the nmap
suite includes an advanced GUI and results viewer (Zenmap
), a flexible data transfer, redirection, and debugging tool (Ncat
), a utility for comparing scan results (Ndiff
), and a packet generation and response analysis tool (Nping
).
Usage
Single host scanning
Using nmap
to scan a single host:
Network scanning
Host Discovery
Generate a live hosts list through a nmap
"ping sweep":
ARP
ping for hosts on the same local subnet.ICMP
echo
requests andTCP
probes on ports 80 and 443 otherwise.
Port Discovery
Print results
Specific service vulnerabilities
Scan Types
-sS
: TCP
SYN
scan.
A SYN
packet is sent. In response, a SYN/ACK
indicates the port is listening (open), while a RST
(reset) packet is indicative of a non-listener. No response or an ICMP
unreachable error means the port is filtered.
-sT
: TCP
connect scan.
Does not require admin privilege. Instead of writing raw packets as most other scan types do, nmap
asks the underlying operating system to establish a connection with the target machine. Works the same way as the TCP
SYN
scan, only closing the TCP
handshake.
-sU
: UDP
scan.
Can be combined with a TCP
scan. A UDP
packet is sent. Open and filtered ports rarely send any response. If an ICMP
port unreachable error (type 3, code 3) is returned, the port is closed.
-sN
, -sF
, -sX
: TCP
NULL
, FIN
, and Xmas
scans.
NULL scan: Does not set any bits (
TCP
flag header is 0).FIN
scan: Sets just theTCP
FIN
bit.Xmas scan: Sets the
FIN
,PSH
, andURG
flags, "lighting the packet up like a Christmas tree."
If the system scanned is RFC compliant, a RST
packet will be received if the port is closed and no response at all if the port is open. The port is marked filtered if an ICMP
unreachable error (type 3
, code 0, 1, 2, 3, 9, 10, or 13) is received. The key advantage to these scan types is that they might sneak through certain non-stateful firewalls and packet filtering routers.
-sI <REMOTE_ZOMBIE_HOST>
: idle / zombie scan.
Channel the ports scan through a non controlled remote host. Reference: https://nmap.org/book/idlescan.html
.
Target Specification
nmap
supports multiple way to specify a target host, either as an input command line parameter or through a file:
IP address or hostname (example: 192.168.15.15 / www.google.com).
IP range (example: 192.168.0.*).
CIDR-style subnet (example: 192.168.0.0/24).
Common options
-p <PORT | PORTS | PORT_RANGE>
: scan specified ports.
Individual port numbers, comma separated list of ports or hyphen separated range can be used. When scanning a combination of protocols, a particular protocol can be specified by preceding the port numbers by T:
for TCP
, U:
for UDP
.
Example: -p U:53,111,137, T:21-25,80,139,443,8080
-sn
: no port scan (ping scan only).
Instructs nmap
not to do a port scan after host discovery, and only print out the available hosts that responded to the host discovery probes.
-Pn
: skip the host discovery phase and assume every hosts is up.
By default, nmap
use a ping scan to determine if the host is up before starting the specified scan. If specified, this flag tells nmap
to skip the host discovery phase and directly start the specified scan.
-n
: skipDNS
resolution.
Instructs nmap
to never do reverse DNS
resolution on the active IP addresses it may find. Can reduce scanning times.
--dns-servers <NAMESERVER>
.
Instructs nmap
to use the specified nameserver for DNS
resolution.
-PR
:ARP
ping.
Instructs nmap
to use ARP
requests to conduct host discovery on LA-T4N
network.
--sV
: enables version probing.
Instructs nmap
to try to determine the service protocol, the application name, the version number, hostname, device type and OS family of the target.
-O
: enables OS detection.
Instructs nmap
to try to determine the OS and OS details of the target.
--sC
: enables default script scanning.
Instructs nmap
to perform enumeration using its NSE
script engine and a default set of scripts.
-A
: "aggressive" scan options (equivalent to-sV
,-O
, and-sC
).
Tells nmap
to perform OS detection (-O
), version scanning (-sV
), script scanning (-sC
) and traceroute (--traceroute
).
-T <paranoid/0 | sneaky/1 | polite/2 | normal/3 | aggressive/4 | insane/5>
: timing template.
Instructs nmap
to use the specified scan / timing template: - paranoid/0
and sneaky/1
are for IDS
evasion and are incredibly slow. - polite/2
mode slows down the scan to use less bandwidth and resources from the targeted hosts. A polite
scan may be 10 times slower than a normal
scan. - normal/3
mode is the default scan mode.
- aggressive/4
mode speeds scans up by making the assumption that the scan is conducted on a reasonably fast and reliable network. - insane/5
mode assumes that the scan is conducted on an extraordinarily fast network or sacrifices some accuracy for speed.
nmap Scripting Engine (NSE)
nmap
's NSE
scripts are categorized in a list of categories they belong to. The following categories are currently defined:
auth
broadcast
brute
default
discovery
dos
exploit
external
fuzzer
intrusive
malware
safe
version
vuln
nmap
's NSE
usage:
nmap output parsing
nmap-parse-output
The nmap-parse-output
utility can be used to parse and extract information from nmap
outputs (in the xml
format).
NmaptoCSV
The NmaptoCSV
Python script can be used to convert nmap
outputs (regular, GNMAP
, or XML
formats) to the CSV
format.
Pivot scans through compromised hosts
Netcat
As described above, netcat
can be used to conduct a basic ports scan from a compromised host.
Static nmap
Prebuild static and standalone binaries of nmap
are available on the following GitHub repository. nmap
is compiled from the official GitHub repository sources automatically with GitHub Actions
.
The run-nmap.sh
or run-nmap.ps1
scripts can be used to run the prebuild nmap
on a compromised host without external dependencies. Additionally, the binary comes with the various NSE
scripts and modules necessary to conduct version fingerprinting.
Proychains
Proxychains
(or ProxyChains-NG
) can be used to conduct ports scans through a proxy (supported proxies types: http
, socks4
and socks5
).
Note that a few restrictions apply whenever conducting a ports scan through a proxy:
HTTP
/socks4
can only be used to conductTCP
scan.ICMP
packets can not pass through the proxy (nmap
's-Pn
option).RAW
packets cannot be redirected throughproxychains
as it is designed to relay fullTCP
connections only (nmap
's-sT
option).DNS
resolutions should not be conducted through a proxy if confidentially is of importance and to reduce scan time (nmap
's-n
).OS fingerprinting based on features of the IP stack may not work properly.
For example, nmap
can be used to conduct a ports scan and services discovery through a proxy using proxychains
:
Metasploit
The following Metasploit
modules can be used to conduct a ports scan:
auxiliary/scanner/portscan/syn
auxiliary/scanner/portscan/tcp
auxiliary/scanner/portscan/ack
auxiliary/scanner/portscan/ftpbounce
auxiliary/scanner/portscan/xmas
The port range default to 1-10000
. To scan all possible ports the 0-65535
ports range should be specified (set PORTS 0-65535
).
The modules can be used directly or through a meterpreter
session to use the compromised host as a pivot.
Local netstat execution trough remote code execution
A remote execution utility, such as a PsExec
-like tool or CrackMapExec
, can be used to retrieve the locally exposed services on a target through netstat
if valid credentials could be obtained for remote code execution.
For more information about the tools usage refer to the [Windows] Lateral movements
note for more information.
Graphical ports scanning utilities
Advanced port scanner
Advanced port scanner
is a Windows GUI multithreaded ports and services scanner that can be both installed and used in standalone mode.
The tool provides easy access to the main identified services (HTTP
, HTTPS
, SSH
, RDP
, SMB
, etc.) by starting the associated Windows built-ins.
netscan
SoftPerfect
's NetScan
is an advanced and lightweight Windows GUI network scanner utility, available as a standalone binary. NetScan
supports the Windows 7
through Windows 10
, and Windows Server 2008 R2
through Windows Server 2019
operating systems.
Note that the free edition of NetScan
can only be used to display a maximum of 10 devices.
In addition to IPv4 and IPv6 hosts discovery and ports scanning, NetScan
provides the following key features:
Discovery of network shares and integration with Windows built-in network share explorer and drive mapping functionalities.
Sending of
Wake-on-LAN (WoL)
messages.Discovery of
Dynamic Host Configuration Protocol (DHCP)
servers.Automatic discovery of network interface IP range.
Remote execution of
SSH
,PowerShell
andVBScript
command execution.
Cracked commercial editions of NetScan
have been observed to be used in the wild by malicious actors, notably in ransomware attack scenarios, with the deployment of NetScan
standalone binary on a compromised system to discover and map remote C$
network shares.
Last updated