Port scanning with Nmap in Cybersecurity - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using Nmap to scan ports, it's important to understand how the time it takes grows as the number of ports increases.
We want to know how the scanning effort changes when scanning more ports.
Analyze the time complexity of the following Nmap port scanning command.
nmap -p 1-1000 192.168.1.1
This command scans ports 1 through 1000 on the target IP address to check which ports are open.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sending a probe to each port to check its status.
- How many times: Once for each port in the specified range (e.g., 1000 times for ports 1 to 1000).
As the number of ports to scan increases, the number of probes sent grows in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 probes sent |
| 100 | 100 probes sent |
| 1000 | 1000 probes sent |
Pattern observation: The number of operations grows directly with the number of ports scanned.
Time Complexity: O(n)
This means the scanning time increases in a straight line as you scan more ports.
[X] Wrong: "Scanning more ports takes the same time because the tool is fast."
[OK] Correct: Each port requires a separate check, so more ports mean more work and more time.
Understanding how scanning time grows helps you explain network scanning efficiency and resource use clearly in real situations.
"What if Nmap scanned ports in parallel instead of one by one? How would the time complexity change?"
Practice
nmap in cybersecurity?Solution
Step 1: Understand what port scanning means
Port scanning is the process of checking which ports on a device are open and listening for connections.Step 2: Identify Nmap's role
Nmap is a tool designed to perform port scanning to find open ports and services on devices.Final Answer:
To find open ports on a network device -> Option AQuick Check:
Port scanning = Finding open ports [OK]
- Confusing port scanning with encryption
- Thinking Nmap creates firewalls
- Assuming Nmap monitors user activity
Solution
Step 1: Recall Nmap command structure
Nmap commands start with 'nmap' followed by options and then the target IP.Step 2: Identify correct option for scanning
The '-sS' option is a common scan type (TCP SYN scan) and is valid syntax.Final Answer:
nmap -sS 192.168.1.1 -> Option DQuick Check:
Correct Nmap scan syntax = nmap -sS 192.168.1.1 [OK]
- Using 'scan' as a command option
- Using invalid options like '-open' or '--check'
- Omitting the scan type option
nmap -p 22,80 192.168.0.10?Solution
Step 1: Understand the '-p' option in Nmap
The '-p' option specifies which ports to scan. Comma-separated values mean specific ports.Step 2: Analyze the ports listed
Ports 22 and 80 are explicitly listed, so only these two ports will be scanned.Final Answer:
Scan ports 22 and 80 on 192.168.0.10 -> Option AQuick Check:
'-p 22,80' means scan ports 22 and 80 [OK]
- Assuming '-p 22,80' scans all ports
- Thinking it scans a range from 22 to 80
- Ignoring the port list format
nmap -p 80-22 192.168.1.5Solution
Step 1: Check port range syntax
Port ranges must be in ascending order, e.g., 22-80, not 80-22.Step 2: Verify other parts of the command
The IP address format is correct, and scan type is optional; default scan works.Final Answer:
Port range is reversed; should be 22-80 -> Option BQuick Check:
Port ranges must ascend, not descend [OK]
- Using descending port ranges
- Thinking IP format is wrong
- Believing scan type is always required
Solution
Step 1: Understand how to specify IP ranges in Nmap
Nmap accepts explicit ranges like '192.168.1.1-192.168.1.254' to scan all addresses in that range.Step 2: Check port and target correctness
Port 80 is specified correctly with '-p 80'. The range '192.168.1.1-192.168.1.254' covers all hosts from .1 to .254.Step 3: Evaluate other options
nmap -p 80 192.168.1.0-254 scans from 192.168.1.0 to 192.168.1.254, including the unwanted network address .0. nmap -p 80 192.168.1.1/24 uses CIDR /24 which scans the entire subnet (.0 to .255). nmap -p 80 192.168.1.0/24 scans the entire subnet including .0 and .255.Final Answer:
nmap -p 80 192.168.1.1-192.168.1.254 -> Option CQuick Check:
Explicit IP range with '-p 80' = nmap -p 80 192.168.1.1-192.168.1.254 [OK]
- Using shorthand range 192.168.1.0-254 (includes .0)
- Confusing CIDR notation with explicit ranges
- Including network address (.0) in scan
